View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.submodule;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.util.Collection;
19  
20  import org.eclipse.jgit.api.Git;
21  import org.eclipse.jgit.api.SubmoduleUpdateCommand;
22  import org.eclipse.jgit.api.errors.GitAPIException;
23  import org.eclipse.jgit.dircache.DirCache;
24  import org.eclipse.jgit.dircache.DirCacheEditor;
25  import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
26  import org.eclipse.jgit.dircache.DirCacheEntry;
27  import org.eclipse.jgit.junit.RepositoryTestCase;
28  import org.eclipse.jgit.lib.ConfigConstants;
29  import org.eclipse.jgit.lib.Constants;
30  import org.eclipse.jgit.lib.FileMode;
31  import org.eclipse.jgit.lib.ObjectId;
32  import org.eclipse.jgit.lib.Repository;
33  import org.eclipse.jgit.lib.StoredConfig;
34  import org.eclipse.jgit.revwalk.RevCommit;
35  import org.eclipse.jgit.storage.file.FileBasedConfig;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests of {@link SubmoduleUpdateCommand}
40   */
41  public class SubmoduleUpdateTest extends RepositoryTestCase {
42  
43  	@Test
44  	public void repositoryWithNoSubmodules() throws GitAPIException {
45  		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
46  		Collection<String> modules = command.call();
47  		assertNotNull(modules);
48  		assertTrue(modules.isEmpty());
49  	}
50  
51  	@Test
52  	public void repositoryWithSubmodule() throws Exception {
53  		writeTrashFile("file.txt", "content");
54  		Git git = Git.wrap(db);
55  		git.add().addFilepattern("file.txt").call();
56  		final RevCommit commit = git.commit().setMessage("create file").call();
57  
58  		final String path = "sub";
59  		DirCache cache = db.lockDirCache();
60  		DirCacheEditor editor = cache.editor();
61  		editor.add(new PathEdit(path) {
62  
63  			@Override
64  			public void apply(DirCacheEntry ent) {
65  				ent.setFileMode(FileMode.GITLINK);
66  				ent.setObjectId(commit);
67  			}
68  		});
69  		editor.commit();
70  
71  		StoredConfig config = db.getConfig();
72  		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
73  				ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
74  						.toString());
75  		config.save();
76  
77  		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
78  				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
79  		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
80  				ConfigConstants.CONFIG_KEY_PATH, path);
81  		modulesConfig.save();
82  
83  		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
84  		Collection<String> updated = command.call();
85  		assertNotNull(updated);
86  		assertEquals(1, updated.size());
87  		assertEquals(path, updated.iterator().next());
88  
89  		try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
90  			assertTrue(generator.next());
91  			try (Repository subRepo = generator.getRepository()) {
92  				assertNotNull(subRepo);
93  				assertEquals(commit, subRepo.resolve(Constants.HEAD));
94  			}
95  		}
96  	}
97  
98  	@Test
99  	public void repositoryWithUnconfiguredSubmodule() throws IOException,
100 			GitAPIException {
101 		final ObjectId id = ObjectId
102 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
103 		final String path = "sub";
104 		DirCache cache = db.lockDirCache();
105 		DirCacheEditor editor = cache.editor();
106 		editor.add(new PathEdit(path) {
107 
108 			@Override
109 			public void apply(DirCacheEntry ent) {
110 				ent.setFileMode(FileMode.GITLINK);
111 				ent.setObjectId(id);
112 			}
113 		});
114 		editor.commit();
115 
116 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
117 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
118 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
119 				ConfigConstants.CONFIG_KEY_PATH, path);
120 		String url = "git://server/repo.git";
121 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
122 				ConfigConstants.CONFIG_KEY_URL, url);
123 		String update = "rebase";
124 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
125 				ConfigConstants.CONFIG_KEY_UPDATE, update);
126 		modulesConfig.save();
127 
128 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
129 		Collection<String> updated = command.call();
130 		assertNotNull(updated);
131 		assertTrue(updated.isEmpty());
132 	}
133 
134 	@Test
135 	public void repositoryWithInitializedSubmodule() throws IOException,
136 			GitAPIException {
137 		final ObjectId id = ObjectId
138 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
139 		final String path = "sub";
140 		DirCache cache = db.lockDirCache();
141 		DirCacheEditor editor = cache.editor();
142 		editor.add(new PathEdit(path) {
143 
144 			@Override
145 			public void apply(DirCacheEntry ent) {
146 				ent.setFileMode(FileMode.GITLINK);
147 				ent.setObjectId(id);
148 			}
149 		});
150 		editor.commit();
151 
152 		try (Repository subRepo = Git.init().setBare(false)
153 				.setDirectory(new File(db.getWorkTree(), path)).call()
154 				.getRepository()) {
155 			assertNotNull(subRepo);
156 		}
157 
158 		SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
159 		Collection<String> updated = command.call();
160 		assertNotNull(updated);
161 		assertTrue(updated.isEmpty());
162 	}
163 }