View Javadoc
1   /*
2    * Copyright (C) 2014 Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 			// expected, requires argument
114 		}
115 	}
116 
117 	/**
118 	 * See bug 484951: "git repo -h" should not print unexpected values
119 	 *
120 	 * @throws Exception
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 		// Find the longest common prefix ends with "/" as rootUri.
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 }