Skip to content

Commit 18f346a

Browse files
authored
fix(git): add support for git clone fallback with ref checkout (#4186)
Signed-off-by: Brandt Keller <[email protected]>
1 parent 9e8ba70 commit 18f346a

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/internal/git/repository.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,37 @@ func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Reposi
9494
if gitCred != nil {
9595
cloneOpts.Auth = &gitCred.Auth
9696
}
97+
98+
// Use this independent of a *git.Repository
99+
checkout := func(refPlain string, ref plumbing.ReferenceName, r *Repository) error {
100+
// Optionally checkout ref
101+
if ref != emptyRef && !ref.IsBranch() {
102+
// Remove the "refs/tags/" prefix from the ref.
103+
stripped := strings.TrimPrefix(refPlain, "refs/tags/")
104+
// Use the plain ref as part of the branch name so it is unique and doesn't conflict with other refs.
105+
alias := fmt.Sprintf("zarf-ref-%s", stripped)
106+
trunkBranchName := plumbing.NewBranchReferenceName(alias)
107+
// Checkout the ref as a branch.
108+
err := r.checkoutRefAsBranch(stripped, trunkBranchName)
109+
if err != nil {
110+
return err
111+
}
112+
}
113+
return nil
114+
}
115+
97116
repo, err := git.PlainCloneContext(ctx, r.path, false, cloneOpts)
98117
if err != nil {
99118
l.Info("falling back to host 'git', failed to clone the repo with Zarf", "url", gitURLNoRef, "error", err)
100119
err := r.gitCloneFallback(ctx, gitURLNoRef, ref, shallow)
101120
if err != nil {
102121
return nil, err
103122
}
123+
err = checkout(refPlain, ref, r)
124+
if err != nil {
125+
return nil, err
126+
}
127+
return r, nil
104128
}
105129

106130
// If we're cloning the whole repo, we need to also fetch the other branches besides the default.
@@ -118,18 +142,9 @@ func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Reposi
118142
}
119143
}
120144

121-
// Optionally checkout ref
122-
if ref != emptyRef && !ref.IsBranch() {
123-
// Remove the "refs/tags/" prefix from the ref.
124-
stripped := strings.TrimPrefix(refPlain, "refs/tags/")
125-
// Use the plain ref as part of the branch name so it is unique and doesn't conflict with other refs.
126-
alias := fmt.Sprintf("zarf-ref-%s", stripped)
127-
trunkBranchName := plumbing.NewBranchReferenceName(alias)
128-
// Checkout the ref as a branch.
129-
err := r.checkoutRefAsBranch(stripped, trunkBranchName)
130-
if err != nil {
131-
return nil, err
132-
}
145+
err = checkout(refPlain, ref, r)
146+
if err != nil {
147+
return nil, err
133148
}
134149

135150
return r, nil

0 commit comments

Comments
 (0)