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

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