-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage_repos.go
88 lines (78 loc) · 1.62 KB
/
mage_repos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// +build mage
package main
import (
"os/exec"
"time"
ignore "github.com/get-woke/go-gitignore"
"github.com/go-git/go-git/v5"
"github.com/dmoles/adler/server/util"
)
var repo *git.Repository
var wt *git.Worktree
var gi *ignore.GitIgnore
func gitStatus(path string) git.StatusCode {
// cheap workaround for https://github.com/go-git/go-git/issues/119
cmd := exec.Command("git", "status", "-s", path)
output, err := cmd.Output()
if err != nil {
return git.Untracked
}
if len(output) >= 2 {
for i := 0; i < 2; i++ {
s := git.StatusCode(output[i])
if s != git.Unmodified {
return s
}
}
}
return git.Unmodified
}
func gitCommitTime(path string) (*time.Time, error) {
repo, err := repository()
if err != nil {
return nil, err
}
lo := git.LogOptions{
FileName: &path,
Order: git.LogOrderCommitterTime,
}
commits, err := repo.Log(&lo)
if err != nil {
return nil, err
}
lastCommit, err := commits.Next()
if err != nil {
return nil, err
}
commitTime := lastCommit.Committer.When
return &commitTime, nil
}
func gitIgnored(path string) (*bool, error) {
gitIgnore, err := gitIgnore()
if err != nil {
return nil, err
}
ignored := gitIgnore.MatchesPath(path)
return &ignored, nil
}
func repository() (*git.Repository, error) {
if repo == nil {
projectRoot := util.ProjectRoot()
r, err := git.PlainOpen(projectRoot)
if err != nil {
return nil, err
}
repo = r
}
return repo, nil
}
func gitIgnore() (*ignore.GitIgnore, error) {
if gi == nil {
gitIgnore, err := ignore.CompileIgnoreFile(".gitignore")
if err != nil {
return nil, err
}
gi = gitIgnore
}
return gi, nil
}