您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Status.java 7.5KB

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