[sparklediff] check for revisions before the window is created

This commit is contained in:
Hylke Bons 2010-07-06 08:51:37 +01:00
parent d454b03b90
commit f1e956460c
2 changed files with 51 additions and 42 deletions

View file

@ -19,6 +19,7 @@ using Mono.Unix;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace SparkleShare {
@ -85,6 +86,7 @@ namespace SparkleShare {
}
if (args.Length > 0) {
if (args [0].Equals ("--help") || args [0].Equals ("-h")) {
ShowHelp ();
Environment.Exit (0);
@ -95,9 +97,17 @@ namespace SparkleShare {
if (File.Exists (file_path)) {
Gtk.Application.Init ();
string [] revisions = GetRevisionsForFilePath (file_path);
// Quit if the given file doesn't have any history
if (revisions.Length < 2) {
Console.WriteLine ("SparkleDiff: " + file_path + ": File has no history.");
Environment.Exit (-1);
}
SparkleDiffWindow sparkle_diff_window;
sparkle_diff_window = new SparkleDiffWindow (file_path);
sparkle_diff_window = new SparkleDiffWindow (file_path, revisions);
sparkle_diff_window.ShowAll ();
// The main loop
@ -106,14 +116,39 @@ namespace SparkleShare {
} else {
Console.WriteLine ("SparkleDiff: " + file_path + ": No such file or directory.");
Environment.Exit (0);
Environment.Exit (-1);
}
}
}
// Gets a list of all earlier revisions of this file
public static string [] GetRevisionsForFilePath (string file_path)
{
Process process = new Process ();
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = SparkleDiff.GetGitRoot (file_path);
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "log --format=\"%H\" " + SparkleDiff.GetPathFromGitRoot (file_path);
process.Start ();
string output = process.StandardOutput.ReadToEnd ();
string [] revisions = Regex.Split (output.Trim (), "\n");
return revisions;
}
// Prints the help output
public static void ShowHelp ()

View file

@ -37,25 +37,19 @@ namespace SparkleShare {
private string [] Revisions;
public SparkleDiffWindow (string file_path) : base ("")
// TODO: Make the 2nd argument an array of strings
public SparkleDiffWindow (string file_path, string [] revisions) : base ("")
{
Revisions = GetRevisionsForFilePath (file_path);
if (Revisions.Length < 2) {
Console.WriteLine ("SparkleDiff: " + file_path + ": File has no history.");
}
string file_name = System.IO.Path.GetFileName (file_path);
Revisions = revisions;
SetPosition (WindowPosition.Center);
BorderWidth = 12;
IconName = "image-x-generic";
DeleteEvent += Quit;
IconName = "image-x-generic";
// TRANSLATORS: The parameter is a filename
Title = String.Format(_("Comparing Revisions of {0}"), file_name);
@ -164,7 +158,8 @@ namespace SparkleShare {
}
private void ResizeToViews () {
private void ResizeToViews ()
{
int new_width = ViewLeft.GetImage ().Pixbuf.Width + ViewRight.GetImage ().Pixbuf.Width + 100;
int new_height = 200;
@ -182,7 +177,8 @@ namespace SparkleShare {
}
// Hooks up two views so their scrollbars will be kept in sync
private void HookUpViews () {
private void HookUpViews ()
{
ViewLeft.ScrolledWindow.Hadjustment.ValueChanged += SyncViewsHorizontally;
ViewLeft.ScrolledWindow.Vadjustment.ValueChanged += SyncViewsVertically;
@ -193,7 +189,8 @@ namespace SparkleShare {
// Keeps the two image views in sync horizontally
private void SyncViewsHorizontally (object o, EventArgs args) {
private void SyncViewsHorizontally (object o, EventArgs args)
{
Adjustment source_adjustment = (Adjustment) o;
@ -206,7 +203,8 @@ namespace SparkleShare {
// Keeps the two image views in sync vertically
private void SyncViewsVertically (object o, EventArgs args) {
private void SyncViewsVertically (object o, EventArgs args)
{
Adjustment source_adjustment = (Adjustment) o;
@ -218,29 +216,6 @@ namespace SparkleShare {
}
// Gets a list of all earlier revisions of this file
private string [] GetRevisionsForFilePath (string file_path)
{
Process process = new Process ();
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = SparkleDiff.GetGitRoot (file_path);
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "log --format=\"%H\" " + SparkleDiff.GetPathFromGitRoot (file_path);
process.Start ();
string output = process.StandardOutput.ReadToEnd ();
string [] revisions = Regex.Split (output.Trim (), "\n");
return revisions;
}
// Converts a UNIX timestamp to a more usable time object
public DateTime UnixTimestampToDateTime (int timestamp)
{
@ -250,10 +225,9 @@ namespace SparkleShare {
// Quits the program
private void Quit (object o, EventArgs args) {
private void Quit (object o, EventArgs args)
{
Environment.Exit (0);
}
}