View Javadoc
1   /*
2    * Copyright (C) 2014 Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  import static org.junit.Assert.assertFalse;
13  import static org.junit.Assert.assertTrue;
14  import static org.junit.Assert.fail;
15  
16  import java.io.File;
17  import java.util.Arrays;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.junit.JGitTestUtil;
21  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
22  import org.eclipse.jgit.lib.Repository;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class RepoTest extends CLIRepositoryTestCase {
27  	private Repository defaultDb;
28  	private Repository notDefaultDb;
29  	private Repository groupADb;
30  	private Repository groupBDb;
31  
32  	private String rootUri;
33  	private String defaultUri;
34  	private String notDefaultUri;
35  	private String groupAUri;
36  	private String groupBUri;
37  
38  	@Override
39  	@Before
40  	public void setUp() throws Exception {
41  		super.setUp();
42  
43  		defaultDb = createWorkRepository();
44  		try (Git git = new Git(defaultDb)) {
45  			JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world");
46  			git.add().addFilepattern("hello.txt").call();
47  			git.commit().setMessage("Initial commit").call();
48  		}
49  
50  		notDefaultDb = createWorkRepository();
51  		try (Git git = new Git(notDefaultDb)) {
52  			JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
53  			git.add().addFilepattern("world.txt").call();
54  			git.commit().setMessage("Initial commit").call();
55  		}
56  
57  		groupADb = createWorkRepository();
58  		try (Git git = new Git(groupADb)) {
59  			JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
60  			git.add().addFilepattern("a.txt").call();
61  			git.commit().setMessage("Initial commit").call();
62  		}
63  
64  		groupBDb = createWorkRepository();
65  		try (Git git = new Git(groupBDb)) {
66  			JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
67  			git.add().addFilepattern("b.txt").call();
68  			git.commit().setMessage("Initial commit").call();
69  		}
70  
71  		resolveRelativeUris();
72  	}
73  
74  	@Test
75  	public void testMissingPath() throws Exception {
76  		try {
77  			execute("git repo");
78  			fail("Must die");
79  		} catch (Die e) {
80  			// expected, requires argument
81  		}
82  	}
83  
84  	/**
85  	 * See bug 484951: "git repo -h" should not print unexpected values
86  	 *
87  	 * @throws Exception
88  	 */
89  	@Test
90  	public void testZombieHelpArgument() throws Exception {
91  		String[] output = execute("git repo -h");
92  		String all = Arrays.toString(output);
93  		assertTrue("Unexpected help output: " + all,
94  				all.contains("jgit repo"));
95  		assertFalse("Unexpected help output: " + all,
96  				all.contains("jgit repo VAL"));
97  	}
98  
99  	@Test
100 	public void testAddRepoManifest() throws Exception {
101 		StringBuilder xmlContent = new StringBuilder();
102 		xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
103 			.append("<manifest>")
104 			.append("<remote name=\"remote1\" fetch=\".\" />")
105 			.append("<default revision=\"master\" remote=\"remote1\" />")
106 			.append("<project path=\"foo\" name=\"")
107 			.append(defaultUri)
108 			.append("\" groups=\"a,test\" />")
109 			.append("<project path=\"bar\" name=\"")
110 			.append(notDefaultUri)
111 			.append("\" groups=\"notdefault\" />")
112 			.append("<project path=\"a\" name=\"")
113 			.append(groupAUri)
114 			.append("\" groups=\"a\" />")
115 			.append("<project path=\"b\" name=\"")
116 			.append(groupBUri)
117 			.append("\" groups=\"b\" />")
118 			.append("</manifest>");
119 		writeTrashFile("manifest.xml", xmlContent.toString());
120 		StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
121 			.append(rootUri)
122 			.append("\" --groups=\"all,-a\" \"")
123 			.append(db.getWorkTree().getAbsolutePath())
124 			.append("/manifest.xml\"");
125 		execute(cmd.toString());
126 
127 		File file = new File(db.getWorkTree(), "foo/hello.txt");
128 		assertFalse("\"all,-a\" doesn't have foo", file.exists());
129 		file = new File(db.getWorkTree(), "bar/world.txt");
130 		assertTrue("\"all,-a\" has bar", file.exists());
131 		file = new File(db.getWorkTree(), "a/a.txt");
132 		assertFalse("\"all,-a\" doesn't have a", file.exists());
133 		file = new File(db.getWorkTree(), "b/b.txt");
134 		assertTrue("\"all,-a\" has have b", file.exists());
135 	}
136 
137 	private void resolveRelativeUris() {
138 		// Find the longest common prefix ends with "/" as rootUri.
139 		defaultUri = defaultDb.getDirectory().toURI().toString();
140 		notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
141 		groupAUri = groupADb.getDirectory().toURI().toString();
142 		groupBUri = groupBDb.getDirectory().toURI().toString();
143 		int start = 0;
144 		while (start <= defaultUri.length()) {
145 			int newStart = defaultUri.indexOf('/', start + 1);
146 			String prefix = defaultUri.substring(0, newStart);
147 			if (!notDefaultUri.startsWith(prefix) ||
148 					!groupAUri.startsWith(prefix) ||
149 					!groupBUri.startsWith(prefix)) {
150 				start++;
151 				rootUri = defaultUri.substring(0, start) + "manifest";
152 				defaultUri = defaultUri.substring(start);
153 				notDefaultUri = notDefaultUri.substring(start);
154 				groupAUri = groupAUri.substring(start);
155 				groupBUri = groupBUri.substring(start);
156 				return;
157 			}
158 			start = newStart;
159 		}
160 	}
161 }