Skip to content

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 1 commit into from
Jan 16, 2014
Merged

Merge #608

Show file tree
Hide file tree
Changes from all commits
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
190 changes: 189 additions & 1 deletion LibGit2Sharp.Tests/MergeFixture.cs
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;

Expand Down Expand Up @@ -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);
Copy link
Contributor

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.

Assert.Equal(repo.Branches["SecondBranch"].Tip, mergeResult.Commit);
Assert.Equal(repo.Branches["SecondBranch"].Tip, repo.Head.Tip);

The change I made to pass the tests with is below:
https://github.com/libgit2/libgit2sharp/pull/608/files#r9262152

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);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also add a Assert.Null(mergeResult.Commit) here?


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);
}
}
}
33 changes: 33 additions & 0 deletions LibGit2Sharp/Core/GitMergeOpts.cs
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;
}
}
47 changes: 47 additions & 0 deletions LibGit2Sharp/Core/GitMergeResult.cs
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;
}
}
}
}
}
57 changes: 57 additions & 0 deletions LibGit2Sharp/Core/GitMergeTreeOpts.cs
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;
}
}
13 changes: 13 additions & 0 deletions LibGit2Sharp/Core/Handles/GitMergeHeadHandle.cs
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;
}
}
}
13 changes: 13 additions & 0 deletions LibGit2Sharp/Core/Handles/GitMergeResultHandle.cs
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;
}
}
}
Loading