From ca697bb3ba148e2d0d09546d5a669ee6ef9f3a87 Mon Sep 17 00:00:00 2001 From: "J. Zebedee" Date: Fri, 15 Mar 2019 04:48:28 -0400 Subject: [PATCH] Add AuthorDate to GitCommit/Log (#10) * Add AuthorDate to GitCommit * Add AuthorDate to Log commit format * Add AuthorDate to ParseCommit * Update Commit tests --- src/corgit/GitArguments.cs | 2 +- src/corgit/GitCommit.cs | 9 +++++++-- src/corgit/GitParsing.cs | 23 ++++++++++++++++++++--- test/corgit.tests/Git/CommitTests.cs | 18 ++++++++++++------ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/corgit/GitArguments.cs b/src/corgit/GitArguments.cs index e79c204..ff53c58 100644 --- a/src/corgit/GitArguments.cs +++ b/src/corgit/GitArguments.cs @@ -75,7 +75,7 @@ public LogOptions(int? maxEntries = 32, bool reverse = false) } public static IEnumerable Log(LogOptions options = default, IEnumerable paths = null) { - const string CommitFormat = "%H\n%ae\n%P\n%B"; + const string CommitFormat = "%H\n%at\n%ae\n%P\n%B"; const string Separator = "%x00%x00"; yield return "log"; diff --git a/src/corgit/GitCommit.cs b/src/corgit/GitCommit.cs index dbb5631..0f1942a 100644 --- a/src/corgit/GitCommit.cs +++ b/src/corgit/GitCommit.cs @@ -9,7 +9,8 @@ public sealed class GitCommit : IEquatable public GitCommit(string hash, string message, string[] parents, - string authorEmail) + string authorEmail, + DateTimeOffset authorDate) { if (hash?.Length != 40) throw new ArgumentException("Invalid SHA1 hash", nameof(hash)); @@ -18,12 +19,14 @@ public GitCommit(string hash, this.Message = message; this.Parents = parents; this.AuthorEmail = authorEmail; + this.AuthorDate = authorDate; } public string Hash { get; } public string Message { get; } public string[] Parents { get; } public string AuthorEmail { get; } + public DateTimeOffset AuthorDate { get; } public string Subject { @@ -42,7 +45,8 @@ public bool Equals(GitCommit other) && other.Hash == Hash && other.Message == Message && StructuralComparisons.StructuralEqualityComparer.Equals(other.Parents, Parents) - && other.AuthorEmail == AuthorEmail; + && other.AuthorEmail == AuthorEmail + && other.AuthorDate == AuthorDate; public override int GetHashCode() { @@ -51,6 +55,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(Message); hashCode = hashCode * -1521134295 + StructuralComparisons.StructuralEqualityComparer.GetHashCode(Parents); hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(AuthorEmail); + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(AuthorDate); return hashCode; } diff --git a/src/corgit/GitParsing.cs b/src/corgit/GitParsing.cs index 91c213c..95adfbb 100644 --- a/src/corgit/GitParsing.cs +++ b/src/corgit/GitParsing.cs @@ -107,7 +107,7 @@ public static List ParseLog(string log) return commits; } - private static readonly Regex r_parseCommit = new Regex(@"^([0-9a-f]{40})\n(.*)\n(.*)\n([\s\S]*)$", RegexOptions.Multiline | RegexOptions.Compiled); + private static readonly Regex r_parseCommit = new Regex(@"^([0-9a-f]{40})\n(\d+)\n(.*)\n(.*)\n([\s\S]*)$", RegexOptions.Multiline | RegexOptions.Compiled); public static GitCommit ParseCommit(string commit) { var match = r_parseCommit.Match(commit.Trim()); @@ -116,8 +116,25 @@ public static GitCommit ParseCommit(string commit) return null; } - var parents = (match.Groups[3].Success && !string.IsNullOrEmpty(match.Groups[3].Value)) ? match.Groups[3].Value.Split(' ') : null; - return new GitCommit(match.Groups[1].Value, match.Groups[4].Value, parents, match.Groups[2].Value); + var hash = match.Groups[1].Value; + + DateTimeOffset authorDate; + { + var unixTimeGroup = match.Groups[2].Value; + authorDate = DateTimeOffset.FromUnixTimeSeconds(long.Parse(unixTimeGroup)); + } + + var authorEmail = match.Groups[3].Value; + + string[] parents; + { + var parentGroup = match.Groups[4]; + parents = (parentGroup.Success && !string.IsNullOrEmpty(parentGroup.Value)) ? parentGroup.Value.Split(' ') : null; + } + + var message = match.Groups[5].Value; + + return new GitCommit(hash, message, parents, authorEmail, authorDate); } public static ReadOnlySpan ParseStatusEntry(ReadOnlySpan entry, out GitFileStatus fileStatus) diff --git a/test/corgit.tests/Git/CommitTests.cs b/test/corgit.tests/Git/CommitTests.cs index 5c3da83..008d35b 100644 --- a/test/corgit.tests/Git/CommitTests.cs +++ b/test/corgit.tests/Git/CommitTests.cs @@ -11,6 +11,7 @@ public class CommitTests public void ParseSingleParentCommit() { const string GIT_OUTPUT_SINGLE_PARENT = "52c293a05038d865604c2284aa8698bd087915a1\n" + + "1234567\n" + "john.doe@mail.com\n" + "8e5a374372b8393906c7e380dbb09349c5385554\n" + "This is a commit message."; @@ -18,7 +19,8 @@ public void ParseSingleParentCommit() var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1", "This is a commit message.", new[] { "8e5a374372b8393906c7e380dbb09349c5385554" }, - "john.doe@mail.com"); + "john.doe@mail.com", + DateTimeOffset.FromUnixTimeSeconds(1234567)); var actual = GitParsing.ParseCommit(GIT_OUTPUT_SINGLE_PARENT); Assert.StrictEqual(expected, actual); } @@ -27,6 +29,7 @@ public void ParseSingleParentCommit() public void ParseMultipleParentCommit() { const string GIT_OUTPUT_MULTIPLE_PARENTS = "52c293a05038d865604c2284aa8698bd087915a1\n" + + "1234567\n" + "john.doe@mail.com\n" + "8e5a374372b8393906c7e380dbb09349c5385554 df27d8c75b129ab9b178b386077da2822101b217\n" + "This is a commit message."; @@ -34,7 +37,8 @@ public void ParseMultipleParentCommit() var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1", "This is a commit message.", new[] { "8e5a374372b8393906c7e380dbb09349c5385554", "df27d8c75b129ab9b178b386077da2822101b217" }, - "john.doe@mail.com"); + "john.doe@mail.com", + DateTimeOffset.FromUnixTimeSeconds(1234567)); var actual = GitParsing.ParseCommit(GIT_OUTPUT_MULTIPLE_PARENTS); Assert.StrictEqual(expected, actual); } @@ -43,14 +47,16 @@ public void ParseMultipleParentCommit() public void ParseNoParentCommit() { const string GIT_OUTPUT_NO_PARENTS = "52c293a05038d865604c2284aa8698bd087915a1\n" - + "john.doe@mail.com\n" - + "\n" - + "This is a commit message."; + + "1234567\n" + + "john.doe@mail.com\n" + + "\n" + + "This is a commit message."; var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1", "This is a commit message.", null, - "john.doe@mail.com"); + "john.doe@mail.com", + DateTimeOffset.FromUnixTimeSeconds(1234567)); var actual = GitParsing.ParseCommit(GIT_OUTPUT_NO_PARENTS); Assert.StrictEqual(expected, actual); }