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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 2011, 2015 François Rey <eclipse.org_@_francois_._rey_._name> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.io.IOException;
  12. import java.text.MessageFormat;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.TreeSet;
  19. import org.eclipse.jgit.api.Git;
  20. import org.eclipse.jgit.api.StatusCommand;
  21. import org.eclipse.jgit.api.errors.GitAPIException;
  22. import org.eclipse.jgit.errors.NoWorkTreeException;
  23. import org.eclipse.jgit.lib.Constants;
  24. import org.eclipse.jgit.lib.IndexDiff.StageState;
  25. import org.eclipse.jgit.lib.Ref;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.pgm.internal.CLIText;
  28. import org.eclipse.jgit.pgm.opt.UntrackedFilesHandler;
  29. import org.kohsuke.args4j.Argument;
  30. import org.kohsuke.args4j.Option;
  31. import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
  32. /**
  33. * Status command
  34. */
  35. @Command(usage = "usage_Status", common = true)
  36. class Status extends TextBuiltin {
  37. protected final String statusFileListFormat = CLIText.get().statusFileListFormat;
  38. protected final String statusFileListFormatWithPrefix = CLIText.get().statusFileListFormatWithPrefix;
  39. protected final String statusFileListFormatUnmerged = CLIText.get().statusFileListFormatUnmerged;
  40. @Option(name = "--porcelain", usage = "usage_machineReadableOutput")
  41. protected boolean porcelain;
  42. @Option(name = "--untracked-files", aliases = { "-u", "-uno", "-uall" }, usage = "usage_untrackedFilesMode", handler = UntrackedFilesHandler.class)
  43. protected String untrackedFilesMode = "all"; // default value //$NON-NLS-1$
  44. @Argument(required = false, index = 0, metaVar = "metaVar_paths")
  45. @Option(name = "--", metaVar = "metaVar_paths", handler = RestOfArgumentsHandler.class)
  46. protected List<String> filterPaths;
  47. /** {@inheritDoc} */
  48. @Override
  49. protected void run() {
  50. try (Git git = new Git(db)) {
  51. StatusCommand statusCommand = git.status();
  52. if (filterPaths != null) {
  53. for (String path : filterPaths) {
  54. statusCommand.addPath(path);
  55. }
  56. }
  57. org.eclipse.jgit.api.Status status = statusCommand.call();
  58. printStatus(status);
  59. } catch (GitAPIException | NoWorkTreeException | IOException e) {
  60. throw die(e.getMessage(), e);
  61. }
  62. }
  63. private void printStatus(org.eclipse.jgit.api.Status status)
  64. throws IOException {
  65. if (porcelain)
  66. printPorcelainStatus(status);
  67. else
  68. printLongStatus(status);
  69. }
  70. private void printPorcelainStatus(org.eclipse.jgit.api.Status status)
  71. throws IOException {
  72. Collection<String> added = status.getAdded();
  73. Collection<String> changed = status.getChanged();
  74. Collection<String> removed = status.getRemoved();
  75. Collection<String> modified = status.getModified();
  76. Collection<String> missing = status.getMissing();
  77. Map<String, StageState> conflicting = status.getConflictingStageState();
  78. // build a sorted list of all paths except untracked and ignored
  79. TreeSet<String> sorted = new TreeSet<>();
  80. sorted.addAll(added);
  81. sorted.addAll(changed);
  82. sorted.addAll(removed);
  83. sorted.addAll(modified);
  84. sorted.addAll(missing);
  85. sorted.addAll(conflicting.keySet());
  86. // list each path
  87. for (String path : sorted) {
  88. char x = ' ';
  89. char y = ' ';
  90. if (added.contains(path))
  91. x = 'A';
  92. else if (changed.contains(path))
  93. x = 'M';
  94. else if (removed.contains(path))
  95. x = 'D';
  96. if (modified.contains(path))
  97. y = 'M';
  98. else if (missing.contains(path))
  99. y = 'D';
  100. if (conflicting.containsKey(path)) {
  101. StageState stageState = conflicting.get(path);
  102. switch (stageState) {
  103. case BOTH_DELETED:
  104. x = 'D';
  105. y = 'D';
  106. break;
  107. case ADDED_BY_US:
  108. x = 'A';
  109. y = 'U';
  110. break;
  111. case DELETED_BY_THEM:
  112. x = 'U';
  113. y = 'D';
  114. break;
  115. case ADDED_BY_THEM:
  116. x = 'U';
  117. y = 'A';
  118. break;
  119. case DELETED_BY_US:
  120. x = 'D';
  121. y = 'U';
  122. break;
  123. case BOTH_ADDED:
  124. x = 'A';
  125. y = 'A';
  126. break;
  127. case BOTH_MODIFIED:
  128. x = 'U';
  129. y = 'U';
  130. break;
  131. default:
  132. throw new IllegalArgumentException("Unknown StageState: " //$NON-NLS-1$
  133. + stageState);
  134. }
  135. }
  136. printPorcelainLine(x, y, path);
  137. }
  138. // untracked are always at the end of the list
  139. if ("all".equals(untrackedFilesMode)) { //$NON-NLS-1$
  140. TreeSet<String> untracked = new TreeSet<>(
  141. status.getUntracked());
  142. for (String path : untracked)
  143. printPorcelainLine('?', '?', path);
  144. }
  145. }
  146. private void printPorcelainLine(char x, char y, String path)
  147. throws IOException {
  148. StringBuilder lineBuilder = new StringBuilder();
  149. lineBuilder.append(x).append(y).append(' ').append(path);
  150. outw.println(lineBuilder.toString());
  151. }
  152. private void printLongStatus(org.eclipse.jgit.api.Status status)
  153. throws IOException {
  154. // Print current branch name
  155. final Ref head = db.exactRef(Constants.HEAD);
  156. if (head != null && head.isSymbolic()) {
  157. String branch = Repository.shortenRefName(head.getLeaf().getName());
  158. outw.println(CLIText.formatLine(MessageFormat.format(
  159. CLIText.get().onBranch, branch)));
  160. } else
  161. outw.println(CLIText.formatLine(CLIText.get().notOnAnyBranch));
  162. // List changes
  163. boolean firstHeader = true;
  164. Collection<String> added = status.getAdded();
  165. Collection<String> changed = status.getChanged();
  166. Collection<String> removed = status.getRemoved();
  167. Collection<String> modified = status.getModified();
  168. Collection<String> missing = status.getMissing();
  169. Collection<String> untracked = status.getUntracked();
  170. Map<String, StageState> unmergedStates = status
  171. .getConflictingStageState();
  172. Collection<String> toBeCommitted = new ArrayList<>(added);
  173. toBeCommitted.addAll(changed);
  174. toBeCommitted.addAll(removed);
  175. int nbToBeCommitted = toBeCommitted.size();
  176. if (nbToBeCommitted > 0) {
  177. printSectionHeader(CLIText.get().changesToBeCommitted);
  178. printList(CLIText.get().statusNewFile,
  179. CLIText.get().statusModified, CLIText.get().statusRemoved,
  180. toBeCommitted, added, changed, removed);
  181. firstHeader = false;
  182. }
  183. Collection<String> notStagedForCommit = new ArrayList<>(modified);
  184. notStagedForCommit.addAll(missing);
  185. int nbNotStagedForCommit = notStagedForCommit.size();
  186. if (nbNotStagedForCommit > 0) {
  187. if (!firstHeader)
  188. printSectionHeader(""); //$NON-NLS-1$
  189. printSectionHeader(CLIText.get().changesNotStagedForCommit);
  190. printList(CLIText.get().statusModified,
  191. CLIText.get().statusRemoved, null, notStagedForCommit,
  192. modified, missing, null);
  193. firstHeader = false;
  194. }
  195. int nbUnmerged = unmergedStates.size();
  196. if (nbUnmerged > 0) {
  197. if (!firstHeader)
  198. printSectionHeader(""); //$NON-NLS-1$
  199. printSectionHeader(CLIText.get().unmergedPaths);
  200. printUnmerged(unmergedStates);
  201. firstHeader = false;
  202. }
  203. int nbUntracked = untracked.size();
  204. if (nbUntracked > 0 && ("all".equals(untrackedFilesMode))) { //$NON-NLS-1$
  205. if (!firstHeader)
  206. printSectionHeader(""); //$NON-NLS-1$
  207. printSectionHeader(CLIText.get().untrackedFiles);
  208. printList(untracked);
  209. }
  210. }
  211. /**
  212. * Print section header
  213. *
  214. * @param pattern
  215. * a {@link java.lang.String} object.
  216. * @param arguments
  217. * a {@link java.lang.Object} object.
  218. * @throws java.io.IOException
  219. */
  220. protected void printSectionHeader(String pattern, Object... arguments)
  221. throws IOException {
  222. if (!porcelain) {
  223. outw.println(CLIText.formatLine(MessageFormat.format(pattern,
  224. arguments)));
  225. if (!pattern.isEmpty())
  226. outw.println(CLIText.formatLine("")); //$NON-NLS-1$
  227. outw.flush();
  228. }
  229. }
  230. /**
  231. * Print String list
  232. *
  233. * @param list
  234. * a {@link java.util.Collection} object.
  235. * @return a int.
  236. * @throws java.io.IOException
  237. */
  238. protected int printList(Collection<String> list) throws IOException {
  239. if (!list.isEmpty()) {
  240. List<String> sortedList = new ArrayList<>(list);
  241. java.util.Collections.sort(sortedList);
  242. for (String filename : sortedList) {
  243. outw.println(CLIText.formatLine(String.format(
  244. statusFileListFormat, filename)));
  245. }
  246. outw.flush();
  247. return list.size();
  248. }
  249. return 0;
  250. }
  251. /**
  252. * Print String list
  253. *
  254. * @param status1
  255. * a {@link java.lang.String} object.
  256. * @param status2
  257. * a {@link java.lang.String} object.
  258. * @param status3
  259. * a {@link java.lang.String} object.
  260. * @param list
  261. * a {@link java.util.Collection} object.
  262. * @param set1
  263. * a {@link java.util.Collection} object.
  264. * @param set2
  265. * a {@link java.util.Collection} object.
  266. * @param set3
  267. * a {@link java.util.Collection} object.
  268. * @return a int.
  269. * @throws java.io.IOException
  270. */
  271. protected int printList(String status1, String status2, String status3,
  272. Collection<String> list, Collection<String> set1,
  273. Collection<String> set2,
  274. Collection<String> set3)
  275. throws IOException {
  276. List<String> sortedList = new ArrayList<>(list);
  277. java.util.Collections.sort(sortedList);
  278. for (String filename : sortedList) {
  279. String prefix;
  280. if (set1.contains(filename))
  281. prefix = status1;
  282. else if (set2.contains(filename))
  283. prefix = status2;
  284. else
  285. // if (set3.contains(filename))
  286. prefix = status3;
  287. outw.println(CLIText.formatLine(String.format(
  288. statusFileListFormatWithPrefix, prefix, filename)));
  289. outw.flush();
  290. }
  291. return list.size();
  292. }
  293. private void printUnmerged(Map<String, StageState> unmergedStates)
  294. throws IOException {
  295. List<String> paths = new ArrayList<>(unmergedStates.keySet());
  296. Collections.sort(paths);
  297. for (String path : paths) {
  298. StageState state = unmergedStates.get(path);
  299. String stateDescription = getStageStateDescription(state);
  300. outw.println(CLIText.formatLine(String.format(
  301. statusFileListFormatUnmerged, stateDescription, path)));
  302. outw.flush();
  303. }
  304. }
  305. private static String getStageStateDescription(StageState stageState) {
  306. CLIText text = CLIText.get();
  307. switch (stageState) {
  308. case BOTH_DELETED:
  309. return text.statusBothDeleted;
  310. case ADDED_BY_US:
  311. return text.statusAddedByUs;
  312. case DELETED_BY_THEM:
  313. return text.statusDeletedByThem;
  314. case ADDED_BY_THEM:
  315. return text.statusAddedByThem;
  316. case DELETED_BY_US:
  317. return text.statusDeletedByUs;
  318. case BOTH_ADDED:
  319. return text.statusBothAdded;
  320. case BOTH_MODIFIED:
  321. return text.statusBothModified;
  322. default:
  323. throw new IllegalArgumentException("Unknown StageState: " //$NON-NLS-1$
  324. + stageState);
  325. }
  326. }
  327. }