-
Notifications
You must be signed in to change notification settings - Fork 899
Merge #608
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
Merged
Merged
Merge #608
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Linq; | ||
using System; | ||
using System.Linq; | ||
using LibGit2Sharp.Tests.TestHelpers; | ||
using Xunit; | ||
|
||
|
@@ -80,5 +81,192 @@ public void CanRetrieveTheBranchBeingMerged() | |
Assert.Null(mergedHeads[1].Tip); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanMergeRepoNonFastForward() | ||
{ | ||
const string firstBranchFileName = "first branch file.txt"; | ||
const string secondBranchFileName = "second branch file.txt"; | ||
const string sharedBranchFileName = "first+second branch file.txt"; | ||
|
||
string path = CloneStandardTestRepo(); | ||
|
||
using (var repo = new Repository(path)) | ||
{ | ||
var firstBranch = repo.CreateBranch("FirstBranch"); | ||
firstBranch.Checkout(); | ||
var originalTreeCount = firstBranch.Tip.Tree.Count; | ||
|
||
// Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). | ||
AddFileCommitToRepo(repo, sharedBranchFileName); | ||
|
||
var secondBranch = repo.CreateBranch("SecondBranch"); | ||
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). | ||
AddFileCommitToRepo(repo, firstBranchFileName); | ||
|
||
secondBranch.Checkout(); | ||
|
||
// Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). | ||
AddFileCommitToRepo(repo, secondBranchFileName); | ||
|
||
MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); | ||
|
||
Assert.Equal(MergeStatus.NonFastForward, mergeResult.Status); | ||
|
||
Assert.Equal(repo.Head.Tip, mergeResult.Commit); | ||
Assert.Equal(originalTreeCount + 3, mergeResult.Commit.Tree.Count); // Expecting original tree count plussed by the 3 added files. | ||
Assert.Equal(2, mergeResult.Commit.Parents.Count()); // Merge commit should have 2 parents | ||
} | ||
} | ||
|
||
[Fact] | ||
public void IsUpToDateMerge() | ||
{ | ||
const string sharedBranchFileName = "first+second branch file.txt"; | ||
|
||
string path = CloneStandardTestRepo(); | ||
using (var repo = new Repository(path)) | ||
{ | ||
var firstBranch = repo.CreateBranch("FirstBranch"); | ||
firstBranch.Checkout(); | ||
|
||
// Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). | ||
AddFileCommitToRepo(repo, sharedBranchFileName); | ||
|
||
var secondBranch = repo.CreateBranch("SecondBranch"); | ||
|
||
secondBranch.Checkout(); | ||
|
||
MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); | ||
|
||
Assert.Equal(MergeStatus.UpToDate, mergeResult.Status); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanFastForwardRepos() | ||
{ | ||
const string firstBranchFileName = "first branch file.txt"; | ||
const string sharedBranchFileName = "first+second branch file.txt"; | ||
|
||
string path = CloneStandardTestRepo(); | ||
using (var repo = new Repository(path)) | ||
{ | ||
// Reset the index and the working tree. | ||
repo.Reset(ResetMode.Hard); | ||
|
||
// Clean the working directory. | ||
repo.RemoveUntrackedFiles(); | ||
|
||
var firstBranch = repo.CreateBranch("FirstBranch"); | ||
firstBranch.Checkout(); | ||
|
||
// Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). | ||
AddFileCommitToRepo(repo, sharedBranchFileName); | ||
|
||
var secondBranch = repo.CreateBranch("SecondBranch"); | ||
|
||
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). | ||
AddFileCommitToRepo(repo, firstBranchFileName); | ||
|
||
secondBranch.Checkout(); | ||
|
||
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(0, repo.Index.RetrieveStatus().Count()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ConflictingMergeRepos() | ||
{ | ||
const string firstBranchFileName = "first branch file.txt"; | ||
const string secondBranchFileName = "second branch file.txt"; | ||
const string sharedBranchFileName = "first+second branch file.txt"; | ||
|
||
string path = CloneStandardTestRepo(); | ||
using (var repo = new Repository(path)) | ||
{ | ||
var firstBranch = repo.CreateBranch("FirstBranch"); | ||
firstBranch.Checkout(); | ||
|
||
// Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). | ||
AddFileCommitToRepo(repo, sharedBranchFileName); | ||
|
||
var secondBranch = repo.CreateBranch("SecondBranch"); | ||
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). | ||
AddFileCommitToRepo(repo, firstBranchFileName); | ||
AddFileCommitToRepo(repo, sharedBranchFileName, "The first branches comment"); // Change file in first branch | ||
|
||
secondBranch.Checkout(); | ||
// Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). | ||
AddFileCommitToRepo(repo, secondBranchFileName); | ||
AddFileCommitToRepo(repo, sharedBranchFileName, "The second branches comment"); // Change file in second branch | ||
|
||
MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); | ||
|
||
Assert.Equal(MergeStatus.Conflicts, mergeResult.Status); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also add a |
||
|
||
Assert.Null(mergeResult.Commit); | ||
Assert.Equal(1, repo.Index.Conflicts.Count()); | ||
|
||
var conflict = repo.Index.Conflicts.First(); | ||
var changes = repo.Diff.Compare(repo.Lookup<Blob>(conflict.Theirs.Id), repo.Lookup<Blob>(conflict.Ours.Id)); | ||
|
||
Assert.False(changes.IsBinaryComparison); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ConflictingMergeReposBinary() | ||
{ | ||
const string firstBranchFileName = "first branch file.bin"; | ||
const string secondBranchFileName = "second branch file.bin"; | ||
const string sharedBranchFileName = "first+second branch file.bin"; | ||
|
||
string path = CloneStandardTestRepo(); | ||
using (var repo = new Repository(path)) | ||
{ | ||
var firstBranch = repo.CreateBranch("FirstBranch"); | ||
firstBranch.Checkout(); | ||
|
||
// Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). | ||
AddFileCommitToRepo(repo, sharedBranchFileName); | ||
|
||
var secondBranch = repo.CreateBranch("SecondBranch"); | ||
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). | ||
AddFileCommitToRepo(repo, firstBranchFileName); | ||
AddFileCommitToRepo(repo, sharedBranchFileName, "\0The first branches comment\0"); // Change file in first branch | ||
|
||
secondBranch.Checkout(); | ||
// Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). | ||
AddFileCommitToRepo(repo, secondBranchFileName); | ||
AddFileCommitToRepo(repo, sharedBranchFileName, "\0The second branches comment\0"); // Change file in second branch | ||
|
||
MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); | ||
|
||
Assert.Equal(MergeStatus.Conflicts, mergeResult.Status); | ||
|
||
Assert.Equal(1, repo.Index.Conflicts.Count()); | ||
|
||
Conflict conflict = repo.Index.Conflicts.First(); | ||
|
||
var changes = repo.Diff.Compare(repo.Lookup<Blob>(conflict.Theirs.Id), repo.Lookup<Blob>(conflict.Ours.Id)); | ||
|
||
Assert.True(changes.IsBinaryComparison); | ||
} | ||
} | ||
|
||
private Commit AddFileCommitToRepo(IRepository repository, string filename, string content = null) | ||
{ | ||
Touch(repository.Info.WorkingDirectory, filename, content); | ||
|
||
repository.Index.Stage(filename); | ||
|
||
return repository.Commit("New commit", Constants.Signature, Constants.Signature); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibGit2Sharp.Core | ||
{ | ||
internal enum GitMergeFlags | ||
{ | ||
/// <summary> | ||
/// Default | ||
/// </summary> | ||
GIT_MERGE_DEFAULT = 0, | ||
|
||
/// <summary> | ||
/// Do not fast-forward. | ||
/// </summary> | ||
GIT_MERGE_NO_FASTFORWARD = 1, | ||
|
||
/// <summary> | ||
/// Only perform fast-forward. | ||
/// </summary> | ||
GIT_MERGE_FASTFORWARD_ONLY = 2, | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct GitMergeOpts | ||
{ | ||
public uint Version; | ||
|
||
public GitMergeFlags MergeFlags; | ||
public GitMergeTreeOpts MergeTreeOpts; | ||
public GitCheckoutOpts CheckoutOpts; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using LibGit2Sharp.Core; | ||
using LibGit2Sharp.Core.Handles; | ||
|
||
namespace LibGit2Sharp | ||
{ | ||
internal class GitMergeResult | ||
{ | ||
internal GitMergeResult(GitMergeResultHandle handle) | ||
{ | ||
IsUpToDate = Proxy.git_merge_result_is_uptodate(handle); | ||
IsFastForward = Proxy.git_merge_result_is_fastforward(handle); | ||
|
||
if (IsFastForward) | ||
{ | ||
FastForwardId = Proxy.git_merge_result_fastforward_oid(handle); | ||
} | ||
} | ||
|
||
public virtual bool IsUpToDate { get; private set; } | ||
|
||
public virtual bool IsFastForward { get; private set; } | ||
|
||
/// <summary> | ||
/// The ID that a fast-forward merge should advance to. | ||
/// </summary> | ||
public virtual ObjectId FastForwardId { get; private set; } | ||
|
||
public virtual MergeStatus Status | ||
{ | ||
get | ||
{ | ||
if (IsUpToDate) | ||
{ | ||
return MergeStatus.UpToDate; | ||
} | ||
else if (IsFastForward) | ||
{ | ||
return MergeStatus.FastForward; | ||
} | ||
else | ||
{ | ||
return MergeStatus.NonFastForward; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibGit2Sharp.Core | ||
{ | ||
[Flags] | ||
internal enum GitMergeTreeFlags | ||
{ | ||
/// <summary> | ||
/// No options. | ||
/// </summary> | ||
GIT_MERGE_TREE_NORMAL = 0, | ||
|
||
/// <summary> | ||
/// GIT_MERGE_TREE_FIND_RENAMES in libgit2 | ||
/// </summary> | ||
GIT_MERGE_TREE_FIND_RENAMES = (1 << 0), | ||
} | ||
|
||
internal enum GitMergeAutomergeFlags | ||
{ | ||
GIT_MERGE_AUTOMERGE_NORMAL = 0, | ||
GIT_MERGE_AUTOMERGE_NONE = 1, | ||
GIT_MERGE_AUTOMERGE_FAVOR_OURS = 2, | ||
GIT_MERGE_AUTOMERGE_FAVOR_THEIRS = 3, | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct GitMergeTreeOpts | ||
{ | ||
public uint Version; | ||
|
||
public GitMergeTreeFlags MergeTreeFlags; | ||
|
||
/// <summary> | ||
/// Similarity to consider a file renamed. | ||
/// </summary> | ||
public uint RenameThreshold; | ||
|
||
/// <summary> | ||
/// Maximum similarity sources to examine (overrides | ||
/// 'merge.renameLimit' config (default 200) | ||
/// </summary> | ||
public uint TargetLimit; | ||
|
||
/// <summary> | ||
/// Pluggable similarityMetric; pass IntPtr.Zero | ||
/// to use internal metric. | ||
/// </summary> | ||
public IntPtr SimilarityMetric; | ||
|
||
/// <summary> | ||
/// Flags for automerging content. | ||
/// </summary> | ||
public GitMergeAutomergeFlags MergeAutomergeFlags; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibGit2Sharp.Core.Handles | ||
{ | ||
internal class GitMergeHeadHandle : SafeHandleBase | ||
{ | ||
protected override bool ReleaseHandleImpl() | ||
{ | ||
Proxy.git_merge_head_free(handle); | ||
return true; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibGit2Sharp.Core.Handles | ||
{ | ||
internal class GitMergeResultHandle : SafeHandleBase | ||
{ | ||
protected override bool ReleaseHandleImpl() | ||
{ | ||
Proxy.git_merge_result_free(handle); | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamill @nulltoken
Hi guys,
I'm implementing a bit of the merge stuff now and on fast forwards the head is being detached onto the merged commit. The reason these tests pass is because I must've screwed up on these lines. They should be checking the SecondBranch as it's the one that has been merged up to FirstBranch.
The change I made to pass the tests with is below:
https://github.com/libgit2/libgit2sharp/pull/608/files#r9262152