Skip to content
Browse files

work in progress on fast forward merging!

  • Loading branch information...
1 parent f2418bf commit b90556a8270b2583269e9e28e47d24b1730b60f1 @henon committed Mar 31, 2010
View
3 GitSharp.Tests/GitSharp.Core/RevWalk/MergeBaseDetectionTest.cs
@@ -41,6 +41,7 @@
using System.Text;
using GitSharp.Core.RevWalk;
using GitSharp.Core.Tests;
+using GitSharp.Core.Util;
using NUnit.Framework;
namespace GitSharp.Core.Tests.RevWalk
@@ -51,6 +52,8 @@ public class MergeBaseDetectionTest : SampleDataRepositoryTestCase
[Test]
public void IsMergedIntoTest()
{
+ var userhome= FS.userHome();
+
var rw = new GitSharp.Core.RevWalk.RevWalk(db);
Assert.IsTrue( rw.isMergedInto(new RevCommit(db.Resolve("a")), new RevCommit(db.Resolve("c"))));
}
View
24 GitSharp.Tests/GitSharp/MergeTests.cs
@@ -44,17 +44,37 @@ public void MergeStrategyTheirs()
/// When not allowing a fast forward merge, a new commit is created the merged commits as parent.
/// </summary>
[Test]
- public void MergeStrategyRecursive_FastForwardOff()
+ public void MergeStrategyRecursive_FastForward()
{
using (var repo = GetTrashRepository())
{
var a = repo.Get<Branch>("a");
var c = repo.Get<Branch>("c");
- var result = Git.Merge(new MergeOptions { NoFastForward = true, Branches = new[]{ a, c }, MergeStrategy = MergeStrategy.Recursive });
+ var result = a.Merge(c, MergeStrategy.Recursive);
+ Assert.IsTrue(result.Success);
+ var a_commit = repo.Get<Branch>("a").CurrentCommit;
+ var c_commit = repo.Get<Branch>("c").CurrentCommit;
+ Assert.AreEqual(c_commit, result.Commit);
+ Assert.AreEqual(c_commit, a_commit);
+ }
+ }
+
+ /// <summary>
+ /// When not allowing a fast forward merge, a new commit is created the merged commits as parent.
+ /// </summary>
+ [Test]
+ public void MergeStrategyRecursive_No_FastForward()
+ {
+ using (var repo = GetTrashRepository())
+ {
+ var a = repo.Get<Branch>("a");
+ var c = repo.Get<Branch>("c");
+ var result = Git.Merge(new MergeOptions { NoFastForward = true, Branches = new[] { a, c }, MergeStrategy = MergeStrategy.Recursive });
Assert.IsTrue(result.Success);
Assert.AreEqual(repo.Get<Branch>("c").CurrentCommit.Tree, a.CurrentCommit.Tree);
var a_commit = repo.Get<Branch>("a").CurrentCommit;
var c_commit = repo.Get<Branch>("c").CurrentCommit;
+ Assert.AreNotEqual(c_commit, result.Commit);
Assert.IsTrue(a_commit.Parents.Contains(c_commit));
Assert.IsTrue(a_commit.Parents.Contains(repo.Tags["A"].Target as Commit));
Assert.AreEqual(2, a.CurrentCommit.Parents.Count());
View
50 GitSharp/Commands/MergeCommand.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using GitSharp.Core.Merge;
+using GitSharp.Core.RevWalk;
namespace GitSharp.Commands
{
@@ -13,28 +14,51 @@ public static MergeResult Execute(MergeOptions options)
{
options.Validate();
- var merger = SelectMerger(options);
- bool success = merger.Merge(options.Commits.Select(c => ((Core.Commit)c).CommitId).ToArray());
- var result = new MergeResult { Success = success };
- result.Tree = new Tree(options.Repository, merger.GetResultTreeId());
- if (options.NoCommit)
+ var result = new MergeResult();
+ if (CanFastForward(options))
{
-
+ result.Success=true;
+ result.Commit = options.Commits[1];
+ result.Tree = result.Commit.Tree;
+ UpdateBranch(options, result);
}
else
{
- if (string.IsNullOrEmpty(options.Message))
+ var merger = SelectMerger(options);
+ result.Success = merger.Merge(options.Commits.Select(c => ((Core.Commit)c).CommitId).ToArray());
+ result.Tree = new Tree(options.Repository, merger.GetResultTreeId());
+ if (options.NoCommit)
+ {
+ // this is empty on purpose
+ }
+ else
{
- options.Message = FormatMergeMessage(options);
+ if (string.IsNullOrEmpty(options.Message))
+ {
+ options.Message = FormatMergeMessage(options);
+ }
+ var author = Author.GetDefaultAuthor(options.Repository);
+ result.Commit = Commit.Create(options.Message, options.Commits, result.Tree, author, author, DateTimeOffset.Now);
+ UpdateBranch(options, result);
}
- var author = Author.GetDefaultAuthor(options.Repository);
- result.Commit = Commit.Create(options.Message, options.Commits, result.Tree, author, author, DateTimeOffset.Now);
- if (options.Branches.Length >= 1 && options.Branches[0] is Branch)
- Ref.Update("refs/heads/" + options.Branches[0].Name, result.Commit);
}
return result;
}
+ private static void UpdateBranch(MergeOptions options, MergeResult result)
+ {
+ if (options.Branches.Length >= 1 && options.Branches[0] is Branch)
+ Ref.Update("refs/heads/" + options.Branches[0].Name, result.Commit);
+ }
+
+ private static bool CanFastForward(MergeOptions options)
+ {
+ if (options.MergeStrategy != MergeStrategy.Recursive || options.NoFastForward || options.Commits.Length != 2)
+ return false;
+ var rw = new RevWalk(options.Repository);
+ return rw.isMergedInto(new RevCommit(options.Commits[0]._id), new RevCommit(options.Commits[1]._id));
+ }
+
private static string FormatMergeMessage(MergeOptions options)
{
if (options.Branches.Length > 0 && options.Branches[0] is Branch)
@@ -54,7 +78,7 @@ private static Merger SelectMerger(MergeOptions options)
case MergeStrategy.Recursive:
return Core.Merge.MergeStrategy.SimpleTwoWayInCore.NewMerger(options.Repository);
}
- throw new ArgumentException("Invalid merge option: "+options.MergeStrategy);
+ throw new ArgumentException("Invalid merge option: " + options.MergeStrategy);
}
}

0 comments on commit b90556a

Please sign in to comment.
Something went wrong with that request. Please try again.