aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/format/gitignore/dir.go18
-rw-r--r--plumbing/object/commit.go11
-rw-r--r--plumbing/object/commit_test.go70
3 files changed, 83 insertions, 16 deletions
diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go
index 3c4469a..d8fb30c 100644
--- a/plumbing/format/gitignore/dir.go
+++ b/plumbing/format/gitignore/dir.go
@@ -5,10 +5,10 @@ import (
"bytes"
"io"
"os"
- "os/user"
"strings"
"github.com/go-git/go-billy/v5"
+ "github.com/go-git/go-git/v5/internal/path_util"
"github.com/go-git/go-git/v5/plumbing/format/config"
gioutil "github.com/go-git/go-git/v5/utils/ioutil"
)
@@ -27,21 +27,7 @@ const (
// readIgnoreFile reads a specific git ignore file.
func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) {
- if strings.HasPrefix(ignoreFile, "~") {
- firstSlash := strings.Index(ignoreFile, "/")
- if firstSlash == 1 {
- home, err := os.UserHomeDir()
- if err == nil {
- ignoreFile = strings.Replace(ignoreFile, "~", home, 1)
- }
- } else if firstSlash > 1 {
- username := ignoreFile[1:firstSlash]
- userAccount, err := user.Lookup(username)
- if err == nil {
- ignoreFile = strings.Replace(ignoreFile, ignoreFile[:firstSlash], userAccount.HomeDir, 1)
- }
- }
- }
+ ignoreFile, _ = path_util.ReplaceTildeWithHome(ignoreFile)
f, err := fs.Open(fs.Join(append(path, ignoreFile)...))
if err == nil {
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)
+ }
+}