-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit.go
99 lines (83 loc) · 2.09 KB
/
git.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
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bytes"
"io"
"os/exec"
"path/filepath"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
func isRepo(path string) bool {
_, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{DetectDotGit: true})
return err == nil
}
func findMostRecentCommit(p string, related []Include) *object.Commit {
repo, err := git.PlainOpenWithOptions(p, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
logErr.Fatal("Can't open repository", p, err)
}
wt, _ := repo.Worktree()
cIter, err := repo.Log(
&git.LogOptions{
Order: git.LogOrderCommitterTime,
All: false,
PathFilter: func(path string) bool {
if filepath.Join(wt.Filesystem.Root(), path) == abs(p) {
return true
}
for _, f := range related {
if filepath.Join(wt.Filesystem.Root(), path) == abs(f.path) {
return true
}
}
return false
},
},
)
if err != nil {
logErr.Fatal("Can't read git log", p, err)
}
var commit *object.Commit
if err := cIter.ForEach(func(o *object.Commit) error {
commit = o
// Stop at first found, return EOF
return io.EOF
}); err != io.EOF && err != nil {
logErr.Fatalf("Error while walking commits: %v", err)
}
return commit
}
func findMostRecentCommitCmd(p string, related []Include) *object.Commit {
// Use the git command
// see https://github.com/go-git/go-git/issues/137
args := []string{
"log",
"--max-count=1",
"--pretty=format:%H",
"--",
p,
}
for _, r := range related {
args = append(args, r.path)
}
cmd := exec.Command("git", args...)
logDebug.Println(cmd)
// Run git log from the directory of the catalog item
cmd.Dir = filepath.Dir(p)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
logErr.Fatal("git log error:", err)
}
repo, err := git.PlainOpenWithOptions(p, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
logErr.Fatal("Can't open repository", p, err)
}
commit, err := repo.CommitObject(plumbing.NewHash(out.String()))
if err != nil {
logErr.Fatal(err)
}
return commit
}