public virtual void TestCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty () { string dirName = "testCloneTargetDirectoryNotEmpty"; FilePath directory = CreateTempDirectory(dirName); CloneCommand command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); // clone again command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); try { git2 = command.Call(); // we shouldn't get here NUnit.Framework.Assert.Fail("destination directory already exists and is not an empty folder, cloning should fail" ); } catch (JGitInternalException e) { NUnit.Framework.Assert.IsTrue(e.Message.Contains("not an empty directory")); NUnit.Framework.Assert.IsTrue(e.Message.Contains(dirName)); } }
public virtual void TestCloneRepositoryOnlyOneBranch() { FilePath directory = CreateTempDirectory("testCloneRepositoryWithBranch"); CloneCommand command = Git.CloneRepository(); command.SetBranch("refs/heads/master"); command.SetBranchesToClone(Collections.SingletonList("refs/heads/master")); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/master" ); NUnit.Framework.Assert.AreEqual("refs/remotes/origin/master", AllRefNames(git2.BranchList ().SetListMode(ListBranchCommand.ListMode.REMOTE).Call())); // Same thing, but now test with bare repo directory = CreateTempDirectory("testCloneRepositoryWithBranch_bare"); command = Git.CloneRepository(); command.SetBranch("refs/heads/master"); command.SetBranchesToClone(Collections.SingletonList("refs/heads/master")); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); command.SetBare(true); git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/master" ); NUnit.Framework.Assert.AreEqual("refs/heads/master", AllRefNames(git2.BranchList( ).SetListMode(ListBranchCommand.ListMode.ALL).Call())); }
public virtual void TestCloneRepositoryWithBranch() { try { FilePath directory = CreateTempDirectory("testCloneRepositoryWithBranch"); CloneCommand command = Git.CloneRepository(); command.SetBranch("refs/heads/master"); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/master" ); NUnit.Framework.Assert.AreEqual("refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test" , AllRefNames(git2.BranchList().SetListMode(ListBranchCommand.ListMode.ALL).Call ())); // Same thing, but now without checkout directory = CreateTempDirectory("testCloneRepositoryWithBranch_bare"); command = Git.CloneRepository(); command.SetBranch("refs/heads/master"); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); command.SetNoCheckout(true); git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/master" ); NUnit.Framework.Assert.AreEqual("refs/remotes/origin/master, refs/remotes/origin/test" , AllRefNames(git2.BranchList().SetListMode(ListBranchCommand.ListMode.ALL).Call ())); // Same thing, but now test with bare repo directory = CreateTempDirectory("testCloneRepositoryWithBranch_bare"); command = Git.CloneRepository(); command.SetBranch("refs/heads/master"); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); command.SetBare(true); git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/master" ); NUnit.Framework.Assert.AreEqual("refs/heads/master, refs/heads/test", AllRefNames (git2.BranchList().SetListMode(ListBranchCommand.ListMode.ALL).Call())); } catch (Exception e) { NUnit.Framework.Assert.Fail(e.Message); } }
public virtual void TestCloneRepository() { FilePath directory = CreateTempDirectory("testCloneRepository"); CloneCommand command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); ObjectId id = git2.GetRepository().Resolve("tag-for-blob"); NUnit.Framework.Assert.IsNotNull(id); NUnit.Framework.Assert.AreEqual(git2.GetRepository().GetFullBranch(), "refs/heads/test" ); NUnit.Framework.Assert.AreEqual("origin", git2.GetRepository().GetConfig().GetString (ConfigConstants.CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REMOTE )); NUnit.Framework.Assert.AreEqual("refs/heads/test", git2.GetRepository().GetConfig ().GetString(ConfigConstants.CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_MERGE )); NUnit.Framework.Assert.AreEqual(2, git2.BranchList().SetListMode(ListBranchCommand.ListMode .REMOTE).Call().Count); }
public virtual void TestCloneWithAutoSetupRebase() { FilePath directory = CreateTempDirectory("testCloneRepository1"); CloneCommand command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsFalse(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false)); FileBasedConfig userConfig = SystemReader.GetInstance().OpenUserConfig(null, git. GetRepository().FileSystem); userConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants .CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_ALWAYS); userConfig.Save(); directory = CreateTempDirectory("testCloneRepository2"); command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsTrue(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false)); userConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants .CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_REMOTE); userConfig.Save(); directory = CreateTempDirectory("testCloneRepository2"); command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); git2 = command.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsTrue(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false)); }
public virtual void TestCloneRepository() { try { FilePath directory = CreateTempDirectory("testCloneRepository"); CloneCommand command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = command.Call(); NUnit.Framework.Assert.IsNotNull(git2); } catch (Exception e) { NUnit.Framework.Assert.Fail(e.Message); } }
public virtual void TestLsRemote() { FilePath directory = CreateTempDirectory("testRepository"); CloneCommand command = Git.CloneRepository(); command.SetDirectory(directory); command.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); command.SetCloneAllBranches(true); Git git2 = command.Call(); AddRepoToClose(git2.GetRepository()); LsRemoteCommand lsRemoteCommand = git2.LsRemote(); ICollection <Ref> refs = lsRemoteCommand.Call(); NUnit.Framework.Assert.IsNotNull(refs); NUnit.Framework.Assert.AreEqual(6, refs.Count); }
public virtual void TestCloneRepositoryWithMultipleHeadBranches() { git.Checkout().SetName(Constants.MASTER).Call(); git.BranchCreate().SetName("a").Call(); FilePath directory = CreateTempDirectory("testCloneRepositoryWithMultipleHeadBranches" ); CloneCommand clone = Git.CloneRepository(); clone.SetDirectory(directory); clone.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); Git git2 = clone.Call(); AddRepoToClose(git2.GetRepository()); NUnit.Framework.Assert.IsNotNull(git2); NUnit.Framework.Assert.AreEqual(Constants.MASTER, git2.GetRepository().GetBranch( )); }
public virtual void TestCloneRepositoryWithSubmodules() { git.Checkout().SetName(Constants.MASTER).Call(); string file = "file.txt"; WriteTrashFile(file, "content"); git.Add().AddFilepattern(file).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); git.Add().AddFilepattern(path).AddFilepattern(Constants.DOT_GIT_MODULES).Call(); git.Commit().SetMessage("adding submodule").Call(); FilePath directory = CreateTempDirectory("testCloneRepositoryWithSubmodules"); CloneCommand clone = Git.CloneRepository(); clone.SetDirectory(directory); clone.SetCloneSubmodules(true); clone.SetURI("file://" + git.GetRepository().WorkTree.GetPath()); 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()); 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(commit, pathStatus.GetHeadId()); NUnit.Framework.Assert.AreEqual(commit, pathStatus.GetIndexId()); }
/// <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); }
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()); }