@@ -80,24 +80,9 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
80
80
ref .Commit = "HEAD" // default branch
81
81
}
82
82
83
- if ! commitSHA .MatchString (ref .Commit ) {
84
- cmd := exec .CommandContext (ctx , "git" , "ls-remote" , "--exit-code" , ref .Remote , ref .Commit )
85
- cmd .Env = g .gitCommandEnv ()
86
- out , err := cmd .Output ()
87
- if err != nil {
88
- if cmd .ProcessState .ExitCode () == 2 {
89
- return "" , fmt .Errorf ("repository does not contain ref %s, output: %q: %w" , path , string (out ), err )
90
- }
91
- return "" , err
92
- }
93
- if len (out ) < 40 {
94
- return "" , fmt .Errorf ("unexpected git command output: %q" , string (out ))
95
- }
96
- sha := string (out [:40 ])
97
- if ! commitSHA .MatchString (sha ) {
98
- return "" , fmt .Errorf ("invalid commit sha %q" , sha )
99
- }
100
- ref .Commit = sha
83
+ err = g .resolveGitRef (ctx , path , ref )
84
+ if err != nil {
85
+ return "" , err
101
86
}
102
87
103
88
cache , err := cacheDir ()
@@ -129,6 +114,29 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
129
114
return local , err
130
115
}
131
116
117
+ func (g gitRemoteLoader ) resolveGitRef (ctx context.Context , path string , ref * gitutil.GitRef ) error {
118
+ if ! commitSHA .MatchString (ref .Commit ) {
119
+ cmd := exec .CommandContext (ctx , "git" , "ls-remote" , "--exit-code" , ref .Remote , ref .Commit )
120
+ cmd .Env = g .gitCommandEnv ()
121
+ out , err := cmd .Output ()
122
+ if err != nil {
123
+ if cmd .ProcessState .ExitCode () == 2 {
124
+ return fmt .Errorf ("repository does not contain ref %s, output: %q: %w" , path , string (out ), err )
125
+ }
126
+ return err
127
+ }
128
+ if len (out ) < 40 {
129
+ return fmt .Errorf ("unexpected git command output: %q" , string (out ))
130
+ }
131
+ sha := string (out [:40 ])
132
+ if ! commitSHA .MatchString (sha ) {
133
+ return fmt .Errorf ("invalid commit sha %q" , sha )
134
+ }
135
+ ref .Commit = sha
136
+ }
137
+ return nil
138
+ }
139
+
132
140
func (g gitRemoteLoader ) checkout (ctx context.Context , path string , ref * gitutil.GitRef ) error {
133
141
err := os .MkdirAll (path , 0o700 )
134
142
if err != nil {
0 commit comments