Skip to content

Commit

Permalink
Add AuthorDate to GitCommit/Log (#10)
Browse files Browse the repository at this point in the history
* Add AuthorDate to GitCommit

* Add AuthorDate to Log commit format

* Add AuthorDate to ParseCommit

* Update Commit tests
  • Loading branch information
jzebedee authored Mar 15, 2019
1 parent e0e88d7 commit ca697bb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/corgit/GitArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public LogOptions(int? maxEntries = 32, bool reverse = false)
}
public static IEnumerable<string> Log(LogOptions options = default, IEnumerable<string> 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";
Expand Down
9 changes: 7 additions & 2 deletions src/corgit/GitCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public sealed class GitCommit : IEquatable<GitCommit>
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));
Expand All @@ -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
{
Expand All @@ -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()
{
Expand All @@ -51,6 +55,7 @@ public override int GetHashCode()
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Message);
hashCode = hashCode * -1521134295 + StructuralComparisons.StructuralEqualityComparer.GetHashCode(Parents);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(AuthorEmail);
hashCode = hashCode * -1521134295 + EqualityComparer<DateTimeOffset>.Default.GetHashCode(AuthorDate);
return hashCode;
}

Expand Down
23 changes: 20 additions & 3 deletions src/corgit/GitParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static List<GitCommit> 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());
Expand All @@ -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<char> ParseStatusEntry(ReadOnlySpan<char> entry, out GitFileStatus fileStatus)
Expand Down
18 changes: 12 additions & 6 deletions test/corgit.tests/Git/CommitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ public class CommitTests
public void ParseSingleParentCommit()
{
const string GIT_OUTPUT_SINGLE_PARENT = "52c293a05038d865604c2284aa8698bd087915a1\n"
+ "1234567\n"
+ "[email protected]\n"
+ "8e5a374372b8393906c7e380dbb09349c5385554\n"
+ "This is a commit message.";

var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1",
"This is a commit message.",
new[] { "8e5a374372b8393906c7e380dbb09349c5385554" },
"[email protected]");
"[email protected]",
DateTimeOffset.FromUnixTimeSeconds(1234567));
var actual = GitParsing.ParseCommit(GIT_OUTPUT_SINGLE_PARENT);
Assert.StrictEqual(expected, actual);
}
Expand All @@ -27,14 +29,16 @@ public void ParseSingleParentCommit()
public void ParseMultipleParentCommit()
{
const string GIT_OUTPUT_MULTIPLE_PARENTS = "52c293a05038d865604c2284aa8698bd087915a1\n"
+ "1234567\n"
+ "[email protected]\n"
+ "8e5a374372b8393906c7e380dbb09349c5385554 df27d8c75b129ab9b178b386077da2822101b217\n"
+ "This is a commit message.";

var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1",
"This is a commit message.",
new[] { "8e5a374372b8393906c7e380dbb09349c5385554", "df27d8c75b129ab9b178b386077da2822101b217" },
"[email protected]");
"[email protected]",
DateTimeOffset.FromUnixTimeSeconds(1234567));
var actual = GitParsing.ParseCommit(GIT_OUTPUT_MULTIPLE_PARENTS);
Assert.StrictEqual(expected, actual);
}
Expand All @@ -43,14 +47,16 @@ public void ParseMultipleParentCommit()
public void ParseNoParentCommit()
{
const string GIT_OUTPUT_NO_PARENTS = "52c293a05038d865604c2284aa8698bd087915a1\n"
+ "[email protected]\n"
+ "\n"
+ "This is a commit message.";
+ "1234567\n"
+ "[email protected]\n"
+ "\n"
+ "This is a commit message.";

var expected = new GitCommit("52c293a05038d865604c2284aa8698bd087915a1",
"This is a commit message.",
null,
"[email protected]");
"[email protected]",
DateTimeOffset.FromUnixTimeSeconds(1234567));
var actual = GitParsing.ParseCommit(GIT_OUTPUT_NO_PARENTS);
Assert.StrictEqual(expected, actual);
}
Expand Down

0 comments on commit ca697bb

Please sign in to comment.