8000 Introduce merge.ff configuration handling · GiTechLab/libgit2sharp@6bfe4ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bfe4ab

Browse files
author
Edward Thomson
committed
Introduce merge.ff configuration handling
When the provided FastForwardStrategy is FastForwardStrategy.Default, honor the merge.ff configuration setting. When merge.ff=only, enable fast-forward only mode; when merge.ff=false, enable no-ff mode.
1 parent 413a892 commit 6bfe4ab

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

LibGit2Sharp.Tests/MergeFixture.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,19 @@ public void FastForwardNonFastForwardableMergeThrows()
471471
}
472472
}
473473

474+
[Fact]
475+
public void CanForceFastForwardMergeThroughConfig()
476+
{
477+
string path = CloneMergeTestRepo();
478+
using (var repo = new Repository(path))
479+
{
480+
repo.Config.Set("merge.ff", "only");
481+
482+
Commit commitToMerge = repo.Branches["normal_merge"].Tip;
483+
Assert.Throws<NonFastForwardException>(() => repo.Merge(commitToMerge, Constants.Signature, new MergeOptions()));
484+
}
485+
}
486+
474487
[Fact]
475488
public void CanMergeAndNotCommit()
476489
{
@@ -508,6 +521,24 @@ public void CanForceNonFastForwardMerge()
508521
}
509522
}
510523

524+
[Fact]
525+
public void CanForceNonFastForwardMergeThroughConfig()
526+
{
527+
string path = CloneMergeTestRepo();
528+
using (var repo = new Repository(path))
529+
{
530+
repo.Config.Set("merge.ff", "false");
531+
532+
Commit commitToMerge = repo.Branches["fast_forward"].Tip;
533+
534+
MergeResult result = repo.Merge(commitToMerge, Constants.Signature, new MergeOptions());
535+
536+
Assert.Equal(MergeStatus.NonFastForward, result.Status);
537+
Assert.Equal("f58f780d5a0ae392efd4a924450b1bbdc0577d32", result.Commit.Id.Sha);
538+
Assert.False(repo.Index.RetrieveStatus().Any());
539+
}
540+
}
541+
511542
[Fact]
512543
public void VerifyUpToDateMerge()
513544
{

LibGit2Sharp/MergeOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class MergeOptions : IConvertableToGitCheckoutOpts
1212
/// Initializes a new instance of the <see cref="MergeOptions"/> class.
1313
/// <para>
1414
/// Default behavior:
15-
/// A fast-forward merge will be performed if possible.
15+
/// A fast-forward merge will be performed if possible, unless the merge.ff configuration option is set.
1616
/// A merge commit will be committed, if one was created.
1717
/// Merge will attempt to find renames.
1818
/// </para>
@@ -110,9 +110,9 @@ CheckoutStrategy IConvertableToGitCheckoutOpts.CheckoutStrategy
110110
public enum FastForwardStrategy
111111
{
112112
/// <summary>
113-
/// Default fast-forward strategy. This will perform a fast-forward merge
114-
/// if possible, otherwise will perform a non-fast-forward merge that
115-
/// results in a merge commit.
113+
/// Default fast-forward strategy. If the merge.ff configuration option is set,
114+
/// it will be used. If it is not set, this will perform a fast-forward merge if
115+
/// possible, otherwise a non-fast-forward merge that results in a merge commit.
116116
/// </summary>
117117
Default = 0,
118118

LibGit2Sharp/Repository.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,21 @@ public CherryPickResult CherryPick(Commit commit, Signature committer, CherryPic
11961196
return result;
11971197
}
11981198

1199+
private FastForwardStrategy FastForwardStrategyFromMergePreference(GitMergePreference preference)
1200+
{
1201+
switch (preference)
1202+
{
1203+
case GitMergePreference.GIT_MERGE_PREFERENCE_NONE:
1204+
return FastForwardStrategy.Default;
1205+
case GitMergePreference.GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY:
1206+
return FastForwardStrategy.FastForwardOnly;
1207+
case GitMergePreference.GIT_MERGE_PREFERENCE_NO_FASTFORWARD:
1208+
return FastForwardStrategy.NoFastFoward;
1209+
default:
1210+
throw new InvalidOperationException(String.Format("Unknown merge preference: {0}", preference));
1211+
}
1212+
}
1213+
11991214
/// <summary>
12001215
/// Internal implementation of merge.
12011216
/// </summary>
@@ -1217,7 +1232,10 @@ private MergeResult Merge(GitMergeHeadHandle[] mergeHeads, Signature merger, Mer
12171232
return new MergeResult(MergeStatus.UpToDate);
12181233
}
12191234

1220-
switch(options.FastForwardStrategy)
1235+
FastForwardStrategy fastForwardStrategy = (options.FastForwardStrategy != FastForwardStrategy.Default) ?
1236+
options.FastForwardStrategy : FastForwardStrategyFromMergePreference(mergePreference);
1237+
1238+
switch(fastForwardStrategy)
12211239
{
12221240
case FastForwardStrategy.Default:
12231241
if (mergeAnalysis.HasFlag(GitMergeAnalysis.GIT_MERGE_ANALYSIS_FASTFORWARD))

0 commit comments

Comments
 (0)
0