public virtual void TestDetachedHeadOnCheckout() { CheckoutCommand co = git.Checkout(); co.SetName("master").Call(); string commitId = db.GetRef(Constants.MASTER).GetObjectId().Name; co = git.Checkout(); co.SetName(commitId).Call(); Ref head = db.GetRef(Constants.HEAD); NUnit.Framework.Assert.IsFalse(head.IsSymbolic()); NUnit.Framework.Assert.AreSame(head, head.GetTarget()); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> private RevCommit TryFastForward(string headName, RevCommit oldCommit, RevCommit newCommit) { bool tryRebase = false; foreach (RevCommit parentCommit in newCommit.Parents) { if (parentCommit.Equals(oldCommit)) { tryRebase = true; } } if (!tryRebase) { return(null); } CheckoutCommand co = new CheckoutCommand(repo); try { co.SetName(newCommit.Name).Call(); if (headName.StartsWith(Constants.R_HEADS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetExpectedOldObjectId(oldCommit); rup.SetNewObjectId(newCommit); rup.SetRefLogMessage("Fast-foward from " + oldCommit.Name + " to " + newCommit.Name , false); RefUpdate.Result res = rup.Update(walk); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FORCED: { break; } default: { throw new IOException("Could not fast-forward"); } } } return(newCommit); } catch (RefAlreadyExistsException e) { throw new JGitInternalException(e.Message, e); } catch (RefNotFoundException e) { throw new JGitInternalException(e.Message, e); } catch (InvalidRefNameException e) { throw new JGitInternalException(e.Message, e); } }
public virtual void TestCheckoutWithNonDeletedFiles() { FilePath testFile = WriteTrashFile("temp", string.Empty); FileInputStream fis = new FileInputStream(testFile); try { FileUtils.Delete(testFile); return; } catch (IOException) { } finally { // the test makes only sense if deletion of // a file with open stream fails fis.Close(); } FileUtils.Delete(testFile); CheckoutCommand co = git.Checkout(); // delete Test.txt in branch test testFile = new FilePath(db.WorkTree, "Test.txt"); NUnit.Framework.Assert.IsTrue(testFile.Exists()); FileUtils.Delete(testFile); NUnit.Framework.Assert.IsFalse(testFile.Exists()); git.Add().AddFilepattern("Test.txt"); git.Commit().SetMessage("Delete Test.txt").SetAll(true).Call(); git.Checkout().SetName("master").Call(); NUnit.Framework.Assert.IsTrue(testFile.Exists()); // lock the file so it can't be deleted (in Windows, that is) fis = new FileInputStream(testFile); try { NUnit.Framework.Assert.AreEqual(CheckoutResult.Status.NOT_TRIED, co.GetResult().GetStatus ()); co.SetName("test").Call(); NUnit.Framework.Assert.IsTrue(testFile.Exists()); NUnit.Framework.Assert.AreEqual(CheckoutResult.Status.NONDELETED, co.GetResult(). GetStatus()); NUnit.Framework.Assert.IsTrue(co.GetResult().GetUndeletedList().Contains("Test.txt" )); } finally { fis.Close(); } }
public virtual void TestCheckoutWithConflict() { CheckoutCommand co = git.Checkout(); try { WriteTrashFile("Test.txt", "Another change"); NUnit.Framework.Assert.AreEqual(CheckoutResult.Status.NOT_TRIED, co.GetResult().GetStatus ()); co.SetName("master").Call(); NUnit.Framework.Assert.Fail("Should have failed"); } catch (Exception) { NUnit.Framework.Assert.AreEqual(CheckoutResult.Status.CONFLICTS, co.GetResult().GetStatus ()); NUnit.Framework.Assert.IsTrue(co.GetResult().GetConflictList().Contains("Test.txt" )); } }