You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Status.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (C) 2011, 2015 François Rey <eclipse.org_@_francois_._rey_._name>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.TreeSet;
  52. import org.eclipse.jgit.api.Git;
  53. import org.eclipse.jgit.api.StatusCommand;
  54. import org.eclipse.jgit.api.errors.GitAPIException;
  55. import org.eclipse.jgit.errors.NoWorkTreeException;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.IndexDiff.StageState;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.pgm.internal.CLIText;
  61. import org.eclipse.jgit.pgm.opt.UntrackedFilesHandler;
  62. import org.kohsuke.args4j.Argument;
  63. import org.kohsuke.args4j.Option;
  64. import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
  65. /**
  66. * Status command
  67. */
  68. @Command(usage = "usage_Status", common = true)
  69. class Status extends TextBuiltin {
  70. protected final String statusFileListFormat = CLIText.get().statusFileListFormat;
  71. protected final String statusFileListFormatWithPrefix = CLIText.get().statusFileListFormatWithPrefix;
  72. protected final String statusFileListFormatUnmerged = CLIText.get().statusFileListFormatUnmerged;
  73. @Option(name = "--porcelain", usage = "usage_machineReadableOutput")
  74. protected boolean porcelain;
  75. @Option(name = "--untracked-files", aliases = { "-u", "-uno", "-uall" }, usage = "usage_untrackedFilesMode", handler = UntrackedFilesHandler.class)
  76. protected String untrackedFilesMode = "all"; // default value //$NON-NLS-1$
  77. @Argument(required = false, index = 0, metaVar = "metaVar_paths")
  78. @Option(name = "--", metaVar = "metaVar_paths", handler = RestOfArgumentsHandler.class)
  79. protected List<String> filterPaths;
  80. /** {@inheritDoc} */
  81. @Override
  82. protected void run() {
  83. try (Git git = new Git(db)) {
  84. StatusCommand statusCommand = git.status();
  85. if (filterPaths != null && filterPaths.size() > 0) {
  86. for (String path : filterPaths) {
  87. statusCommand.addPath(path);
  88. }
  89. }
  90. org.eclipse.jgit.api.Status status = statusCommand.call();
  91. printStatus(status);
  92. } catch (GitAPIException | NoWorkTreeException | IOException e) {
  93. throw die(e.getMessage(), e);
  94. }
  95. }
  96. private void printStatus(org.eclipse.jgit.api.Status status)
  97. throws IOException {
  98. if (porcelain)
  99. printPorcelainStatus(status);
  100. else
  101. printLongStatus(status);
  102. }
  103. private void printPorcelainStatus(org.eclipse.jgit.api.Status status)
  104. throws IOException {
  105. Collection<String> added = status.getAdded();
  106. Collection<String> changed = status.getChanged();
  107. Collection<String> removed = status.getRemoved();
  108. Collection<String> modified = status.getModified();
  109. Collection<String> missing = status.getMissing();
  110. Map<String, StageState> conflicting = status.getConflictingStageState();
  111. // build a sorted list of all paths except untracked and ignored
  112. TreeSet<String> sorted = new TreeSet<>();
  113. sorted.addAll(added);
  114. sorted.addAll(changed);
  115. sorted.addAll(removed);
  116. sorted.addAll(modified);
  117. sorted.addAll(missing);
  118. sorted.addAll(conflicting.keySet());
  119. // list each path
  120. for (String path : sorted) {
  121. char x = ' ';
  122. char y = ' ';
  123. if (added.contains(path))
  124. x = 'A';
  125. else if (changed.contains(path))
  126. x = 'M';
  127. else if (removed.contains(path))
  128. x = 'D';
  129. if (modified.contains(path))
  130. y = 'M';
  131. else if (missing.contains(path))
  132. y = 'D';
  133. if (conflicting.containsKey(path)) {
  134. StageState stageState = conflicting.get(path);
  135. switch (stageState) {
  136. case BOTH_DELETED:
  137. x = 'D';
  138. y = 'D';
  139. break;
  140. case ADDED_BY_US:
  141. x = 'A';
  142. y = 'U';
  143. break;
  144. case DELETED_BY_THEM:
  145. x = 'U';
  146. y = 'D';
  147. break;
  148. case ADDED_BY_THEM:
  149. x = 'U';
  150. y = 'A';
  151. break;
  152. case DELETED_BY_US:
  153. x = 'D';
  154. y = 'U';
  155. break;
  156. case BOTH_ADDED:
  157. x = 'A';
  158. y = 'A';
  159. break;
  160. case BOTH_MODIFIED:
  161. x = 'U';
  162. y = 'U';
  163. break;
  164. default:
  165. throw new IllegalArgumentException("Unknown StageState: " //$NON-NLS-1$
  166. + stageState);
  167. }
  168. }
  169. printPorcelainLine(x, y, path);
  170. }
  171. // untracked are always at the end of the list
  172. if ("all".equals(untrackedFilesMode)) { //$NON-NLS-1$
  173. TreeSet<String> untracked = new TreeSet<>(
  174. status.getUntracked());
  175. for (String path : untracked)
  176. printPorcelainLine('?', '?', path);
  177. }
  178. }
  179. private void printPorcelainLine(char x, char y, String path)
  180. throws IOException {
  181. StringBuilder lineBuilder = new StringBuilder();
  182. lineBuilder.append(x).append(y).append(' ').append(path);
  183. outw.println(lineBuilder.toString());
  184. }
  185. private void printLongStatus(org.eclipse.jgit.api.Status status)
  186. throws IOException {
  187. // Print current branch name
  188. final Ref head = db.exactRef(Constants.HEAD);
  189. if (head != null && head.isSymbolic()) {
  190. String branch = Repository.shortenRefName(head.getLeaf().getName());
  191. outw.println(CLIText.formatLine(MessageFormat.format(
  192. CLIText.get().onBranch, branch)));
  193. } else
  194. outw.println(CLIText.formatLine(CLIText.get().notOnAnyBranch));
  195. // List changes
  196. boolean firstHeader = true;
  197. Collection<String> added = status.getAdded();
  198. Collection<String> changed = status.getChanged();
  199. Collection<String> removed = status.getRemoved();
  200. Collection<String> modified = status.getModified();
  201. Collection<String> missing = status.getMissing();
  202. Collection<String> untracked = status.getUntracked();
  203. Map<String, StageState> unmergedStates = status
  204. .getConflictingStageState();
  205. Collection<String> toBeCommitted = new ArrayList<>(added);
  206. toBeCommitted.addAll(changed);
  207. toBeCommitted.addAll(removed);
  208. int nbToBeCommitted = toBeCommitted.size();
  209. if (nbToBeCommitted > 0) {
  210. printSectionHeader(CLIText.get().changesToBeCommitted);
  211. printList(CLIText.get().statusNewFile,
  212. CLIText.get().statusModified, CLIText.get().statusRemoved,
  213. toBeCommitted, added, changed, removed);
  214. firstHeader = false;
  215. }
  216. Collection<String> notStagedForCommit = new ArrayList<>(modified);
  217. notStagedForCommit.addAll(missing);
  218. int nbNotStagedForCommit = notStagedForCommit.size();
  219. if (nbNotStagedForCommit > 0) {
  220. if (!firstHeader)
  221. printSectionHeader(""); //$NON-NLS-1$
  222. printSectionHeader(CLIText.get().changesNotStagedForCommit);
  223. printList(CLIText.get().statusModified,
  224. CLIText.get().statusRemoved, null, notStagedForCommit,
  225. modified, missing, null);
  226. firstHeader = false;
  227. }
  228. int nbUnmerged = unmergedStates.size();
  229. if (nbUnmerged > 0) {
  230. if (!firstHeader)
  231. printSectionHeader(""); //$NON-NLS-1$
  232. printSectionHeader(CLIText.get().unmergedPaths);
  233. printUnmerged(unmergedStates);
  234. firstHeader = false;
  235. }
  236. int nbUntracked = untracked.size();
  237. if (nbUntracked > 0 && ("all".equals(untrackedFilesMode))) { //$NON-NLS-1$
  238. if (!firstHeader)
  239. printSectionHeader(""); //$NON-NLS-1$
  240. printSectionHeader(CLIText.get().untrackedFiles);
  241. printList(untracked);
  242. }
  243. }
  244. /**
  245. * Print section header
  246. *
  247. * @param pattern
  248. * a {@link java.lang.String} object.
  249. * @param arguments
  250. * a {@link java.lang.Object} object.
  251. * @throws java.io.IOException
  252. */
  253. protected void printSectionHeader(String pattern, Object... arguments)
  254. throws IOException {
  255. if (!porcelain) {
  256. outw.println(CLIText.formatLine(MessageFormat.format(pattern,
  257. arguments)));
  258. if (!pattern.isEmpty())
  259. outw.println(CLIText.formatLine("")); //$NON-NLS-1$
  260. outw.flush();
  261. }
  262. }
  263. /**
  264. * Print String list
  265. *
  266. * @param list
  267. * a {@link java.util.Collection} object.
  268. * @return a int.
  269. * @throws java.io.IOException
  270. */
  271. protected int printList(Collection<String> list) throws IOException {
  272. if (!list.isEmpty()) {
  273. List<String> sortedList = new ArrayList<>(list);
  274. java.util.Collections.sort(sortedList);
  275. for (String filename : sortedList) {
  276. outw.println(CLIText.formatLine(String.format(
  277. statusFileListFormat, filename)));
  278. }
  279. outw.flush();
  280. return list.size();
  281. } else
  282. return 0;
  283. }
  284. /**
  285. * Print String list
  286. *
  287. * @param status1
  288. * a {@link java.lang.String} object.
  289. * @param status2
  290. * a {@link java.lang.String} object.
  291. * @param status3
  292. * a {@link java.lang.String} object.
  293. * @param list
  294. * a {@link java.util.Collection} object.
  295. * @param set1
  296. * a {@link java.util.Collection} object.
  297. * @param set2
  298. * a {@link java.util.Collection} object.
  299. * @param set3
  300. * a {@link java.util.Collection} object.
  301. * @return a int.
  302. * @throws java.io.IOException
  303. */
  304. protected int printList(String status1, String status2, String status3,
  305. Collection<String> list, Collection<String> set1,
  306. Collection<String> set2,
  307. Collection<String> set3)
  308. throws IOException {
  309. List<String> sortedList = new ArrayList<>(list);
  310. java.util.Collections.sort(sortedList);
  311. for (String filename : sortedList) {
  312. String prefix;
  313. if (set1.contains(filename))
  314. prefix = status1;
  315. else if (set2.contains(filename))
  316. prefix = status2;
  317. else
  318. // if (set3.contains(filename))
  319. prefix = status3;
  320. outw.println(CLIText.formatLine(String.format(
  321. statusFileListFormatWithPrefix, prefix, filename)));
  322. outw.flush();
  323. }
  324. return list.size();
  325. }
  326. private void printUnmerged(Map<String, StageState> unmergedStates)
  327. throws IOException {
  328. List<String> paths = new ArrayList<>(unmergedStates.keySet());
  329. Collections.sort(paths);
  330. for (String path : paths) {
  331. StageState state = unmergedStates.get(path);
  332. String stateDescription = getStageStateDescription(state);
  333. outw.println(CLIText.formatLine(String.format(
  334. statusFileListFormatUnmerged, stateDescription, path)));
  335. outw.flush();
  336. }
  337. }
  338. private static String getStageStateDescription(StageState stageState) {
  339. CLIText text = CLIText.get();
  340. switch (stageState) {
  341. case BOTH_DELETED:
  342. return text.statusBothDeleted;
  343. case ADDED_BY_US:
  344. return text.statusAddedByUs;
  345. case DELETED_BY_THEM:
  346. return text.statusDeletedByThem;
  347. case ADDED_BY_THEM:
  348. return text.statusAddedByThem;
  349. case DELETED_BY_US:
  350. return text.statusDeletedByUs;
  351. case BOTH_ADDED:
  352. return text.statusBothAdded;
  353. case BOTH_MODIFIED:
  354. return text.statusBothModified;
  355. default:
  356. throw new IllegalArgumentException("Unknown StageState: " //$NON-NLS-1$
  357. + stageState);
  358. }
  359. }
  360. }