Skip to content

Commit 14adc23

Browse files
committed
enhance: warn users if they are using vimdiff or nvimdiff as external merge/diff tool (#1742)
Signed-off-by: leo <[email protected]>
1 parent b135d59 commit 14adc23

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/Commands/DiffTool.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ public void Open()
1717
var tool = Native.OS.GetDiffMergeTool(true);
1818
if (tool == null)
1919
{
20-
App.RaiseException(Context, "Invalid merge tool in preference setting!");
20+
App.RaiseException(Context, "Invalid diff/merge tool in preference setting!");
2121
return;
2222
}
2323

2424
if (string.IsNullOrEmpty(tool.Cmd))
2525
{
26+
if (!CheckGitConfiguration())
27+
return;
28+
2629
Args = $"difftool -g --no-prompt {_option}";
2730
}
2831
else
@@ -41,6 +44,34 @@ public void Open()
4144
}
4245
}
4346

47+
private bool CheckGitConfiguration()
48+
{
49+
var config = new Config(WorkingDirectory).ReadAll();
50+
if (config.TryGetValue("diff.guitool", out var guiTool))
51+
return CheckCLIBasedTool(guiTool);
52+
if (config.TryGetValue("merge.guitool", out var mergeGuiTool))
53+
return CheckCLIBasedTool(mergeGuiTool);
54+
if (config.TryGetValue("diff.tool", out var diffTool))
55+
return CheckCLIBasedTool(diffTool);
56+
if (config.TryGetValue("merge.tool", out var mergeTool))
57+
return CheckCLIBasedTool(mergeTool);
58+
59+
App.RaiseException(Context, "Missing git configuration: diff.guitool");
60+
return false;
61+
}
62+
63+
private bool CheckCLIBasedTool(string tool)
64+
{
65+
if (tool.StartsWith("vimdiff", StringComparison.Ordinal) ||
66+
tool.StartsWith("nvimdiff", StringComparison.Ordinal))
67+
{
68+
App.RaiseException(Context, $"CLI based diff tool \"{tool}\" is not supported by this app!");
69+
return false;
70+
}
71+
72+
return true;
73+
}
74+
4475
private Models.DiffOption _option;
4576
}
4677
}

src/Commands/MergeTool.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
namespace SourceGit.Commands
45
{
@@ -16,13 +17,17 @@ public async Task<bool> OpenAsync()
1617
var tool = Native.OS.GetDiffMergeTool(false);
1718
if (tool == null)
1819
{
19-
App.RaiseException(Context, "Invalid merge tool in preference setting!");
20+
App.RaiseException(Context, "Invalid diff/merge tool in preference setting!");
2021
return false;
2122
}
2223

2324
if (string.IsNullOrEmpty(tool.Cmd))
2425
{
25-
Args = $"mergetool {_file}";
26+
var ok = await CheckGitConfigurationAsync();
27+
if (!ok)
28+
return false;
29+
30+
Args = $"mergetool -g --no-prompt {_file}";
2631
}
2732
else
2833
{
@@ -33,6 +38,28 @@ public async Task<bool> OpenAsync()
3338
return await ExecAsync().ConfigureAwait(false);
3439
}
3540

41+
private async Task<bool> CheckGitConfigurationAsync()
42+
{
43+
var tool = await new Config(WorkingDirectory).GetAsync("merge.guitool");
44+
if (string.IsNullOrEmpty(tool))
45+
tool = await new Config(WorkingDirectory).GetAsync("merge.tool");
46+
47+
if (string.IsNullOrEmpty(tool))
48+
{
49+
App.RaiseException(Context, "Missing git configuration: merge.guitool");
50+
return false;
51+
}
52+
53+
if (tool.StartsWith("vimdiff", StringComparison.Ordinal) ||
54+
tool.StartsWith("nvimdiff", StringComparison.Ordinal))
55+
{
56+
App.RaiseException(Context, $"CLI based merge tool \"{tool}\" is not supported by this app!");
57+
return false;
58+
}
59+
60+
return true;
61+
}
62+
3663
private string _file;
3764
}
3865
}

0 commit comments

Comments
 (0)