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  			public void apply(DirCacheEntry ent) {
97  				ent.setFileMode(FileMode.GITLINK);
98  				ent.setObjectId(commit);
99  			}
100 		});
101 		editor.commit();
102 
103 		StoredConfig config = db.getConfig();
104 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
105 				ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
106 						.toString());
107 		config.save();
108 
109 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
110 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
111 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
112 				ConfigConstants.CONFIG_KEY_PATH, path);
113 		modulesConfig.save();
114 
115 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
116 		Collection<String> updated = command.call();
117 		assertNotNull(updated);
118 		assertEquals(1, updated.size());
119 		assertEquals(path, updated.iterator().next());
120 
121 		SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
122 		assertTrue(generator.next());
123 		Repository subRepo = generator.getRepository();
124 		assertNotNull(subRepo);
125 		assertEquals(commit, subRepo.resolve(Constants.HEAD));
126 		subRepo.close();
127 	}
128 
129 	@Test
130 	public void repositoryWithUnconfiguredSubmodule() throws IOException,
131 			GitAPIException {
132 		final ObjectId id = ObjectId
133 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
134 		final String path = "sub";
135 		DirCache cache = db.lockDirCache();
136 		DirCacheEditor editor = cache.editor();
137 		editor.add(new PathEdit(path) {
138 
139 			public void apply(DirCacheEntry ent) {
140 				ent.setFileMode(FileMode.GITLINK);
141 				ent.setObjectId(id);
142 			}
143 		});
144 		editor.commit();
145 
146 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
147 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
148 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
149 				ConfigConstants.CONFIG_KEY_PATH, path);
150 		String url = "git://server/repo.git";
151 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
152 				ConfigConstants.CONFIG_KEY_URL, url);
153 		String update = "rebase";
154 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
155 				ConfigConstants.CONFIG_KEY_UPDATE, update);
156 		modulesConfig.save();
157 
158 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
159 		Collection<String> updated = command.call();
160 		assertNotNull(updated);
161 		assertTrue(updated.isEmpty());
162 	}
163 
164 	@Test
165 	public void repositoryWithInitializedSubmodule() throws IOException,
166 			GitAPIException {
167 		final ObjectId id = ObjectId
168 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
169 		final String path = "sub";
170 		DirCache cache = db.lockDirCache();
171 		DirCacheEditor editor = cache.editor();
172 		editor.add(new PathEdit(path) {
173 
174 			public void apply(DirCacheEntry ent) {
175 				ent.setFileMode(FileMode.GITLINK);
176 				ent.setObjectId(id);
177 			}
178 		});
179 		editor.commit();
180 
181 		Repository subRepo = Git.init().setBare(false)
182 				.setDirectory(new File(db.getWorkTree(), path)).call()
183 				.getRepository();
184 		assertNotNull(subRepo);
185 
186 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
187 		Collection<String> updated = command.call();
188 		assertNotNull(updated);
189 		assertTrue(updated.isEmpty());
190 	}
191 }