This repository was archived by the owner on Jul 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
65 lines (49 loc) · 1.21 KB
/
Copy pathgit.go
File metadata and controls
65 lines (49 loc) · 1.21 KB
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
package indexwait
import (
"os"
"path"
"time"
"github.com/dsoprea/go-logging"
"github.com/go-git/go-git/v5"
)
// Git provides a VCS implementation for Git.
type Git struct {
}
// Name returns the name of the VCS method.
func (*Git) Name() string {
return "git"
}
// GetHeadCommit returns the current revision of the package/module.
func (*Git) GetHeadCommit(packagePath string) (revision string, timestamp time.Time, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
repositoryPath := packagePath
// Search from current directory up to find the repository root.
for repositoryPath != "/" && repositoryPath != "." {
metaPath := path.Join(repositoryPath, ".git")
f, err := os.Open(metaPath)
f.Close()
if err == nil {
break
} else if os.IsNotExist(err) != true {
log.Panic(err)
}
repositoryPath = path.Dir(repositoryPath)
}
gr, err := git.PlainOpen(repositoryPath)
log.PanicIf(err)
lo := new(git.LogOptions)
ci, err := gr.Log(lo)
log.PanicIf(err)
commit, err := ci.Next()
log.PanicIf(err)
revision = commit.Hash.String()
timestamp = commit.Committer.When
return revision, timestamp, nil
}
func init() {
registerVcs(new(Git))
}