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
44 package org.eclipse.jgit.lib;
45
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.Git;
53 import org.eclipse.jgit.api.errors.GitAPIException;
54 import org.eclipse.jgit.errors.NoWorkTreeException;
55 import org.eclipse.jgit.internal.storage.file.FileRepository;
56 import org.eclipse.jgit.junit.JGitTestUtil;
57 import org.eclipse.jgit.junit.RepositoryTestCase;
58 import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
59 import org.eclipse.jgit.treewalk.FileTreeIterator;
60 import org.junit.Before;
61 import org.junit.experimental.theories.DataPoints;
62 import org.junit.experimental.theories.Theories;
63 import org.junit.experimental.theories.Theory;
64 import org.junit.runner.RunWith;
65
66 @RunWith(Theories.class)
67 public class IndexDiffSubmoduleTest extends RepositoryTestCase {
68
69 protected FileRepository submodule_db;
70
71
72 protected File submodule_trash;
73
74 @DataPoints
75 public static IgnoreSubmoduleMode allModes[] = IgnoreSubmoduleMode.values();
76
77 @Override
78 @Before
79 public void setUp() throws Exception {
80 super.setUp();
81 FileRepository submoduleStandalone = createWorkRepository();
82 JGitTestUtil.writeTrashFile(submoduleStandalone, "fileInSubmodule",
83 "submodule");
84 Git submoduleStandaloneGit = Git.wrap(submoduleStandalone);
85 submoduleStandaloneGit.add().addFilepattern("fileInSubmodule").call();
86 submoduleStandaloneGit.commit().setMessage("add file to submodule")
87 .call();
88
89 submodule_db = (FileRepository) Git.wrap(db).submoduleAdd()
90 .setPath("modules/submodule")
91 .setURI(submoduleStandalone.getDirectory().toURI().toString())
92 .call();
93 submoduleStandalone.close();
94 submodule_trash = submodule_db.getWorkTree();
95 addRepoToClose(submodule_db);
96 writeTrashFile("fileInRoot", "root");
97 Git rootGit = Git.wrap(db);
98 rootGit.add().addFilepattern("fileInRoot").call();
99 rootGit.commit().setMessage("add submodule and root file").call();
100 }
101
102 @Theory
103 public void testInitiallyClean(IgnoreSubmoduleMode mode)
104 throws IOException {
105 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
106 new FileTreeIterator(db));
107 indexDiff.setIgnoreSubmoduleMode(mode);
108 assertFalse(indexDiff.diff());
109 }
110
111 @Theory
112 public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
113 throws IOException {
114 writeTrashFile("fileInRoot", "2");
115
116 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
117 new FileTreeIterator(db));
118 indexDiff.setIgnoreSubmoduleMode(mode);
119 assertTrue(indexDiff.diff());
120 }
121
122 @Theory
123 public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
124 throws IOException {
125 JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
126 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
127 new FileTreeIterator(db));
128 indexDiff.setIgnoreSubmoduleMode(mode);
129 if (mode.equals(IgnoreSubmoduleMode.ALL)
130 || mode.equals(IgnoreSubmoduleMode.DIRTY))
131 assertFalse("diff should be false with mode=" + mode,
132 indexDiff.diff());
133 else
134 assertTrue("diff should be true with mode=" + mode,
135 indexDiff.diff());
136 }
137
138 @Theory
139 public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
140 throws IOException, GitAPIException {
141 JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
142 Git submoduleGit = Git.wrap(submodule_db);
143 submoduleGit.add().addFilepattern("fileInSubmodule").call();
144 submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
145
146 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
147 new FileTreeIterator(db));
148 indexDiff.setIgnoreSubmoduleMode(mode);
149 if (mode.equals(IgnoreSubmoduleMode.ALL))
150 assertFalse("diff should be false with mode=" + mode,
151 indexDiff.diff());
152 else
153 assertTrue("diff should be true with mode=" + mode,
154 indexDiff.diff());
155 }
156
157 @Theory
158 public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
159 throws IOException, GitAPIException {
160 JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
161 Git submoduleGit = Git.wrap(submodule_db);
162 submoduleGit.add().addFilepattern("fileInSubmodule").call();
163
164 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
165 new FileTreeIterator(db));
166 indexDiff.setIgnoreSubmoduleMode(mode);
167 if (mode.equals(IgnoreSubmoduleMode.ALL)
168 || mode.equals(IgnoreSubmoduleMode.DIRTY))
169 assertFalse("diff should be false with mode=" + mode,
170 indexDiff.diff());
171 else
172 assertTrue("diff should be true with mode=" + mode,
173 indexDiff.diff());
174 }
175
176 @Theory
177 public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
178 throws IOException, GitAPIException, NoWorkTreeException {
179 JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
180 Git submoduleGit = Git.wrap(submodule_db);
181 submoduleGit.add().addFilepattern("fileInSubmodule").call();
182 JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
183
184 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
185 new FileTreeIterator(db));
186 indexDiff.setIgnoreSubmoduleMode(mode);
187 if (mode.equals(IgnoreSubmoduleMode.ALL)
188 || mode.equals(IgnoreSubmoduleMode.DIRTY))
189 assertFalse("diff should be false with mode=" + mode,
190 indexDiff.diff());
191 else
192 assertTrue("diff should be true with mode=" + mode,
193 indexDiff.diff());
194 }
195
196 @Theory
197 public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
198 throws IOException {
199 JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
200 "2");
201 IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
202 new FileTreeIterator(db));
203 indexDiff.setIgnoreSubmoduleMode(mode);
204 if (mode.equals(IgnoreSubmoduleMode.ALL)
205 || mode.equals(IgnoreSubmoduleMode.DIRTY)
206 || mode.equals(IgnoreSubmoduleMode.UNTRACKED))
207 assertFalse("diff should be false with mode=" + mode,
208 indexDiff.diff());
209 else
210 assertTrue("diff should be true with mode=" + mode,
211 indexDiff.diff());
212 }
213 }