This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
/
commit.go
71 lines (57 loc) · 1.9 KB
/
commit.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
package git
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/velocity-ci/velocity/backend/pkg/velocity/logging"
"github.com/velocity-ci/velocity/backend/pkg/exec"
"go.uber.org/zap"
)
type RawCommit struct {
SHA string
AuthorDate time.Time
AuthorEmail string
AuthorName string
Signed string
Message string
}
func (r *RawRepository) GetCommitInfo(sha string) (*RawCommit, error) {
r.RLock()
defer r.RUnlock()
shCmd := []string{"git", "show", "-s", `--format=%H%n%aI%n%aE%n%aN%n%GK%n%s`, sha}
s := exec.Run(shCmd, r.Directory, []string{}, nil)
if len(s.Stdout) < 6 {
logging.GetLogger().Error("unexpected commit info output", zap.Strings("stdout", s.Stdout), zap.Strings("stderr", s.Stderr))
return nil, fmt.Errorf("unexpected commit info output")
}
authorDate, _ := time.Parse(time.RFC3339, strings.TrimSpace(s.Stdout[1]))
return &RawCommit{
SHA: strings.TrimSpace(s.Stdout[0]),
AuthorDate: authorDate,
AuthorEmail: strings.TrimSpace(s.Stdout[2]),
AuthorName: strings.TrimSpace(s.Stdout[3]),
Signed: strings.TrimSpace(s.Stdout[4]),
Message: strings.TrimSpace(s.Stdout[5]),
}, nil
}
func (r *RawRepository) GetCurrentCommitInfo() (*RawCommit, error) {
r.RLock()
defer r.RUnlock()
shCmd := []string{"git", "rev-parse", "HEAD"}
s := exec.Run(shCmd, r.Directory, []string{}, nil)
// GetLogger().Debug("git rev-parse HEAD", zap.Strings("stdout", s.Stdout), zap.Strings("stderr", s.Stderr))
return r.GetCommitInfo(strings.TrimSpace(s.Stdout[0]))
}
func (r *RawRepository) GetTotalCommits() uint64 {
r.RLock()
defer r.RUnlock()
shCmd := []string{"git", "rev-list", "--all", "--count"}
// writer := &BlankWriter{}
s := exec.Run(shCmd, r.Directory, []string{}, nil)
total, err := strconv.ParseUint(strings.TrimSpace(s.Stdout[0]), 10, 64)
if err != nil {
logging.GetLogger().Error("could not get total commits", zap.Error(err))
}
return total
}