/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> private void CloneSubmodules(Repository clonedRepo) { SubmoduleInitCommand init = new SubmoduleInitCommand(clonedRepo); if (init.Call().IsEmpty()) { return; } SubmoduleUpdateCommand update = new SubmoduleUpdateCommand(clonedRepo); Configure(update); update.SetProgressMonitor(monitor); if (!update.Call().IsEmpty()) { SubmoduleWalk walk = SubmoduleWalk.ForIndex(clonedRepo); while (walk.Next()) { Repository subRepo = walk.GetRepository(); if (subRepo != null) { try { CloneSubmodules(subRepo); } finally { subRepo.Close(); } } } } }
/// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override IDictionary <string, SubmoduleStatus> Call() { CheckCallable(); try { SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo); if (!paths.IsEmpty()) { generator.SetFilter(PathFilterGroup.CreateFromStrings(paths)); } IDictionary <string, SubmoduleStatus> statuses = new Dictionary <string, SubmoduleStatus >(); while (generator.Next()) { SubmoduleStatus status = GetStatus(generator); statuses.Put(status.GetPath(), status); } return(statuses); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new JGitInternalException(e.Message, e); } }
/// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override ICollection <string> Call() { CheckCallable(); try { SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo); if (!paths.IsEmpty()) { generator.SetFilter(PathFilterGroup.CreateFromStrings(paths)); } StoredConfig config = repo.GetConfig(); IList <string> initialized = new AList <string>(); while (generator.Next()) { // Ignore entry if URL is already present in config file if (generator.GetConfigUrl() != null) { continue; } string path = generator.GetPath(); // Copy 'url' and 'update' fields from .gitmodules to config // file string url = generator.GetRemoteUrl(); string update = generator.GetModulesUpdate(); if (url != null) { config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants. CONFIG_KEY_URL, url); } if (update != null) { config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants. CONFIG_KEY_UPDATE, update); } if (url != null || update != null) { initialized.AddItem(path); } } // Save repository config if any values were updated if (!initialized.IsEmpty()) { config.Save(); } return(initialized); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new JGitInternalException(e.Message, e); } }
public virtual void CommitNewSubmodule() { Git git = new Git(db); WriteTrashFile("file.txt", "content"); git.Add().AddFilepattern("file.txt").Call(); RevCommit commit = git.Commit().SetMessage("create file").Call(); SubmoduleAddCommand command = new SubmoduleAddCommand(db); string path = "sub"; command.SetPath(path); string uri = db.Directory.ToURI().ToString(); command.SetURI(uri); Repository repo = command.Call(); NUnit.Framework.Assert.IsNotNull(repo); AddRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.ForIndex(db); NUnit.Framework.Assert.IsTrue(generator.Next()); NUnit.Framework.Assert.AreEqual(path, generator.GetPath()); NUnit.Framework.Assert.AreEqual(commit, generator.GetObjectId()); NUnit.Framework.Assert.AreEqual(uri, generator.GetModulesUrl()); NUnit.Framework.Assert.AreEqual(path, generator.GetModulesPath()); NUnit.Framework.Assert.AreEqual(uri, generator.GetConfigUrl()); Repository subModRepo = generator.GetRepository(); AddRepoToClose(subModRepo); NUnit.Framework.Assert.IsNotNull(subModRepo); NUnit.Framework.Assert.AreEqual(commit, repo.Resolve(Constants.HEAD)); RevCommit submoduleCommit = git.Commit().SetMessage("submodule add").SetOnly(path ).Call(); NUnit.Framework.Assert.IsNotNull(submoduleCommit); TreeWalk walk = new TreeWalk(db); walk.AddTree(commit.Tree); walk.AddTree(submoduleCommit.Tree); walk.Filter = TreeFilter.ANY_DIFF; IList <DiffEntry> diffs = DiffEntry.Scan(walk); NUnit.Framework.Assert.AreEqual(1, diffs.Count); DiffEntry subDiff = diffs[0]; NUnit.Framework.Assert.AreEqual(FileMode.MISSING, subDiff.GetOldMode()); NUnit.Framework.Assert.AreEqual(FileMode.GITLINK, subDiff.GetNewMode()); NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, subDiff.GetOldId().ToObjectId()); NUnit.Framework.Assert.AreEqual(commit, subDiff.GetNewId().ToObjectId()); NUnit.Framework.Assert.AreEqual(path, subDiff.GetNewPath()); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.ConfigInvalidException"></exception> private SubmoduleStatus GetStatus(SubmoduleWalk generator) { ObjectId id = generator.GetObjectId(); string path = generator.GetPath(); // Report missing if no path in .gitmodules file if (generator.GetModulesPath() == null) { return(new SubmoduleStatus(SubmoduleStatusType.MISSING, path, id)); } // Report uninitialized if no URL in config file if (generator.GetConfigUrl() == null) { return(new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path, id)); } // Report uninitialized if no submodule repository Repository subRepo = generator.GetRepository(); if (subRepo == null) { return(new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path, id)); } ObjectId headId; try { headId = subRepo.Resolve(Constants.HEAD); } finally { subRepo.Close(); } // Report uninitialized if no HEAD commit in submodule repository if (headId == null) { return(new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path, id, headId)); } // Report checked out if HEAD commit is different than index commit if (!headId.Equals(id)) { return(new SubmoduleStatus(SubmoduleStatusType.REV_CHECKED_OUT, path, id, headId)); } // Report initialized if HEAD commit is the same as the index commit return(new SubmoduleStatus(SubmoduleStatusType.INITIALIZED, path, id, headId)); }
/// <summary>Execute the SubmoduleUpdateCommand command.</summary> /// <remarks>Execute the SubmoduleUpdateCommand command.</remarks> /// <returns>a collection of updated submodule paths</returns> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException">NGit.Api.Errors.ConcurrentRefUpdateException /// </exception> /// <exception cref="NGit.Api.Errors.CheckoutConflictException">NGit.Api.Errors.CheckoutConflictException /// </exception> /// <exception cref="NGit.Api.Errors.InvalidMergeHeadsException">NGit.Api.Errors.InvalidMergeHeadsException /// </exception> /// <exception cref="NGit.Api.Errors.InvalidConfigurationException">NGit.Api.Errors.InvalidConfigurationException /// </exception> /// <exception cref="NGit.Api.Errors.NoHeadException">NGit.Api.Errors.NoHeadException /// </exception> /// <exception cref="NGit.Api.Errors.NoMessageException">NGit.Api.Errors.NoMessageException /// </exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException">NGit.Api.Errors.RefNotFoundException /// </exception> /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException">NGit.Api.Errors.WrongRepositoryStateException /// </exception> /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException /// </exception> public override ICollection <string> Call() { CheckCallable(); try { SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo); if (!paths.IsEmpty()) { generator.SetFilter(PathFilterGroup.CreateFromStrings(paths)); } IList <string> updated = new AList <string>(); while (generator.Next()) { // Skip submodules not registered in .gitmodules file if (generator.GetModulesPath() == null) { continue; } // Skip submodules not registered in parent repository's config string url = generator.GetConfigUrl(); if (url == null) { continue; } Repository submoduleRepo = generator.GetRepository(); // Clone repository is not present if (submoduleRepo == null) { CloneCommand clone = Git.CloneRepository(); Configure(clone); clone.SetURI(url); clone.SetDirectory(generator.GetDirectory()); if (monitor != null) { clone.SetProgressMonitor(monitor); } submoduleRepo = clone.Call().GetRepository(); } try { RevWalk walk = new RevWalk(submoduleRepo); RevCommit commit = walk.ParseCommit(generator.GetObjectId()); string update = generator.GetConfigUpdate(); if (ConfigConstants.CONFIG_KEY_MERGE.Equals(update)) { MergeCommand merge = new MergeCommand(submoduleRepo); merge.Include(commit); merge.Call(); } else { if (ConfigConstants.CONFIG_KEY_REBASE.Equals(update)) { RebaseCommand rebase = new RebaseCommand(submoduleRepo); rebase.SetUpstream(commit); rebase.Call(); } else { // Checkout commit referenced in parent repository's // index as a detached HEAD DirCacheCheckout co = new DirCacheCheckout(submoduleRepo, submoduleRepo.LockDirCache (), commit.Tree); co.SetFailOnConflict(true); co.Checkout(); RefUpdate refUpdate = submoduleRepo.UpdateRef(Constants.HEAD, true); refUpdate.SetNewObjectId(commit); refUpdate.ForceUpdate(); } } } finally { submoduleRepo.Close(); } updated.AddItem(generator.GetPath()); } return(updated); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new InvalidConfigurationException(e.Message, e); } }
/// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override Repository Call() { CheckCallable(); if (path == null || path.Length == 0) { throw new ArgumentException(JGitText.Get().pathNotConfigured); } if (uri == null || uri.Length == 0) { throw new ArgumentException(JGitText.Get().uriNotConfigured); } try { if (SubmoduleExists()) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().submoduleExists , path)); } } catch (IOException e) { throw new JGitInternalException(e.Message, e); } string resolvedUri; try { resolvedUri = SubmoduleWalk.GetSubmoduleRemoteUrl(repo, uri); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } // Clone submodule repository FilePath moduleDirectory = SubmoduleWalk.GetSubmoduleDirectory(repo, path); CloneCommand clone = Git.CloneRepository(); Configure(clone); clone.SetDirectory(moduleDirectory); clone.SetURI(resolvedUri); if (monitor != null) { clone.SetProgressMonitor(monitor); } Repository subRepo = clone.Call().GetRepository(); // Save submodule URL to parent repository's config StoredConfig config = repo.GetConfig(); config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants. CONFIG_KEY_URL, resolvedUri); try { config.Save(); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } // Save path and URL to parent repository's .gitmodules file FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(repo.WorkTree, Constants .DOT_GIT_MODULES), repo.FileSystem); try { modulesConfig.Load(); modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants .CONFIG_KEY_PATH, path); modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants .CONFIG_KEY_URL, uri); modulesConfig.Save(); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new JGitInternalException(e.Message, e); } AddCommand add = new AddCommand(repo); // Add .gitmodules file to parent repository's index add.AddFilepattern(Constants.DOT_GIT_MODULES); // Add submodule directory to parent repository's index add.AddFilepattern(path); try { add.Call(); } catch (NoFilepatternException e) { throw new JGitInternalException(e.Message, e); } return(subRepo); }
/// <summary>Is the configured already a submodule in the index?</summary> /// <returns>true if submodule exists in index, false otherwise</returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal virtual bool SubmoduleExists() { TreeFilter filter = PathFilter.Create(path); return(SubmoduleWalk.ForIndex(repo).SetFilter(filter).Next()); }
/// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override IDictionary <string, string> Call() { CheckCallable(); try { SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo); if (!paths.IsEmpty()) { generator.SetFilter(PathFilterGroup.CreateFromStrings(paths)); } IDictionary <string, string> synced = new Dictionary <string, string>(); StoredConfig config = repo.GetConfig(); while (generator.Next()) { string remoteUrl = generator.GetRemoteUrl(); if (remoteUrl == null) { continue; } string path = generator.GetPath(); config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants. CONFIG_KEY_URL, remoteUrl); synced.Put(path, remoteUrl); Repository subRepo = generator.GetRepository(); if (subRepo == null) { continue; } StoredConfig subConfig; string branch; try { subConfig = subRepo.GetConfig(); // Get name of remote associated with current branch and // fall back to default remote name as last resort branch = GetHeadBranch(subRepo); string remote = null; if (branch != null) { remote = subConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants .CONFIG_KEY_REMOTE); } if (remote == null) { remote = Constants.DEFAULT_REMOTE_NAME; } subConfig.SetString(ConfigConstants.CONFIG_REMOTE_SECTION, remote, ConfigConstants .CONFIG_KEY_URL, remoteUrl); subConfig.Save(); } finally { subRepo.Close(); } } if (!synced.IsEmpty()) { config.Save(); } return(synced); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new JGitInternalException(e.Message, e); } }
public virtual void TestCloneRepositoryWithNestedSubmodules() { git.Checkout().SetName(Constants.MASTER).Call(); // Create submodule 1 FilePath submodule1 = CreateTempDirectory("testCloneRepositoryWithNestedSubmodules1" ); Git sub1Git = Git.Init().SetDirectory(submodule1).Call(); NUnit.Framework.Assert.IsNotNull(sub1Git); Repository sub1 = sub1Git.GetRepository(); NUnit.Framework.Assert.IsNotNull(sub1); AddRepoToClose(sub1); string file = "file.txt"; string path = "sub"; Write(new FilePath(sub1.WorkTree, file), "content"); sub1Git.Add().AddFilepattern(file).Call(); RevCommit commit = sub1Git.Commit().SetMessage("create file").Call(); NUnit.Framework.Assert.IsNotNull(commit); // Create submodule 2 FilePath submodule2 = CreateTempDirectory("testCloneRepositoryWithNestedSubmodules2" ); Git sub2Git = Git.Init().SetDirectory(submodule2).Call(); NUnit.Framework.Assert.IsNotNull(sub2Git); Repository sub2 = sub2Git.GetRepository(); NUnit.Framework.Assert.IsNotNull(sub2); AddRepoToClose(sub2); Write(new FilePath(sub2.WorkTree, file), "content"); sub2Git.Add().AddFilepattern(file).Call(); RevCommit sub2Head = sub2Git.Commit().SetMessage("create file").Call(); NUnit.Framework.Assert.IsNotNull(sub2Head); // Add submodule 2 to submodule 1 Repository r = sub1Git.SubmoduleAdd().SetPath(path).SetURI(sub2.Directory.ToURI() .ToString()).Call(); NUnit.Framework.Assert.IsNotNull(r); AddRepoToClose(r); RevCommit sub1Head = sub1Git.Commit().SetAll(true).SetMessage("Adding submodule") .Call(); NUnit.Framework.Assert.IsNotNull(sub1Head); // Add submodule 1 to default repository r = git.SubmoduleAdd().SetPath(path).SetURI(sub1.Directory.ToURI().ToString()).Call (); NUnit.Framework.Assert.IsNotNull(r); AddRepoToClose(r); NUnit.Framework.Assert.IsNotNull(git.Commit().SetAll(true).SetMessage("Adding submodule" ).Call()); // Clone default repository and include submodules FilePath directory = CreateTempDirectory("testCloneRepositoryWithNestedSubmodules" ); CloneCommand clone = Git.CloneRepository(); clone.SetDirectory(directory); clone.SetCloneSubmodules(true); clone.SetURI(git.GetRepository().Directory.ToURI().ToString()); Git git2 = clone.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(Constants.MASTER, git2.GetRepository().GetBranch( )); NUnit.Framework.Assert.IsTrue(new FilePath(git2.GetRepository().WorkTree, path + FilePath.separatorChar + file).Exists()); NUnit.Framework.Assert.IsTrue(new FilePath(git2.GetRepository().WorkTree, path + FilePath.separatorChar + path + FilePath.separatorChar + file).Exists()); SubmoduleStatusCommand status = new SubmoduleStatusCommand(git2.GetRepository()); IDictionary <string, SubmoduleStatus> statuses = status.Call(); SubmoduleStatus pathStatus = statuses.Get(path); NUnit.Framework.Assert.IsNotNull(pathStatus); NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.INITIALIZED, pathStatus.GetType ()); NUnit.Framework.Assert.AreEqual(sub1Head, pathStatus.GetHeadId()); NUnit.Framework.Assert.AreEqual(sub1Head, pathStatus.GetIndexId()); SubmoduleWalk walk = SubmoduleWalk.ForIndex(git2.GetRepository()); NUnit.Framework.Assert.IsTrue(walk.Next()); Repository clonedSub1 = walk.GetRepository(); AddRepoToClose(clonedSub1); NUnit.Framework.Assert.IsNotNull(clonedSub1); status = new SubmoduleStatusCommand(clonedSub1); statuses = status.Call(); pathStatus = statuses.Get(path); NUnit.Framework.Assert.IsNotNull(pathStatus); NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.INITIALIZED, pathStatus.GetType ()); NUnit.Framework.Assert.AreEqual(sub2Head, pathStatus.GetHeadId()); NUnit.Framework.Assert.AreEqual(sub2Head, pathStatus.GetIndexId()); NUnit.Framework.Assert.IsFalse(walk.Next()); }