// Returns a list of latest commits // TODO: Method needs to be made a lot faster public List<SparkleCommit> GetCommits(int count) { if (count < 1) count = 30; List <SparkleCommit> commits = new List <SparkleCommit> (); SparkleGit git_log = new SparkleGit (LocalPath, "log -" + count + " --raw -M --date=iso"); Console.OutputEncoding = System.Text.Encoding.Unicode; git_log.Start (); // Reading the standard output HAS to go before // WaitForExit, or it will hang forever on output > 4096 bytes string output = git_log.StandardOutput.ReadToEnd (); git_log.WaitForExit (); string [] lines = output.Split ("\n".ToCharArray ()); List <string> entries = new List <string> (); int j = 0; string entry = "", last_entry = ""; foreach (string line in lines) { if (line.StartsWith ("commit") && j > 0) { entries.Add (entry); entry = ""; } entry += line + "\n"; j++; last_entry = entry; } entries.Add (last_entry); // TODO: Need to optimise for speed foreach (string log_entry in entries) { Regex regex; bool is_merge_commit = false; if (log_entry.Contains ("\nMerge: ")) { regex = new Regex (@"commit ([a-z0-9]{40})\n" + "Merge: .+ .+\n" + "Author: (.+) <(.+)>\n" + "Date: ([0-9]{4})-([0-9]{2})-([0-9]{2}) " + "([0-9]{2}):([0-9]{2}):([0-9]{2}) .([0-9]{4})\n" + "*"); is_merge_commit = true; } else { regex = new Regex (@"commit ([a-z0-9]{40})\n" + "Author: (.+) <(.+)>\n" + "Date: ([0-9]{4})-([0-9]{2})-([0-9]{2}) " + "([0-9]{2}):([0-9]{2}):([0-9]{2}) .([0-9]{4})\n" + "*"); } Match match = regex.Match (log_entry); if (match.Success) { SparkleCommit commit = new SparkleCommit (); commit.Hash = match.Groups [1].Value; commit.UserName = match.Groups [2].Value; commit.UserEmail = match.Groups [3].Value; commit.IsMerge = is_merge_commit; commit.DateTime = new DateTime (int.Parse (match.Groups [4].Value), int.Parse (match.Groups [5].Value), int.Parse (match.Groups [6].Value), int.Parse (match.Groups [7].Value), int.Parse (match.Groups [8].Value), int.Parse (match.Groups [9].Value)); string [] entry_lines = log_entry.Split ("\n".ToCharArray ()); foreach (string entry_line in entry_lines) { if (entry_line.StartsWith (":")) { string change_type = entry_line [37].ToString (); string file_path = entry_line.Substring (39); string to_file_path; if (change_type.Equals ("A")) { commit.Added.Add (file_path); } else if (change_type.Equals ("M")) { commit.Edited.Add (file_path); } else if (change_type.Equals ("D")) { commit.Deleted.Add (file_path); } else if (change_type.Equals ("R")) { int tab_pos = entry_line.LastIndexOf ("\t"); file_path = entry_line.Substring (42, tab_pos - 42); to_file_path = entry_line.Substring (tab_pos + 1); commit.MovedFrom.Add (file_path); commit.MovedTo.Add (to_file_path); } } } commits.Add (commit); } } return commits; }
// Returns a list of latest commits // TODO: Method needs to be made a lot faster public List <SparkleCommit> GetCommits(int count) { if (count < 1) { count = 30; } List <SparkleCommit> commits = new List <SparkleCommit> (); SparkleGit git_log = new SparkleGit(LocalPath, "log -" + count + " --raw -M --date=iso"); Console.OutputEncoding = System.Text.Encoding.Unicode; git_log.Start(); // Reading the standard output HAS to go before // WaitForExit, or it will hang forever on output > 4096 bytes string output = git_log.StandardOutput.ReadToEnd(); git_log.WaitForExit(); string [] lines = output.Split("\n".ToCharArray()); List <string> entries = new List <string> (); int j = 0; string entry = "", last_entry = ""; foreach (string line in lines) { if (line.StartsWith("commit") && j > 0) { entries.Add(entry); entry = ""; } entry += line + "\n"; j++; last_entry = entry; } entries.Add(last_entry); // TODO: Need to optimise for speed foreach (string log_entry in entries) { Regex regex; bool is_merge_commit = false; if (log_entry.Contains("\nMerge: ")) { regex = new Regex(@"commit ([a-z0-9]{40})\n" + "Merge: .+ .+\n" + "Author: (.+) <(.+)>\n" + "Date: ([0-9]{4})-([0-9]{2})-([0-9]{2}) " + "([0-9]{2}):([0-9]{2}):([0-9]{2}) .([0-9]{4})\n" + "*"); is_merge_commit = true; } else { regex = new Regex(@"commit ([a-z0-9]{40})\n" + "Author: (.+) <(.+)>\n" + "Date: ([0-9]{4})-([0-9]{2})-([0-9]{2}) " + "([0-9]{2}):([0-9]{2}):([0-9]{2}) .([0-9]{4})\n" + "*"); } Match match = regex.Match(log_entry); if (match.Success) { SparkleCommit commit = new SparkleCommit(); commit.Hash = match.Groups [1].Value; commit.UserName = match.Groups [2].Value; commit.UserEmail = match.Groups [3].Value; commit.IsMerge = is_merge_commit; commit.DateTime = new DateTime(int.Parse(match.Groups [4].Value), int.Parse(match.Groups [5].Value), int.Parse(match.Groups [6].Value), int.Parse(match.Groups [7].Value), int.Parse(match.Groups [8].Value), int.Parse(match.Groups [9].Value)); string [] entry_lines = log_entry.Split("\n".ToCharArray()); foreach (string entry_line in entry_lines) { if (entry_line.StartsWith(":")) { string change_type = entry_line [37].ToString(); string file_path = entry_line.Substring(39); string to_file_path; if (change_type.Equals("A")) { commit.Added.Add(file_path); } else if (change_type.Equals("M")) { commit.Edited.Add(file_path); } else if (change_type.Equals("D")) { commit.Deleted.Add(file_path); } else if (change_type.Equals("R")) { int tab_pos = entry_line.LastIndexOf("\t"); file_path = entry_line.Substring(42, tab_pos - 42); to_file_path = entry_line.Substring(tab_pos + 1); commit.MovedFrom.Add(file_path); commit.MovedTo.Add(to_file_path); } } } commits.Add(commit); } } return(commits); }
// Returns a list of latest commits public List<SparkleCommit> GetCommits(int count) { if (count < 1) count = 30; List <SparkleCommit> commits = new List <SparkleCommit> (); SparkleGit git_log = new SparkleGit (LocalPath, "log -" + count + " --raw --date=iso"); git_log.Start (); git_log.WaitForExit (); string output = git_log.StandardOutput.ReadToEnd (); string [] lines = output.Split ("\n".ToCharArray ()); List <string> entries = new List <string> (); int j = 0; string entry = ""; foreach (string line in lines) { if (line.StartsWith ("commit") && j > 0) { entries.Add (entry); entry = ""; } entry += line + "\n"; j++; } foreach (string log_entry in entries) { Regex regex = new Regex (@"commit ([a-z0-9]{40})\n" + "Author: (.+) <(.+)>\n" + "Date: ([0-9]{4})-([0-9]{2})-([0-9]{2}) " + "([0-9]{2}):([0-9]{2}):([0-9]{2}) \\+([0-9]{4})\n" + "*"); Match match = regex.Match (log_entry); if (match.Success) { SparkleCommit commit = new SparkleCommit (); commit.Hash = match.Groups [1].Value; commit.UserName = match.Groups [2].Value; commit.UserEmail = match.Groups [3].Value; commit.DateTime = new DateTime (int.Parse (match.Groups [4].Value), int.Parse (match.Groups [5].Value), int.Parse (match.Groups [6].Value), int.Parse (match.Groups [7].Value), int.Parse (match.Groups [8].Value), int.Parse (match.Groups [9].Value)); string [] entry_lines = log_entry.Split ("\n".ToCharArray ()); foreach (string entry_line in entry_lines) { if (entry_line.StartsWith (":")) { string change_type = entry_line [37].ToString (); string file_path = entry_line.Substring (39); if (change_type.Equals ("A")) { commit.Added.Add (file_path); } else if (change_type.Equals ("M")) { commit.Edited.Add (file_path); } else if (change_type.Equals ("D")) { commit.Deleted.Add (file_path); } } } commits.Add (commit); } } return commits; }
// Returns a list of latest commits public List<SparkleCommit> GetCommits(int count) { if (count <= 0) return null; List <SparkleCommit> commits = new List <SparkleCommit> (); string commit_ref = "HEAD"; try { for (int i = 0; i < count; i++) { Commit commit = new Commit (this, commit_ref); SparkleCommit sparkle_commit = new SparkleCommit (); sparkle_commit.UserName = commit.Author.Name; sparkle_commit.UserEmail = commit.Author.EmailAddress; sparkle_commit.DateTime = commit.CommitDate.DateTime; sparkle_commit.Hash = commit.Hash; foreach (Change change in commit.Changes) { if (change.ChangeType.ToString ().Equals ("Added")) sparkle_commit.Added.Add (change.Path); if (change.ChangeType.ToString ().Equals ("Modified")) sparkle_commit.Edited.Add (change.Path); if (change.ChangeType.ToString ().Equals ("Deleted")) sparkle_commit.Deleted.Add (change.Path); } commits.Add (sparkle_commit); commit_ref += "^"; } } catch (System.NullReferenceException) { // FIXME: Doesn't show the first commit because it throws // this exception before getting to it. Seems to be a bug in GitSharp } return commits; }
// Returns a list of latest commits public List<SparkleCommit> GetCommits(int count) { if (count < 0) return null; List <SparkleCommit> commits = new List <SparkleCommit> (); Process process = new Process () { EnableRaisingEvents = true }; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.StartInfo.WorkingDirectory = LocalPath; 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 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 (unix_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 (commit); } return commits; }