forked from gdevic/GitForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMerge.cs
120 lines (106 loc) · 5.31 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
namespace GitForce
{
/// <summary>
/// Manage various merge programs and merge execution
/// </summary>
class ClassMerge
{
// Common merge utilities:
//
// We jam together Windows and Linux utilities
private static readonly string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
private static readonly List<AppHelper> Candidates = new List<AppHelper> {
// Config Short name Path Arguments
// Windows OS:
new AppHelper( "p4merge", Path.Combine(ProgramFiles,@"Perforce\P4Merge.exe"), "%1 %2 %3 %4" ),
new AppHelper( "WinMerge", Path.Combine(ProgramFiles,@"WinMerge\WinMergeU.exe"), "/e /x /u %2 %3 %4" ),
new AppHelper( "BC3", Path.Combine(ProgramFiles,@"Beyond Compare 3\BComp.com"), "%2 %3 /mergeoutput=%4" ),
new AppHelper( "KDiff3", Path.Combine(ProgramFiles,@"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( "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);
// TODO: This might be an option: Set our default tool to be the Git merge tool?
// ClassConfig.SetGlobal("merge.tool", app.Name);
}
}
/// <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);
}
}
}