Skip to content

Commit

Permalink
Use shallow clone to speed up performance
Browse files Browse the repository at this point in the history
* Only git fetch the one target revision that we need
* This does not utilize the history so there is no reason to clone it
* For large repos, this will speed up performance significantly

Signed-off-by: Joshua Novick <[email protected]>
  • Loading branch information
jnovick committed Aug 13, 2024
1 parent 65698c5 commit fe16b69
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
35 changes: 35 additions & 0 deletions ext/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Client interface {
Root() string
Init() error
Fetch(revision string) error
ShallowFetch(revision string, depth int) error
Submodule() error
Checkout(revision string, submoduleEnabled bool) error
LsRefs() (*Refs, error)
Expand Down Expand Up @@ -384,6 +385,40 @@ func (m *nativeGitClient) Fetch(revision string) error {
return err
}

func (m *nativeGitClient) shallowFetch(revision string, depth int) error {
var err error
if revision != "" {
err = m.runCredentialedCmd("fetch", "origin", revision, "--force", "--prune", "--depth", strconv.Itoa(depth))
} else {
err = m.runCredentialedCmd("fetch", "origin", "--force", "--prune", "--depth", strconv.Itoa(depth))
}
return err
}

// Fetch fetches latest updates from origin
func (m *nativeGitClient) ShallowFetch(revision string, depth int) error {
if m.OnFetch != nil {
done := m.OnFetch(m.repoURL)
defer done()
}

err := m.shallowFetch(revision, depth)

// When we have LFS support enabled, check for large files and fetch them too.
// No shallow fetch is possible here
if err == nil && m.IsLFSEnabled() {
largeFiles, err := m.LsLargeFiles()
if err == nil && len(largeFiles) > 0 {
err = m.runCredentialedCmd("lfs", "fetch", "--all")
if err != nil {
return err
}
}
}

return err
}

// LsFiles lists the local working tree, including only files that are under source control
func (m *nativeGitClient) LsFiles(path string, enableNewGitFileGlobbing bool) ([]string, error) {
if enableNewGitFileGlobbing {
Expand Down
19 changes: 19 additions & 0 deletions ext/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ func TestVerifyCommitSignature(t *testing.T) {
}
}

func TestVerifyShallowFetchCheckout(t *testing.T) {
p := t.TempDir()

client, err := NewClientExt("https://github.com/argoproj/argo-cd.git", p, NopCreds{}, false, false, "")
assert.NoError(t, err)

err = client.Init()
assert.NoError(t, err)

err = client.ShallowFetch("HEAD", 1)
assert.NoError(t, err)

commitSHA, err := client.LsRemote("HEAD")
assert.NoError(t, err)

err = client.Checkout(commitSHA, true)
assert.NoError(t, err)
}

func TestNewFactory(t *testing.T) {
addBinDirToPath := path.NewBinDirToPath()
defer addBinDirToPath.Close()
Expand Down
18 changes: 18 additions & 0 deletions ext/git/mocks/Client.go

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

8 changes: 4 additions & 4 deletions pkg/argocd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
if err != nil {
return err
}
err = gitC.Fetch("")
if err != nil {
return err
}

// Set username and e-mail address used to identify the commiter
if wbc.GitCommitUser != "" && wbc.GitCommitEmail != "" {
Expand All @@ -186,6 +182,10 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
return err
}
}
err = gitC.ShallowFetch(checkOutBranch, 1)
if err != nil {
return err
}

// The push branch is by default the same as the checkout branch, unless
// specified after a : separator git-branch annotation, in which case a
Expand Down

0 comments on commit fe16b69

Please sign in to comment.