Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ FROM golang:1.24-alpine AS builder
ARG TARGETOS
ARG TARGETARCH

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

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
4 changes: 2 additions & 2 deletions pkg/mirror/repo_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ func (rp *RepoPool) ObjectExists(ctx context.Context, remote, obj string) error
}

// Clone is wrapper around repositories Clone method
func (rp *RepoPool) Clone(ctx context.Context, remote, dst, branch, pathspec string, rmGitDir bool) (string, error) {
func (rp *RepoPool) Clone(ctx context.Context, remote, dst, branch string, pathspecs []string, rmGitDir bool) (string, error) {
repo, err := rp.Repository(remote)
if err != nil {
return "", err
}
return repo.Clone(ctx, dst, branch, pathspec, rmGitDir)
return repo.Clone(ctx, dst, branch, pathspecs, rmGitDir)
}

// MergeCommits is wrapper around repositories MergeCommits method
Expand Down
63 changes: 9 additions & 54 deletions pkg/mirror/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (r *Repository) ObjectExists(ctx context.Context, obj string) error {
// if ref is commit hash then pathspec will be ignored.
// if rmGitDir is true `.git` folder will be deleted after the clone.
// if dst not empty all its contents will be removed.
func (r *Repository) Clone(ctx context.Context, dst, ref, pathspec string, rmGitDir bool) (string, error) {
func (r *Repository) Clone(ctx context.Context, dst, ref string, pathspecs []string, rmGitDir bool) (string, error) {
if ref == "" {
ref = "HEAD"
}
Expand Down Expand Up @@ -330,26 +330,17 @@ func (r *Repository) Clone(ctx context.Context, dst, ref, pathspec string, rmGit
r.lock.RLock()
defer r.lock.RUnlock()

if IsCommitHash(ref) {
return r.cloneByRef(ctx, dst, ref, pathspec, rmGitDir)
}
return r.cloneByBranch(ctx, dst, ref, pathspec, rmGitDir)
}

func (r *Repository) cloneByBranch(ctx context.Context, dst, branch, pathspec string, rmGitDir bool) (string, error) {
args := []string{"clone", "--no-checkout", "--single-branch"}
if branch != "HEAD" {
args = append(args, "-b", branch)
}
args = append(args, r.dir, dst)
// git clone --no-checkout --single-branch [-b <branch>] <remote> <dst>
// create a thin clone of a repository that only contains the history of the given revision
// git clone --no-checkout --revision <ref> <repo_src> <dst>
args := []string{"clone", "--no-checkout", "--revision", ref, r.dir, dst}
if _, err := runGitCommand(ctx, r.log, nil, "", args...); err != nil {
return "", err
}

args = []string{"checkout", branch}
if pathspec != "" {
args = append(args, "--", pathspec)
args = []string{"checkout", "HEAD"}
if len(pathspecs) > 0 {
args = append(args, "--")
args = append(args, pathspecs...)
}
// git checkout <branch> -- <pathspec>
if _, err := runGitCommand(ctx, r.log, nil, dst, args...); err != nil {
Expand All @@ -358,43 +349,7 @@ func (r *Repository) cloneByBranch(ctx context.Context, dst, branch, pathspec st

// get the hash of the repos HEAD
args = []string{"log", "--pretty=format:%H", "-n", "1", "HEAD"}
if pathspec != "" {
args = append(args, "--", pathspec)
}
// git log --pretty=format:%H -n 1 HEAD [-- <path>]
hash, err := runGitCommand(ctx, r.log, nil, dst, args...)
if err != nil {
return "", err
}

if rmGitDir {
if err := os.RemoveAll(filepath.Join(dst, ".git")); err != nil {
return "", fmt.Errorf("unable to delete git dir err:%w", err)
}
}

return hash, nil
}

func (r *Repository) cloneByRef(ctx context.Context, dst, ref, pathspec string, rmGitDir bool) (string, error) {
// git clone --no-checkout <remote> <dst>
if _, err := runGitCommand(ctx, r.log, nil, "", "clone", "--no-checkout", r.dir, dst); err != nil {
return "", err
}

args := []string{"reset", "--hard", ref}
// git reset --hard <ref>
if _, err := runGitCommand(ctx, r.log, nil, dst, args...); err != nil {
return "", err
}

// get the hash of the repos HEAD
args = []string{"log", "--pretty=format:%H", "-n", "1", "HEAD"}
if pathspec != "" {
args = append(args, "--", pathspec)
}

// git log --pretty=format:%H -n 1 HEAD [-- <path>]
// git log --pretty=format:%H -n 1 HEAD
hash, err := runGitCommand(ctx, r.log, nil, dst, args...)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion pkg/mirror/z_e2e_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Test_mirror_detect_race_clone(t *testing.T) {
tempClone := mustTmpDir(t)
defer os.RemoveAll(tempClone)

if cloneSHA, err := repo.Clone(ctx, tempClone, testMainBranch, "", i%2 == 0); err != nil {
if cloneSHA, err := repo.Clone(ctx, tempClone, testMainBranch, nil, i%2 == 0); err != nil {
t.Fatalf("unexpected error %s", err)
} else {
if cloneSHA != fileSHA2 {
Expand Down
Loading
Loading