|
3 | 3 |
|
4 | 4 | namespace SourceGit.Commands
|
5 | 5 | {
|
6 |
| - public static class MergeTool |
| 6 | + public class MergeTool : Command |
7 | 7 | {
|
8 |
| - public static async Task<bool> OpenForMergeAsync(string repo, int toolType, string toolPath, string file) |
| 8 | + public MergeTool(string repo, int type, string exec, string file) |
9 | 9 | {
|
10 |
| - var cmd = new Command(); |
11 |
| - cmd.WorkingDirectory = repo; |
12 |
| - cmd.Context = repo; |
13 |
| - cmd.RaiseError = true; |
| 10 | + WorkingDirectory = repo; |
| 11 | + Context = exec; |
14 | 12 |
|
15 |
| - // NOTE: If no <file> names are specified, 'git mergetool' will run the merge tool program on every file with merge conflicts. |
16 |
| - var fileArg = string.IsNullOrEmpty(file) ? "" : $"\"{file}\""; |
17 |
| - |
18 |
| - if (toolType == 0) |
19 |
| - { |
20 |
| - cmd.Args = $"mergetool {fileArg}"; |
21 |
| - return await cmd.ExecAsync().ConfigureAwait(false); |
22 |
| - } |
| 13 | + _merger = Models.ExternalMerger.Supported.Find(x => x.Type == type); |
| 14 | + _exec = exec; |
| 15 | + _file = string.IsNullOrEmpty(file) ? "" : $"\"{file}\""; |
| 16 | + } |
23 | 17 |
|
24 |
| - if (!File.Exists(toolPath)) |
| 18 | + public async Task<bool> OpenAsync() |
| 19 | + { |
| 20 | + if (_merger == null) |
25 | 21 | {
|
26 |
| - App.RaiseException(repo, $"Can NOT find external merge tool in '{toolPath}'!"); |
| 22 | + App.RaiseException(Context, "Invalid merge tool in preference setting!"); |
27 | 23 | return false;
|
28 | 24 | }
|
29 | 25 |
|
30 |
| - var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType); |
31 |
| - if (supported == null) |
| 26 | + if (_merger.Type == 0) |
32 | 27 | {
|
33 |
| - App.RaiseException(repo, "Invalid merge tool in preference setting!"); |
34 |
| - return false; |
| 28 | + Args = $"mergetool {_file}"; |
35 | 29 | }
|
36 |
| - |
37 |
| - cmd.Args = $"-c mergetool.sourcegit.cmd=\"\\\"{toolPath}\\\" {supported.Cmd}\" -c mergetool.writeToTemp=true -c mergetool.keepBackup=false -c mergetool.trustExitCode=true mergetool --tool=sourcegit {fileArg}"; |
38 |
| - return await cmd.ExecAsync().ConfigureAwait(false); |
39 |
| - } |
40 |
| - |
41 |
| - public static async Task<bool> OpenForDiffAsync(string repo, int toolType, string toolPath, Models.DiffOption option) |
42 |
| - { |
43 |
| - var cmd = new Command(); |
44 |
| - cmd.WorkingDirectory = repo; |
45 |
| - cmd.Context = repo; |
46 |
| - cmd.RaiseError = true; |
47 |
| - |
48 |
| - if (toolType == 0) |
49 |
| - { |
50 |
| - cmd.Args = $"difftool -g --no-prompt {option}"; |
51 |
| - return await cmd.ExecAsync(); |
52 |
| - } |
53 |
| - |
54 |
| - if (!File.Exists(toolPath)) |
| 30 | + else if (File.Exists(_exec)) |
55 | 31 | {
|
56 |
| - App.RaiseException(repo, $"Can NOT find external diff tool in '{toolPath}'!"); |
57 |
| - return false; |
| 32 | + Args = $"-c mergetool.sourcegit.cmd=\"\\\"{_exec}\\\" {_merger.Cmd}\" -c mergetool.writeToTemp=true -c mergetool.keepBackup=false -c mergetool.trustExitCode=true mergetool --tool=sourcegit {_file}"; |
58 | 33 | }
|
59 |
| - |
60 |
| - var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType); |
61 |
| - if (supported == null) |
| 34 | + else |
62 | 35 | {
|
63 |
| - App.RaiseException(repo, "Invalid merge tool in preference setting!"); |
| 36 | + App.RaiseException(Context, $"Can NOT find external merge tool in '{_exec}'!"); |
64 | 37 | return false;
|
65 | 38 | }
|
66 | 39 |
|
67 |
| - cmd.Args = $"-c difftool.sourcegit.cmd=\"\\\"{toolPath}\\\" {supported.DiffCmd}\" difftool --tool=sourcegit --no-prompt {option}"; |
68 |
| - return await cmd.ExecAsync().ConfigureAwait(false); |
| 40 | + return await ExecAsync().ConfigureAwait(false); |
69 | 41 | }
|
| 42 | + |
| 43 | + private Models.ExternalMerger _merger; |
| 44 | + private string _exec; |
| 45 | + private string _file; |
70 | 46 | }
|
71 | 47 | }
|
0 commit comments