Skip to content

Fix for FastForward detach issue + test. #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions LibGit2Sharp.Tests/MergeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public void CanFastForwardRepos()

MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature);

Assert.Equal(MergeStatus.FastForward, mergeResult.Status);
Assert.Equal(repo.Branches["FirstBranch"].Tip, mergeResult.Commit);
Assert.Equal(repo.Branches["FirstBranch"].Tip, repo.Head.Tip);
Assert.Equal(MergeStatus.FastForward, mergeResult.Status);
Assert.Equal(repo.Branches["SecondBranch"].Tip, mergeResult.Commit);
Assert.Equal(repo.Branches["SecondBranch"].Tip, repo.Head.Tip);
Assert.Equal(0, repo.Index.RetrieveStatus().Count());
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ private void FastForward(Commit fastForwardCommit)

CheckoutTree(fastForwardCommit.Tree, null, checkoutOpts);

Refs.UpdateTarget("HEAD", fastForwardCommit.Id.Sha);
Refs.UpdateTarget(this.Head.CanonicalName, fastForwardCommit.Id.Sha);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this won't work in detached HEAD mode.

Update: I've worked a bit on this. How about the following

diff --git a/LibGit2Sharp.Tests/MergeFixture.cs b/LibGit2Sharp.Tests/MergeFixture.cs
index 8a9976e..2867b9e 100644
--- a/LibGit2Sharp.Tests/MergeFixture.cs
+++ b/LibGit2Sharp.Tests/MergeFixture.cs
@@ -1,7 +1,7 @@
-using System;
-using System.Linq;
+using System.Linq;
 using LibGit2Sharp.Tests.TestHelpers;
 using Xunit;
+using Xunit.Extensions;

 namespace LibGit2Sharp.Tests
 {
@@ -143,8 +143,10 @@ public void IsUpToDateMerge()
             }
         }

-        [Fact]
-        public void CanFastForwardRepos()
+        [Theory]
+        [InlineData(true)]
+        [InlineData(false)]
+        public void CanFastForwardRepos(bool shouldMergeOccurInDetachedHeadState)
         {
             const string firstBranchFileName = "first branch file.txt";
             const string sharedBranchFileName = "first+second branch file.txt";
@@ -171,12 +173,28 @@ public void CanFastForwardRepos()

                 secondBranch.Checkout();

+                if (shouldMergeOccurInDetachedHeadState)
+                {
+                    // Detaches HEAD
+                    repo.Checkout(repo.Head.Tip);
+                }
+
+                Assert.Equal(shouldMergeOccurInDetachedHeadState, repo.Info.IsHeadDetached);
+
                 MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature);

                 Assert.Equal(MergeStatus.FastForward, mergeResult.Status); 
-                Assert.Equal(repo.Branches["SecondBranch"].Tip, mergeResult.Commit);
-                Assert.Equal(repo.Branches["SecondBranch"].Tip, repo.Head.Tip);
+                Assert.Equal(repo.Head.Tip, mergeResult.Commit);
+
                 Assert.Equal(0, repo.Index.RetrieveStatus().Count());
+                Assert.Equal(shouldMergeOccurInDetachedHeadState, repo.Info.IsHeadDetached);
+
+                if (!shouldMergeOccurInDetachedHeadState)
+                {
+                    // Ensure HEAD is still attached and points to SecondBranch
+                    Assert.Equal(repo.Refs.Head.TargetIdentifier, secondBranch.CanonicalName);
+                }
+
             }
         }
diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index 621c522..7854e8c 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -1101,7 +1101,9 @@ private void FastForward(Commit fastForwardCommit)

             CheckoutTree(fastForwardCommit.Tree, null, checkoutOpts);

-            Refs.UpdateTarget(this.Head.CanonicalName, fastForwardCommit.Id.Sha);
+            var reference = Refs.Head.ResolveToDirectReference();
+
+            Refs.UpdateTarget(reference, fastForwardCommit.Id.Sha);

             // TODO: Update Reflog...
         }


// TODO: Update Reflog...
}
Expand Down