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

Status.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2011, 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.text.MessageFormat;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.List;
  48. import org.eclipse.jgit.api.Git;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.Ref;
  51. import org.eclipse.jgit.lib.Repository;
  52. @Command(usage = "usage_Status", common = true)
  53. class Status extends TextBuiltin {
  54. protected final String lineFormat = CLIText.get().lineFormat;
  55. protected final String statusFileListFormat = CLIText.get().statusFileListFormat;
  56. protected final String statusFileListFormatWithPrefix = CLIText.get().statusFileListFormatWithPrefix;
  57. @Override
  58. protected void run() throws Exception {
  59. // Print current branch name
  60. final Ref head = db.getRef(Constants.HEAD);
  61. boolean firstHeader = true;
  62. if (head != null && head.isSymbolic()) {
  63. String branch = Repository.shortenRefName(head.getLeaf().getName());
  64. out.println(CLIText.formatLine(
  65. MessageFormat.format(CLIText.get().onBranch, branch)));
  66. } else
  67. out.println(CLIText.formatLine(CLIText.get().notOnAnyBranch));
  68. // List changes
  69. org.eclipse.jgit.api.Status status = new Git(db).status().call();
  70. Collection<String> added = status.getAdded();
  71. Collection<String> changed = status.getChanged();
  72. Collection<String> removed = status.getRemoved();
  73. Collection<String> modified = status.getModified();
  74. Collection<String> missing = status.getMissing();
  75. Collection<String> untracked = status.getUntracked();
  76. Collection<String> unmerged = status.getConflicting();
  77. Collection<String> toBeCommitted = new ArrayList<String>(added);
  78. toBeCommitted.addAll(changed);
  79. toBeCommitted.addAll(removed);
  80. int nbToBeCommitted = toBeCommitted.size();
  81. if (nbToBeCommitted > 0) {
  82. printSectionHeader(CLIText.get().changesToBeCommitted);
  83. printList(CLIText.get().statusNewFile,
  84. CLIText.get().statusModified, CLIText.get().statusRemoved,
  85. toBeCommitted, added, changed, removed);
  86. firstHeader = false;
  87. }
  88. Collection<String> notStagedForCommit = new ArrayList<String>(modified);
  89. notStagedForCommit.addAll(missing);
  90. int nbNotStagedForCommit = notStagedForCommit.size();
  91. if (nbNotStagedForCommit > 0) {
  92. if (!firstHeader)
  93. printSectionHeader("");
  94. printSectionHeader(CLIText.get().changesNotStagedForCommit);
  95. printList(CLIText.get().statusModified,
  96. CLIText.get().statusRemoved, null, notStagedForCommit,
  97. modified, missing, null);
  98. firstHeader = false;
  99. }
  100. int nbUnmerged = unmerged.size();
  101. if (nbUnmerged > 0) {
  102. if (!firstHeader)
  103. printSectionHeader("");
  104. printSectionHeader(CLIText.get().unmergedPaths);
  105. printList(unmerged);
  106. firstHeader = false;
  107. }
  108. int nbUntracked = untracked.size();
  109. if (nbUntracked > 0) {
  110. if (!firstHeader)
  111. printSectionHeader("");
  112. printSectionHeader(CLIText.get().untrackedFiles);
  113. printList(untracked);
  114. }
  115. }
  116. protected void printSectionHeader(String pattern, Object... arguments) {
  117. out.println(CLIText.formatLine(MessageFormat.format(pattern, arguments)));
  118. if (!pattern.equals(""))
  119. out.println(CLIText.formatLine(""));
  120. out.flush();
  121. }
  122. protected int printList(Collection<String> list) {
  123. if (!list.isEmpty()) {
  124. List<String> sortedList = new ArrayList<String>(list);
  125. java.util.Collections.sort(sortedList);
  126. for (String filename : sortedList) {
  127. out.println(CLIText.formatLine(String.format(
  128. statusFileListFormat, filename)));
  129. }
  130. out.flush();
  131. return list.size();
  132. } else
  133. return 0;
  134. }
  135. protected int printList(String status1, String status2, String status3,
  136. Collection<String> list, Collection<String> set1,
  137. Collection<String> set2,
  138. @SuppressWarnings("unused") Collection<String> set3) {
  139. List<String> sortedList = new ArrayList<String>(list);
  140. java.util.Collections.sort(sortedList);
  141. for (String filename : sortedList) {
  142. String prefix;
  143. if (set1.contains(filename))
  144. prefix = status1;
  145. else if (set2.contains(filename))
  146. prefix = status2;
  147. else
  148. // if (set3.contains(filename))
  149. prefix = status3;
  150. out.println(CLIText.formatLine(String.format(
  151. statusFileListFormatWithPrefix, prefix, filename)));
  152. out.flush();
  153. }
  154. return list.size();
  155. }
  156. }