1 package org
.spearce
.jgit
.lib
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
10 public class T0007_Index
extends RepositoryTestCase
{
12 static boolean canrungitstatus
;
15 canrungitstatus
= system(new File("."),"git --version") == 0;
16 } catch (IOException e
) {
17 System
.out
.println("Warning: cannot invvoke native git to validate index");
18 } catch (InterruptedException e
) {
23 private static int system(File dir
, String cmd
) throws IOException
,
24 InterruptedException
{
25 final Process process
= Runtime
.getRuntime().exec(cmd
, null, dir
);
29 InputStream s
= process
.getErrorStream();
30 for (int c
= s
.read(); c
!= -1; c
= s
.read()) {
31 System
.err
.print((char) c
);
34 } catch (IOException e1
) {
35 // TODO Auto-generated catch block
40 final Thread t2
= new Thread() {
44 InputStream e
= process
.getInputStream();
45 for (int c
= e
.read(); c
!= -1; c
= e
.read()) {
46 System
.out
.print((char) c
);
49 } catch (IOException e1
) {
50 // TODO Auto-generated catch block
57 process
.getOutputStream().close();
58 int ret
= process
.waitFor();
64 public void testCreateEmptyIndex() throws Exception
{
65 GitIndex index
= new GitIndex(db
);
67 // native git doesn't like an empty index
68 // assertEquals(0,system(trash,"git status"));
70 GitIndex indexr
= new GitIndex(db
);
72 assertEquals(0, indexr
.getMembers().length
);
75 public void testCreateSimpleSortTestIndex() throws Exception
{
76 GitIndex index
= new GitIndex(db
);
77 writeTrashFile("a/b", "data:a/b");
78 writeTrashFile("a:b", "data:a:b");
79 writeTrashFile("a.b", "data:a.b");
80 index
.add(trash
, new File(trash
, "a/b"));
81 index
.add(trash
, new File(trash
, "a:b"));
82 index
.add(trash
, new File(trash
, "a.b"));
85 assertEquals("a/b", index
.getEntry("a/b").getName());
86 assertEquals("a:b", index
.getEntry("a:b").getName());
87 assertEquals("a.b", index
.getEntry("a.b").getName());
88 assertNull(index
.getEntry("a*b"));
90 // Repeat test for re-read index
91 GitIndex indexr
= new GitIndex(db
);
93 assertEquals("a/b", indexr
.getEntry("a/b").getName());
94 assertEquals("a:b", indexr
.getEntry("a:b").getName());
95 assertEquals("a.b", indexr
.getEntry("a.b").getName());
96 assertNull(indexr
.getEntry("a*b"));
99 assertEquals(0, system(trash
, "git status"));
102 public void testUpdateSimpleSortTestIndex() throws Exception
{
103 GitIndex index
= new GitIndex(db
);
104 writeTrashFile("a/b", "data:a/b");
105 writeTrashFile("a:b", "data:a:b");
106 writeTrashFile("a.b", "data:a.b");
107 index
.add(trash
, new File(trash
, "a/b"));
108 index
.add(trash
, new File(trash
, "a:b"));
109 index
.add(trash
, new File(trash
, "a.b"));
110 writeTrashFile("a/b", "data:a/b modified");
111 index
.add(trash
, new File(trash
, "a/b"));
114 assertEquals(0, system(trash
, "git status"));
117 public void testWriteTree() throws Exception
{
118 GitIndex index
= new GitIndex(db
);
119 writeTrashFile("a/b", "data:a/b");
120 writeTrashFile("a:b", "data:a:b");
121 writeTrashFile("a.b", "data:a.b");
122 index
.add(trash
, new File(trash
, "a/b"));
123 index
.add(trash
, new File(trash
, "a:b"));
124 index
.add(trash
, new File(trash
, "a.b"));
127 ObjectId id
= index
.writeTree();
128 assertEquals("c696abc3ab8e091c665f49d00eb8919690b3aec3", id
.toString());
130 writeTrashFile("a/b", "data:a/b");
131 index
.add(trash
, new File(trash
, "a/b"));
134 assertEquals(0, system(trash
, "git status"));
137 public void testReadTree() throws Exception
{
139 GitIndex index
= new GitIndex(db
);
140 writeTrashFile("a/b", "data:a/b");
141 writeTrashFile("a:b", "data:a:b");
142 writeTrashFile("a.b", "data:a.b");
143 index
.add(trash
, new File(trash
, "a/b"));
144 index
.add(trash
, new File(trash
, "a:b"));
145 index
.add(trash
, new File(trash
, "a.b"));
148 ObjectId id
= index
.writeTree();
149 System
.out
.println("wrote id " + id
);
150 assertEquals("c696abc3ab8e091c665f49d00eb8919690b3aec3", id
.toString());
151 GitIndex index2
= new GitIndex(db
);
153 index2
.readTree(db
.mapTree(new ObjectId(
154 "c696abc3ab8e091c665f49d00eb8919690b3aec3")));
155 Entry
[] members
= index2
.getMembers();
156 assertEquals(3, members
.length
);
157 assertEquals("a.b", members
[0].getName());
158 assertEquals("a/b", members
[1].getName());
159 assertEquals("a:b", members
[2].getName());
160 assertEquals(3, members
.length
);
162 GitIndex indexr
= new GitIndex(db
);
164 Entry
[] membersr
= indexr
.getMembers();
165 assertEquals(3, membersr
.length
);
166 assertEquals("a.b", membersr
[0].getName());
167 assertEquals("a/b", membersr
[1].getName());
168 assertEquals("a:b", membersr
[2].getName());
169 assertEquals(3, membersr
.length
);
172 assertEquals(0, system(trash
, "git status"));
175 public void testReadTree2() throws Exception
{
176 // Prepare a larger tree to test some odd cases in tree writing
177 GitIndex index
= new GitIndex(db
);
178 File f1
= writeTrashFile("a/a/a/a", "data:a/a/a/a");
179 File f2
= writeTrashFile("a/c/c", "data:a/c/c");
180 File f3
= writeTrashFile("a/b", "data:a/b");
181 File f4
= writeTrashFile("a:b", "data:a:b");
182 File f5
= writeTrashFile("a/d", "data:a/d");
183 File f6
= writeTrashFile("a.b", "data:a.b");
184 index
.add(trash
, f1
);
185 index
.add(trash
, f2
);
186 index
.add(trash
, f3
);
187 index
.add(trash
, f4
);
188 index
.add(trash
, f5
);
189 index
.add(trash
, f6
);
191 ObjectId id
= index
.writeTree();
192 System
.out
.println("wrote id " + id
);
193 assertEquals("ba78e065e2c261d4f7b8f42107588051e87e18e9", id
.toString());
194 GitIndex index2
= new GitIndex(db
);
196 index2
.readTree(db
.mapTree(new ObjectId(
197 "ba78e065e2c261d4f7b8f42107588051e87e18e9")));
198 Entry
[] members
= index2
.getMembers();
199 assertEquals(6, members
.length
);
200 assertEquals("a.b", members
[0].getName());
201 assertEquals("a/a/a/a", members
[1].getName());
202 assertEquals("a/b", members
[2].getName());
203 assertEquals("a/c/c", members
[3].getName());
204 assertEquals("a/d", members
[4].getName());
205 assertEquals("a:b", members
[5].getName());
208 GitIndex indexr
= new GitIndex(db
);
210 Entry
[] membersr
= indexr
.getMembers();
211 assertEquals(6, membersr
.length
);
212 assertEquals("a.b", membersr
[0].getName());
213 assertEquals("a/a/a/a", membersr
[1].getName());
214 assertEquals("a/b", membersr
[2].getName());
215 assertEquals("a/c/c", membersr
[3].getName());
216 assertEquals("a/d", membersr
[4].getName());
217 assertEquals("a:b", membersr
[5].getName());
220 public void testDelete() throws Exception
{
221 GitIndex index
= new GitIndex(db
);
222 writeTrashFile("a/b", "data:a/b");
223 writeTrashFile("a:b", "data:a:b");
224 writeTrashFile("a.b", "data:a.b");
225 index
.add(trash
, new File(trash
, "a/b"));
226 index
.add(trash
, new File(trash
, "a:b"));
227 index
.add(trash
, new File(trash
, "a.b"));
230 index
.remove(trash
, new File(trash
, "a:b"));
232 assertEquals("a.b", index
.getMembers()[0].getName());
233 assertEquals("a/b", index
.getMembers()[1].getName());
235 GitIndex indexr
= new GitIndex(db
);
237 assertEquals("a.b", indexr
.getMembers()[0].getName());
238 assertEquals("a/b", indexr
.getMembers()[1].getName());
241 assertEquals(0, system(trash
, "git status"));
244 public void testCheckout() throws Exception
{
245 // Prepare tree, remote it and checkout
246 GitIndex index
= new GitIndex(db
);
247 File aslashb
= writeTrashFile("a/b", "data:a/b");
248 File acolonb
= writeTrashFile("a:b", "data:a:b");
249 File adotb
= writeTrashFile("a.b", "data:a.b");
250 index
.add(trash
, aslashb
);
251 index
.add(trash
, acolonb
);
252 index
.add(trash
, adotb
);
258 delete(aslashb
.getParentFile());
260 GitIndex index2
= new GitIndex(db
);
261 assertEquals(0, index2
.getMembers().length
);
263 index2
.readTree(db
.mapTree(new ObjectId(
264 "c696abc3ab8e091c665f49d00eb8919690b3aec3")));
266 index2
.checkout(trash
);
267 assertEquals("data:a/b", content(aslashb
));
268 assertEquals("data:a:b", content(acolonb
));
269 assertEquals("data:a.b", content(adotb
));
272 assertEquals(0, system(trash
, "git status"));
275 private String
content(File f
) throws IOException
{
276 byte[] buf
= new byte[(int) f
.length()];
277 FileInputStream is
= new FileInputStream(f
);
278 int read
= is
.read(buf
);
279 assertEquals(f
.length(), read
);
280 return new String(buf
, 0);
283 private void delete(File f
) throws IOException
{
285 throw new IOException("Failed to delete f");