-
Notifications
You must be signed in to change notification settings - Fork 21
/
ClassMerge.cs
131 lines (117 loc) · 6.66 KB
/
ClassMerge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Manage various merge programs and merge execution
/// </summary>
class ClassMerge
{
// Common merge utilities:
//
// We bundle together Windows and Linux utilities
// Since we build this app in 32-bit mode, on 64-bit OS Windows Program Files will return a (x86) folder variant
private static readonly string ProgramFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
private static readonly string ProgramFilesX64 = ProgramFilesX86.Contains(" (x86)") ? ProgramFilesX86.Replace(" (x86)", "") : ProgramFilesX86;
private static readonly List<AppHelper> Candidates = new List<AppHelper> {
// Config Short name Path Arguments
// Windows OS (x32):
new AppHelper( "p4merge", Path.Combine(ProgramFilesX86,@"Perforce\P4Merge.exe"), "%1 %2 %3 %4" ),
new AppHelper( "WinMerge", Path.Combine(ProgramFilesX86,@"WinMerge\WinMergeU.exe"), "/e /x /u %2 %3 %4" ),
new AppHelper( "BC3", Path.Combine(ProgramFilesX86,@"Beyond Compare 3\BComp.exe"), "%2 %3 %1 %4" ),
new AppHelper( "BC4", Path.Combine(ProgramFilesX86,@"Beyond Compare 4\BComp.exe"), "%2 %3 %1 %4" ),
new AppHelper( "KDiff3", Path.Combine(ProgramFilesX86,@"KDiff3\kdiff3.exe"), "%1 %2 %3 -o %4" ),
// Windows OS (x64):
new AppHelper( "p4merge", Path.Combine(ProgramFilesX64,@"Perforce\P4Merge.exe"), "%1 %2 %3 %4" ),
new AppHelper( "WinMerge", Path.Combine(ProgramFilesX64,@"WinMerge\WinMergeU.exe"), "/e /x /u %2 %3 %4" ),
new AppHelper( "BC3", Path.Combine(ProgramFilesX64,@"Beyond Compare 3\BComp.exe"), "%2 %3 %1 %4" ),
new AppHelper( "BC4", Path.Combine(ProgramFilesX64,@"Beyond Compare 4\BComp.exe"), "%2 %3 %1 %4" ),
new AppHelper( "BC5", Path.Combine(ProgramFilesX64,@"Beyond Compare 5\BComp.exe"), "%2 %3 %1 %4" ),
new AppHelper( "KDiff3", Path.Combine(ProgramFilesX64,@"KDiff3\kdiff3.exe"), "%1 %2 %3 -o %4" ),
// Linux OS:
new AppHelper( "KDiff3", @"/usr/bin/kdiff3", "%1 %2 %3 %4" ),
new AppHelper( "TKDiff", @"/usr/bin/tkdiff", "%2 %3 -o %4" ),
new AppHelper( "Meld", @"/usr/bin/meld", "%1 %2 %3 %4" ),
new AppHelper( "xxdiff", @"/usr/bin/xxdiff", "%2 %3 -M %4" ),
new AppHelper( "Diffuse", @"/usr/bin/diffuse", "%1 %2 %3" ),
new AppHelper( "BCompare", @"/usr/bin/bcompare", "%1 %2 %3" ),
new AppHelper( "Emerge", @"/usr/bin/emerge", "%1 %2 %3 %4" ), // Not tested!
new AppHelper( "vimdiff", @"/usr/bin/vimdiff", "%1 %2 %3 %4" ), // Not tested!
new AppHelper( "gvimdiff", @"/usr/bin/gvimdiff", "%1 %2 %3 %4" ), // Not tested!
new AppHelper( "ecmerge", @"/usr/bin/ecmerge", "%1 %2 %3 %4" ), // Not tested!
new AppHelper( "tortoisemerge", @"/usr/bin/tortoisemerge", "%1 %2 %3 %4" ), // Not tested!
new AppHelper( "opendiff", @"/usr/bin/opendiff", "%1 %2 %3 %4" ), // Not tested!
};
private List<AppHelper> merge = new List<AppHelper>();
/// <summary>
/// Init code to be called on the application startup.
/// Return false if no merge utility was found and user wanted to quit the app.
/// </summary>
public bool Initialize()
{
// Verify the application default merge utility
AppHelper app = new AppHelper(Properties.Settings.Default.MergeAppHelper);
if (File.Exists(app.Path))
{
Configure(app);
return true;
}
// Search for any of the predefined tools
merge = GetDetected();
// If none of the pre-set merge apps are present, show the missing merge dialog
// and return with its selection of whether to continue or quit the app
if (merge.Count == 0)
{
FormMergeMissing formMergeMissing = new FormMergeMissing();
return formMergeMissing.ShowDialog() == DialogResult.OK;
}
// Otherwise, at least one merge app is present, select it as default
Properties.Settings.Default.MergeAppHelper = merge[0].ToString();
Configure(merge[0]);
return true;
}
/// <summary>
/// Configure a given application helper to be a Git merge utility
/// </summary>
public static void Configure(AppHelper app)
{
// Configure application only if it is valid
if (app.Name != string.Empty)
{
string path = app.Path.Replace('\\', '/');
string usr = app.Args.
Replace("%1", "$BASE").
Replace("%2", "$LOCAL").
Replace("%3", "$REMOTE").
Replace("%4", "$MERGED");
string arg = "'" + path + "' " + usr;
ClassConfig.SetGlobal("mergetool." + app.Name + ".path", path);
ClassConfig.SetGlobal("mergetool." + app.Name + ".cmd", arg);
ClassConfig.SetGlobal("mergetool." + app.Name + ".trustExitCode", "false");
// Set the default merge tool
ClassConfig.SetGlobal("merge.tool", app.Name);
ClassConfig.SetGlobal("mergetool.keepBackup", "false");
}
}
/// <summary>
/// Return a proper merge command.
/// This function is called from the actual menu item to merge/resolve a file.
/// </summary>
public static string GetMergeCmd()
{
// Get the application default merge utility
AppHelper app = new AppHelper(Properties.Settings.Default.MergeAppHelper);
string cmd = string.Format(" --tool={0} --no-prompt ", app.Name);
return cmd;
}
/// <summary>
/// Returns a list of detected Merge application helpers
/// </summary>
public static List<AppHelper> GetDetected()
{
return AppHelper.Scan(Candidates);
}
}
}