Skip to content

Commit 9a59b63

Browse files
authored
fix: add necessary action for fetch latest git changes (#21)
1 parent ace512c commit 9a59b63

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/app/updater/commit.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
git_internal "github.com/docplanner/helm-repo-updater/internal/app/git"
1212
"github.com/docplanner/helm-repo-updater/internal/app/log"
1313
git "github.com/go-git/go-git/v5"
14+
"github.com/go-git/go-git/v5/config"
1415
"github.com/go-git/go-git/v5/plumbing"
1516
"github.com/go-git/go-git/v5/plumbing/object"
1617
"github.com/go-git/go-git/v5/plumbing/transport"
@@ -242,13 +243,34 @@ func getRepositoryWorktreeWithBranchUpdated(gitConfBranch string, appName string
242243
return gitWUpdated, nil
243244
}
244245

246+
// fetchLatestChangesGitRepository fetch the latest changes in a git repository
247+
func fetchLatestChangesGitRepository(appName string, gitR git.Repository, creds transport.AuthMethod) (*git.Repository, error) {
248+
logCtx := log.WithContext().AddField("application", appName)
249+
logCtx.Debugf("Fetching latest changes of repository")
250+
251+
err := gitR.Fetch(&git.FetchOptions{
252+
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
253+
Auth: creds,
254+
})
255+
if err != nil {
256+
return nil, err
257+
}
258+
259+
return &gitR, nil
260+
}
261+
245262
// cloneGitRepositoryInBranch clone git repository with a specific branch checking if that branch exists already
246263
func cloneGitRepositoryInBranch(appName string, repoUrl string, creds transport.AuthMethod, tempRoot string, gitConfBranch string) (*git.Worktree, error) {
247264
gitR, err := cloneRepository(appName, repoUrl, creds, tempRoot)
248265
if err != nil {
249266
return nil, err
250267
}
251268

269+
gitR, err = fetchLatestChangesGitRepository(appName, *gitR, creds)
270+
if err != nil {
271+
return nil, err
272+
}
273+
252274
gitW, err := getRepositoryWorktreeWithBranchUpdated(gitConfBranch, appName, *gitR, creds)
253275
if err != nil {
254276
return nil, err

0 commit comments

Comments
 (0)