Skip to content

Commit 859900e

Browse files
authored
feat: Use shallow clone to speed up performance (#830)
Signed-off-by: Joshua Novick <[email protected]>
1 parent 0ad5b94 commit 859900e

File tree

6 files changed

+82
-9
lines changed

6 files changed

+82
-9
lines changed

ext/git/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Client interface {
6565
Root() string
6666
Init() error
6767
Fetch(revision string) error
68+
ShallowFetch(revision string, depth int) error
6869
Submodule() error
6970
Checkout(revision string, submoduleEnabled bool) error
7071
LsRefs() (*Refs, error)

ext/git/git_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,25 @@ func TestVerifyCommitSignature(t *testing.T) {
322322
}
323323
}
324324

325+
func TestVerifyShallowFetchCheckout(t *testing.T) {
326+
p := t.TempDir()
327+
328+
client, err := NewClientExt("https://github.com/argoproj/argo-cd.git", p, NopCreds{}, false, false, "")
329+
assert.NoError(t, err)
330+
331+
err = client.Init()
332+
assert.NoError(t, err)
333+
334+
err = client.ShallowFetch("HEAD", 1)
335+
assert.NoError(t, err)
336+
337+
commitSHA, err := client.LsRemote("HEAD")
338+
assert.NoError(t, err)
339+
340+
err = client.Checkout(commitSHA, true)
341+
assert.NoError(t, err)
342+
}
343+
325344
func TestNewFactory(t *testing.T) {
326345
addBinDirToPath := path.NewBinDirToPath()
327346
defer addBinDirToPath.Close()

ext/git/mocks/Client.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/git/writer.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"fmt"
55
"os/exec"
6+
"strconv"
67
"strings"
78

89
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
@@ -159,3 +160,37 @@ func (m *nativeGitClient) runCredentialedCmdWithOutput(args ...string) (string,
159160
cmd.Env = append(cmd.Env, environ...)
160161
return m.runCmdOutput(cmd, runOpts{})
161162
}
163+
164+
func (m *nativeGitClient) shallowFetch(revision string, depth int) error {
165+
var err error
166+
if revision != "" {
167+
err = m.runCredentialedCmd("fetch", "origin", revision, "--force", "--prune", "--depth", strconv.Itoa(depth))
168+
} else {
169+
err = m.runCredentialedCmd("fetch", "origin", "--force", "--prune", "--depth", strconv.Itoa(depth))
170+
}
171+
return err
172+
}
173+
174+
// Fetch fetches latest updates from origin
175+
func (m *nativeGitClient) ShallowFetch(revision string, depth int) error {
176+
if m.OnFetch != nil {
177+
done := m.OnFetch(m.repoURL)
178+
defer done()
179+
}
180+
181+
err := m.shallowFetch(revision, depth)
182+
183+
// When we have LFS support enabled, check for large files and fetch them too.
184+
// No shallow fetch is possible here
185+
if err == nil && m.IsLFSEnabled() {
186+
largeFiles, err := m.LsLargeFiles()
187+
if err == nil && len(largeFiles) > 0 {
188+
err = m.runCredentialedCmd("lfs", "fetch", "--all")
189+
if err != nil {
190+
return err
191+
}
192+
}
193+
}
194+
195+
return err
196+
}

pkg/argocd/git.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
157157
if err != nil {
158158
return err
159159
}
160-
err = gitC.Fetch("")
161-
if err != nil {
162-
return err
163-
}
164160

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

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

pkg/argocd/update_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,7 +3009,7 @@ replacements: []
30093009
app := app.DeepCopy()
30103010
gitMock := &gitmock.Client{}
30113011
gitMock.On("Init").Return(nil)
3012-
gitMock.On("Fetch", mock.Anything).Return(nil)
3012+
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
30133013
gitMock.On("Checkout", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
30143014
args.Assert(t, "mydefaultbranch", false)
30153015
}).Return(nil)
@@ -3035,7 +3035,7 @@ replacements: []
30353035
t.Run("Cannot init", func(t *testing.T) {
30363036
gitMock := &gitmock.Client{}
30373037
gitMock.On("Init").Return(fmt.Errorf("cannot init"))
3038-
gitMock.On("Fetch", mock.Anything).Return(nil)
3038+
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
30393039
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(nil)
30403040
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
30413041
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -3050,7 +3050,7 @@ replacements: []
30503050
t.Run("Cannot fetch", func(t *testing.T) {
30513051
gitMock := &gitmock.Client{}
30523052
gitMock.On("Init").Return(nil)
3053-
gitMock.On("Fetch", mock.Anything).Return(fmt.Errorf("cannot fetch"))
3053+
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(fmt.Errorf("cannot fetch"))
30543054
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(nil)
30553055
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
30563056
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -3064,7 +3064,7 @@ replacements: []
30643064
t.Run("Cannot checkout", func(t *testing.T) {
30653065
gitMock := &gitmock.Client{}
30663066
gitMock.On("Init").Return(nil)
3067-
gitMock.On("Fetch", mock.Anything).Return(nil)
3067+
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
30683068
gitMock.On("Checkout", mock.Anything, mock.Anything).Return(fmt.Errorf("cannot checkout"))
30693069
gitMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
30703070
gitMock.On("Push", mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -3180,7 +3180,7 @@ func mockGit(t *testing.T) (gitMock *gitmock.Client, dir string, cleanup func())
31803180
gitMock = &gitmock.Client{}
31813181
gitMock.On("Root").Return(dir)
31823182
gitMock.On("Init").Return(nil)
3183-
gitMock.On("Fetch", mock.Anything).Return(nil)
3183+
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
31843184
return gitMock, dir, func() {
31853185
_ = os.RemoveAll(dir)
31863186
}

0 commit comments

Comments
 (0)