Skip to content

Commit 1044844

Browse files
committed
refactor: testing merge
- Check exitcode of `git merge-tree --write-tree <branch1> <branch2>` command instead of parsing the output of `git merge-tree <base> <src> <dest>` to testing merge - Requires git >= 2.38.0 Signed-off-by: leo <longshuang@msn.cn>
1 parent df35466 commit 1044844

4 files changed

Lines changed: 27 additions & 55 deletions

File tree

src/Commands/MergeBase.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Commands/MergeTree.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
using System;
1+
using System.Diagnostics;
22
using System.Threading.Tasks;
33

44
namespace SourceGit.Commands
55
{
66
public class MergeTree : Command
77
{
8-
public MergeTree(string repo, string baseTree, string source, string dest)
8+
public MergeTree(string repo, string source, string dest)
99
{
1010
WorkingDirectory = repo;
11-
Context = repo;
12-
RaiseError = false;
13-
Args = $"merge-tree {baseTree} {source} {dest}";
11+
Args = $"merge-tree --write-tree {source} {dest}";
1412
}
1513

1614
public async Task<bool> CheckAsync()
1715
{
18-
var rs = await ReadToEndAsync().ConfigureAwait(false);
19-
if (!rs.IsSuccess)
20-
return false;
16+
using var proc = new Process();
17+
proc.StartInfo = CreateGitStartInfo(false);
2118

22-
var stdout = rs.StdOut;
23-
return !stdout.Contains("\n+>>>>>>>", StringComparison.Ordinal) &&
24-
!stdout.Contains("\n+<<<<<<<", StringComparison.Ordinal) &&
25-
!stdout.Contains("\n->>>>>>>", StringComparison.Ordinal) &&
26-
!stdout.Contains("\n-<<<<<<<", StringComparison.Ordinal);
19+
try
20+
{
21+
proc.Start();
22+
await proc.WaitForExitAsync().ConfigureAwait(false);
23+
return proc.ExitCode == 0;
24+
}
25+
catch
26+
{
27+
// Ignore any exceptions and just return false
28+
return false;
29+
}
2730
}
2831
}
2932
}

src/Models/GitVersions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public static class GitVersions
1616
/// The minimal version of Git that supports the `stash push` command with the `--staged` option.
1717
/// </summary>
1818
public static readonly System.Version STASH_PUSH_ONLY_STAGED = new(2, 35, 0);
19+
20+
/// <summary>
21+
/// The minimal version of Git that supports the `git merge-tree --write-tree` command, which is used for testing merge results without actually performing a merge.
22+
/// </summary>
23+
public static readonly System.Version TESTING_MERGE = new(2, 38, 0);
1924
}
2025
}

src/ViewModels/Merge.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,18 @@ private Models.MergeMode AutoSelectMergeMode()
149149

150150
private void Test()
151151
{
152+
if (Native.OS.GitVersion < Models.GitVersions.TESTING_MERGE)
153+
return;
154+
152155
TestingState = MergeTestingState.Testing;
153156

154157
Task.Run(async () =>
155158
{
156-
var mergeBase = await new Commands.MergeBase(_repo.FullPath, _sourceName, Into)
157-
.GetResultAsync()
159+
var ok = await new Commands.MergeTree(_repo.FullPath, _sourceName, Into)
160+
.CheckAsync()
158161
.ConfigureAwait(false);
159162

160-
if (!string.IsNullOrEmpty(mergeBase))
161-
{
162-
var ok = await new Commands.MergeTree(_repo.FullPath, mergeBase, _sourceName, Into)
163-
.CheckAsync()
164-
.ConfigureAwait(false);
165-
166-
Dispatcher.UIThread.Post(() => TestingState = ok ? MergeTestingState.NoConflicts : MergeTestingState.WillCauseConflicts);
167-
}
168-
else
169-
{
170-
Dispatcher.UIThread.Post(() => TestingState = MergeTestingState.Disabled);
171-
}
163+
Dispatcher.UIThread.Post(() => TestingState = ok ? MergeTestingState.NoConflicts : MergeTestingState.WillCauseConflicts);
172164
});
173165
}
174166

0 commit comments

Comments
 (0)