View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub 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.submodule;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertTrue;
49  
50  import java.io.File;
51  import java.util.Map;
52  import java.util.Map.Entry;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.api.SubmoduleSyncCommand;
56  import org.eclipse.jgit.api.errors.GitAPIException;
57  import org.eclipse.jgit.dircache.DirCache;
58  import org.eclipse.jgit.dircache.DirCacheEditor;
59  import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
60  import org.eclipse.jgit.dircache.DirCacheEntry;
61  import org.eclipse.jgit.junit.RepositoryTestCase;
62  import org.eclipse.jgit.lib.ConfigConstants;
63  import org.eclipse.jgit.lib.Constants;
64  import org.eclipse.jgit.lib.FileMode;
65  import org.eclipse.jgit.lib.ObjectId;
66  import org.eclipse.jgit.lib.Repository;
67  import org.eclipse.jgit.lib.StoredConfig;
68  import org.eclipse.jgit.storage.file.FileBasedConfig;
69  import org.junit.Test;
70  
71  /**
72   * Unit tests of {@link SubmoduleSyncCommand}
73   */
74  public class SubmoduleSyncTest extends RepositoryTestCase {
75  
76  	@Test
77  	public void repositoryWithNoSubmodules() throws GitAPIException {
78  		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
79  		Map<String, String> modules = command.call();
80  		assertNotNull(modules);
81  		assertTrue(modules.isEmpty());
82  	}
83  
84  	@Test
85  	public void repositoryWithSubmodule() throws Exception {
86  		writeTrashFile("file.txt", "content");
87  		Git git = Git.wrap(db);
88  		git.add().addFilepattern("file.txt").call();
89  		git.commit().setMessage("create file").call();
90  
91  		final ObjectId id = ObjectId
92  				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
93  		final String path = "sub";
94  		DirCache cache = db.lockDirCache();
95  		DirCacheEditor editor = cache.editor();
96  		editor.add(new PathEdit(path) {
97  
98  			@Override
99  			public void apply(DirCacheEntry ent) {
100 				ent.setFileMode(FileMode.GITLINK);
101 				ent.setObjectId(id);
102 			}
103 		});
104 		editor.commit();
105 
106 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
107 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
108 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
109 				ConfigConstants.CONFIG_KEY_PATH, path);
110 		String url = "git://server/repo.git";
111 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
112 				ConfigConstants.CONFIG_KEY_URL, url);
113 		modulesConfig.save();
114 
115 		Repository subRepo = Git.cloneRepository()
116 				.setURI(db.getDirectory().toURI().toString())
117 				.setDirectory(new File(db.getWorkTree(), path)).call()
118 				.getRepository();
119 		addRepoToClose(subRepo);
120 		assertNotNull(subRepo);
121 
122 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
123 		assertTrue(generator.next());
124 		assertNull(generator.getConfigUrl());
125 		assertEquals(url, generator.getModulesUrl());
126 
127 		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
128 		Map<String, String> synced = command.call();
129 		assertNotNull(synced);
130 		assertEquals(1, synced.size());
131 		Entry<String, String> module = synced.entrySet().iterator().next();
132 		assertEquals(path, module.getKey());
133 		assertEquals(url, module.getValue());
134 
135 		generator = SubmoduleWalk.forIndex(db);
136 		assertTrue(generator.next());
137 		assertEquals(url, generator.getConfigUrl());
138 		try (Repository subModRepository = generator.getRepository()) {
139 			StoredConfig submoduleConfig = subModRepository.getConfig();
140 			assertEquals(url,
141 					submoduleConfig.getString(
142 							ConfigConstants.CONFIG_REMOTE_SECTION,
143 							Constants.DEFAULT_REMOTE_NAME,
144 							ConfigConstants.CONFIG_KEY_URL));
145 		}
146 	}
147 
148 	@Test
149 	public void repositoryWithRelativeUriSubmodule() throws Exception {
150 		writeTrashFile("file.txt", "content");
151 		Git git = Git.wrap(db);
152 		git.add().addFilepattern("file.txt").call();
153 		git.commit().setMessage("create file").call();
154 
155 		final ObjectId id = ObjectId
156 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
157 		final String path = "sub";
158 		DirCache cache = db.lockDirCache();
159 		DirCacheEditor editor = cache.editor();
160 		editor.add(new PathEdit(path) {
161 
162 			@Override
163 			public void apply(DirCacheEntry ent) {
164 				ent.setFileMode(FileMode.GITLINK);
165 				ent.setObjectId(id);
166 			}
167 		});
168 		editor.commit();
169 
170 		String base = "git://server/repo.git";
171 		FileBasedConfig config = db.getConfig();
172 		config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
173 				Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
174 				base);
175 		config.save();
176 
177 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
178 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
179 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
180 				ConfigConstants.CONFIG_KEY_PATH, path);
181 		String current = "git://server/repo.git";
182 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
183 				ConfigConstants.CONFIG_KEY_URL, current);
184 		modulesConfig.save();
185 
186 		Repository subRepo = Git.cloneRepository()
187 				.setURI(db.getDirectory().toURI().toString())
188 				.setDirectory(new File(db.getWorkTree(), path)).call()
189 				.getRepository();
190 		assertNotNull(subRepo);
191 		addRepoToClose(subRepo);
192 
193 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
194 		assertTrue(generator.next());
195 		assertNull(generator.getConfigUrl());
196 		assertEquals(current, generator.getModulesUrl());
197 
198 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
199 				ConfigConstants.CONFIG_KEY_URL, "../sub.git");
200 		modulesConfig.save();
201 
202 		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
203 		Map<String, String> synced = command.call();
204 		assertNotNull(synced);
205 		assertEquals(1, synced.size());
206 		Entry<String, String> module = synced.entrySet().iterator().next();
207 		assertEquals(path, module.getKey());
208 		assertEquals("git://server/sub.git", module.getValue());
209 
210 		generator = SubmoduleWalk.forIndex(db);
211 		assertTrue(generator.next());
212 		assertEquals("git://server/sub.git", generator.getConfigUrl());
213 		try (Repository subModRepository1 = generator.getRepository()) {
214 			StoredConfig submoduleConfig = subModRepository1.getConfig();
215 			assertEquals("git://server/sub.git",
216 					submoduleConfig.getString(
217 							ConfigConstants.CONFIG_REMOTE_SECTION,
218 							Constants.DEFAULT_REMOTE_NAME,
219 							ConfigConstants.CONFIG_KEY_URL));
220 		}
221 	}
222 }