Permalink
Browse files

add button for creating patch

  • Loading branch information...
1 parent 05cb33a commit 607a698fa901abd774c850e0741178abe2db4386 @yysun committed Feb 5, 2012
View
@@ -10,147 +10,201 @@
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));
- }
- }
+ 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;
+ FileSystemWatcher fileSystemWatcher;
+
+ 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 = new FileSystemWatcher(workingDirectory);
+
+ //fileSystemWatcher.Created += (_, e) => Refresh();
+ //fileSystemWatcher.Changed += (_, e) => Refresh();
+ //fileSystemWatcher.Deleted += (_, e) => Refresh();
+ //fileSystemWatcher.Renamed += (_, e) => Refresh();
+
+ fileSystemWatcher.Changed +=new FileSystemEventHandler(fileSystemWatcher_Changed);
+ fileSystemWatcher.EnableRaisingEvents = true;
+
+ timer = new DispatcherTimer();
+ timer.Interval = TimeSpan.FromMilliseconds(100);
+ timer.Tick+=new EventHandler(timer_Tick);
+ timer.Start();
+ }
+ }
+
+ #region Refresh
+
+ internal DateTime lastTimeRefresh = DateTime.Now.AddDays(-1);
+ internal DateTime nextTimeRefresh = DateTime.Now.AddDays(-1);
+
+ private void fileSystemWatcher_Changed(object source, FileSystemEventArgs e)
+ {
+ if (!NoRefresh)
+ {
+ double delta = DateTime.Now.Subtract(lastTimeRefresh).TotalMilliseconds;
+ if (delta > 500)
+ {
+ NeedRefresh = true;
+ lastTimeRefresh = DateTime.Now;
+ nextTimeRefresh = DateTime.Now;
+ }
+ }
+ }
+
+ internal bool NoRefresh;
+ private bool NeedRefresh;
+
+ private void timer_Tick(Object sender, EventArgs args)
+ {
+ if (NeedRefresh && !NoRefresh)
+ {
+ double delta = DateTime.Now.Subtract(nextTimeRefresh).TotalMilliseconds;
+ if (delta > 200)
+ {
+ System.Diagnostics.Debug.WriteLine("$$$$ Refresh");
+ DisableAutoRefresh();
+ Refresh(true);
+ NoRefresh = false;
+ NeedRefresh = false;
+ nextTimeRefresh = DateTime.Now;
+ }
+ }
+ }
+
+ internal void Refresh(bool reload)
+ {
+ if (reload) tracker.Refresh();
+ GraphChanged(this, null);
+ }
+
+ internal void EnableAutoRefresh()
+ {
+ timer.Start();
+ NoRefresh = false;
+ NeedRefresh = false;
+ lastTimeRefresh = DateTime.Now;
+ }
+
+ internal void DisableAutoRefresh()
+ {
+ timer.Stop();
+ NoRefresh = true;
+ NeedRefresh = false;
+ lastTimeRefresh = DateTime.Now.AddSeconds(2);
+ }
+
+ #endregion
+
+ #region Git commands
+
+ 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);
+ return ret;
+ }
+
+ private void GitRunCmd(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.");
+
+ GitBash.RunCmd(cmd, this.Tracker.GitWorkingDirectory);
+ }
+
+ 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));
+ }
+
+ internal void Patch(string id1, string fileName)
+ {
+ GitRunCmd(string.Format("format-patch {0} -1 --stdout > \"{1}\"", id1, fileName));
+ }
+
+ internal void Patch(string id1, string id2, string fileName)
+ {
+ GitRunCmd(string.Format("format-patch {0}..{1} -o \"{2}\"", id1, id2, fileName));
+ }
+
+ #endregion
+ }
}
View
@@ -14,7 +14,7 @@
<CommandBinding Command="cmd:HistoryViewCommands.GraphLoaded" Executed="GraphLoaded_Executed" />
</Window.CommandBindings>
<Grid x:Name="rootGrid" MouseRightButtonUp="rootGrid_MouseRightButtonUp">
- <GitScc_UI:HistoryGraph HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="graph"/>
+ <GitScc_UI:HistoryGraph HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="graph" Margin="0,90,0,0" />
<my:MainToolBar x:Name="topToolBar" Height="90"
HorizontalAlignment="Stretch" VerticalAlignment="Top">
View
@@ -232,17 +232,21 @@ private void GraphLoaded_Executed(object sender, ExecutedRoutedEventArgs e)
//};
//this.loading.BeginAnimation(UIElement.OpacityProperty, animation);
+ gitViewModel.DisableAutoRefresh();
+
this.loading.Visibility = Visibility.Collapsed;
this.topToolBar.GitViewModel = gitViewModel;
this.Title = gitViewModel.Tracker.HasGitRepository ?
string.Format("{0} ({1})", gitViewModel.Tracker.GitWorkingDirectory, gitViewModel.Tracker.CurrentBranch) :
string.Format("{0} (No Repository)", gitViewModel.WorkingDirectory);
+
+ gitViewModel.EnableAutoRefresh();
}
private void RefreshGraph_Executed(object sender, ExecutedRoutedEventArgs e)
{
this.loading.Visibility = Visibility.Visible;
- gitViewModel.Refresh();
+ gitViewModel.Refresh(true);
this.graph.Show(gitViewModel.Tracker, true);
}
Oops, something went wrong.

0 comments on commit 607a698

Please sign in to comment.