View Javadoc
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  
45  import java.util.Collections;
46  import java.util.HashSet;
47  import java.util.Map;
48  import java.util.Set;
49  
50  import org.eclipse.jgit.lib.IndexDiff;
51  import org.eclipse.jgit.lib.IndexDiff.StageState;
52  
53  /**
54   * A class telling where the working-tree, the index and the current HEAD differ
55   * from each other. Collections are exposed containing the paths of the modified
56   * files. E.g. to find out which files are dirty in the working tree (modified
57   * but not added) you would inspect the collection returned by
58   * {@link #getModified()}.
59   * <p>
60   * The same path can be returned by multiple getters. E.g. if a modification has
61   * been added to the index and afterwards the corresponding working tree file is
62   * again modified this path will be returned by {@link #getModified()} and
63   * {@link #getChanged()}
64   */
65  public class Status {
66  	private final IndexDiff diff;
67  
68  	private final boolean clean;
69  
70  	private final boolean hasUncommittedChanges;
71  
72  	/**
73  	 * Constructor for Status.
74  	 *
75  	 * @param diff
76  	 *            the {@link org.eclipse.jgit.lib.IndexDiff} having the status
77  	 */
78  	public Status(IndexDiff diff) {
79  		super();
80  		this.diff = diff;
81  		hasUncommittedChanges = !diff.getAdded().isEmpty() //
82  				|| !diff.getChanged().isEmpty() //
83  				|| !diff.getRemoved().isEmpty() //
84  				|| !diff.getMissing().isEmpty() //
85  				|| !diff.getModified().isEmpty() //
86  				|| !diff.getConflicting().isEmpty();
87  		clean = !hasUncommittedChanges //
88  				&& diff.getUntracked().isEmpty();
89  	}
90  
91  	/**
92  	 * Whether the status is clean
93  	 *
94  	 * @return {@code true} if no differences exist between the working-tree,
95  	 *         the index, and the current HEAD, {@code false} if differences do
96  	 *         exist
97  	 */
98  	public boolean isClean() {
99  		return clean;
100 	}
101 
102 	/**
103 	 * Whether there are uncommitted changes
104 	 *
105 	 * @return {@code true} if any tracked file is changed
106 	 * @since 3.2
107 	 */
108 	public boolean hasUncommittedChanges() {
109 		return hasUncommittedChanges;
110 	}
111 
112 	/**
113 	 * Get files added to the index
114 	 *
115 	 * @return list of files added to the index, not in HEAD (e.g. what you get
116 	 *         if you call {@code git add ...} on a newly created file)
117 	 */
118 	public Set<String> getAdded() {
119 		return Collections.unmodifiableSet(diff.getAdded());
120 	}
121 
122 	/**
123 	 * Get changed files from HEAD to index
124 	 *
125 	 * @return list of files changed from HEAD to index (e.g. what you get if
126 	 *         you modify an existing file and call 'git add ...' on it)
127 	 */
128 	public Set<String> getChanged() {
129 		return Collections.unmodifiableSet(diff.getChanged());
130 	}
131 
132 	/**
133 	 * Get removed files
134 	 *
135 	 * @return list of files removed from index, but in HEAD (e.g. what you get
136 	 *         if you call 'git rm ...' on a existing file)
137 	 */
138 	public Set<String> getRemoved() {
139 		return Collections.unmodifiableSet(diff.getRemoved());
140 	}
141 
142 	/**
143 	 * Get missing files
144 	 *
145 	 * @return list of files in index, but not filesystem (e.g. what you get if
146 	 *         you call 'rm ...' on a existing file)
147 	 */
148 	public Set<String> getMissing() {
149 		return Collections.unmodifiableSet(diff.getMissing());
150 	}
151 
152 	/**
153 	 * Get modified files relative to the index
154 	 *
155 	 * @return list of files modified on disk relative to the index (e.g. what
156 	 *         you get if you modify an existing file without adding it to the
157 	 *         index)
158 	 */
159 	public Set<String> getModified() {
160 		return Collections.unmodifiableSet(diff.getModified());
161 	}
162 
163 	/**
164 	 * Get untracked files
165 	 *
166 	 * @return list of files that are not ignored, and not in the index. (e.g.
167 	 *         what you get if you create a new file without adding it to the
168 	 *         index)
169 	 */
170 	public Set<String> getUntracked() {
171 		return Collections.unmodifiableSet(diff.getUntracked());
172 	}
173 
174 	/**
175 	 * Get untracked folders
176 	 *
177 	 * @return set of directories that are not ignored, and not in the index.
178 	 */
179 	public Set<String> getUntrackedFolders() {
180 		return Collections.unmodifiableSet(diff.getUntrackedFolders());
181 	}
182 
183 	/**
184 	 * Get conflicting files
185 	 *
186 	 * @return list of files that are in conflict. (e.g what you get if you
187 	 *         modify file that was modified by someone else in the meantime)
188 	 */
189 	public Set<String> getConflicting() {
190 		return Collections.unmodifiableSet(diff.getConflicting());
191 	}
192 
193 	/**
194 	 * Get StageState of conflicting files
195 	 *
196 	 * @return a map from conflicting path to its
197 	 *         {@link org.eclipse.jgit.lib.IndexDiff.StageState}.
198 	 * @since 3.0
199 	 */
200 	public Map<String, StageState> getConflictingStageState() {
201 		return Collections.unmodifiableMap(diff.getConflictingStageStates());
202 	}
203 
204 	/**
205 	 * Get ignored files which are not in the index
206 	 *
207 	 * @return set of files and folders that are ignored and not in the index.
208 	 */
209 	public Set<String> getIgnoredNotInIndex() {
210 		return Collections.unmodifiableSet(diff.getIgnoredNotInIndex());
211 	}
212 
213 	/**
214 	 * Get uncommitted changes, i.e. all files changed in the index or working
215 	 * tree
216 	 *
217 	 * @return set of files and folders that are known to the repo and changed
218 	 *         either in the index or in the working tree.
219 	 * @since 3.2
220 	 */
221 	public Set<String> getUncommittedChanges() {
222 		Set<String> uncommittedChanges = new HashSet<>();
223 		uncommittedChanges.addAll(diff.getAdded());
224 		uncommittedChanges.addAll(diff.getChanged());
225 		uncommittedChanges.addAll(diff.getRemoved());
226 		uncommittedChanges.addAll(diff.getMissing());
227 		uncommittedChanges.addAll(diff.getModified());
228 		uncommittedChanges.addAll(diff.getConflicting());
229 		return uncommittedChanges;
230 	}
231 }