-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGitCliManager.go
187 lines (160 loc) · 5.97 KB
/
GitCliManager.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package git
import (
"go.uber.org/zap"
"gopkg.in/src-d/go-billy.v4/osfs"
"os"
"path/filepath"
"strconv"
"strings"
)
type GitCliManagerImpl struct {
GitManagerBase
logger *zap.SugaredLogger
}
func NewGitCliManagerImpl(baseManager GitManagerBase, logger *zap.SugaredLogger) *GitCliManagerImpl {
return &GitCliManagerImpl{
GitManagerBase: baseManager,
logger: logger,
}
}
const (
GIT_ASK_PASS = "/git-ask-pass.sh"
AUTHENTICATION_FAILED_ERROR = "Authentication failed"
)
func (impl *GitCliManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error {
//-----------------
err := os.MkdirAll(rootDir, 0755)
if err != nil {
return err
}
err = impl.GitInit(gitCtx, rootDir)
if err != nil {
return err
}
return impl.GitCreateRemote(gitCtx, rootDir, remoteUrl)
}
func (impl *GitCliManagerImpl) OpenRepoPlain(checkoutPath string) (*GitRepository, error) {
err := openGitRepo(checkoutPath)
if err != nil {
return nil, err
}
return &GitRepository{
rootDir: checkoutPath,
}, nil
}
func (impl *GitCliManagerImpl) GetCommitsForTag(gitCtx GitContext, checkoutPath, tag string) (GitCommit, error) {
return impl.GitShow(gitCtx, checkoutPath, tag)
}
func (impl *GitCliManagerImpl) GetCommitForHash(gitCtx GitContext, checkoutPath, commitHash string) (GitCommit, error) {
return impl.GitShow(gitCtx, checkoutPath, commitHash)
}
func (impl *GitCliManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (CommitIterator, error) {
commits, err := impl.GetCommits(gitCtx, iteratorRequest.BranchRef, iteratorRequest.Branch, repository.rootDir, iteratorRequest.CommitCount, iteratorRequest.FromCommitHash, iteratorRequest.ToCommitHash)
if err != nil {
impl.logger.Errorw("error in fetching commits for", "err", err, "path", repository.rootDir)
return nil, err
}
return &CommitCliIterator{
commits: commits,
}, nil
}
func openGitRepo(path string) error {
if _, err := filepath.Abs(path); err != nil {
return err
}
fst := osfs.New(path)
_, err := fst.Stat(".git")
if !os.IsNotExist(err) {
return err
}
return nil
}
func (impl *GitCliManagerImpl) GitInit(gitCtx GitContext, rootDir string) error {
impl.logger.Debugw("git", "-C", rootDir, "init")
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "init")
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
return err
}
func (impl *GitCliManagerImpl) GitCreateRemote(gitCtx GitContext, rootDir string, url string) error {
impl.logger.Debugw("git", "-C", rootDir, "remote", "add", "origin", url)
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "remote", "add", "origin", url)
impl.logger.Debugw("url", url, "opt", output, "errMsg", errMsg, "error", err)
return err
}
func (impl *GitCliManagerImpl) GetCommits(gitCtx GitContext, branchRef string, branch string, rootDir string, numCommits int, from string, to string) ([]GitCommit, error) {
baseCmdArgs := []string{"-C", rootDir, "log"}
rangeCmdArgs := []string{branchRef}
extraCmdArgs := []string{"-n", strconv.Itoa(numCommits), "--date=iso-strict", GITFORMAT}
cmdArgs := impl.getCommandForLogRange(branchRef, from, to, rangeCmdArgs, baseCmdArgs, extraCmdArgs)
impl.logger.Debugw("git", cmdArgs)
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", cmdArgs...)
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
if err != nil {
return nil, err
}
commits, err := impl.processGitLogOutput(output)
if err != nil {
return nil, err
}
return commits, nil
}
func (impl *GitCliManagerImpl) getCommandForLogRange(branchRef string, from string, to string, rangeCmdArgs []string, baseCmdArgs []string, extraCmdArgs []string) []string {
if from != "" && to != "" {
rangeCmdArgs = []string{from + "^.." + to}
} else if from != "" {
rangeCmdArgs = []string{from + "^.." + branchRef}
} else if to != "" {
rangeCmdArgs = []string{to}
}
return append(baseCmdArgs, append(rangeCmdArgs, extraCmdArgs...)...)
}
func (impl *GitCliManagerImpl) GitShow(gitCtx GitContext, rootDir string, hash string) (GitCommit, error) {
impl.logger.Debugw("git", "-C", rootDir, "show", hash, "--date=iso-strict", GITFORMAT, "-s")
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "show", hash, "--date=iso-strict", GITFORMAT, "-s")
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
if err != nil {
return nil, err
}
commits, err := impl.processGitLogOutput(output)
if err != nil || len(commits) == 0 {
return nil, err
}
return commits[0], nil
}
func (impl *GitCliManagerImpl) GetCommitStats(gitCtx GitContext, commit GitCommit, checkoutPath string) (FileStats, error) {
gitCommit := commit.GetCommit()
fileStat, errorMsg, err := impl.FetchDiffStatBetweenCommits(gitCtx, gitCommit.Commit, "", checkoutPath)
if err != nil {
impl.logger.Errorw("error in fetching fileStat of commit: ", gitCommit.Commit, "checkoutPath", checkoutPath, "errorMsg", errorMsg, "err", err)
return nil, err
}
return getFileStat(fileStat)
}
func (impl *GitCliManagerImpl) processGitLogOutput(out string) ([]GitCommit, error) {
gitCommits := make([]GitCommit, 0)
if len(out) == 0 {
return gitCommits, nil
}
gitCommitFormattedList, err := parseFormattedLogOutput(out)
if err != nil {
return gitCommits, err
}
for _, formattedCommit := range gitCommitFormattedList {
subject := strings.TrimSpace(formattedCommit.Subject)
body := strings.TrimSpace(formattedCommit.Body)
message := subject
if len(body) > 0 {
message = strings.Join([]string{subject, body}, "\n")
}
cm := GitCommitBase{
Commit: formattedCommit.Commit,
Author: formattedCommit.Commiter.Name + " <" + formattedCommit.Commiter.Email + ">",
Date: formattedCommit.Commiter.Date,
Message: message,
}
gitCommits = append(gitCommits, &GitCommitCli{
GitCommitBase: cm,
})
}
return gitCommits, nil
}