2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.lib
;
42 import java
.io
.IOException
;
43 import java
.util
.HashSet
;
45 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
48 * Compares the Index, a Tree, and the working directory
50 public class IndexDiff
{
51 private GitIndex index
;
55 * Construct an indexdiff for diffing the workdir against
61 public IndexDiff(Repository repository
) throws IOException
{
62 this.tree
= repository
.mapTree("HEAD");
63 this.index
= repository
.getIndex();
67 * Construct an indexdiff for diffing the workdir against both
68 * the index and a tree.
73 public IndexDiff(Tree tree
, GitIndex index
) {
78 boolean anyChanges
= false;
81 * Run the diff operation. Until this is called, all lists will be empty
82 * @return if anything is different between index, tree, and workdir
85 public boolean diff() throws IOException
{
86 final File root
= index
.getRepository().getWorkDir();
87 new IndexTreeWalker(index
, tree
, root
, new AbstractIndexTreeVisitor() {
88 public void visitEntry(TreeEntry treeEntry
, Entry indexEntry
, File file
) {
89 if (treeEntry
== null) {
90 added
.add(indexEntry
.getName());
92 } else if (indexEntry
== null) {
93 if (!(treeEntry
instanceof Tree
))
94 removed
.add(treeEntry
.getFullName());
97 if (!treeEntry
.getId().equals(indexEntry
.getObjectId())) {
98 changed
.add(indexEntry
.getName());
103 if (indexEntry
!= null) {
104 if (!file
.exists()) {
105 missing
.add(indexEntry
.getName());
108 if (indexEntry
.isModified(root
, true)) {
109 modified
.add(indexEntry
.getName());
120 HashSet
<String
> added
= new HashSet
<String
>();
121 HashSet
<String
> changed
= new HashSet
<String
>();
122 HashSet
<String
> removed
= new HashSet
<String
>();
123 HashSet
<String
> missing
= new HashSet
<String
>();
124 HashSet
<String
> modified
= new HashSet
<String
>();
127 * @return list of files added to the index, not in the tree
129 public HashSet
<String
> getAdded() {
134 * @return list of files changed from tree to index
136 public HashSet
<String
> getChanged() {
141 * @return list of files removed from index, but in tree
143 public HashSet
<String
> getRemoved() {
148 * @return list of files in index, but not filesystem
150 public HashSet
<String
> getMissing() {
155 * @return list of files on modified on disk relative to the index
157 public HashSet
<String
> getModified() {