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

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