forked from yysun/Git-Source-Control-Provider
/
GitViewModel.cs
156 lines (130 loc) · 5.47 KB
/
GitViewModel.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Input;
using GitScc;
using System.Windows.Threading;
namespace GitUI
{
public static class HistoryViewCommands
{
public static readonly RoutedUICommand CloseCommitDetails = new RoutedUICommand("CloseCommitDetails", "CloseCommitDetails", typeof(MainWindow));
public static readonly RoutedUICommand OpenCommitDetails = new RoutedUICommand("OpenCommitDetails", "OpenCommitDetails", typeof(MainWindow));
public static readonly RoutedUICommand SelectCommit = new RoutedUICommand("SelectCommit", "SelectCommit", typeof(MainWindow));
public static readonly RoutedUICommand CompareCommits = new RoutedUICommand("CompareCommits", "CompareCommits", typeof(MainWindow));
public static readonly RoutedUICommand ExportGraph = new RoutedUICommand("ExportGraph", "ExportGraph", typeof(MainWindow));
public static readonly RoutedUICommand RefreshGraph = new RoutedUICommand("RefreshGraph", "RefreshGraph", typeof(MainWindow));
public static readonly RoutedUICommand ScrollToCommit = new RoutedUICommand("ScrollToCommit", "ScrollToCommit", typeof(MainWindow));
public static readonly RoutedUICommand GraphLoaded = new RoutedUICommand("GraphLoaded", "GraphLoaded", typeof(MainWindow));
}
public class GitViewModel
{
#region singleton
private static GitViewModel current;
public static GitViewModel Current
{
get
{
if (current == null) current = new GitViewModel();
return current;
}
}
#endregion
public event EventHandler GraphChanged = delegate { };
private GitFileStatusTracker tracker;
private string workingDirectory;
public GitFileStatusTracker Tracker { get { return tracker; } }
public string WorkingDirectory { get { return workingDirectory; } }
DispatcherTimer timer;
private GitViewModel()
{
var args = Environment.GetCommandLineArgs();
workingDirectory = args.Length > 1 ? args[1] :
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
tracker = new GitFileStatusTracker(workingDirectory);
if (tracker.HasGitRepository) workingDirectory = tracker.GitWorkingDirectory;
if (Directory.Exists(workingDirectory))
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(workingDirectory);
fileSystemWatcher.Created += (_, e) => Refresh();
fileSystemWatcher.Changed += (_, e) => Refresh();
fileSystemWatcher.Deleted += (_, e) => Refresh();
fileSystemWatcher.Renamed += (_, e) => Refresh();
fileSystemWatcher.EnableRaisingEvents = true;
}
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick +=new EventHandler(timer_Tick);
}
private bool NoRefresh;
private void timer_Tick(Object sender, EventArgs args)
{
timer.Stop(); // one time deal
if (!NoRefresh)
{
NoRefresh = true;
tracker.Refresh();
GraphChanged(this, null);
NoRefresh = false;
}
}
internal void Refresh()
{
timer.Start();
//double delta = DateTime.Now.Subtract(lastTimeRefresh).TotalMilliseconds;
//if (delta > 100)
//{
//System.Diagnostics.Debug.WriteLine("==== GitViewModel Refresh {0}", delta);
//tracker.Refresh();
//}
//lastTimeRefresh = DateTime.Now;
}
internal void RefreshGraph()
{
GraphChanged(this, null);
}
private string GitRun(string cmd)
{
if (!GitBash.Exists) throw new Exception("git.exe is not found.");
if (this.Tracker == null) throw new Exception("Git repository is not found.");
var ret = GitBash.Run(cmd, this.Tracker.GitWorkingDirectory);
Refresh();
return ret;
}
internal string AddTag(string name, string id)
{
return GitRun(string.Format("tag \"{0}\" {1}", name, id));
}
internal string GetTagId(string name)
{
return GitRun("show-ref refs/tags/" + name);
}
internal string DeleteTag(string name)
{
return GitRun("tag -d " + name);
}
internal string AddBranch(string name, string id)
{
return GitRun(string.Format("branch \"{0}\" {1}", name, id));
}
internal string GetBranchId(string name)
{
return GitRun("show-ref refs/heads/" + name);
}
internal string DeleteBranch(string name)
{
return GitRun("branch -d " + name);
}
internal string CheckoutBranch(string name)
{
return GitRun("checkout " + name);
}
internal string Archive(string id, string fileName)
{
return GitRun(string.Format("archive {0} --format=zip --output \"{1}\"", id, fileName));
}
}
}