1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.File;
50 import java.io.IOException;
51
52 import org.eclipse.jgit.api.errors.GitAPIException;
53 import org.eclipse.jgit.api.errors.NoFilepatternException;
54 import org.eclipse.jgit.errors.NoWorkTreeException;
55 import org.eclipse.jgit.junit.RepositoryTestCase;
56 import org.eclipse.jgit.lib.Sets;
57 import org.junit.Test;
58
59 public class StatusCommandTest extends RepositoryTestCase {
60
61 @Test
62 public void testEmptyStatus() throws NoWorkTreeException,
63 GitAPIException {
64 Git git = new Git(db);
65
66 Status stat = git.status().call();
67 assertEquals(0, stat.getAdded().size());
68 assertEquals(0, stat.getChanged().size());
69 assertEquals(0, stat.getMissing().size());
70 assertEquals(0, stat.getModified().size());
71 assertEquals(0, stat.getRemoved().size());
72 assertEquals(0, stat.getUntracked().size());
73 }
74
75 @Test
76 public void testDifferentStates() throws IOException,
77 NoFilepatternException, GitAPIException {
78 Git git = new Git(db);
79 writeTrashFile("a", "content of a");
80 writeTrashFile("b", "content of b");
81 writeTrashFile("c", "content of c");
82 git.add().addFilepattern("a").addFilepattern("b").call();
83 Status stat = git.status().call();
84 assertEquals(Sets.of("a", "b"), stat.getAdded());
85 assertEquals(0, stat.getChanged().size());
86 assertEquals(0, stat.getMissing().size());
87 assertEquals(0, stat.getModified().size());
88 assertEquals(0, stat.getRemoved().size());
89 assertEquals(Sets.of("c"), stat.getUntracked());
90 git.commit().setMessage("initial").call();
91
92 writeTrashFile("a", "modified content of a");
93 writeTrashFile("b", "modified content of b");
94 writeTrashFile("d", "content of d");
95 git.add().addFilepattern("a").addFilepattern("d").call();
96 writeTrashFile("a", "again modified content of a");
97 stat = git.status().call();
98 assertEquals(Sets.of("d"), stat.getAdded());
99 assertEquals(Sets.of("a"), stat.getChanged());
100 assertEquals(0, stat.getMissing().size());
101 assertEquals(Sets.of("b", "a"), stat.getModified());
102 assertEquals(0, stat.getRemoved().size());
103 assertEquals(Sets.of("c"), stat.getUntracked());
104 git.add().addFilepattern(".").call();
105 git.commit().setMessage("second").call();
106
107 stat = git.status().call();
108 assertEquals(0, stat.getAdded().size());
109 assertEquals(0, stat.getChanged().size());
110 assertEquals(0, stat.getMissing().size());
111 assertEquals(0, stat.getModified().size());
112 assertEquals(0, stat.getRemoved().size());
113 assertEquals(0, stat.getUntracked().size());
114
115 deleteTrashFile("a");
116 assertFalse(new File(git.getRepository().getWorkTree(), "a").exists());
117 git.add().addFilepattern("a").setUpdate(true).call();
118 writeTrashFile("a", "recreated content of a");
119 stat = git.status().call();
120 assertEquals(0, stat.getAdded().size());
121 assertEquals(0, stat.getChanged().size());
122 assertEquals(0, stat.getMissing().size());
123 assertEquals(0, stat.getModified().size());
124 assertEquals(Sets.of("a"), stat.getRemoved());
125 assertEquals(Sets.of("a"), stat.getUntracked());
126 git.commit().setMessage("t").call();
127
128 writeTrashFile("sub/a", "sub-file");
129 stat = git.status().call();
130 assertEquals(1, stat.getUntrackedFolders().size());
131 assertTrue(stat.getUntrackedFolders().contains("sub"));
132 }
133
134 @Test
135 public void testDifferentStatesWithPaths() throws IOException,
136 NoFilepatternException, GitAPIException {
137 Git git = new Git(db);
138 writeTrashFile("a", "content of a");
139 writeTrashFile("D/b", "content of b");
140 writeTrashFile("D/c", "content of c");
141 writeTrashFile("D/D/d", "content of d");
142 git.add().addFilepattern(".").call();
143
144 writeTrashFile("a", "new content of a");
145 writeTrashFile("D/b", "new content of b");
146 writeTrashFile("D/D/d", "new content of d");
147
148
149
150 Status stat = git.status().addPath("x").call();
151 assertEquals(0, stat.getModified().size());
152
153
154 stat = git.status().addPath("a").call();
155 assertEquals(Sets.of("a"), stat.getModified());
156
157
158 stat = git.status().addPath("D").call();
159 assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified());
160
161
162 stat = git.status().addPath("D/D").addPath("a").call();
163 assertEquals(Sets.of("a", "D/D/d"), stat.getModified());
164
165
166 stat = git.status().call();
167 assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified());
168 }
169 }