MCPcopy Create free account
hub / github.com/cli/cli / fetchCommitSHA

Function fetchCommitSHA

pkg/cmd/extension/http.go:175–207  ·  view source on GitHub ↗

fetchCommitSHA finds full commit SHA from a target ref in a repo

(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string)

Source from the content-addressed store, hash-verified

173
174// fetchCommitSHA finds full commit SHA from a target ref in a repo
175func fetchCommitSHA(httpClient *http.Client, baseRepo ghrepo.Interface, targetRef string) (string, error) {
176 url, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(baseRepo.RepoHost()), "repos", baseRepo.RepoOwner(), baseRepo.RepoName(), "commits", targetRef)
177 if err != nil {
178 return "", err
179 }
180 req, err := http.NewRequest("GET", url.String(), nil)
181 if err != nil {
182 return "", err
183 }
184
185 req.Header.Set("Accept", "application/vnd.github.v3.sha")
186 // TODO(api-client-rollout)
187 // This has been deferred from moving to api.Client due to its custom Accept header and bare SHA response body.
188 resp, err := httpClient.Do(req)
189 if err != nil {
190 return "", err
191 }
192
193 defer resp.Body.Close()
194 if resp.StatusCode == 422 {
195 return "", commitNotFoundErr
196 }
197 if resp.StatusCode > 299 {
198 return "", api.HandleHTTPError(resp)
199 }
200
201 body, err := io.ReadAll(resp.Body)
202 if err != nil {
203 return "", err
204 }
205
206 return string(body), nil
207}

Callers 1

installGitMethod · 0.85

Calls 10

JoinPathWithHostPrefixFunction · 0.92
RESTPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
RepoHostMethod · 0.65
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
StringMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected