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 TestPullConfigRemoteBranch() { Git localGit = SetUpRepoWithRemote(); Ref remote = localGit.BranchList().SetListMode(ListBranchCommand.ListMode.REMOTE) .Call()[0]; NUnit.Framework.Assert.AreEqual("refs/remotes/origin/master", remote.GetName()); // by default, we should create pull configuration CreateBranch(localGit, "newFromRemote", false, remote.GetName(), null); NUnit.Framework.Assert.AreEqual("origin", localGit.GetRepository().GetConfig().GetString ("branch", "newFromRemote", "remote")); localGit.BranchDelete().SetBranchNames("newFromRemote").Call(); // the pull configuration should be gone after deletion NUnit.Framework.Assert.IsNull(localGit.GetRepository().GetConfig().GetString("branch" , "newFromRemote", "remote")); CreateBranch(localGit, "newFromRemote", false, remote.GetName(), null); NUnit.Framework.Assert.AreEqual("origin", localGit.GetRepository().GetConfig().GetString ("branch", "newFromRemote", "remote")); localGit.BranchDelete().SetBranchNames("refs/heads/newFromRemote").Call(); // the pull configuration should be gone after deletion NUnit.Framework.Assert.IsNull(localGit.GetRepository().GetConfig().GetString("branch" , "newFromRemote", "remote")); // use --no-track CreateBranch(localGit, "newFromRemote", false, remote.GetName(), CreateBranchCommand.SetupUpstreamMode .NOTRACK); NUnit.Framework.Assert.IsNull(localGit.GetRepository().GetConfig().GetString("branch" , "newFromRemote", "remote")); localGit.BranchDelete().SetBranchNames("newFromRemote").Call(); }
public virtual void TestPushRefUpdate() { Git git = new Git(db); Git git2 = new Git(CreateBareRepository()); StoredConfig config = git.GetRepository().GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(git2.GetRepository().Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.AddPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*")); remoteConfig.Update(config); config.Save(); WriteTrashFile("f", "content of f"); git.Add().AddFilepattern("f").Call(); RevCommit commit = git.Commit().SetMessage("adding f").Call(); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); git.Push().SetRemote("test").Call(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/master" )); git.BranchCreate().SetName("refs/heads/test").Call(); git.Checkout().SetName("refs/heads/test").Call(); for (int i = 0; i < 6; i++) { WriteTrashFile("f" + i, "content of f" + i); git.Add().AddFilepattern("f" + i).Call(); commit = git.Commit().SetMessage("adding f" + i).Call(); git.Push().SetRemote("test").Call(); git2.GetRepository().GetAllRefs(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/test" ), "failed to update on attempt " + i); } }
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 TestCheckout() { git.Checkout().SetName("test").Call(); NUnit.Framework.Assert.AreEqual("[Test.txt, mode:100644, content:Some change]", IndexState (CONTENT)); Ref result = git.Checkout().SetName("master").Call(); NUnit.Framework.Assert.AreEqual("[Test.txt, mode:100644, content:Hello world]", IndexState (CONTENT)); NUnit.Framework.Assert.AreEqual("refs/heads/master", result.GetName()); NUnit.Framework.Assert.AreEqual("refs/heads/master", git.GetRepository().GetFullBranch ()); }
public virtual void TestPullFastForward() { PullResult res = target.Pull().Call(); // nothing to update since we don't have different data yet NUnit.Framework.Assert.IsTrue(res.GetFetchResult().GetTrackingRefUpdates().IsEmpty ()); NUnit.Framework.Assert.AreEqual(RebaseResult.Status.UP_TO_DATE, res.GetRebaseResult ().GetStatus()); AssertFileContentsEqual(targetFile, "Hello world"); // change the source file WriteToFile(sourceFile, "Another change"); source.Add().AddFilepattern("SomeFile.txt").Call(); source.Commit().SetMessage("Some change in remote").Call(); res = target.Pull().Call(); NUnit.Framework.Assert.IsFalse(res.GetFetchResult().GetTrackingRefUpdates().IsEmpty ()); NUnit.Framework.Assert.AreEqual(RebaseResult.Status.FAST_FORWARD, res.GetRebaseResult ().GetStatus()); AssertFileContentsEqual(targetFile, "Another change"); NUnit.Framework.Assert.AreEqual(RepositoryState.SAFE, target.GetRepository().GetRepositoryState ()); res = target.Pull().Call(); NUnit.Framework.Assert.AreEqual(RebaseResult.Status.UP_TO_DATE, res.GetRebaseResult ().GetStatus()); }
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 Models.Repository GetIncomingChanges(Models.Repository repository) { try { NGit.Api.Git git = NGit.Api.Git.Open(repository.Path); Repository repo = git.GetRepository(); repo.GetIndexFile(); //Repository repo = new FileRepository(new Sharpen.FilePath(path)); //NGit.Api.Git git = new NGit.Api.Git(repo); //TODO: Find out how git works really, and fix this. FetchCommand fc = git.Fetch(); FetchResult fr = fc.Call(); LogCommand lc = git.Log(); Iterable <RevCommit> lr = lc.Call(); if (git != null && repo != null && fr != null && lr != null) { List <Watchtower.Models.Changeset> commits = new List <Watchtower.Models.Changeset>(); foreach (RevCommit rc in lr) { //TODO: Find out if these values are right. Watchtower.Models.Changeset c = new Watchtower.Models.Changeset(rc.GetParent(0).GetHashCode().ToString(), rc.GetHashCode().ToString(), rc.GetCommitterIdent().GetEmailAddress(), rc.GetCommitterIdent().GetWhen(), rc.GetCommitterIdent().GetName(), rc.GetFullMessage()); commits.Add(c); } repository.IncomingChangesets = commits; } } catch { } return(repository); }
public virtual void TestCherryPickOverExecutableChangeOnNonExectuableFileSystem() { Git git = new Git(db); FilePath file = WriteTrashFile("test.txt", "a"); NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call()); NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit1").Call()); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetCreateBranch(true).SetName("a" ).Call()); WriteTrashFile("test.txt", "b"); NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call()); RevCommit commit2 = git.Commit().SetMessage("commit2").Call(); NUnit.Framework.Assert.IsNotNull(commit2); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName(Constants.MASTER).Call()); DirCache cache = db.LockDirCache(); cache.GetEntry("test.txt").FileMode = FileMode.EXECUTABLE_FILE; cache.Write(); NUnit.Framework.Assert.IsTrue(cache.Commit()); cache.Unlock(); NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit3").Call()); git.GetRepository().GetConfig().SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null , ConfigConstants.CONFIG_KEY_FILEMODE, false); CherryPickResult result = git.CherryPick().Include(commit2).Call(); NUnit.Framework.Assert.IsNotNull(result); NUnit.Framework.Assert.AreEqual(CherryPickResult.CherryPickStatus.OK, result.GetStatus ()); }
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 override void SetUp() { base.SetUp(); dbTarget = CreateWorkRepository(); source = new Git(db); target = new Git(dbTarget); // put some file in the source repo sourceFile = new FilePath(db.WorkTree, "SomeFile.txt"); WriteToFile(sourceFile, "Hello world"); // and commit it source.Add().AddFilepattern("SomeFile.txt").Call(); source.Commit().SetMessage("Initial commit for source").Call(); // configure the target repo to connect to the source via "origin" StoredConfig targetConfig = ((FileBasedConfig)dbTarget.GetConfig()); targetConfig.SetString("branch", "master", "remote", "origin"); targetConfig.SetString("branch", "master", "merge", "refs/heads/master"); RemoteConfig config = new RemoteConfig(targetConfig, "origin"); config.AddURI(new URIish(source.GetRepository().WorkTree.GetPath())); config.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); config.Update(targetConfig); targetConfig.Save(); targetFile = new FilePath(dbTarget.WorkTree, "SomeFile.txt"); // make sure we have the same content target.Pull().Call(); AssertFileContentsEqual(targetFile, "Hello world"); }
public bool VerifyRepository(string path) { bool result = false; try { NGit.Api.Git git = NGit.Api.Git.Open(path); Repository repo = git.GetRepository(); ////Repository repo = new FileRepository(new Sharpen.FilePath(path)); ////NGit.Api.Git git = new NGit.Api.Git(repo); //FetchCommand fc = git.Fetch(); //FetchResult fr = fc.Call(); //LogCommand lc = git.Log(); //Iterable<RevCommit> lr = lc.Call(); string indexFile = repo.GetIndexFile(); //repo.GetRef(); //repo.ReadOrigHead(); if (null != git && null != repo && !string.IsNullOrEmpty(indexFile)) { result = true; } } catch { result = false; } return(result); }
public virtual void TestPushWithoutPushRefSpec() { Git git = new Git(db); Git git2 = new Git(CreateBareRepository()); StoredConfig config = git.GetRepository().GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(git2.GetRepository().Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); remoteConfig.Update(config); config.Save(); WriteTrashFile("f", "content of f"); git.Add().AddFilepattern("f").Call(); RevCommit commit = git.Commit().SetMessage("adding f").Call(); git.Checkout().SetName("not-pushed").SetCreateBranch(true).Call(); git.Checkout().SetName("branchtopush").SetCreateBranch(true).Call(); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/branchtopush" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); git.Push().SetRemote("test").Call(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/branchtopush" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); }
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()); }
public virtual void TestPullConfigLocalBranch() { Git localGit = SetUpRepoWithRemote(); // by default, we should not create pull configuration CreateBranch(localGit, "newFromMaster", false, "master", null); NUnit.Framework.Assert.IsNull(localGit.GetRepository().GetConfig().GetString("branch" , "newFromMaster", "remote")); localGit.BranchDelete().SetBranchNames("newFromMaster").Call(); // use --track CreateBranch(localGit, "newFromMaster", false, "master", CreateBranchCommand.SetupUpstreamMode .TRACK); NUnit.Framework.Assert.AreEqual(".", localGit.GetRepository().GetConfig().GetString ("branch", "newFromMaster", "remote")); localGit.BranchDelete().SetBranchNames("refs/heads/newFromMaster").Call(); // the pull configuration should be gone after deletion NUnit.Framework.Assert.IsNull(localGit.GetRepository().GetConfig().GetString("branch" , "newFromRemote", "remote")); }
public virtual void TestDifferentStates() { Git git = new Git(db); WriteTrashFile("a", "content of a"); WriteTrashFile("b", "content of b"); WriteTrashFile("c", "content of c"); git.Add().AddFilepattern("a").AddFilepattern("b").Call(); Status stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(Set("a", "b"), Set(stat.GetAdded())); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(Set("c"), Set(stat.GetUntracked())); git.Commit().SetMessage("initial").Call(); WriteTrashFile("a", "modified content of a"); WriteTrashFile("b", "modified content of b"); WriteTrashFile("d", "content of d"); git.Add().AddFilepattern("a").AddFilepattern("d").Call(); WriteTrashFile("a", "again modified content of a"); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(Set("d"), Set(stat.GetAdded())); NUnit.Framework.Assert.AreEqual(Set("a"), Set(stat.GetChanged())); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.CollectionAssert.AreEquivalent(Set("b", "a"), Set(stat.GetModified())); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(Set("c"), Set(stat.GetUntracked())); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("second").Call(); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(0, stat.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetUntracked().Count); DeleteTrashFile("a"); NUnit.Framework.Assert.IsFalse(new FilePath(git.GetRepository().WorkTree, "a").Exists ()); git.Add().AddFilepattern("a").SetUpdate(true).Call(); WriteTrashFile("a", "recreated content of a"); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(0, stat.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(Set("a"), Set(stat.GetRemoved())); NUnit.Framework.Assert.AreEqual(Set("a"), Set(stat.GetUntracked())); git.Commit().SetMessage("t").Call(); }
/// <exception cref="NGit.Errors.NoWorkTreeException"></exception> /// <exception cref="System.IO.IOException"></exception> public static void ValidateIndex(Git git) { DirCache dc = git.GetRepository().LockDirCache(); ObjectReader r = git.GetRepository().ObjectDatabase.NewReader(); try { for (int i = 0; i < dc.GetEntryCount(); ++i) { DirCacheEntry entry = dc.GetEntry(i); if (entry.Length > 0) { NUnit.Framework.Assert.AreEqual(entry.Length, r.GetObjectSize(entry.GetObjectId() , ObjectReader.OBJ_ANY)); } } } finally { dc.Unlock(); r.Release(); } }
public virtual void TestUpdateWorkingDirectoryFromIndex2() { CheckoutCommand co = git.Checkout(); FsTick(git.GetRepository().GetIndexFile()); FilePath written1 = WriteTrashFile(FILE1, "3(modified)"); FilePath written2 = WriteTrashFile(FILE2, "a(modified)"); FsTick(written2); // make sure that we get unsmudged entries for FILE1 and FILE2 WriteTrashFile(FILE3, "foo"); git.Add().AddFilepattern(FILE3).Call(); FsTick(git.GetRepository().GetIndexFile()); git.Add().AddFilepattern(FILE1).AddFilepattern(FILE2).Call(); FsTick(git.GetRepository().GetIndexFile()); WriteTrashFile(FILE1, "3(modified again)"); WriteTrashFile(FILE2, "a(modified again)"); FsTick(written2); co.AddPath(FILE1).SetStartPoint(secondCommit).Call(); NUnit.Framework.Assert.AreEqual("2", Read(written1)); NUnit.Framework.Assert.AreEqual("a(modified again)", Read(written2)); ValidateIndex(git); }
/// <exception cref="NGit.Errors.NoWorkTreeException"></exception> /// <exception cref="System.IO.IOException"></exception> public static void ValidateIndex(Git git) { DirCache dc = git.GetRepository().LockDirCache(); ObjectReader r = git.GetRepository().ObjectDatabase.NewReader(); try { for (int i = 0; i < dc.GetEntryCount(); ++i) { DirCacheEntry entry = dc.GetEntry(i); if (entry.Length > 0) { NUnit.Framework.Assert.AreEqual(entry.Length, r.GetObjectSize(entry.GetObjectId() , ObjectReader.OBJ_ANY)); } } } finally { dc.Unlock(); r.Release(); } }
public virtual void RenameBranchNoConfigValues() { StoredConfig config = git.GetRepository().GetConfig(); config.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER); config.Save(); string branch = "b1"; NUnit.Framework.Assert.IsTrue(config.GetNames(ConfigConstants.CONFIG_BRANCH_SECTION , Constants.MASTER).IsEmpty()); NUnit.Framework.Assert.IsNotNull(git.BranchRename().SetNewName(branch).Call()); config = git.GetRepository().GetConfig(); NUnit.Framework.Assert.IsTrue(config.GetNames(ConfigConstants.CONFIG_BRANCH_SECTION , Constants.MASTER).IsEmpty()); NUnit.Framework.Assert.IsTrue(config.GetNames(ConfigConstants.CONFIG_BRANCH_SECTION , branch).IsEmpty()); }
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 RefLogIncludesCommitMessage() { PersonIdent who = new PersonIdent("user", "*****@*****.**"); DeleteTrashFile("file.txt"); RevCommit stashed = git.StashCreate().SetPerson(who).Call(); NUnit.Framework.Assert.IsNotNull(stashed); NUnit.Framework.Assert.AreEqual("content", Read(committedFile)); ValidateStashedCommit(stashed); ReflogReader reader = new ReflogReader(git.GetRepository(), Constants.R_STASH); ReflogEntry entry = reader.GetLastEntry(); NUnit.Framework.Assert.IsNotNull(entry); NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entry.GetOldId()); NUnit.Framework.Assert.AreEqual(stashed, entry.GetNewId()); NUnit.Framework.Assert.AreEqual(who, entry.GetWho()); NUnit.Framework.Assert.AreEqual(stashed.GetFullMessage(), entry.GetComment()); }
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)); }
/// <summary> /// httpプロトコルバッファ値を設定。 /// この設定がないと大きなファイルをpushできない。 /// </summary> /// <param name="folderPath">リポジトリパス</param> private void SettingHttpBufferSize(string folderPath) { FilePath path = new FilePath(folderPath, @".git"); FileRepository db = new FileRepository(path); Git git = new Git(db); var config = git.GetRepository().GetConfig(); config.SetString("http", null, "postBuffer", "524288000"); config.Save(); }
public override void SetUp() { base.SetUp(); dbTarget = CreateWorkRepository(); source = new Git(db); target = new Git(dbTarget); // put some file in the source repo sourceFile = new FilePath(db.WorkTree, "SomeFile.txt"); WriteToFile(sourceFile, "Hello world"); // and commit it source.Add().AddFilepattern("SomeFile.txt").Call(); source.Commit().SetMessage("Initial commit for source").Call(); // configure the target repo to connect to the source via "origin" StoredConfig targetConfig = ((FileBasedConfig)dbTarget.GetConfig()); targetConfig.SetString("branch", "master", "remote", "origin"); targetConfig.SetString("branch", "master", "merge", "refs/heads/master"); RemoteConfig config = new RemoteConfig(targetConfig, "origin"); config.AddURI(new URIish(source.GetRepository().WorkTree.GetPath())); config.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); config.Update(targetConfig); targetConfig.Save(); targetFile = new FilePath(dbTarget.WorkTree, "SomeFile.txt"); // make sure we have the same content target.Pull().Call(); target.Checkout().SetStartPoint("refs/remotes/origin/master").SetName("master").Call (); targetConfig.SetString("branch", "master", "merge", "refs/heads/master"); targetConfig.SetBoolean("branch", "master", "rebase", true); targetConfig.Save(); AssertFileContentsEqual(targetFile, "Hello world"); }
/// <summary> /// Commit a file with the specified contents on the specified branch, /// creating the branch if it didn't exist before. /// </summary> /// <remarks> /// Commit a file with the specified contents on the specified branch, /// creating the branch if it didn't exist before. /// <p> /// It switches back to the original branch after the commit if there was /// one. /// </remarks> /// <param name="filename"></param> /// <param name="contents"></param> /// <param name="branch"></param> /// <returns>the created commit</returns> protected internal virtual RevCommit CommitFile(string filename, string contents, string branch) { try { Git git = new Git(db); string originalBranch = git.GetRepository().GetFullBranch(); if (git.GetRepository().GetRef(branch) == null) { git.BranchCreate().SetName(branch).Call(); } git.Checkout().SetName(branch).Call(); WriteTrashFile(filename, contents); git.Add().AddFilepattern(filename).Call(); RevCommit commit = git.Commit().SetMessage(branch + ": " + filename).Call(); if (originalBranch != null) { git.Checkout().SetName(originalBranch).Call(); } return commit; } catch (IOException e) { throw new RuntimeException(e); } catch (GitAPIException e) { throw new RuntimeException(e); } }
public virtual void TestFileModeMerge() { if (!FS.DETECTED.SupportsExecute()) { return; } // Only Java6 Git git = new Git(db); WriteTrashFile("mergeableMode", "a"); SetExecutable(git, "mergeableMode", false); WriteTrashFile("conflictingModeWithBase", "a"); SetExecutable(git, "conflictingModeWithBase", false); RevCommit initialCommit = AddAllAndCommit(git); // switch branch CreateBranch(initialCommit, "refs/heads/side"); CheckoutBranch("refs/heads/side"); SetExecutable(git, "mergeableMode", true); WriteTrashFile("conflictingModeNoBase", "b"); SetExecutable(git, "conflictingModeNoBase", true); RevCommit sideCommit = AddAllAndCommit(git); // switch branch CreateBranch(initialCommit, "refs/heads/side2"); CheckoutBranch("refs/heads/side2"); SetExecutable(git, "mergeableMode", false); NUnit.Framework.Assert.IsFalse(new FilePath(git.GetRepository().WorkTree, "conflictingModeNoBase" ).Exists()); WriteTrashFile("conflictingModeNoBase", "b"); SetExecutable(git, "conflictingModeNoBase", false); AddAllAndCommit(git); // merge MergeCommandResult result = git.Merge().Include(sideCommit.Id).SetStrategy(MergeStrategy .RESOLVE).Call(); NUnit.Framework.Assert.AreEqual(MergeStatus.CONFLICTING, result.GetMergeStatus()); NUnit.Framework.Assert.IsTrue(CanExecute(git, "mergeableMode")); NUnit.Framework.Assert.IsFalse(CanExecute(git, "conflictingModeNoBase")); }
private bool CanExecute(Git git, string path) { return FS.DETECTED.CanExecute(new FilePath(git.GetRepository().WorkTree, path)); }
public virtual void TestCherryPickOverExecutableChangeOnNonExectuableFileSystem() { Git git = new Git(db); FilePath file = WriteTrashFile("test.txt", "a"); NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call()); NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit1").Call()); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetCreateBranch(true).SetName("a" ).Call()); WriteTrashFile("test.txt", "b"); NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call()); RevCommit commit2 = git.Commit().SetMessage("commit2").Call(); NUnit.Framework.Assert.IsNotNull(commit2); NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName(Constants.MASTER).Call()); DirCache cache = db.LockDirCache(); cache.GetEntry("test.txt").FileMode = FileMode.EXECUTABLE_FILE; cache.Write(); NUnit.Framework.Assert.IsTrue(cache.Commit()); cache.Unlock(); NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit3").Call()); db.FileSystem.SetExecute(file, false); git.GetRepository().GetConfig().SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null , ConfigConstants.CONFIG_KEY_FILEMODE, false); CherryPickResult result = git.CherryPick().Include(commit2).Call(); NUnit.Framework.Assert.IsNotNull(result); NUnit.Framework.Assert.AreEqual(CherryPickResult.CherryPickStatus.OK, result.GetStatus ()); }
public virtual void TestDifferentStates() { Git git = new Git(db); WriteTrashFile("a", "content of a"); WriteTrashFile("b", "content of b"); WriteTrashFile("c", "content of c"); git.Add().AddFilepattern("a").AddFilepattern("b").Call(); Status stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(Set("a", "b"), Set (stat.GetAdded())); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(Set("c"), Set (stat.GetUntracked())); git.Commit().SetMessage("initial").Call(); WriteTrashFile("a", "modified content of a"); WriteTrashFile("b", "modified content of b"); WriteTrashFile("d", "content of d"); git.Add().AddFilepattern("a").AddFilepattern("d").Call(); WriteTrashFile("a", "again modified content of a"); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(Set("d"), Set (stat.GetAdded())); NUnit.Framework.Assert.AreEqual(Set("a"), Set (stat.GetChanged())); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(Set("b", "a"), Set (stat.GetModified())); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(Set("c"), Set (stat.GetUntracked())); git.Add().AddFilepattern(".").Call(); git.Commit().SetMessage("second").Call(); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(0, stat.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetRemoved().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetUntracked().Count); DeleteTrashFile("a"); NUnit.Framework.Assert.IsFalse(new FilePath(git.GetRepository().WorkTree, "a").Exists ()); git.Add().AddFilepattern("a").SetUpdate(true).Call(); WriteTrashFile("a", "recreated content of a"); stat = git.Status().Call(); NUnit.Framework.Assert.AreEqual(0, stat.GetAdded().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetChanged().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetMissing().Count); NUnit.Framework.Assert.AreEqual(0, stat.GetModified().Count); NUnit.Framework.Assert.AreEqual(Set("a"), Set (stat.GetRemoved())); NUnit.Framework.Assert.AreEqual(Set("a"), Set (stat.GetUntracked())); git.Commit().SetMessage("t").Call(); }
public virtual void DropWithInvalidLogIndex() { Write(committedFile, "content2"); Ref stashRef = git.GetRepository().GetRef(Constants.R_STASH); NUnit.Framework.Assert.IsNull(stashRef); RevCommit stashed = git.StashCreate().Call(); NUnit.Framework.Assert.IsNotNull(stashed); stashRef = git.GetRepository().GetRef(Constants.R_STASH); NUnit.Framework.Assert.AreEqual(stashed, git.GetRepository().GetRef(Constants.R_STASH ).GetObjectId()); try { NUnit.Framework.Assert.IsNull(git.StashDrop().SetStashRef(100).Call()); NUnit.Framework.Assert.Fail("Exception not thrown"); } catch (JGitInternalException e) { NUnit.Framework.Assert.IsNotNull(e.Message); NUnit.Framework.Assert.IsTrue(e.Message.Length > 0); } }
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()); }
/// <summary> /// カレントブランチ名を取得する /// </summary> /// <param name="git">Gitクラス</param> /// <returns>ブランチ名</returns> public static string GetCurrentBranch(Git git) { return git.GetRepository().GetBranch(); }
public virtual void TestPushRefUpdate() { Git git = new Git(db); Git git2 = new Git(CreateBareRepository()); StoredConfig config = git.GetRepository().GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(git2.GetRepository().Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.AddPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*")); remoteConfig.Update(config); config.Save(); WriteTrashFile("f", "content of f"); git.Add().AddFilepattern("f").Call(); RevCommit commit = git.Commit().SetMessage("adding f").Call(); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); git.Push().SetRemote("test").Call(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/master" )); git.BranchCreate().SetName("refs/heads/test").Call(); git.Checkout().SetName("refs/heads/test").Call(); for (int i = 0; i < 6; i++) { WriteTrashFile("f" + i, "content of f" + i); git.Add().AddFilepattern("f" + i).Call(); commit = git.Commit().SetMessage("adding f" + i).Call(); git.Push().SetRemote("test").Call(); git2.GetRepository().GetAllRefs(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/test" ), "failed to update on attempt " + i); } }
private void SetExecutable(Git git, string path, bool executable) { FS.DETECTED.SetExecute(new FilePath(git.GetRepository().WorkTree, path), executable ); }
public virtual void TestPushWithoutPushRefSpec() { Git git = new Git(db); Git git2 = new Git(CreateBareRepository()); StoredConfig config = git.GetRepository().GetConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(git2.GetRepository().Directory.ToURI().ToURL()); remoteConfig.AddURI(uri); remoteConfig.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); remoteConfig.Update(config); config.Save(); WriteTrashFile("f", "content of f"); git.Add().AddFilepattern("f").Call(); RevCommit commit = git.Commit().SetMessage("adding f").Call(); git.Checkout().SetName("not-pushed").SetCreateBranch(true).Call(); git.Checkout().SetName("branchtopush").SetCreateBranch(true).Call(); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/branchtopush" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); git.Push().SetRemote("test").Call(); NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/branchtopush" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed" )); NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master" )); }
/// <exception cref="System.Exception"></exception> private string GetHead(Git git, string path) { try { Repository repo = git.GetRepository(); ObjectId headId = repo.Resolve(Constants.HEAD + "^{commit}"); TreeWalk tw = TreeWalk.ForPath(repo, path, new RevWalk(repo).ParseTree(headId)); return Sharpen.Runtime.GetStringForBytes(tw.ObjectReader.Open(tw.GetObjectId(0)). GetBytes()); } catch (Exception) { return string.Empty; } }
public virtual void TestRenameLocalBranch() { // null newName not allowed try { git.BranchRename().Call(); } catch (InvalidRefNameException) { } // expected // invalid newName not allowed try { git.BranchRename().SetNewName("In va lid").Call(); } catch (InvalidRefNameException) { } // expected // not existing name not allowed try { git.BranchRename().SetOldName("notexistingbranch").SetNewName("newname").Call(); } catch (RefNotFoundException) { } // expected // create some branch CreateBranch(git, "existing", false, "master", null); // a local branch Ref branch = CreateBranch(git, "fromMasterForRename", false, "master", null); NUnit.Framework.Assert.AreEqual(Constants.R_HEADS + "fromMasterForRename", branch .GetName()); Ref renamed = git.BranchRename().SetOldName("fromMasterForRename").SetNewName("newName" ).Call(); NUnit.Framework.Assert.AreEqual(Constants.R_HEADS + "newName", renamed.GetName()); try { git.BranchRename().SetOldName(renamed.GetName()).SetNewName("existing").Call(); NUnit.Framework.Assert.Fail("Should have failed"); } catch (RefAlreadyExistsException) { } // expected try { git.BranchRename().SetNewName("In va lid").Call(); NUnit.Framework.Assert.Fail("Rename with invalid ref name should fail"); } catch (InvalidRefNameException) { } // expected // rename without old name and detached head not allowed RefUpdate rup = git.GetRepository().UpdateRef(Constants.HEAD, true); rup.SetNewObjectId(initialCommit); rup.ForceUpdate(); try { git.BranchRename().SetNewName("detached").Call(); } catch (DetachedHeadException) { } }