@@ -174,11 +174,36 @@ func (g *GitGetter) GetFile(dst string, u *url.URL) error {
174174}
175175
176176func (g * GitGetter ) checkout (ctx context.Context , dst string , ref string ) error {
177- cmd := exec .CommandContext (ctx , "git" , "checkout" , ref , "--" )
177+ resolvedRef , err := resolveCheckoutRef (ctx , dst , ref )
178+ if err != nil {
179+ return err
180+ }
181+
182+ cmd := exec .CommandContext (ctx , "git" , "checkout" , resolvedRef )
178183 cmd .Dir = dst
179184 return getRunCommand (cmd )
180185}
181186
187+ func resolveCheckoutRef (ctx context.Context , dst , ref string ) (string , error ) {
188+ candidates := []string {
189+ ref ,
190+ "refs/remotes/origin/" + ref ,
191+ "refs/tags/" + ref ,
192+ }
193+
194+ for _ , candidate := range candidates {
195+ cmd := exec .CommandContext (ctx , "git" , "rev-parse" , "--verify" , "--quiet" , "--end-of-options" , candidate + "^{commit}" )
196+ cmd .Dir = dst
197+
198+ resolvedRef , err := cmd .Output ()
199+ if err == nil {
200+ return strings .TrimSpace (string (resolvedRef )), nil
201+ }
202+ }
203+
204+ return "" , fmt .Errorf ("invalid ref: %q" , ref )
205+ }
206+
182207// gitCommitIDRegex is a pattern intended to match strings that seem
183208// "likely to be" git commit IDs, rather than named refs. This cannot be
184209// an exact decision because it's valid to name a branch or tag after a series
0 commit comments