import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.submodule.SubmoduleStatus;
private final String REMOTE = "origin";
+ private final String PATH = "sub";
+
@Before
public void setUpSubmodules()
throws Exception {
addRepoToClose(sub1);
String file = "file.txt";
- String path = "sub";
write(new File(sub1.getWorkTree(), file), "content");
sub1Git.add().addFilepattern(file).call();
assertNotNull(sub2Head);
// Add submodule 2 to submodule 1
- Repository r2 = sub1Git.submoduleAdd().setPath(path)
+ Repository r2 = sub1Git.submoduleAdd().setPath(PATH)
.setURI(sub2.getDirectory().toURI().toString()).call();
assertNotNull(r2);
addRepoToClose(r2);
assertNotNull(sub1Head);
// Add submodule 1 to default repository
- Repository r1 = git.submoduleAdd().setPath(path)
+ Repository r1 = git.submoduleAdd().setPath(PATH)
.setURI(sub1.getDirectory().toURI().toString()).call();
assertNotNull(r1);
addRepoToClose(r1);
@Test
public void shouldFetchSubmodulesWhenOnDemandAndRevisionChanged()
throws Exception {
+ RevCommit update = updateSubmoduleRevision();
+ FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
+
+ // The first submodule should have been updated
+ assertTrue(result.submoduleResults().containsKey("sub"));
+ FetchResult subResult = result.submoduleResults().get("sub");
+
+ // The second submodule should not get updated
+ assertTrue(subResult.submoduleResults().isEmpty());
+ assertSubmoduleFetchHeads(commit1, submodule2Head);
+
+ // After fetch the parent repo's fetch head should be the commit
+ // that updated the submodule.
+ assertEquals(update,
+ git2.getRepository().resolve(Constants.FETCH_HEAD));
+ }
+
+ @Test
+ public void shouldNotFetchSubmodulesWhenOnDemandAndRevisionNotChanged()
+ throws Exception {
+ FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
+ assertTrue(result.submoduleResults().isEmpty());
+ assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
+ }
+
+ @Test
+ public void shouldNotFetchSubmodulesWhenSubmoduleConfigurationSetToNo()
+ throws Exception {
+ StoredConfig config = git2.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_SUBMODULE_SECTION, PATH,
+ ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES,
+ FetchRecurseSubmodulesMode.NO);
+ config.save();
+ updateSubmoduleRevision();
+ FetchResult result = fetch(null);
+ assertTrue(result.submoduleResults().isEmpty());
+ assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
+ }
+
+ @Test
+ public void shouldFetchSubmodulesWhenSubmoduleConfigurationSetToYes()
+ throws Exception {
+ StoredConfig config = git2.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_SUBMODULE_SECTION, PATH,
+ ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES,
+ FetchRecurseSubmodulesMode.YES);
+ config.save();
+ FetchResult result = fetch(null);
+ assertTrue(result.submoduleResults().containsKey("sub"));
+ FetchResult subResult = result.submoduleResults().get("sub");
+ assertTrue(subResult.submoduleResults().containsKey("sub"));
+ assertSubmoduleFetchHeads(commit1, commit2);
+ }
+
+ @Test
+ public void shouldNotFetchSubmodulesWhenFetchConfigurationSetToNo()
+ throws Exception {
+ StoredConfig config = git2.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_FETCH_SECTION, null,
+ ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
+ FetchRecurseSubmodulesMode.NO);
+ config.save();
+ updateSubmoduleRevision();
+ FetchResult result = fetch(null);
+ assertTrue(result.submoduleResults().isEmpty());
+ assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
+ }
+
+ @Test
+ public void shouldFetchSubmodulesWhenFetchConfigurationSetToYes()
+ throws Exception {
+ StoredConfig config = git2.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_FETCH_SECTION, null,
+ ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
+ FetchRecurseSubmodulesMode.YES);
+ config.save();
+ FetchResult result = fetch(null);
+ assertTrue(result.submoduleResults().containsKey("sub"));
+ FetchResult subResult = result.submoduleResults().get("sub");
+ assertTrue(subResult.submoduleResults().containsKey("sub"));
+ assertSubmoduleFetchHeads(commit1, commit2);
+ }
+
+ private RevCommit updateSubmoduleRevision() throws Exception {
// Fetch the submodule in the original git and reset it to
// the commit that was created
try (SubmoduleWalk w = SubmoduleWalk.forIndex(git.getRepository())) {
assertEquals(commit1, subStatus.getHeadId());
assertEquals(SubmoduleStatusType.INITIALIZED, subStatus.getType());
- FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
-
- // The first submodule should have been updated
- assertTrue(result.submoduleResults().containsKey("sub"));
- FetchResult subResult = result.submoduleResults().get("sub");
-
- // The second submodule should not get updated
- assertTrue(subResult.submoduleResults().isEmpty());
- assertSubmoduleFetchHeads(commit1, submodule2Head);
-
- // After fetch the parent repo's fetch head should be the commit
- // that updated the submodule.
- assertEquals(update,
- git2.getRepository().resolve(Constants.FETCH_HEAD));
- }
-
- @Test
- public void shouldNotFetchSubmodulesWhenOnDemandAndRevisionNotChanged()
- throws Exception {
- FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
- assertTrue(result.submoduleResults().isEmpty());
- assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
+ return update;
}
private FetchResult fetch(FetchRecurseSubmodulesMode mode)
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.NullProgressMonitor;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
refSpecs = new ArrayList<>(3);
}
- private FetchRecurseSubmodulesMode getRecurseMode(Repository repository,
- String path) {
+ private FetchRecurseSubmodulesMode getRecurseMode(String path) {
// Use the caller-specified mode, if set
if (submoduleRecurseMode != null) {
return submoduleRecurseMode;
}
- // Fall back to submodule config, if set
- FetchRecurseSubmodulesMode mode = repository.getConfig().getEnum(
+ // Fall back to submodule.name.fetchRecurseSubmodules, if set
+ FetchRecurseSubmodulesMode mode = repo.getConfig().getEnum(
FetchRecurseSubmodulesMode.values(),
ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES, null);
return mode;
}
+ // Fall back to fetch.recurseSubmodules, if set
+ mode = repo.getConfig().getEnum(FetchRecurseSubmodulesMode.values(),
+ ConfigConstants.CONFIG_FETCH_SECTION, null,
+ ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES, null);
+ if (mode != null) {
+ return mode;
+ }
+
// Default to on-demand mode
return FetchRecurseSubmodulesMode.ON_DEMAND;
}
- private boolean isRecurseSubmodules() {
- return submoduleRecurseMode != null
- && submoduleRecurseMode != FetchRecurseSubmodulesMode.NO;
- }
-
private void fetchSubmodules(FetchResult results)
throws org.eclipse.jgit.api.errors.TransportException,
GitAPIException, InvalidConfigurationException {
try (SubmoduleWalk walk = new SubmoduleWalk(repo);
RevWalk revWalk = new RevWalk(repo)) {
// Walk over submodules in the parent repository's FETCH_HEAD.
- walk.setTree(revWalk.parseTree(repo.resolve(Constants.FETCH_HEAD)));
+ ObjectId fetchHead = repo.resolve(Constants.FETCH_HEAD);
+ if (fetchHead == null) {
+ return;
+ }
+ walk.setTree(revWalk.parseTree(fetchHead));
while (walk.next()) {
Repository submoduleRepo = walk.getRepository();
}
FetchRecurseSubmodulesMode recurseMode = getRecurseMode(
- submoduleRepo, walk.getPath());
+ walk.getPath());
// When the fetch mode is "yes" we always fetch. When the mode
// is "on demand", we only fetch if the submodule's revision was
configure(transport);
FetchResult result = transport.fetch(monitor, refSpecs);
- if (!repo.isBare() && (!result.getTrackingRefUpdates().isEmpty()
- || isRecurseSubmodules())) {
+ if (!repo.isBare()) {
fetchSubmodules(result);
}