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.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  import java.util.Collection;
52  
53  import org.eclipse.jgit.api.Git;
54  import org.eclipse.jgit.api.SubmoduleUpdateCommand;
55  import org.eclipse.jgit.api.errors.GitAPIException;
56  import org.eclipse.jgit.dircache.DirCache;
57  import org.eclipse.jgit.dircache.DirCacheEditor;
58  import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
59  import org.eclipse.jgit.dircache.DirCacheEntry;
60  import org.eclipse.jgit.junit.RepositoryTestCase;
61  import org.eclipse.jgit.lib.ConfigConstants;
62  import org.eclipse.jgit.lib.Constants;
63  import org.eclipse.jgit.lib.FileMode;
64  import org.eclipse.jgit.lib.ObjectId;
65  import org.eclipse.jgit.lib.Repository;
66  import org.eclipse.jgit.lib.StoredConfig;
67  import org.eclipse.jgit.revwalk.RevCommit;
68  import org.eclipse.jgit.storage.file.FileBasedConfig;
69  import org.junit.Test;
70  
71  /**
72   * Unit tests of {@link SubmoduleUpdateCommand}
73   */
74  public class SubmoduleUpdateTest extends RepositoryTestCase {
75  
76  	@Test
77  	public void repositoryWithNoSubmodules() throws GitAPIException {
78  		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
79  		Collection<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  		final RevCommit commit = git.commit().setMessage("create file").call();
90  
91  		final String path = "sub";
92  		DirCache cache = db.lockDirCache();
93  		DirCacheEditor editor = cache.editor();
94  		editor.add(new PathEdit(path) {
95  
96  			@Override
97  			public void apply(DirCacheEntry ent) {
98  				ent.setFileMode(FileMode.GITLINK);
99  				ent.setObjectId(commit);
100 			}
101 		});
102 		editor.commit();
103 
104 		StoredConfig config = db.getConfig();
105 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
106 				ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
107 						.toString());
108 		config.save();
109 
110 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
111 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
112 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
113 				ConfigConstants.CONFIG_KEY_PATH, path);
114 		modulesConfig.save();
115 
116 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
117 		Collection<String> updated = command.call();
118 		assertNotNull(updated);
119 		assertEquals(1, updated.size());
120 		assertEquals(path, updated.iterator().next());
121 
122 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
123 		assertTrue(generator.next());
124 		try (Repository subRepo = generator.getRepository()) {
125 			assertNotNull(subRepo);
126 			assertEquals(commit, subRepo.resolve(Constants.HEAD));
127 		}
128 	}
129 
130 	@Test
131 	public void repositoryWithUnconfiguredSubmodule() throws IOException,
132 			GitAPIException {
133 		final ObjectId id = ObjectId
134 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
135 		final String path = "sub";
136 		DirCache cache = db.lockDirCache();
137 		DirCacheEditor editor = cache.editor();
138 		editor.add(new PathEdit(path) {
139 
140 			@Override
141 			public void apply(DirCacheEntry ent) {
142 				ent.setFileMode(FileMode.GITLINK);
143 				ent.setObjectId(id);
144 			}
145 		});
146 		editor.commit();
147 
148 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
149 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
150 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
151 				ConfigConstants.CONFIG_KEY_PATH, path);
152 		String url = "git://server/repo.git";
153 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
154 				ConfigConstants.CONFIG_KEY_URL, url);
155 		String update = "rebase";
156 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
157 				ConfigConstants.CONFIG_KEY_UPDATE, update);
158 		modulesConfig.save();
159 
160 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
161 		Collection<String> updated = command.call();
162 		assertNotNull(updated);
163 		assertTrue(updated.isEmpty());
164 	}
165 
166 	@Test
167 	public void repositoryWithInitializedSubmodule() throws IOException,
168 			GitAPIException {
169 		final ObjectId id = ObjectId
170 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
171 		final String path = "sub";
172 		DirCache cache = db.lockDirCache();
173 		DirCacheEditor editor = cache.editor();
174 		editor.add(new PathEdit(path) {
175 
176 			@Override
177 			public void apply(DirCacheEntry ent) {
178 				ent.setFileMode(FileMode.GITLINK);
179 				ent.setObjectId(id);
180 			}
181 		});
182 		editor.commit();
183 
184 		Repository subRepo = Git.init().setBare(false)
185 				.setDirectory(new File(db.getWorkTree(), path)).call()
186 				.getRepository();
187 		assertNotNull(subRepo);
188 
189 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
190 		Collection<String> updated = command.call();
191 		assertNotNull(updated);
192 		assertTrue(updated.isEmpty());
193 	}
194 }