forked from gdevic/GitForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassDiff.cs
112 lines (99 loc) · 4.44 KB
/
ClassDiff.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
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Manage various diff programs and diff execution
/// </summary>
public class ClassDiff
{
// Common diff utilities:
//
// We jam together Windows and Linux utilities
private static 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" ),
new AppHelper( "WinMerge", Path.Combine(ProgramFiles,@"WinMerge\WinMergeU.exe"), "/e /x /u %1 %2" ),
new AppHelper( "BC3", Path.Combine(ProgramFiles,@"Beyond Compare 3\BComp.com"), "%1 %2" ),
new AppHelper( "KDiff3", Path.Combine(ProgramFiles,@"KDiff3\kdiff3.exe"), "%1 %2" ),
// Linux OS:
new AppHelper( "KDiff3", @"/usr/bin/kdiff3", "%1 %2" ),
new AppHelper( "TKDiff", @"/usr/bin/tkdiff", "%1 %2" ),
new AppHelper( "Meld", @"/usr/bin/meld", "%1 %2" ),
new AppHelper( "xxdiff", @"/usr/bin/xxdiff", "%1 %2" ),
new AppHelper( "Diffuse", @"/usr/bin/diffuse", "%1 %2" ),
};
private List<AppHelper> diff = new List<AppHelper>();
/// <summary>
/// Init code to be called on the application startup.
/// Return false if no diff utility was found and user wanted to quit the app.
/// </summary>
public bool Initialize()
{
// Verify the application default diff utility
AppHelper app = new AppHelper(Properties.Settings.Default.DiffAppHelper);
if (File.Exists(app.Path))
{
Configure(app);
return true;
}
// Search for any of the predefined tools
diff = GetDetected();
// If none of the pre-set diff apps are present, show the missing diff dialog
// and return with its selection of whether to continue or quit the app
if (diff.Count == 0)
{
FormDiffMissing formDiffMissing = new FormDiffMissing();
return formDiffMissing.ShowDialog() == DialogResult.OK;
}
// Otherwise, at least one diff app is present, select it as default
Properties.Settings.Default.DiffAppHelper = diff[0].ToString();
Configure(diff[0]);
return true;
}
/// <summary>
/// Configure a given application helper to be a Git diff 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", "$LOCAL").
Replace("%2", "$REMOTE");
string arg = "'" + path + "' " + usr;
ClassConfig.SetGlobal("difftool." + app.Name + ".path", path);
ClassConfig.SetGlobal("difftool." + app.Name + ".cmd", arg);
// TODO: This might be an option: Set our default tool to be the Git gui tool?
// ClassConfig.SetGlobal("diff.guitool", app.Name);
}
}
/// <summary>
/// Return a proper diff command.
/// This function is called from the actual menu item to diff files.
/// </summary>
public static string GetDiffCmd()
{
// Get the application default visual diff utility
AppHelper app = new AppHelper(Properties.Settings.Default.DiffAppHelper);
string cmd = string.Format(" --tool={0} --no-prompt ", app.Name);
return cmd;
}
/// <summary>
/// Returns a list of detected diff application helpers
/// </summary>
public static List<AppHelper> GetDetected()
{
return AppHelper.Scan(Candidates);
}
}
}