@@ -11,6 +11,7 @@ import (
11
11
git_internal "github.com/docplanner/helm-repo-updater/internal/app/git"
12
12
"github.com/docplanner/helm-repo-updater/internal/app/log"
13
13
git "github.com/go-git/go-git/v5"
14
+ "github.com/go-git/go-git/v5/config"
14
15
"github.com/go-git/go-git/v5/plumbing"
15
16
"github.com/go-git/go-git/v5/plumbing/object"
16
17
"github.com/go-git/go-git/v5/plumbing/transport"
@@ -242,13 +243,34 @@ func getRepositoryWorktreeWithBranchUpdated(gitConfBranch string, appName string
242
243
return gitWUpdated , nil
243
244
}
244
245
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
+
245
262
// cloneGitRepositoryInBranch clone git repository with a specific branch checking if that branch exists already
246
263
func cloneGitRepositoryInBranch (appName string , repoUrl string , creds transport.AuthMethod , tempRoot string , gitConfBranch string ) (* git.Worktree , error ) {
247
264
gitR , err := cloneRepository (appName , repoUrl , creds , tempRoot )
248
265
if err != nil {
249
266
return nil , err
250
267
}
251
268
269
+ gitR , err = fetchLatestChangesGitRepository (appName , * gitR , creds )
270
+ if err != nil {
271
+ return nil , err
272
+ }
273
+
252
274
gitW , err := getRepositoryWorktreeWithBranchUpdated (gitConfBranch , appName , * gitR , creds )
253
275
if err != nil {
254
276
return nil , err
0 commit comments