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 		try (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 		try (SubmoduleWalk 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 
149 	@Test
150 	public void repositoryWithRelativeUriSubmodule() throws Exception {
151 		writeTrashFile("file.txt", "content");
152 		Git git = Git.wrap(db);
153 		git.add().addFilepattern("file.txt").call();
154 		git.commit().setMessage("create file").call();
155 
156 		final ObjectId id = ObjectId
157 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
158 		final String path = "sub";
159 		DirCache cache = db.lockDirCache();
160 		DirCacheEditor editor = cache.editor();
161 		editor.add(new PathEdit(path) {
162 
163 			@Override
164 			public void apply(DirCacheEntry ent) {
165 				ent.setFileMode(FileMode.GITLINK);
166 				ent.setObjectId(id);
167 			}
168 		});
169 		editor.commit();
170 
171 		String base = "git://server/repo.git";
172 		FileBasedConfig config = db.getConfig();
173 		config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
174 				Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
175 				base);
176 		config.save();
177 
178 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
179 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
180 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
181 				ConfigConstants.CONFIG_KEY_PATH, path);
182 		String current = "git://server/repo.git";
183 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
184 				ConfigConstants.CONFIG_KEY_URL, current);
185 		modulesConfig.save();
186 
187 		Repository subRepo = Git.cloneRepository()
188 				.setURI(db.getDirectory().toURI().toString())
189 				.setDirectory(new File(db.getWorkTree(), path)).call()
190 				.getRepository();
191 		assertNotNull(subRepo);
192 		addRepoToClose(subRepo);
193 
194 		try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
195 			assertTrue(generator.next());
196 			assertNull(generator.getConfigUrl());
197 			assertEquals(current, generator.getModulesUrl());
198 		}
199 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
200 				ConfigConstants.CONFIG_KEY_URL, "../sub.git");
201 		modulesConfig.save();
202 
203 		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
204 		Map<String, String> synced = command.call();
205 		assertNotNull(synced);
206 		assertEquals(1, synced.size());
207 		Entry<String, String> module = synced.entrySet().iterator().next();
208 		assertEquals(path, module.getKey());
209 		assertEquals("git://server/sub.git", module.getValue());
210 
211 		try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
212 			assertTrue(generator.next());
213 			assertEquals("git://server/sub.git", generator.getConfigUrl());
214 			try (Repository subModRepository1 = generator.getRepository()) {
215 				StoredConfig submoduleConfig = subModRepository1.getConfig();
216 				assertEquals("git://server/sub.git",
217 						submoduleConfig.getString(
218 								ConfigConstants.CONFIG_REMOTE_SECTION,
219 								Constants.DEFAULT_REMOTE_NAME,
220 								ConfigConstants.CONFIG_KEY_URL));
221 			}
222 		}
223 	}
224 }