aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
authorArieh Schneier <15041913+AriehSchneier@users.noreply.github.com>2023-07-06 08:45:49 +1000
committerArieh Schneier <15041913+AriehSchneier@users.noreply.github.com>2023-07-06 08:45:49 +1000
commite0fc8a8f00c238f7f7cdf0e0c59e743550587bdf (patch)
tree8b6d76278f0a5020fc7fc675908abc29e2a8025d /plumbing
parentdd4e2b7f4b01e2aaafcf182d2884a186a6f37d21 (diff)
downloadgo-git-e0fc8a8f00c238f7f7cdf0e0c59e743550587bdf.tar.gz
plumbing: blame, Complete rewrite. Fixes #603
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/object/commit.go11
-rw-r--r--plumbing/object/commit_test.go70
2 files changed, 81 insertions, 0 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go
index d2f7184..8a0f35c 100644
--- a/plumbing/object/commit.go
+++ b/plumbing/object/commit.go
@@ -376,6 +376,17 @@ func (c *Commit) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
return openpgp.CheckArmoredDetachedSignature(keyring, er, signature, nil)
}
+// Less defines a compare function to determine which commit is 'earlier' by:
+// - First use Committer.When
+// - If Committer.When are equal then use Author.When
+// - If Author.When also equal then compare the string value of the hash
+func (c *Commit) Less(rhs *Commit) bool {
+ return c.Committer.When.Before(rhs.Committer.When) ||
+ (c.Committer.When.Equal(rhs.Committer.When) &&
+ (c.Author.When.Before(rhs.Author.When) ||
+ (c.Author.When.Equal(rhs.Author.When) && bytes.Compare(c.Hash[:], rhs.Hash[:]) < 0)))
+}
+
func indent(t string) string {
var output []string
for _, line := range strings.Split(t, "\n") {
diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go
index a939bc6..4b0f6b4 100644
--- a/plumbing/object/commit_test.go
+++ b/plumbing/object/commit_test.go
@@ -503,3 +503,73 @@ func (s *SuiteCommit) TestEncodeWithoutSignature(c *C) {
"\n"+
"Merge branch 'master' of github.com:tyba/git-fixture\n")
}
+
+func (s *SuiteCommit) TestLess(c *C) {
+ when1 := time.Now()
+ when2 := when1.Add(time.Hour)
+
+ hash1 := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea")
+ hash2 := plumbing.NewHash("2669dce138d9b841a518c64b10914d88f5e488ea")
+
+ commitLessTests := []struct {
+ Committer1When, Committer2When time.Time
+ Author1When, Author2When time.Time
+ Hash1, Hash2 plumbing.Hash
+ Exp bool
+ }{
+ {when1, when1, when1, when1, hash1, hash2, true},
+ {when1, when1, when1, when1, hash2, hash1, false},
+ {when1, when1, when1, when2, hash1, hash2, true},
+ {when1, when1, when1, when2, hash2, hash1, true},
+ {when1, when1, when2, when1, hash1, hash2, false},
+ {when1, when1, when2, when1, hash2, hash1, false},
+ {when1, when1, when2, when2, hash1, hash2, true},
+ {when1, when1, when2, when2, hash2, hash1, false},
+ {when1, when2, when1, when1, hash1, hash2, true},
+ {when1, when2, when1, when1, hash2, hash1, true},
+ {when1, when2, when1, when2, hash1, hash2, true},
+ {when1, when2, when1, when2, hash2, hash1, true},
+ {when1, when2, when2, when1, hash1, hash2, true},
+ {when1, when2, when2, when1, hash2, hash1, true},
+ {when1, when2, when2, when2, hash1, hash2, true},
+ {when1, when2, when2, when2, hash2, hash1, true},
+ {when2, when1, when1, when1, hash1, hash2, false},
+ {when2, when1, when1, when1, hash2, hash1, false},
+ {when2, when1, when1, when2, hash1, hash2, false},
+ {when2, when1, when1, when2, hash2, hash1, false},
+ {when2, when1, when2, when1, hash1, hash2, false},
+ {when2, when1, when2, when1, hash2, hash1, false},
+ {when2, when1, when2, when2, hash1, hash2, false},
+ {when2, when1, when2, when2, hash2, hash1, false},
+ {when2, when2, when1, when1, hash1, hash2, true},
+ {when2, when2, when1, when1, hash2, hash1, false},
+ {when2, when2, when1, when2, hash1, hash2, true},
+ {when2, when2, when1, when2, hash2, hash1, true},
+ {when2, when2, when2, when1, hash1, hash2, false},
+ {when2, when2, when2, when1, hash2, hash1, false},
+ {when2, when2, when2, when2, hash1, hash2, true},
+ {when2, when2, when2, when2, hash2, hash1, false},
+ }
+
+ for _, t := range commitLessTests {
+ commit1 := &Commit{
+ Hash: t.Hash1,
+ Author: Signature{
+ When: t.Author1When,
+ },
+ Committer: Signature{
+ When: t.Committer1When,
+ },
+ }
+ commit2 := &Commit{
+ Hash: t.Hash2,
+ Author: Signature{
+ When: t.Author2When,
+ },
+ Committer: Signature{
+ When: t.Committer2When,
+ },
+ }
+ c.Assert(commit1.Less(commit2), Equals, t.Exp)
+ }
+}