Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / IndexDiff.java
blob86f83b99000b04d8a07e6eb41ac810957266e971
1 /*
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
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
22 * written permission.
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;
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.HashSet;
45 import org.spearce.jgit.lib.GitIndex.Entry;
47 /**
48 * Compares the Index, a Tree, and the working directory
50 public class IndexDiff {
51 private GitIndex index;
52 private Tree tree;
54 /**
55 * Construct an indexdiff for diffing the workdir against
56 * the index.
58 * @param repository
59 * @throws IOException
61 public IndexDiff(Repository repository) throws IOException {
62 this.tree = repository.mapTree("HEAD");
63 this.index = repository.getIndex();
66 /**
67 * Construct an indexdiff for diffing the workdir against both
68 * the index and a tree.
70 * @param tree
71 * @param index
73 public IndexDiff(Tree tree, GitIndex index) {
74 this.tree = tree;
75 this.index = index;
78 boolean anyChanges = false;
80 /**
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
83 * @throws IOException
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());
91 anyChanges = true;
92 } else if (indexEntry == null) {
93 if (!(treeEntry instanceof Tree))
94 removed.add(treeEntry.getFullName());
95 anyChanges = true;
96 } else {
97 if (!treeEntry.getId().equals(indexEntry.getObjectId())) {
98 changed.add(indexEntry.getName());
99 anyChanges = true;
103 if (indexEntry != null) {
104 if (!file.exists()) {
105 missing.add(indexEntry.getName());
106 anyChanges = true;
107 } else {
108 if (indexEntry.isModified(root, true)) {
109 modified.add(indexEntry.getName());
110 anyChanges = true;
115 }).walk();
117 return anyChanges;
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() {
130 return added;
134 * @return list of files changed from tree to index
136 public HashSet<String> getChanged() {
137 return changed;
141 * @return list of files removed from index, but in tree
143 public HashSet<String> getRemoved() {
144 return removed;
148 * @return list of files in index, but not filesystem
150 public HashSet<String> getMissing() {
151 return missing;
155 * @return list of files on modified on disk relative to the index
157 public HashSet<String> getModified() {
158 return modified;