View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import java.util.Collections;
13  import java.util.HashSet;
14  import java.util.Map;
15  import java.util.Set;
16  
17  import org.eclipse.jgit.lib.IndexDiff;
18  import org.eclipse.jgit.lib.IndexDiff.StageState;
19  
20  /**
21   * A class telling where the working-tree, the index and the current HEAD differ
22   * from each other. Collections are exposed containing the paths of the modified
23   * files. E.g. to find out which files are dirty in the working tree (modified
24   * but not added) you would inspect the collection returned by
25   * {@link #getModified()}.
26   * <p>
27   * The same path can be returned by multiple getters. E.g. if a modification has
28   * been added to the index and afterwards the corresponding working tree file is
29   * again modified this path will be returned by {@link #getModified()} and
30   * {@link #getChanged()}
31   */
32  public class Status {
33  	private final IndexDiff diff;
34  
35  	private final boolean clean;
36  
37  	private final boolean hasUncommittedChanges;
38  
39  	/**
40  	 * Constructor for Status.
41  	 *
42  	 * @param diff
43  	 *            the {@link org.eclipse.jgit.lib.IndexDiff} having the status
44  	 */
45  	public Status(IndexDiff diff) {
46  		super();
47  		this.diff = diff;
48  		hasUncommittedChanges = !diff.getAdded().isEmpty() //
49  				|| !diff.getChanged().isEmpty() //
50  				|| !diff.getRemoved().isEmpty() //
51  				|| !diff.getMissing().isEmpty() //
52  				|| !diff.getModified().isEmpty() //
53  				|| !diff.getConflicting().isEmpty();
54  		clean = !hasUncommittedChanges //
55  				&& diff.getUntracked().isEmpty();
56  	}
57  
58  	/**
59  	 * Whether the status is clean
60  	 *
61  	 * @return {@code true} if no differences exist between the working-tree,
62  	 *         the index, and the current HEAD, {@code false} if differences do
63  	 *         exist
64  	 */
65  	public boolean isClean() {
66  		return clean;
67  	}
68  
69  	/**
70  	 * Whether there are uncommitted changes
71  	 *
72  	 * @return {@code true} if any tracked file is changed
73  	 * @since 3.2
74  	 */
75  	public boolean hasUncommittedChanges() {
76  		return hasUncommittedChanges;
77  	}
78  
79  	/**
80  	 * Get files added to the index
81  	 *
82  	 * @return list of files added to the index, not in HEAD (e.g. what you get
83  	 *         if you call {@code git add ...} on a newly created file)
84  	 */
85  	public Set<String> getAdded() {
86  		return Collections.unmodifiableSet(diff.getAdded());
87  	}
88  
89  	/**
90  	 * Get changed files from HEAD to index
91  	 *
92  	 * @return list of files changed from HEAD to index (e.g. what you get if
93  	 *         you modify an existing file and call 'git add ...' on it)
94  	 */
95  	public Set<String> getChanged() {
96  		return Collections.unmodifiableSet(diff.getChanged());
97  	}
98  
99  	/**
100 	 * Get removed files
101 	 *
102 	 * @return list of files removed from index, but in HEAD (e.g. what you get
103 	 *         if you call 'git rm ...' on a existing file)
104 	 */
105 	public Set<String> getRemoved() {
106 		return Collections.unmodifiableSet(diff.getRemoved());
107 	}
108 
109 	/**
110 	 * Get missing files
111 	 *
112 	 * @return list of files in index, but not filesystem (e.g. what you get if
113 	 *         you call 'rm ...' on a existing file)
114 	 */
115 	public Set<String> getMissing() {
116 		return Collections.unmodifiableSet(diff.getMissing());
117 	}
118 
119 	/**
120 	 * Get modified files relative to the index
121 	 *
122 	 * @return list of files modified on disk relative to the index (e.g. what
123 	 *         you get if you modify an existing file without adding it to the
124 	 *         index)
125 	 */
126 	public Set<String> getModified() {
127 		return Collections.unmodifiableSet(diff.getModified());
128 	}
129 
130 	/**
131 	 * Get untracked files
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 	/**
142 	 * Get untracked folders
143 	 *
144 	 * @return set of directories that are not ignored, and not in the index.
145 	 */
146 	public Set<String> getUntrackedFolders() {
147 		return Collections.unmodifiableSet(diff.getUntrackedFolders());
148 	}
149 
150 	/**
151 	 * Get conflicting files
152 	 *
153 	 * @return list of files that are in conflict. (e.g what you get if you
154 	 *         modify file that was modified by someone else in the meantime)
155 	 */
156 	public Set<String> getConflicting() {
157 		return Collections.unmodifiableSet(diff.getConflicting());
158 	}
159 
160 	/**
161 	 * Get StageState of conflicting files
162 	 *
163 	 * @return a map from conflicting path to its
164 	 *         {@link org.eclipse.jgit.lib.IndexDiff.StageState}.
165 	 * @since 3.0
166 	 */
167 	public Map<String, StageState> getConflictingStageState() {
168 		return Collections.unmodifiableMap(diff.getConflictingStageStates());
169 	}
170 
171 	/**
172 	 * Get ignored files which are not in the index
173 	 *
174 	 * @return set of files and folders that are ignored and not in the index.
175 	 */
176 	public Set<String> getIgnoredNotInIndex() {
177 		return Collections.unmodifiableSet(diff.getIgnoredNotInIndex());
178 	}
179 
180 	/**
181 	 * Get uncommitted changes, i.e. all files changed in the index or working
182 	 * tree
183 	 *
184 	 * @return set of files and folders that are known to the repo and changed
185 	 *         either in the index or in the working tree.
186 	 * @since 3.2
187 	 */
188 	public Set<String> getUncommittedChanges() {
189 		Set<String> uncommittedChanges = new HashSet<>();
190 		uncommittedChanges.addAll(diff.getAdded());
191 		uncommittedChanges.addAll(diff.getChanged());
192 		uncommittedChanges.addAll(diff.getRemoved());
193 		uncommittedChanges.addAll(diff.getMissing());
194 		uncommittedChanges.addAll(diff.getModified());
195 		uncommittedChanges.addAll(diff.getConflicting());
196 		return uncommittedChanges;
197 	}
198 }