@@ -94,13 +94,37 @@ func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Reposi
94
94
if gitCred != nil {
95
95
cloneOpts .Auth = & gitCred .Auth
96
96
}
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
+
97
116
repo , err := git .PlainCloneContext (ctx , r .path , false , cloneOpts )
98
117
if err != nil {
99
118
l .Info ("falling back to host 'git', failed to clone the repo with Zarf" , "url" , gitURLNoRef , "error" , err )
100
119
err := r .gitCloneFallback (ctx , gitURLNoRef , ref , shallow )
101
120
if err != nil {
102
121
return nil , err
103
122
}
123
+ err = checkout (refPlain , ref , r )
124
+ if err != nil {
125
+ return nil , err
126
+ }
127
+ return r , nil
104
128
}
105
129
106
130
// 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
118
142
}
119
143
}
120
144
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
133
148
}
134
149
135
150
return r , nil
0 commit comments