Skip to content

Commit 2a99117

Browse files
committed
code improv to propagae cli output
1 parent b832ab4 commit 2a99117

File tree

7 files changed

+29
-35
lines changed

7 files changed

+29
-35
lines changed

pkg/RepoManages.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (impl RepoManagerImpl) updatePipelineMaterialCommit(gitCtx git.GitContext,
200200

201201
fetchCount := impl.configuration.GitHistoryCount
202202
var repository *git.GitRepository
203-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, pipelineMaterial.Value, "", "", fetchCount, material.CheckoutLocation, true)
203+
commits, _, _, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, pipelineMaterial.Value, "", "", fetchCount, material.CheckoutLocation, true)
204204
//commits, err := impl.FetchChanges(pipelineMaterial.Id, "", "", 0)
205205
if err == nil {
206206
impl.logger.Infow("commits found", "commit", commits)
@@ -670,7 +670,7 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe
670670
return nil, err
671671
}
672672

673-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1, gitMaterial.CheckoutLocation, false)
673+
commits, _, _, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1, gitMaterial.CheckoutLocation, false)
674674

675675
if commits == nil {
676676
return nil, err
@@ -720,9 +720,9 @@ func (impl RepoManagerImpl) GetCommitMetadataForPipelineMaterial(gitCtx git.GitC
720720
impl.locker.ReturnLocker(gitMaterial.Id)
721721
}()
722722
var repository *git.GitRepository
723-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, branchName, "", gitHash, 1, gitMaterial.CheckoutLocation, true)
723+
commits, cliOutput, _, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, branchName, "", gitHash, 1, gitMaterial.CheckoutLocation, true)
724724
if err != nil {
725-
if strings.Contains(err.Error(), git.NO_COMMIT_CUSTOM_ERROR_MESSAGE) {
725+
if strings.Contains(cliOutput, git.NO_COMMIT_CUSTOM_ERROR_MESSAGE) {
726726
impl.logger.Warnw("No commit found for given hash", "hash", gitHash, "branchName", branchName)
727727
return nil, nil
728728
}

pkg/git/GitBaseManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type GitManager interface {
3636
// GetCommitStats retrieves the stats for the given commit vs its parent
3737
GetCommitStats(gitCtx GitContext, commit GitCommit, checkoutPath string) (FileStats, error)
3838
// GetCommitIterator returns an iterator for the provided git repo and iterator request describing the commits to fetch
39-
GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (CommitIterator, error)
39+
GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (commitIterator CommitIterator, cliOutput string, errMsg string, err error)
4040
// GetCommitForHash retrieves the commit reference for given tag
4141
GetCommitForHash(gitCtx GitContext, checkoutPath, commitHash string) (GitCommit, error)
4242
// GetCommitsForTag retrieves the commit reference for given tag

pkg/git/GitCliManager.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package git
1818

1919
import (
20-
"errors"
2120
"go.uber.org/zap"
2221
"gopkg.in/src-d/go-billy.v4/osfs"
2322
"os"
@@ -80,16 +79,16 @@ func (impl *GitCliManagerImpl) GetCommitForHash(gitCtx GitContext, checkoutPath,
8079

8180
return impl.GitShow(gitCtx, checkoutPath, commitHash)
8281
}
83-
func (impl *GitCliManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (CommitIterator, error) {
82+
func (impl *GitCliManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (commitIterator CommitIterator, cliOutput string, errMsg string, err error) {
8483

85-
commits, err := impl.GetCommits(gitCtx, iteratorRequest.BranchRef, iteratorRequest.Branch, repository.rootDir, iteratorRequest.CommitCount, iteratorRequest.FromCommitHash, iteratorRequest.ToCommitHash)
84+
commits, cliOutput, errMsg, err := impl.GetCommits(gitCtx, iteratorRequest.BranchRef, iteratorRequest.Branch, repository.rootDir, iteratorRequest.CommitCount, iteratorRequest.FromCommitHash, iteratorRequest.ToCommitHash)
8685
if err != nil {
8786
impl.logger.Errorw("error in fetching commits for", "err", err, "path", repository.rootDir)
88-
return nil, err
87+
return nil, cliOutput, errMsg, err
8988
}
9089
return &CommitCliIterator{
9190
commits: commits,
92-
}, nil
91+
}, "", "", nil
9392
}
9493

9594
func openGitRepo(path string) error {
@@ -117,25 +116,22 @@ func (impl *GitCliManagerImpl) GitCreateRemote(gitCtx GitContext, rootDir string
117116
return err
118117
}
119118

120-
func (impl *GitCliManagerImpl) GetCommits(gitCtx GitContext, branchRef string, branch string, rootDir string, numCommits int, from string, to string) ([]GitCommit, error) {
119+
func (impl *GitCliManagerImpl) GetCommits(gitCtx GitContext, branchRef string, branch string, rootDir string, numCommits int, from string, to string) (commits []GitCommit, cliOutput string, errMsg string, err error) {
121120
baseCmdArgs := []string{"-C", rootDir, "log"}
122121
rangeCmdArgs := []string{branchRef}
123122
extraCmdArgs := []string{"-n", strconv.Itoa(numCommits), "--date=iso-strict", GITFORMAT}
124123
cmdArgs := impl.getCommandForLogRange(branchRef, from, to, rangeCmdArgs, baseCmdArgs, extraCmdArgs)
125124
impl.logger.Debugw("git", cmdArgs)
126-
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", cmdArgs...)
127-
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
125+
cliOutput, errMsg, err = impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", cmdArgs...)
126+
impl.logger.Debugw("root", rootDir, "cliOutput", cliOutput, "errMsg", errMsg, "error", err)
128127
if err != nil {
129-
if strings.Contains(output, NO_COMMIT_GIT_ERROR_MESSAGE) {
130-
return nil, errors.New(NO_COMMIT_CUSTOM_ERROR_MESSAGE)
131-
}
132-
return nil, err
128+
return nil, cliOutput, errMsg, err
133129
}
134-
commits, err := impl.processGitLogOutput(output)
130+
commits, err = impl.processGitLogOutput(cliOutput)
135131
if err != nil {
136-
return nil, err
132+
return nil, "", "", err
137133
}
138-
return commits, nil
134+
return commits, "", "", nil
139135
}
140136

141137
func (impl *GitCliManagerImpl) getCommandForLogRange(branchRef string, from string, to string, rangeCmdArgs []string, baseCmdArgs []string, extraCmdArgs []string) []string {

pkg/git/GoGitSDKManager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ func (impl *GoGitSDKManagerImpl) GetCommitForHash(gitCtx GitContext, checkoutPat
9494
return gitCommit, nil
9595
}
9696

97-
func (impl *GoGitSDKManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (CommitIterator, error) {
97+
func (impl *GoGitSDKManagerImpl) GetCommitIterator(gitCtx GitContext, repository *GitRepository, iteratorRequest IteratorRequest) (commitIterator CommitIterator, cliOutput string, errMsg string, err error) {
9898

9999
ref, err := repository.Reference(plumbing.ReferenceName(iteratorRequest.BranchRef), true)
100100
if err != nil && err == plumbing.ErrReferenceNotFound {
101-
return nil, fmt.Errorf("ref not found %s branch %s", err, iteratorRequest.Branch)
101+
return nil, "", "", fmt.Errorf("ref not found %s branch %s", err, iteratorRequest.Branch)
102102
} else if err != nil {
103-
return nil, fmt.Errorf("error in getting reference %s branch %s", err, iteratorRequest.Branch)
103+
return nil, "", "", fmt.Errorf("error in getting reference %s branch %s", err, iteratorRequest.Branch)
104104
}
105105
itr, err := repository.Log(&git.LogOptions{From: ref.Hash()})
106106
if err != nil {
107-
return nil, fmt.Errorf("error in getting iterator %s branch %s", err, iteratorRequest.Branch)
107+
return nil, "", "", fmt.Errorf("error in getting iterator %s branch %s", err, iteratorRequest.Branch)
108108
}
109-
return &CommitGoGitIterator{itr}, nil
109+
return &CommitGoGitIterator{itr}, "", "", nil
110110
}
111111

112112
func (impl *GoGitSDKManagerImpl) OpenRepoPlain(checkoutPath string) (*GitRepository, error) {

pkg/git/RepositoryManager.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type RepositoryManager interface {
4949
// Clean cleans a directory
5050
Clean(cloneDir string) error
5151
// ChangesSinceByRepository returns the latest commits list for the given range and count for an existing repo
52-
ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) ([]*GitCommitBase, error)
52+
ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) (gitCommits []*GitCommitBase, cliOutput string, errMsg string, err error)
5353
// GetCommitMetadata retrieves the commit metadata for given hash
5454
GetCommitMetadata(gitCtx GitContext, checkoutPath, commitHash string) (*GitCommitBase, error)
5555
// GetCommitForTag retrieves the commit metadata for given tag
@@ -238,20 +238,19 @@ func (impl *RepositoryManagerImpl) GetCommitMetadata(gitCtx GitContext, checkout
238238

239239
// from -> old commit
240240
// to -> new commit
241-
func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) ([]*GitCommitBase, error) {
241+
func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) (gitCommits []*GitCommitBase, cliOutput string, errMsg string, err error) {
242242
// fix for azure devops (manual trigger webhook bases pipeline) :
243243
// branch name comes as 'refs/heads/master', we need to extract actual branch name out of it.
244244
// https://stackoverflow.com/questions/59956206/how-to-get-a-branch-name-with-a-slash-in-azure-devops
245245

246-
var err error
247246
if count == 0 {
248247
count = impl.configuration.GitHistoryCount
249248
}
250249

251250
if openNewGitRepo {
252251
repository, err = impl.gitManager.OpenRepoPlain(checkoutPath)
253252
if err != nil {
254-
return nil, err
253+
return nil, "", "", err
255254
}
256255
}
257256

@@ -260,7 +259,7 @@ func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, r
260259
util.TriggerGitOperationMetrics("changesSinceByRepository", start, err)
261260
}()
262261
branch, branchRef := GetBranchReference(branch)
263-
itr, err := impl.gitManager.GetCommitIterator(gitCtx, repository, IteratorRequest{
262+
itr, cliOutput, errMsg, err := impl.gitManager.GetCommitIterator(gitCtx, repository, IteratorRequest{
264263
BranchRef: branchRef,
265264
Branch: branch,
266265
CommitCount: count,
@@ -269,9 +268,8 @@ func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, r
269268
})
270269
if err != nil {
271270
impl.logger.Errorw("error in getting iterator", "branch", branch, "err", err)
272-
return nil, err
271+
return nil, cliOutput, errMsg, err
273272
}
274-
var gitCommits []*GitCommitBase
275273
itrCounter := 0
276274
commitToFind := len(to) == 0 //no commit mentioned
277275
breakLoop := false
@@ -337,7 +335,7 @@ func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, r
337335
}
338336
}()
339337
}
340-
return gitCommits, err
338+
return gitCommits, "", "", err
341339
}
342340

343341
func (impl *RepositoryManagerImpl) TrimLastGitCommit(gitCommits []*GitCommitBase, count int) []*GitCommitBase {

pkg/git/RepositoryManagerIT_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func TestRepositoryManager_ChangesSinceByRepository(t *testing.T) {
599599
r, err := repositoryManagerImpl.gitManager.OpenRepoPlain(tt.payload.checkoutPath)
600600
assert.Nil(t, err)
601601
t.Run(tt.name, func(t *testing.T) {
602-
got, err := repositoryManagerImpl.ChangesSinceByRepository(BuildGitContext(context.Background()), r, tt.payload.branch, tt.payload.from, tt.payload.to, tt.payload.count, tt.payload.checkoutPath)
602+
got, _, _, err := repositoryManagerImpl.ChangesSinceByRepository(BuildGitContext(context.Background()), r, tt.payload.branch, tt.payload.from, tt.payload.to, tt.payload.count, tt.payload.checkoutPath)
603603
if (err != nil) != tt.wantErr {
604604
t.Errorf("ChangesSinceByRepository() error in %s, error = %v, wantErr %v", tt.name, err, tt.wantErr)
605605
return

pkg/git/Watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e
248248
lastSeenHash = material.LastSeenHash
249249
}
250250
fetchCount := impl.configuration.GitHistoryCount
251-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", fetchCount, checkoutLocation, false)
251+
commits, _, _, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", fetchCount, checkoutLocation, false)
252252
if err != nil {
253253
material.Errored = true
254254
material.ErrorMsg = err.Error()

0 commit comments

Comments
 (0)