[repo] port GetCommits to git#

This commit is contained in:
Hylke Bons 2010-10-10 22:39:00 +01:00
parent d5dc2cdfee
commit 43cfef51ad

View file

@ -967,107 +967,44 @@ namespace SparkleLib {
public List <SparkleCommit> GetCommits (int count) public List <SparkleCommit> GetCommits (int count)
{ {
if (count < 0) if (count <= 0)
return null; return null;
List <SparkleCommit> commits = new List <SparkleCommit> (); List <SparkleCommit> commits = new List <SparkleCommit> ();
Process process = new Process () { string commit_ref = "HEAD";
EnableRaisingEvents = true
};
process.StartInfo.RedirectStandardOutput = true; try {
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = LocalPath;
TimeZone current_time_zone = TimeZone.CurrentTimeZone; for (int i = 0; i < count; i++) {
DateTime current_date = DateTime.Now;
TimeSpan current_offset = current_time_zone.GetUtcOffset (current_date); Commit commit = new Commit (this, commit_ref);
string utc_offset = current_offset.ToString ();
int unix_timestamp = 0; SparkleCommit sparkle_commit = new SparkleCommit (commit.Author.Name, commit.Author.EmailAddress,
commit.CommitDate.DateTime, commit.Hash);
if (utc_offset.StartsWith ("-")) { foreach (Change change in commit.Changes) {
// Add the timezone difference in hours when in a positive timezone if (change.ChangeType.ToString ().Equals ("Added"))
unix_timestamp = -3600 * int.Parse (utc_offset.Substring (1, 2)); sparkle_commit.Added.Add (change.Path);
} else { if (change.ChangeType.ToString ().Equals ("Modified"))
sparkle_commit.Edited.Add (change.Path);
// Remove the timezone difference in hours when in a negative timezone if (change.ChangeType.ToString ().Equals ("Deleted"))
unix_timestamp = 3600 * int.Parse (utc_offset.Substring (0, 2)); sparkle_commit.Deleted.Add (change.Path);
}
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "log --format=\"%at\t%an\t%ae\t%H\" -" + count;
process.Start ();
process.WaitForExit ();
string output = process.StandardOutput.ReadToEnd ().Trim ();
output = output.TrimStart ("\n".ToCharArray ());
string [] lines = Regex.Split (output, "\n");
Array.Sort (lines);
Array.Reverse (lines);
foreach (string line in lines) {
string [] parts = Regex.Split (line, "\t");
int local_timestamp = unix_timestamp + int.Parse (parts [0]);
string user_name = parts [1];
string user_email = parts [2];
string hash = parts [3];
DateTime date_time = SparkleHelpers.UnixTimestampToDateTime (local_timestamp);
SparkleCommit commit = new SparkleCommit (user_name, user_email, date_time, hash);
// Find out what has changed in the commit.
// --name-status lists affected files with the modification type,
// -C detects renames
process.StartInfo.Arguments = "show " + hash + " --name-status -C";
process.Start ();
process.WaitForExit ();
output = process.StandardOutput.ReadToEnd ().Trim ();
output = output.TrimStart ("\n".ToCharArray ());
string [] file_lines = Regex.Split (output, "\n");
foreach (string file_line in file_lines) {
string file_path = "";
if (file_line.Length > 1)
file_path = file_line.Substring (2);
if (file_line.StartsWith ("M\t"))
commit.Edited.Add (file_path);
if (file_line.StartsWith ("A\t"))
commit.Added.Add (file_path);
if (file_line.StartsWith ("D\t"))
commit.Deleted.Add (file_path);
if (file_line.StartsWith ("R")) {
file_path = file_line.Substring (5);
string [] paths = Regex.Split (file_path, "\t");
commit.MovedFrom.Add (paths [0]);
commit.MovedTo.Add (paths [1]);
} }
commits.Add (sparkle_commit);
commit_ref += "^";
} }
commits.Add (commit); } catch (System.NullReferenceException) {
// FIXME: Doesn't show the first commit because it throws
// this exception before getting to it.
} }