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  			public void apply(DirCacheEntry ent) {
99  				ent.setFileMode(FileMode.GITLINK);
100 				ent.setObjectId(id);
101 			}
102 		});
103 		editor.commit();
104 
105 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
106 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
107 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
108 				ConfigConstants.CONFIG_KEY_PATH, path);
109 		String url = "git://server/repo.git";
110 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
111 				ConfigConstants.CONFIG_KEY_URL, url);
112 		modulesConfig.save();
113 
114 		Repository subRepo = Git.cloneRepository()
115 				.setURI(db.getDirectory().toURI().toString())
116 				.setDirectory(new File(db.getWorkTree(), path)).call()
117 				.getRepository();
118 		addRepoToClose(subRepo);
119 		assertNotNull(subRepo);
120 
121 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
122 		assertTrue(generator.next());
123 		assertNull(generator.getConfigUrl());
124 		assertEquals(url, generator.getModulesUrl());
125 
126 		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
127 		Map<String, String> synced = command.call();
128 		assertNotNull(synced);
129 		assertEquals(1, synced.size());
130 		Entry<String, String> module = synced.entrySet().iterator().next();
131 		assertEquals(path, module.getKey());
132 		assertEquals(url, module.getValue());
133 
134 		generator = SubmoduleWalk.forIndex(db);
135 		assertTrue(generator.next());
136 		assertEquals(url, generator.getConfigUrl());
137 		Repository subModRepository = generator.getRepository();
138 		StoredConfig submoduleConfig = subModRepository.getConfig();
139 		subModRepository.close();
140 		assertEquals(url, submoduleConfig.getString(
141 				ConfigConstants.CONFIG_REMOTE_SECTION,
142 				Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
143 	}
144 
145 	@Test
146 	public void repositoryWithRelativeUriSubmodule() throws Exception {
147 		writeTrashFile("file.txt", "content");
148 		Git git = Git.wrap(db);
149 		git.add().addFilepattern("file.txt").call();
150 		git.commit().setMessage("create file").call();
151 
152 		final ObjectId id = ObjectId
153 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
154 		final String path = "sub";
155 		DirCache cache = db.lockDirCache();
156 		DirCacheEditor editor = cache.editor();
157 		editor.add(new PathEdit(path) {
158 
159 			public void apply(DirCacheEntry ent) {
160 				ent.setFileMode(FileMode.GITLINK);
161 				ent.setObjectId(id);
162 			}
163 		});
164 		editor.commit();
165 
166 		String base = "git://server/repo.git";
167 		FileBasedConfig config = db.getConfig();
168 		config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
169 				Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
170 				base);
171 		config.save();
172 
173 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
174 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
175 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
176 				ConfigConstants.CONFIG_KEY_PATH, path);
177 		String current = "git://server/repo.git";
178 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
179 				ConfigConstants.CONFIG_KEY_URL, current);
180 		modulesConfig.save();
181 
182 		Repository subRepo = Git.cloneRepository()
183 				.setURI(db.getDirectory().toURI().toString())
184 				.setDirectory(new File(db.getWorkTree(), path)).call()
185 				.getRepository();
186 		assertNotNull(subRepo);
187 		addRepoToClose(subRepo);
188 
189 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
190 		assertTrue(generator.next());
191 		assertNull(generator.getConfigUrl());
192 		assertEquals(current, generator.getModulesUrl());
193 
194 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
195 				ConfigConstants.CONFIG_KEY_URL, "../sub.git");
196 		modulesConfig.save();
197 
198 		SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
199 		Map<String, String> synced = command.call();
200 		assertNotNull(synced);
201 		assertEquals(1, synced.size());
202 		Entry<String, String> module = synced.entrySet().iterator().next();
203 		assertEquals(path, module.getKey());
204 		assertEquals("git://server/sub.git", module.getValue());
205 
206 		generator = SubmoduleWalk.forIndex(db);
207 		assertTrue(generator.next());
208 		assertEquals("git://server/sub.git", generator.getConfigUrl());
209 		Repository subModRepository1 = generator.getRepository();
210 		StoredConfig submoduleConfig = subModRepository1.getConfig();
211 		subModRepository1.close();
212 		assertEquals("git://server/sub.git", submoduleConfig.getString(
213 				ConfigConstants.CONFIG_REMOTE_SECTION,
214 				Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
215 	}
216 }