Skip to content

Commit

Permalink
feat: Use shallow clone to speed up performance (#830)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Novick <joshua.novick@hunters.ai>
  • Loading branch information
jnovick authored Aug 14, 2024
1 parent 0ad5b94 commit 859900e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 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
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.

35 changes: 35 additions & 0 deletions ext/git/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"fmt"
"os/exec"
"strconv"
"strings"

"github.com/argoproj-labs/argocd-image-updater/pkg/log"
Expand Down Expand Up @@ -159,3 +160,37 @@ func (m *nativeGitClient) runCredentialedCmdWithOutput(args ...string) (string,
cmd.Env = append(cmd.Env, environ...)
return m.runCmdOutput(cmd, runOpts{})
}

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
}
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
10 changes: 5 additions & 5 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3009,7 +3009,7 @@ replacements: []
app := app.DeepCopy()
gitMock := &gitmock.Client{}
gitMock.On("Init").Return(nil)
gitMock.On("Fetch", mock.Anything).Return(nil)
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Checkout", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Assert(t, "mydefaultbranch", false)
}).Return(nil)
Expand All @@ -3035,7 +3035,7 @@ replacements: []
t.Run("Cannot init", func(t *testing.T) {
gitMock := &gitmock.Client{}
gitMock.On("Init").Return(fmt.Errorf("cannot init"))
gitMock.On("Fetch", mock.Anything).Return(nil)
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand All @@ -3050,7 +3050,7 @@ replacements: []
t.Run("Cannot fetch", func(t *testing.T) {
gitMock := &gitmock.Client{}
gitMock.On("Init").Return(nil)
gitMock.On("Fetch", mock.Anything).Return(fmt.Errorf("cannot fetch"))
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(fmt.Errorf("cannot fetch"))
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand All @@ -3064,7 +3064,7 @@ replacements: []
t.Run("Cannot checkout", func(t *testing.T) {
gitMock := &gitmock.Client{}
gitMock.On("Init").Return(nil)
gitMock.On("Fetch", mock.Anything).Return(nil)
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(fmt.Errorf("cannot checkout"))
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand Down Expand Up @@ -3180,7 +3180,7 @@ func mockGit(t *testing.T) (gitMock *gitmock.Client, dir string, cleanup func())
gitMock = &gitmock.Client{}
gitMock.On("Root").Return(dir)
gitMock.On("Init").Return(nil)
gitMock.On("Fetch", mock.Anything).Return(nil)
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
return gitMock, dir, func() {
_ = os.RemoveAll(dir)
}
Expand Down

0 comments on commit 859900e

Please sign in to comment.