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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.IndexDiff.StageState;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.lib.Repository;
  56. @Command(usage = "usage_Status", common = true)
  57. class Status extends TextBuiltin {
  58. protected final String lineFormat = CLIText.get().lineFormat;
  59. protected final String statusFileListFormat = CLIText.get().statusFileListFormat;
  60. protected final String statusFileListFormatWithPrefix = CLIText.get().statusFileListFormatWithPrefix;
  61. protected final String statusFileListFormatUnmerged = CLIText.get().statusFileListFormatUnmerged;
  62. @Override
  63. protected void run() throws Exception {
  64. // Print current branch name
  65. final Ref head = db.getRef(Constants.HEAD);
  66. boolean firstHeader = true;
  67. if (head != null && head.isSymbolic()) {
  68. String branch = Repository.shortenRefName(head.getLeaf().getName());
  69. outw.println(CLIText.formatLine(
  70. MessageFormat.format(CLIText.get().onBranch, branch)));
  71. } else
  72. outw.println(CLIText.formatLine(CLIText.get().notOnAnyBranch));
  73. // List changes
  74. org.eclipse.jgit.api.Status status = new Git(db).status().call();
  75. Collection<String> added = status.getAdded();
  76. Collection<String> changed = status.getChanged();
  77. Collection<String> removed = status.getRemoved();
  78. Collection<String> modified = status.getModified();
  79. Collection<String> missing = status.getMissing();
  80. Collection<String> untracked = status.getUntracked();
  81. Map<String, StageState> unmergedStates = status
  82. .getConflictingStageState();
  83. Collection<String> toBeCommitted = new ArrayList<String>(added);
  84. toBeCommitted.addAll(changed);
  85. toBeCommitted.addAll(removed);
  86. int nbToBeCommitted = toBeCommitted.size();
  87. if (nbToBeCommitted > 0) {
  88. printSectionHeader(CLIText.get().changesToBeCommitted);
  89. printList(CLIText.get().statusNewFile,
  90. CLIText.get().statusModified, CLIText.get().statusRemoved,
  91. toBeCommitted, added, changed, removed);
  92. firstHeader = false;
  93. }
  94. Collection<String> notStagedForCommit = new ArrayList<String>(modified);
  95. notStagedForCommit.addAll(missing);
  96. int nbNotStagedForCommit = notStagedForCommit.size();
  97. if (nbNotStagedForCommit > 0) {
  98. if (!firstHeader)
  99. printSectionHeader(""); //$NON-NLS-1$
  100. printSectionHeader(CLIText.get().changesNotStagedForCommit);
  101. printList(CLIText.get().statusModified,
  102. CLIText.get().statusRemoved, null, notStagedForCommit,
  103. modified, missing, null);
  104. firstHeader = false;
  105. }
  106. int nbUnmerged = unmergedStates.size();
  107. if (nbUnmerged > 0) {
  108. if (!firstHeader)
  109. printSectionHeader(""); //$NON-NLS-1$
  110. printSectionHeader(CLIText.get().unmergedPaths);
  111. printUnmerged(unmergedStates);
  112. firstHeader = false;
  113. }
  114. int nbUntracked = untracked.size();
  115. if (nbUntracked > 0) {
  116. if (!firstHeader)
  117. printSectionHeader(""); //$NON-NLS-1$
  118. printSectionHeader(CLIText.get().untrackedFiles);
  119. printList(untracked);
  120. }
  121. }
  122. protected void printSectionHeader(String pattern, Object... arguments)
  123. throws IOException {
  124. outw.println(CLIText.formatLine(MessageFormat
  125. .format(pattern, arguments)));
  126. if (!pattern.equals("")) //$NON-NLS-1$
  127. outw.println(CLIText.formatLine("")); //$NON-NLS-1$
  128. outw.flush();
  129. }
  130. protected int printList(Collection<String> list) throws IOException {
  131. if (!list.isEmpty()) {
  132. List<String> sortedList = new ArrayList<String>(list);
  133. java.util.Collections.sort(sortedList);
  134. for (String filename : sortedList) {
  135. outw.println(CLIText.formatLine(String.format(
  136. statusFileListFormat, filename)));
  137. }
  138. outw.flush();
  139. return list.size();
  140. } else
  141. return 0;
  142. }
  143. protected int printList(String status1, String status2, String status3,
  144. Collection<String> list, Collection<String> set1,
  145. Collection<String> set2,
  146. @SuppressWarnings("unused") Collection<String> set3)
  147. throws IOException {
  148. List<String> sortedList = new ArrayList<String>(list);
  149. java.util.Collections.sort(sortedList);
  150. for (String filename : sortedList) {
  151. String prefix;
  152. if (set1.contains(filename))
  153. prefix = status1;
  154. else if (set2.contains(filename))
  155. prefix = status2;
  156. else
  157. // if (set3.contains(filename))
  158. prefix = status3;
  159. outw.println(CLIText.formatLine(String.format(
  160. statusFileListFormatWithPrefix, prefix, filename)));
  161. outw.flush();
  162. }
  163. return list.size();
  164. }
  165. private void printUnmerged(Map<String, StageState> unmergedStates)
  166. throws IOException {
  167. List<String> paths = new ArrayList<String>(unmergedStates.keySet());
  168. Collections.sort(paths);
  169. for (String path : paths) {
  170. StageState state = unmergedStates.get(path);
  171. String stateDescription = getStageStateDescription(state);
  172. outw.println(CLIText.formatLine(String.format(
  173. statusFileListFormatUnmerged, stateDescription, path)));
  174. outw.flush();
  175. }
  176. }
  177. private static String getStageStateDescription(StageState stageState) {
  178. CLIText text = CLIText.get();
  179. switch (stageState) {
  180. case BOTH_DELETED:
  181. return text.statusBothDeleted;
  182. case ADDED_BY_US:
  183. return text.statusAddedByUs;
  184. case DELETED_BY_THEM:
  185. return text.statusDeletedByThem;
  186. case ADDED_BY_THEM:
  187. return text.statusAddedByThem;
  188. case DELETED_BY_US:
  189. return text.statusDeletedByUs;
  190. case BOTH_ADDED:
  191. return text.statusBothAdded;
  192. case BOTH_MODIFIED:
  193. return text.statusBothModified;
  194. default:
  195. throw new IllegalArgumentException("Unknown StageState: " //$NON-NLS-1$
  196. + stageState);
  197. }
  198. }
  199. }