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  	 * @param diff
74  	 */
75  	public Status(IndexDiff diff) {
76  		super();
77  		this.diff = diff;
78  		hasUncommittedChanges = !diff.getAdded().isEmpty() //
79  				|| !diff.getChanged().isEmpty() //
80  				|| !diff.getRemoved().isEmpty() //
81  				|| !diff.getMissing().isEmpty() //
82  				|| !diff.getModified().isEmpty() //
83  				|| !diff.getConflicting().isEmpty();
84  		clean = !hasUncommittedChanges //
85  				&& diff.getUntracked().isEmpty();
86  	}
87  
88  	/**
89  	 * @return true if no differences exist between the working-tree, the index,
90  	 *         and the current HEAD, false if differences do exist
91  	 */
92  	public boolean isClean() {
93  		return clean;
94  	}
95  
96  	/**
97  	 * @return true if any tracked file is changed
98  	 *
99  	 * @since 3.2
100 	 */
101 	public boolean hasUncommittedChanges() {
102 		return hasUncommittedChanges;
103 	}
104 
105 	/**
106 	 * @return list of files added to the index, not in HEAD (e.g. what you get
107 	 *         if you call 'git add ...' on a newly created file)
108 	 */
109 	public Set<String> getAdded() {
110 		return Collections.unmodifiableSet(diff.getAdded());
111 	}
112 
113 	/**
114 	 * @return list of files changed from HEAD to index (e.g. what you get if
115 	 *         you modify an existing file and call 'git add ...' on it)
116 	 */
117 	public Set<String> getChanged() {
118 		return Collections.unmodifiableSet(diff.getChanged());
119 	}
120 
121 	/**
122 	 * @return list of files removed from index, but in HEAD (e.g. what you get
123 	 *         if you call 'git rm ...' on a existing file)
124 	 */
125 	public Set<String> getRemoved() {
126 		return Collections.unmodifiableSet(diff.getRemoved());
127 	}
128 
129 	/**
130 	 * @return list of files in index, but not filesystem (e.g. what you get if
131 	 *         you call 'rm ...' on a existing file)
132 	 */
133 	public Set<String> getMissing() {
134 		return Collections.unmodifiableSet(diff.getMissing());
135 	}
136 
137 	/**
138 	 * @return list of files modified on disk relative to the index (e.g. what
139 	 *         you get if you modify an existing file without adding it to the
140 	 *         index)
141 	 */
142 	public Set<String> getModified() {
143 		return Collections.unmodifiableSet(diff.getModified());
144 	}
145 
146 	/**
147 	 * @return list of files that are not ignored, and not in the index. (e.g.
148 	 *         what you get if you create a new file without adding it to the
149 	 *         index)
150 	 */
151 	public Set<String> getUntracked() {
152 		return Collections.unmodifiableSet(diff.getUntracked());
153 	}
154 
155 	/**
156 	 * @return set of directories that are not ignored, and not in the index.
157 	 */
158 	public Set<String> getUntrackedFolders() {
159 		return Collections.unmodifiableSet(diff.getUntrackedFolders());
160 	}
161 
162 	/**
163 	 * @return list of files that are in conflict. (e.g what you get if you
164 	 *         modify file that was modified by someone else in the meantime)
165 	 */
166 	public Set<String> getConflicting() {
167 		return Collections.unmodifiableSet(diff.getConflicting());
168 	}
169 
170 	/**
171 	 * @return a map from conflicting path to its {@link StageState}.
172 	 * @since 3.0
173 	 */
174 	public Map<String, StageState> getConflictingStageState() {
175 		return Collections.unmodifiableMap(diff.getConflictingStageStates());
176 	}
177 
178 	/**
179 	 * @return set of files and folders that are ignored and not in the index.
180 	 */
181 	public Set<String> getIgnoredNotInIndex() {
182 		return Collections.unmodifiableSet(diff.getIgnoredNotInIndex());
183 	}
184 
185 	/**
186 	 * @return set of files and folders that are known to the repo and changed
187 	 *         either in the index or in the working tree.
188 	 *
189 	 * @since 3.2
190 	 */
191 	public Set<String> getUncommittedChanges() {
192 		Set<String> uncommittedChanges = new HashSet<String>();
193 		uncommittedChanges.addAll(diff.getAdded());
194 		uncommittedChanges.addAll(diff.getChanged());
195 		uncommittedChanges.addAll(diff.getRemoved());
196 		uncommittedChanges.addAll(diff.getMissing());
197 		uncommittedChanges.addAll(diff.getModified());
198 		uncommittedChanges.addAll(diff.getConflicting());
199 		return uncommittedChanges;
200 	}
201 }