-
Notifications
You must be signed in to change notification settings - Fork 10
/
GoGitSDKManager.go
136 lines (116 loc) · 3.73 KB
/
GoGitSDKManager.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
package git
import (
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"go.uber.org/zap"
"os"
)
type GoGitSDKManagerImpl struct {
GitManagerBase
logger *zap.SugaredLogger
}
func NewGoGitSDKManagerImpl(baseManager GitManagerBase, logger *zap.SugaredLogger) *GoGitSDKManagerImpl {
return &GoGitSDKManagerImpl{
GitManagerBase: baseManager,
logger: logger,
}
}
func (impl *GoGitSDKManagerImpl) GetCommitsForTag(gitCtx GitContext, checkoutPath, tag string) (GitCommit, error) {
r, err := impl.OpenRepoPlain(checkoutPath)
if err != nil {
return nil, err
}
tagRef, err := r.Tag(tag)
if err != nil {
impl.logger.Errorw("error in fetching tag", "path", checkoutPath, "tag", tag, "err", err)
return nil, err
}
commit, err := r.CommitObject(plumbing.NewHash(tagRef.Hash().String()))
if err != nil {
impl.logger.Errorw("error in fetching tag", "path", checkoutPath, "hash", tagRef, "err", err)
return nil, err
}
cm := GitCommitBase{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
gitCommit := &GitCommitGoGit{
GitCommitBase: cm,
Cm: commit,
}
return gitCommit, nil
}
func (impl *GoGitSDKManagerImpl) GetCommitForHash(gitCtx GitContext, checkoutPath, commitHash string) (GitCommit, error) {
r, err := impl.OpenRepoPlain(checkoutPath)
if err != nil {
return nil, err
}
commit, err := r.CommitObject(plumbing.NewHash(commitHash))
if err != nil {
impl.logger.Errorw("error in fetching commit", "path", checkoutPath, "hash", commitHash, "err", err)
return nil, err
}
cm := GitCommitBase{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
gitCommit := &GitCommitGoGit{
GitCommitBase: cm,
Cm: commit,
}
return gitCommit, nil
}
func (impl *GoGitSDKManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (CommitIterator, error) {
ref, err := repository.Reference(plumbing.ReferenceName(iteratorRequest.BranchRef), true)
if err != nil && err == plumbing.ErrReferenceNotFound {
return nil, fmt.Errorf("ref not found %s branch %s", err, iteratorRequest.Branch)
} else if err != nil {
return nil, fmt.Errorf("error in getting reference %s branch %s", err, iteratorRequest.Branch)
}
itr, err := repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, fmt.Errorf("error in getting iterator %s branch %s", err, iteratorRequest.Branch)
}
return &CommitGoGitIterator{itr}, nil
}
func (impl *GoGitSDKManagerImpl) OpenRepoPlain(checkoutPath string) (*GitRepository, error) {
r, err := git.PlainOpen(checkoutPath)
if err != nil {
impl.logger.Errorf("error in OpenRepoPlain go-git %s for path %s", err, checkoutPath)
return nil, err
}
return &GitRepository{Repository: r}, err
}
func (impl *GoGitSDKManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error {
//-----------------
err := os.MkdirAll(rootDir, 0755)
if err != nil {
return err
}
repo, err := git.PlainInit(rootDir, isBare)
if err != nil {
return err
}
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{remoteUrl},
})
return err
}
func (impl *GoGitSDKManagerImpl) GetCommitStats(gitCtx GitContext, commit GitCommit, checkoutPath string) (FileStats, error) {
if IsRepoShallowCloned(checkoutPath) {
return impl.GitManagerBase.FetchDiffStatBetweenCommits(gitCtx, commit.GetCommit().Commit, "", checkoutPath)
}
gitCommit := commit.(*GitCommitGoGit)
stats, err := gitCommit.Cm.Stats()
if err != nil {
return nil, err
}
return transformFileStats(stats), err
}