Skip to content
Open
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
48 changes: 48 additions & 0 deletions LibGit2Sharp.Tests/MergeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,54 @@ public void CanSpecifyConflictFileStrategy(CheckoutFileConflictStrategy conflict
}
}

[Theory]
[InlineData(CheckoutFileConflictStrategy.Merge)]
[InlineData(CheckoutFileConflictStrategy.Diff3)]
[InlineData(CheckoutFileConflictStrategy.ZDiff3)]
public void CanSpecifyMergeConflictFileStrategy(CheckoutFileConflictStrategy conflictStrategy)
{
const string conflictFile = "a.txt";
const string expectedFileFormat = "a.{0}.txt";
const string conflictBranchName = "conflicts";
const string resultsBranchName = "results";

string expectedFileName = string.Format(expectedFileFormat, conflictStrategy.ToString().ToLower());
string path = SandboxMergeConflictTestRepo();

using (var repo = new Repository(path))
{
Branch branch = repo.Branches[conflictBranchName];
Assert.NotNull(branch);

// Ensure the working directory is clean before merging
repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();

MergeOptions mergeOptions = new MergeOptions()
{
FileConflictStrategy = conflictStrategy
};

MergeResult result = repo.Merge(branch, Constants.Signature, mergeOptions);
Assert.Equal(MergeStatus.Conflicts, result.Status);

// Get the information on the conflict.
Conflict conflict = repo.Index.Conflicts[conflictFile];

Assert.NotNull(conflict);
Assert.NotNull(conflict.Theirs);
Assert.NotNull(conflict.Ours);

Commit expectedCommit = repo.Branches[resultsBranchName].Tip;
Blob expectedBlob = (Blob)expectedCommit[expectedFileName].Target;
string expectedContent = expectedBlob.GetContentText(new FilteringOptions(expectedFileName));

// Verify the content of the file on disk contains conflict markers.
string fileContent = File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, conflictFile));
Assert.Equal(expectedContent, fileContent);
}
}

[Theory]
[InlineData(MergeFileFavor.Ours)]
[InlineData(MergeFileFavor.Theirs)]
Expand Down
7 changes: 7 additions & 0 deletions LibGit2Sharp.Tests/Resources/mergeconflict_testrepo_wd/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A
B
C
D
E
F
G
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/main
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd4b13da8859445f4357c1e24d92b483ffe2dc85 refs/heads/conflicts
0076e9fe99d1c2df56cd939a897db42d714b088b refs/heads/main
139641856b0ef7941f583972614c1df9bb26939b refs/heads/results
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
P pack-5fd168964fc34fdc1e4a50ae098e21799dd906a4.pack
P pack-9ea8f8acf571abc3f715ea91223671d542aacb6c.pack

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pack-refs with: peeled fully-peeled sorted
cd4b13da8859445f4357c1e24d92b483ffe2dc85 refs/heads/conflicts
0076e9fe99d1c2df56cd939a897db42d714b088b refs/heads/main
139641856b0ef7941f583972614c1df9bb26939b refs/heads/results
7 changes: 7 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static BaseFixture()
public static string ShallowTestRepoPath { get; private set; }
public static string MergedTestRepoWorkingDirPath { get; private set; }
public static string MergeTestRepoWorkingDirPath { get; private set; }
public static string MergeConflictTestRepoWorkingDirPath { get; private set; }
public static string MergeRenamesTestRepoWorkingDirPath { get; private set; }
public static string RevertTestRepoWorkingDirPath { get; private set; }
public static string SubmoduleTestRepoWorkingDirPath { get; private set; }
Expand Down Expand Up @@ -81,6 +82,7 @@ private static void SetUpTestEnvironment()
MergedTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "mergedrepo_wd");
MergeRenamesTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "mergerenames_wd");
MergeTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "merge_testrepo_wd");
MergeConflictTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "mergeconflict_testrepo_wd");
RevertTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "revert_testrepo_wd");
SubmoduleTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "submodule_wd");
SubmoduleTargetTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "submodule_target_wd");
Expand Down Expand Up @@ -201,6 +203,11 @@ protected string SandboxMergeTestRepo()
return Sandbox(MergeTestRepoWorkingDirPath);
}

protected string SandboxMergeConflictTestRepo()
{
return Sandbox(MergeConflictTestRepoWorkingDirPath);
}

protected string SandboxRevertTestRepo()
{
return Sandbox(RevertTestRepoWorkingDirPath);
Expand Down
7 changes: 6 additions & 1 deletion LibGit2Sharp/CheckoutFileConflictStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public enum CheckoutFileConflictStrategy
/// <summary>
/// Write diff3 formated files for conflicts.
/// </summary>
Diff3
Diff3,

/// <summary>
/// Write zdiff3 formated files for conflicts.
/// </summary>
ZDiff3
}
}
5 changes: 5 additions & 0 deletions LibGit2Sharp/Core/GitCheckoutOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ internal enum CheckoutStrategy
/// </summary>
GIT_CHECKOUT_DONT_WRITE_INDEX = (1 << 23),

/// <summary>
/// Include common ancestor data in zdiff3 format for conflicts
/// </summary>
GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3 = (1 << 25),

// THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions LibGit2Sharp/Core/GitCheckoutOptsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ internal static CheckoutStrategy CheckoutStrategyFromFileConflictStrategy(Checko
case CheckoutFileConflictStrategy.Diff3:
flags = CheckoutStrategy.GIT_CHECKOUT_CONFLICT_STYLE_DIFF3;
break;

case CheckoutFileConflictStrategy.ZDiff3:
flags = CheckoutStrategy.GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3;
break;
}

return flags;
Expand Down
5 changes: 5 additions & 0 deletions LibGit2Sharp/Core/GitMergeOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,10 @@ internal enum GitMergeFileFlag
/// Take extra time to find minimal diff
/// </summary>
GIT_MERGE_FILE_DIFF_MINIMAL = (1 << 7),

/// <summary>
/// Create zdiff3 ("zealous diff3")-style files
/// </summary>
GIT_MERGE_FILE_STYLE_ZDIFF3 = (1 << 8),
}
}