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.pgm;
44
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47 import static org.junit.Assert.fail;
48
49 import java.io.File;
50 import java.util.Arrays;
51
52 import org.eclipse.jgit.api.Git;
53 import org.eclipse.jgit.junit.JGitTestUtil;
54 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
55 import org.eclipse.jgit.lib.Repository;
56 import org.junit.Before;
57 import org.junit.Test;
58
59 public class RepoTest extends CLIRepositoryTestCase {
60 private Repository defaultDb;
61 private Repository notDefaultDb;
62 private Repository groupADb;
63 private Repository groupBDb;
64
65 private String rootUri;
66 private String defaultUri;
67 private String notDefaultUri;
68 private String groupAUri;
69 private String groupBUri;
70
71 @Override
72 @Before
73 public void setUp() throws Exception {
74 super.setUp();
75
76 defaultDb = createWorkRepository();
77 try (Git git = new Git(defaultDb)) {
78 JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world");
79 git.add().addFilepattern("hello.txt").call();
80 git.commit().setMessage("Initial commit").call();
81 }
82
83 notDefaultDb = createWorkRepository();
84 try (Git git = new Git(notDefaultDb)) {
85 JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
86 git.add().addFilepattern("world.txt").call();
87 git.commit().setMessage("Initial commit").call();
88 }
89
90 groupADb = createWorkRepository();
91 try (Git git = new Git(groupADb)) {
92 JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
93 git.add().addFilepattern("a.txt").call();
94 git.commit().setMessage("Initial commit").call();
95 }
96
97 groupBDb = createWorkRepository();
98 try (Git git = new Git(groupBDb)) {
99 JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
100 git.add().addFilepattern("b.txt").call();
101 git.commit().setMessage("Initial commit").call();
102 }
103
104 resolveRelativeUris();
105 }
106
107 @Test
108 public void testMissingPath() throws Exception {
109 try {
110 execute("git repo");
111 fail("Must die");
112 } catch (Die e) {
113
114 }
115 }
116
117
118
119
120
121
122 @Test
123 public void testZombieHelpArgument() throws Exception {
124 String[] output = execute("git repo -h");
125 String all = Arrays.toString(output);
126 assertTrue("Unexpected help output: " + all,
127 all.contains("jgit repo"));
128 assertFalse("Unexpected help output: " + all,
129 all.contains("jgit repo VAL"));
130 }
131
132 @Test
133 public void testAddRepoManifest() throws Exception {
134 StringBuilder xmlContent = new StringBuilder();
135 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
136 .append("<manifest>")
137 .append("<remote name=\"remote1\" fetch=\".\" />")
138 .append("<default revision=\"master\" remote=\"remote1\" />")
139 .append("<project path=\"foo\" name=\"")
140 .append(defaultUri)
141 .append("\" groups=\"a,test\" />")
142 .append("<project path=\"bar\" name=\"")
143 .append(notDefaultUri)
144 .append("\" groups=\"notdefault\" />")
145 .append("<project path=\"a\" name=\"")
146 .append(groupAUri)
147 .append("\" groups=\"a\" />")
148 .append("<project path=\"b\" name=\"")
149 .append(groupBUri)
150 .append("\" groups=\"b\" />")
151 .append("</manifest>");
152 writeTrashFile("manifest.xml", xmlContent.toString());
153 StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
154 .append(rootUri)
155 .append("\" --groups=\"all,-a\" \"")
156 .append(db.getWorkTree().getAbsolutePath())
157 .append("/manifest.xml\"");
158 execute(cmd.toString());
159
160 File file = new File(db.getWorkTree(), "foo/hello.txt");
161 assertFalse("\"all,-a\" doesn't have foo", file.exists());
162 file = new File(db.getWorkTree(), "bar/world.txt");
163 assertTrue("\"all,-a\" has bar", file.exists());
164 file = new File(db.getWorkTree(), "a/a.txt");
165 assertFalse("\"all,-a\" doesn't have a", file.exists());
166 file = new File(db.getWorkTree(), "b/b.txt");
167 assertTrue("\"all,-a\" has have b", file.exists());
168 }
169
170 private void resolveRelativeUris() {
171
172 defaultUri = defaultDb.getDirectory().toURI().toString();
173 notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
174 groupAUri = groupADb.getDirectory().toURI().toString();
175 groupBUri = groupBDb.getDirectory().toURI().toString();
176 int start = 0;
177 while (start <= defaultUri.length()) {
178 int newStart = defaultUri.indexOf('/', start + 1);
179 String prefix = defaultUri.substring(0, newStart);
180 if (!notDefaultUri.startsWith(prefix) ||
181 !groupAUri.startsWith(prefix) ||
182 !groupBUri.startsWith(prefix)) {
183 start++;
184 rootUri = defaultUri.substring(0, start) + "manifest";
185 defaultUri = defaultUri.substring(start);
186 notDefaultUri = notDefaultUri.substring(start);
187 groupAUri = groupAUri.substring(start);
188 groupBUri = groupBUri.substring(start);
189 return;
190 }
191 start = newStart;
192 }
193 }
194 }