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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2011, 2013 Christian Halstrick <christian.halstrick@sap.com>
  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.api;
  44. import java.util.Collections;
  45. import java.util.HashSet;
  46. import java.util.Map;
  47. import java.util.Set;
  48. import org.eclipse.jgit.lib.IndexDiff;
  49. import org.eclipse.jgit.lib.IndexDiff.StageState;
  50. /**
  51. * A class telling where the working-tree, the index and the current HEAD differ
  52. * from each other. Collections are exposed containing the paths of the modified
  53. * files. E.g. to find out which files are dirty in the working tree (modified
  54. * but not added) you would inspect the collection returned by
  55. * {@link #getModified()}.
  56. * <p>
  57. * The same path can be returned by multiple getters. E.g. if a modification has
  58. * been added to the index and afterwards the corresponding working tree file is
  59. * again modified this path will be returned by {@link #getModified()} and
  60. * {@link #getChanged()}
  61. */
  62. public class Status {
  63. private final IndexDiff diff;
  64. private final boolean clean;
  65. private final boolean hasUncommittedChanges;;
  66. /**
  67. * @param diff
  68. */
  69. public Status(IndexDiff diff) {
  70. super();
  71. this.diff = diff;
  72. hasUncommittedChanges = !diff.getAdded().isEmpty() //
  73. || !diff.getChanged().isEmpty() //
  74. || !diff.getRemoved().isEmpty() //
  75. || !diff.getMissing().isEmpty() //
  76. || !diff.getModified().isEmpty() //
  77. || !diff.getConflicting().isEmpty();
  78. clean = !hasUncommittedChanges //
  79. && diff.getUntracked().isEmpty();
  80. }
  81. /**
  82. * @return true if no differences exist between the working-tree, the index,
  83. * and the current HEAD, false if differences do exist
  84. */
  85. public boolean isClean() {
  86. return clean;
  87. }
  88. /**
  89. * @return true if any tracked file is changed
  90. *
  91. * @since 3.2
  92. */
  93. public boolean hasUncommittedChanges() {
  94. return hasUncommittedChanges;
  95. }
  96. /**
  97. * @return list of files added to the index, not in HEAD (e.g. what you get
  98. * if you call 'git add ...' on a newly created file)
  99. */
  100. public Set<String> getAdded() {
  101. return Collections.unmodifiableSet(diff.getAdded());
  102. }
  103. /**
  104. * @return list of files changed from HEAD to index (e.g. what you get if
  105. * you modify an existing file and call 'git add ...' on it)
  106. */
  107. public Set<String> getChanged() {
  108. return Collections.unmodifiableSet(diff.getChanged());
  109. }
  110. /**
  111. * @return list of files removed from index, but in HEAD (e.g. what you get
  112. * if you call 'git rm ...' on a existing file)
  113. */
  114. public Set<String> getRemoved() {
  115. return Collections.unmodifiableSet(diff.getRemoved());
  116. }
  117. /**
  118. * @return list of files in index, but not filesystem (e.g. what you get if
  119. * you call 'rm ...' on a existing file)
  120. */
  121. public Set<String> getMissing() {
  122. return Collections.unmodifiableSet(diff.getMissing());
  123. }
  124. /**
  125. * @return list of files modified on disk relative to the index (e.g. what
  126. * you get if you modify an existing file without adding it to the
  127. * index)
  128. */
  129. public Set<String> getModified() {
  130. return Collections.unmodifiableSet(diff.getModified());
  131. }
  132. /**
  133. * @return list of files that are not ignored, and not in the index. (e.g.
  134. * what you get if you create a new file without adding it to the
  135. * index)
  136. */
  137. public Set<String> getUntracked() {
  138. return Collections.unmodifiableSet(diff.getUntracked());
  139. }
  140. /**
  141. * @return set of directories that are not ignored, and not in the index.
  142. */
  143. public Set<String> getUntrackedFolders() {
  144. return Collections.unmodifiableSet(diff.getUntrackedFolders());
  145. }
  146. /**
  147. * @return list of files that are in conflict. (e.g what you get if you
  148. * modify file that was modified by someone else in the meantime)
  149. */
  150. public Set<String> getConflicting() {
  151. return Collections.unmodifiableSet(diff.getConflicting());
  152. }
  153. /**
  154. * @return a map from conflicting path to its {@link StageState}.
  155. * @since 3.0
  156. */
  157. public Map<String, StageState> getConflictingStageState() {
  158. return Collections.unmodifiableMap(diff.getConflictingStageStates());
  159. }
  160. /**
  161. * @return set of files and folders that are ignored and not in the index.
  162. */
  163. public Set<String> getIgnoredNotInIndex() {
  164. return Collections.unmodifiableSet(diff.getIgnoredNotInIndex());
  165. }
  166. /**
  167. * @return set of files and folders that are known to the repo and changed
  168. * either in the index or in the working tree.
  169. *
  170. * @since 3.2
  171. */
  172. public Set<String> getUncommittedChanges() {
  173. Set<String> uncommittedChanges = new HashSet<String>();
  174. uncommittedChanges.addAll(diff.getAdded());
  175. uncommittedChanges.addAll(diff.getChanged());
  176. uncommittedChanges.addAll(diff.getRemoved());
  177. uncommittedChanges.addAll(diff.getMissing());
  178. uncommittedChanges.addAll(diff.getModified());
  179. uncommittedChanges.addAll(diff.getConflicting());
  180. return uncommittedChanges;
  181. }
  182. }