Skip to content

Commit 8fba721

Browse files
authored
Merge pull request #48 from utilitywarehouse/as-clone
use revision flag to simplify repo cloning
2 parents eeca550 + 24a90f0 commit 8fba721

File tree

5 files changed

+55
-98
lines changed

5 files changed

+55
-98
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ FROM golang:1.24-alpine AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

6-
RUN apk --no-cache add git openssh-client
6+
# '--repository' flag used to install latest git v 2.49
7+
# can be removed once alpine is updated to 3.22
8+
RUN apk --no-cache add git openssh-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main
79

810
WORKDIR /workspace
911
# Copy the Go Modules manifests

pkg/mirror/repo_pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ func (rp *RepoPool) ObjectExists(ctx context.Context, remote, obj string) error
303303
}
304304

305305
// Clone is wrapper around repositories Clone method
306-
func (rp *RepoPool) Clone(ctx context.Context, remote, dst, branch, pathspec string, rmGitDir bool) (string, error) {
306+
func (rp *RepoPool) Clone(ctx context.Context, remote, dst, branch string, pathspecs []string, rmGitDir bool) (string, error) {
307307
repo, err := rp.Repository(remote)
308308
if err != nil {
309309
return "", err
310310
}
311-
return repo.Clone(ctx, dst, branch, pathspec, rmGitDir)
311+
return repo.Clone(ctx, dst, branch, pathspecs, rmGitDir)
312312
}
313313

314314
// MergeCommits is wrapper around repositories MergeCommits method

pkg/mirror/repository.go

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (r *Repository) ObjectExists(ctx context.Context, obj string) error {
302302
// if ref is commit hash then pathspec will be ignored.
303303
// if rmGitDir is true `.git` folder will be deleted after the clone.
304304
// if dst not empty all its contents will be removed.
305-
func (r *Repository) Clone(ctx context.Context, dst, ref, pathspec string, rmGitDir bool) (string, error) {
305+
func (r *Repository) Clone(ctx context.Context, dst, ref string, pathspecs []string, rmGitDir bool) (string, error) {
306306
if ref == "" {
307307
ref = "HEAD"
308308
}
@@ -330,26 +330,17 @@ func (r *Repository) Clone(ctx context.Context, dst, ref, pathspec string, rmGit
330330
r.lock.RLock()
331331
defer r.lock.RUnlock()
332332

333-
if IsCommitHash(ref) {
334-
return r.cloneByRef(ctx, dst, ref, pathspec, rmGitDir)
335-
}
336-
return r.cloneByBranch(ctx, dst, ref, pathspec, rmGitDir)
337-
}
338-
339-
func (r *Repository) cloneByBranch(ctx context.Context, dst, branch, pathspec string, rmGitDir bool) (string, error) {
340-
args := []string{"clone", "--no-checkout", "--single-branch"}
341-
if branch != "HEAD" {
342-
args = append(args, "-b", branch)
343-
}
344-
args = append(args, r.dir, dst)
345-
// git clone --no-checkout --single-branch [-b <branch>] <remote> <dst>
333+
// create a thin clone of a repository that only contains the history of the given revision
334+
// git clone --no-checkout --revision <ref> <repo_src> <dst>
335+
args := []string{"clone", "--no-checkout", "--revision", ref, r.dir, dst}
346336
if _, err := runGitCommand(ctx, r.log, nil, "", args...); err != nil {
347337
return "", err
348338
}
349339

350-
args = []string{"checkout", branch}
351-
if pathspec != "" {
352-
args = append(args, "--", pathspec)
340+
args = []string{"checkout", "HEAD"}
341+
if len(pathspecs) > 0 {
342+
args = append(args, "--")
343+
args = append(args, pathspecs...)
353344
}
354345
// git checkout <branch> -- <pathspec>
355346
if _, err := runGitCommand(ctx, r.log, nil, dst, args...); err != nil {
@@ -358,43 +349,7 @@ func (r *Repository) cloneByBranch(ctx context.Context, dst, branch, pathspec st
358349

359350
// get the hash of the repos HEAD
360351
args = []string{"log", "--pretty=format:%H", "-n", "1", "HEAD"}
361-
if pathspec != "" {
362-
args = append(args, "--", pathspec)
363-
}
364-
// git log --pretty=format:%H -n 1 HEAD [-- <path>]
365-
hash, err := runGitCommand(ctx, r.log, nil, dst, args...)
366-
if err != nil {
367-
return "", err
368-
}
369-
370-
if rmGitDir {
371-
if err := os.RemoveAll(filepath.Join(dst, ".git")); err != nil {
372-
return "", fmt.Errorf("unable to delete git dir err:%w", err)
373-
}
374-
}
375-
376-
return hash, nil
377-
}
378-
379-
func (r *Repository) cloneByRef(ctx context.Context, dst, ref, pathspec string, rmGitDir bool) (string, error) {
380-
// git clone --no-checkout <remote> <dst>
381-
if _, err := runGitCommand(ctx, r.log, nil, "", "clone", "--no-checkout", r.dir, dst); err != nil {
382-
return "", err
383-
}
384-
385-
args := []string{"reset", "--hard", ref}
386-
// git reset --hard <ref>
387-
if _, err := runGitCommand(ctx, r.log, nil, dst, args...); err != nil {
388-
return "", err
389-
}
390-
391-
// get the hash of the repos HEAD
392-
args = []string{"log", "--pretty=format:%H", "-n", "1", "HEAD"}
393-
if pathspec != "" {
394-
args = append(args, "--", pathspec)
395-
}
396-
397-
// git log --pretty=format:%H -n 1 HEAD [-- <path>]
352+
// git log --pretty=format:%H -n 1 HEAD
398353
hash, err := runGitCommand(ctx, r.log, nil, dst, args...)
399354
if err != nil {
400355
return "", err

pkg/mirror/z_e2e_race_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Test_mirror_detect_race_clone(t *testing.T) {
9393
tempClone := mustTmpDir(t)
9494
defer os.RemoveAll(tempClone)
9595

96-
if cloneSHA, err := repo.Clone(ctx, tempClone, testMainBranch, "", i%2 == 0); err != nil {
96+
if cloneSHA, err := repo.Clone(ctx, tempClone, testMainBranch, nil, i%2 == 0); err != nil {
9797
t.Fatalf("unexpected error %s", err)
9898
} else {
9999
if cloneSHA != fileSHA2 {

0 commit comments

Comments
 (0)