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

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