Skip to content

Commit

Permalink
Skip fetch if revision to check out exists locally
Browse files Browse the repository at this point in the history
  • Loading branch information
shady-canva committed Jun 4, 2024
1 parent 2639c59 commit 4700291
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
17 changes: 13 additions & 4 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2432,10 +2432,19 @@ func checkoutRevision(gitClient git.Client, revision string, submoduleEnabled bo
return status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err)
}

// Fetching with no revision first. Fetching with an explicit version can cause repo bloat. https://github.com/argoproj/argo-cd/issues/8845
err = gitClient.Fetch("")
if err != nil {
return status.Errorf(codes.Internal, "Failed to fetch default: %v", err)
revisionPresent := gitClient.IsRevisionPresent(revision)

log.WithFields(map[string]interface{}{
"skipFetch": revisionPresent,
}).Infof("Checking out revision %v", revision)

// Fetching can be skipped if the revision is already present locally.
if !revisionPresent {
// Fetching with no revision first. Fetching with an explicit version can cause repo bloat. https://github.com/argoproj/argo-cd/issues/8845
err = gitClient.Fetch("")
if err != nil {
return status.Errorf(codes.Internal, "Failed to fetch default: %v", err)
}
}

err = gitClient.Checkout(revision, submoduleEnabled)
Expand Down
16 changes: 16 additions & 0 deletions util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Client interface {
VerifyCommitSignature(string) (string, error)
IsAnnotatedTag(string) bool
ChangedFiles(revision string, targetRevision string) ([]string, error)
IsRevisionPresent(revision string) bool
}

type EventHandlers struct {
Expand Down Expand Up @@ -357,6 +358,21 @@ func (m *nativeGitClient) fetch(revision string) error {
return err
}

// IsRevisionPresent checks to see if the given revision already exists locally.
func (m *nativeGitClient) IsRevisionPresent(revision string) bool {
if revision == "" {
return false
}

cmd := exec.Command("git", "cat-file", "-t", revision)
out, err := m.runCmdOutput(cmd, runOpts{SkipErrorLogging: true})
if out == "commit" && err == nil {
return true
} else {
return false
}
}

// Fetch fetches latest updates from origin
func (m *nativeGitClient) Fetch(revision string) error {
if m.OnFetch != nil {
Expand Down
18 changes: 18 additions & 0 deletions util/git/mocks/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4700291

Please sign in to comment.