2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org
.spearce
.jgit
.lib
;
43 import java
.io
.IOException
;
45 public class IndexDiffTest
extends RepositoryTestCase
{
46 public void testAdded() throws IOException
{
47 GitIndex index
= new GitIndex(db
);
48 writeTrashFile("file1", "file1");
49 writeTrashFile("dir/subfile", "dir/subfile");
50 Tree tree
= new Tree(db
);
52 index
.add(trash
, new File(trash
, "file1"));
53 index
.add(trash
, new File(trash
, "dir/subfile"));
54 IndexDiff diff
= new IndexDiff(tree
, index
);
56 assertEquals(2, diff
.getAdded().size());
57 assertTrue(diff
.getAdded().contains("file1"));
58 assertTrue(diff
.getAdded().contains("dir/subfile"));
59 assertEquals(0, diff
.getChanged().size());
60 assertEquals(0, diff
.getModified().size());
61 assertEquals(0, diff
.getRemoved().size());
64 public void testRemoved() throws IOException
{
65 GitIndex index
= new GitIndex(db
);
66 writeTrashFile("file2", "file2");
67 writeTrashFile("dir/file3", "dir/file3");
69 Tree tree
= new Tree(db
);
70 tree
.addFile("file2");
71 tree
.addFile("dir/file3");
72 assertEquals(2, tree
.memberCount());
73 tree
.findBlobMember("file2").setId(ObjectId
.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
74 Tree tree2
= (Tree
) tree
.findTreeMember("dir");
75 tree2
.findBlobMember("file3").setId(ObjectId
.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
76 tree2
.setId(new ObjectWriter(db
).writeTree(tree2
));
77 tree
.setId(new ObjectWriter(db
).writeTree(tree
));
79 IndexDiff diff
= new IndexDiff(tree
, index
);
81 assertEquals(2, diff
.getRemoved().size());
82 assertTrue(diff
.getRemoved().contains("file2"));
83 assertTrue(diff
.getRemoved().contains("dir/file3"));
84 assertEquals(0, diff
.getChanged().size());
85 assertEquals(0, diff
.getModified().size());
86 assertEquals(0, diff
.getAdded().size());
89 public void testModified() throws IOException
{
90 GitIndex index
= new GitIndex(db
);
93 index
.add(trash
, writeTrashFile("file2", "file2"));
94 index
.add(trash
, writeTrashFile("dir/file3", "dir/file3"));
96 writeTrashFile("dir/file3", "changed");
98 Tree tree
= new Tree(db
);
99 tree
.addFile("file2").setId(ObjectId
.fromString("0123456789012345678901234567890123456789"));
100 tree
.addFile("dir/file3").setId(ObjectId
.fromString("0123456789012345678901234567890123456789"));
101 assertEquals(2, tree
.memberCount());
103 Tree tree2
= (Tree
) tree
.findTreeMember("dir");
104 tree2
.setId(new ObjectWriter(db
).writeTree(tree2
));
105 tree
.setId(new ObjectWriter(db
).writeTree(tree
));
106 IndexDiff diff
= new IndexDiff(tree
, index
);
108 assertEquals(2, diff
.getChanged().size());
109 assertTrue(diff
.getChanged().contains("file2"));
110 assertTrue(diff
.getChanged().contains("dir/file3"));
111 assertEquals(1, diff
.getModified().size());
112 assertTrue(diff
.getModified().contains("dir/file3"));
113 assertEquals(0, diff
.getAdded().size());
114 assertEquals(0, diff
.getRemoved().size());
115 assertEquals(0, diff
.getMissing().size());
118 public void testUnchangedSimple() throws IOException
{
119 GitIndex index
= new GitIndex(db
);
121 index
.add(trash
, writeTrashFile("a.b", "a.b"));
122 index
.add(trash
, writeTrashFile("a.c", "a.c"));
123 index
.add(trash
, writeTrashFile("a=c", "a=c"));
124 index
.add(trash
, writeTrashFile("a=d", "a=d"));
126 Tree tree
= new Tree(db
);
127 // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
128 tree
.addFile("a.b").setId(ObjectId
.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
129 tree
.addFile("a.c").setId(ObjectId
.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
130 tree
.addFile("a=c").setId(ObjectId
.fromString("06022365ddbd7fb126761319633bf73517770714"));
131 tree
.addFile("a=d").setId(ObjectId
.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
133 tree
.setId(new ObjectWriter(db
).writeTree(tree
));
135 IndexDiff diff
= new IndexDiff(tree
, index
);
137 assertEquals(0, diff
.getChanged().size());
138 assertEquals(0, diff
.getAdded().size());
139 assertEquals(0, diff
.getRemoved().size());
140 assertEquals(0, diff
.getMissing().size());
141 assertEquals(0, diff
.getModified().size());
145 * This test has both files and directories that involve
146 * the tricky ordering used by Git.
148 * @throws IOException
150 public void testUnchangedComplex() throws IOException
{
151 GitIndex index
= new GitIndex(db
);
153 index
.add(trash
, writeTrashFile("a.b", "a.b"));
154 index
.add(trash
, writeTrashFile("a.c", "a.c"));
155 index
.add(trash
, writeTrashFile("a/b.b/b", "a/b.b/b"));
156 index
.add(trash
, writeTrashFile("a/b", "a/b"));
157 index
.add(trash
, writeTrashFile("a/c", "a/c"));
158 index
.add(trash
, writeTrashFile("a=c", "a=c"));
159 index
.add(trash
, writeTrashFile("a=d", "a=d"));
161 Tree tree
= new Tree(db
);
162 // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
163 tree
.addFile("a.b").setId(ObjectId
.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
164 tree
.addFile("a.c").setId(ObjectId
.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
165 tree
.addFile("a/b.b/b").setId(ObjectId
.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
166 tree
.addFile("a/b").setId(ObjectId
.fromString("db89c972fc57862eae378f45b74aca228037d415"));
167 tree
.addFile("a/c").setId(ObjectId
.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
168 tree
.addFile("a=c").setId(ObjectId
.fromString("06022365ddbd7fb126761319633bf73517770714"));
169 tree
.addFile("a=d").setId(ObjectId
.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
171 Tree tree3
= (Tree
) tree
.findTreeMember("a/b.b");
172 tree3
.setId(new ObjectWriter(db
).writeTree(tree3
));
173 Tree tree2
= (Tree
) tree
.findTreeMember("a");
174 tree2
.setId(new ObjectWriter(db
).writeTree(tree2
));
175 tree
.setId(new ObjectWriter(db
).writeTree(tree
));
177 IndexDiff diff
= new IndexDiff(tree
, index
);
179 assertEquals(0, diff
.getChanged().size());
180 assertEquals(0, diff
.getAdded().size());
181 assertEquals(0, diff
.getRemoved().size());
182 assertEquals(0, diff
.getMissing().size());
183 assertEquals(0, diff
.getModified().size());