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

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