Skip to content

Commit be23a37

Browse files
Merge remote-tracking branch 'origin/main' into dependabot-version-upgrade
2 parents f7f3f87 + bb239d5 commit be23a37

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

pkg/RepoManages.go

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

201201
fetchCount := impl.configuration.GitHistoryCount
202-
commits, err := impl.repositoryManager.ChangesSince(gitCtx, material.CheckoutLocation, pipelineMaterial.Value, "", "", fetchCount)
202+
var repository *git.GitRepository
203+
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, pipelineMaterial.Value, "", "", fetchCount, material.CheckoutLocation, true)
203204
//commits, err := impl.FetchChanges(pipelineMaterial.Id, "", "", 0)
204205
if err == nil {
205206
impl.logger.Infow("commits found", "commit", commits)
@@ -353,11 +354,13 @@ func (impl RepoManagerImpl) checkoutMaterial(gitCtx git.GitContext, material *sq
353354
gitCtx = gitCtx.WithCredentials(userName, password).
354355
WithCloningMode(impl.configuration.CloningMode)
355356

356-
checkoutPath, checkoutLocationForFetching, err := impl.repositoryManager.GetCheckoutPathAndLocation(gitCtx, material, gitProvider.Url)
357+
checkoutPath, _, _, err := impl.repositoryManager.GetCheckoutLocationFromGitUrl(material, gitCtx.CloningMode)
357358
if err != nil {
358359
return material, err
359360
}
360361

362+
checkoutLocationForFetching := impl.repositoryManager.GetCheckoutLocation(gitCtx, material, gitProvider.Url, checkoutPath)
363+
361364
err = impl.repositoryManager.Add(gitCtx, material.GitProviderId, checkoutPath, material.Url, gitProvider.AuthMode, gitProvider.SshPrivateKey)
362365
if err == nil {
363366
material.CheckoutLocation = checkoutLocationForFetching
@@ -669,7 +672,7 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe
669672
return nil, err
670673
}
671674

672-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1, gitMaterial.CheckoutLocation)
675+
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1, gitMaterial.CheckoutLocation, false)
673676

674677
if commits == nil {
675678
return nil, err
@@ -719,7 +722,8 @@ func (impl RepoManagerImpl) GetCommitMetadataForPipelineMaterial(gitCtx git.GitC
719722
repoLock.Mutex.Unlock()
720723
impl.locker.ReturnLocker(gitMaterial.Id)
721724
}()
722-
commits, err := impl.repositoryManager.ChangesSince(gitCtx, gitMaterial.CheckoutLocation, branchName, "", gitHash, 1)
725+
var repository *git.GitRepository
726+
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repository, branchName, "", gitHash, 1, gitMaterial.CheckoutLocation, true)
723727
if err != nil {
724728
impl.logger.Errorw("error while fetching commit info", "pipelineMaterialId", pipelineMaterialId, "gitHash", gitHash, "err", err)
725729
return nil, err

pkg/git/RepositoryManager.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ type RepositoryManager interface {
4343
Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error
4444
GetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error)
4545
FetchRepo(gitCtx GitContext, location string) error
46-
GetLocationForMaterial(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error)
47-
GetCheckoutPathAndLocation(gitCtx GitContext, material *sql.GitMaterial, url string) (string, string, error)
46+
GetCheckoutLocationFromGitUrl(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error)
47+
GetCheckoutLocation(gitCtx GitContext, material *sql.GitMaterial, url, checkoutPath string) string
4848
TrimLastGitCommit(gitCommits []*GitCommitBase, count int) []*GitCommitBase
4949
// Clean cleans a directory
5050
Clean(cloneDir string) error
51-
// ChangesSince given the checkput path, retrieves the latest commits for the gt repo existing on the path
52-
ChangesSince(gitCtx GitContext, checkoutPath string, branch string, from string, to string, count int) ([]*GitCommitBase, error)
5351
// ChangesSinceByRepository returns the latest commits list for the given range and count for an existing repo
54-
ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string) ([]*GitCommitBase, error)
52+
ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) ([]*GitCommitBase, error)
5553
// GetCommitMetadata retrieves the commit metadata for given hash
5654
GetCommitMetadata(gitCtx GitContext, checkoutPath, commitHash string) (*GitCommitBase, error)
5755
// GetCommitForTag retrieves the commit metadata for given tag
@@ -84,7 +82,7 @@ func (impl *RepositoryManagerImpl) IsSpaceAvailableOnDisk() bool {
8482
return availableSpace > int64(impl.configuration.MinLimit)*1024*1024
8583
}
8684

87-
func (impl *RepositoryManagerImpl) GetLocationForMaterial(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error) {
85+
func (impl *RepositoryManagerImpl) GetCheckoutLocationFromGitUrl(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error) {
8886
//gitRegex := `/(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/`
8987
httpsRegex := `^https.*`
9088
httpsMatched, err := regexp.MatchString(httpsRegex, material.Url)
@@ -104,15 +102,8 @@ func (impl *RepositoryManagerImpl) GetLocationForMaterial(material *sql.GitMater
104102
return "", httpsMatched, sshMatched, fmt.Errorf("unsupported format url %s", material.Url)
105103
}
106104

107-
func (impl *RepositoryManagerImpl) GetCheckoutPathAndLocation(gitCtx GitContext, material *sql.GitMaterial, url string) (string, string, error) {
108-
var checkoutPath string
109-
var checkoutLocationForFetching string
110-
checkoutPath, _, _, err := impl.GetLocationForMaterial(material, gitCtx.CloningMode)
111-
if err != nil {
112-
return checkoutPath, checkoutLocationForFetching, err
113-
}
114-
checkoutLocationForFetching = checkoutPath
115-
return checkoutPath, checkoutLocationForFetching, nil
105+
func (impl *RepositoryManagerImpl) GetCheckoutLocation(gitCtx GitContext, material *sql.GitMaterial, url, checkoutPath string) string {
106+
return checkoutPath
116107
}
117108

118109
func (impl *RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error {
@@ -239,12 +230,23 @@ func (impl *RepositoryManagerImpl) GetCommitMetadata(gitCtx GitContext, checkout
239230

240231
// from -> old commit
241232
// to -> new commit
242-
func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string) ([]*GitCommitBase, error) {
233+
func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string, openNewGitRepo bool) ([]*GitCommitBase, error) {
243234
// fix for azure devops (manual trigger webhook bases pipeline) :
244235
// branch name comes as 'refs/heads/master', we need to extract actual branch name out of it.
245236
// https://stackoverflow.com/questions/59956206/how-to-get-a-branch-name-with-a-slash-in-azure-devops
246237

247238
var err error
239+
if count == 0 {
240+
count = impl.configuration.GitHistoryCount
241+
}
242+
243+
if openNewGitRepo {
244+
repository, err = impl.gitManager.OpenRepoPlain(checkoutPath)
245+
if err != nil {
246+
return nil, err
247+
}
248+
}
249+
248250
start := time.Now()
249251
defer func() {
250252
util.TriggerGitOperationMetrics("changesSinceByRepository", start, err)
@@ -332,25 +334,6 @@ func (impl *RepositoryManagerImpl) TrimLastGitCommit(gitCommits []*GitCommitBase
332334
return gitCommits
333335
}
334336

335-
func (impl *RepositoryManagerImpl) ChangesSince(gitCtx GitContext, checkoutPath string, branch string, from string, to string, count int) ([]*GitCommitBase, error) {
336-
var err error
337-
start := time.Now()
338-
defer func() {
339-
util.TriggerGitOperationMetrics("changesSince", start, err)
340-
}()
341-
if count == 0 {
342-
count = impl.configuration.GitHistoryCount
343-
}
344-
r, err := impl.gitManager.OpenRepoPlain(checkoutPath)
345-
if err != nil {
346-
return nil, err
347-
}
348-
///---------------------
349-
return impl.ChangesSinceByRepository(gitCtx, r, branch, from, to, count, checkoutPath)
350-
///----------------------
351-
352-
}
353-
354337
func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, error) {
355338
// add private key
356339
var err error

pkg/git/Util.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ const (
4646

4747
func GetProjectName(url string) string {
4848
//if url = https://github.com/devtron-labs/git-sensor.git then it will return git-sensor
49-
projName := strings.Split(url, ".")[1]
50-
projectName := projName[strings.LastIndex(projName, "/")+1:]
51-
return projectName
49+
url = url[strings.LastIndex(url, "/")+1:]
50+
return strings.TrimSuffix(url, ".git")
5251
}
5352
func GetCheckoutPath(url string, cloneLocation string) string {
5453
//url= https://github.com/devtron-labs/git-sensor.git cloneLocation= git-base/1/github.com/prakash100198

pkg/git/Watcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e
199199
// there might be the case if ssh private key gets flush from disk, so creating and single retrying in this case
200200
if gitProvider.AuthMode == sql.AUTH_MODE_SSH {
201201
if strings.Contains(material.CheckoutLocation, "/.git") {
202-
location, _, _, err = impl.repositoryManager.GetLocationForMaterial(material, gitCtx.CloningMode)
202+
location, _, _, err = impl.repositoryManager.GetCheckoutLocationFromGitUrl(material, gitCtx.CloningMode)
203203
if err != nil {
204204
impl.logger.Errorw("error in getting clone location ", "material", material, "err", err)
205205
return err
@@ -246,7 +246,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e
246246
lastSeenHash = material.LastSeenHash
247247
}
248248
fetchCount := impl.configuration.GitHistoryCount
249-
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", fetchCount, checkoutLocation)
249+
commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", fetchCount, checkoutLocation, false)
250250
if err != nil {
251251
material.Errored = true
252252
material.ErrorMsg = err.Error()

0 commit comments

Comments
 (0)