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

Method serveGitHTTP

script/ghmock/gittransport.go:36–157  ·  view source on GitHub ↗

serveGitHTTP delegates smart HTTP to Git before the JSON fixture handler reads the body. Pack streams must not be truncated or schema-checked.

(w http.ResponseWriter, r *http.Request, entry loggedRequest)

Source from the content-addressed store, hash-verified

34// serveGitHTTP delegates smart HTTP to Git before the JSON fixture handler
35// reads the body. Pack streams must not be truncated or schema-checked.
36func (m *mockServer) serveGitHTTP(w http.ResponseWriter, r *http.Request, entry loggedRequest) bool {
37 if m.git == nil || isAPIHost(entry.Host) {
38 return false
39 }
40 parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
41 if len(parts) < 3 {
42 return false
43 }
44 endpoint := strings.Join(parts[2:], "/")
45 if endpoint != "info/refs" && endpoint != "git-upload-pack" && endpoint != "git-receive-pack" {
46 return false
47 }
48 entry.Name = r.Method + " " + entry.Host + r.URL.Path
49 service := endpoint
50 if endpoint == "info/refs" {
51 service = r.URL.Query().Get("service")
52 }
53 entry.Git = &gitRequest{Service: service}
54 defer func() { m.record(entry) }()
55 fail := func(status int, err error) {
56 entry.Status = status
57 entry.Git.Error = err.Error()
58 http.Error(w, "ghmock: "+err.Error(), status)
59 }
60 host, repo, err := parseGitRepo(entry.Host + "/" + strings.Join(parts[:2], "/"))
61 if err != nil {
62 fail(http.StatusBadRequest, err)
63 return true
64 }
65 if service != "git-upload-pack" && service != "git-receive-pack" {
66 fail(http.StatusBadRequest, fmt.Errorf("unsupported Git service %q", service))
67 return true
68 }
69 if (endpoint == "info/refs" && r.Method != http.MethodGet) || (endpoint != "info/refs" && r.Method != http.MethodPost) {
70 fail(http.StatusMethodNotAllowed, fmt.Errorf("method %s is not supported for %s", r.Method, endpoint))
71 return true
72 }
73 repoDir := gitRepoPath(m.git.world, host, repo)
74 if _, err := os.Stat(filepath.Join(repoDir, "HEAD")); err != nil {
75 if errors.Is(err, os.ErrNotExist) {
76 fail(http.StatusNotFound, fmt.Errorf("no Git repository %s/%s; create it with ghmock repo", host, repo))
77 } else {
78 fail(http.StatusInternalServerError, err)
79 }
80 return true
81 }
82 env, err := gitEnvironment(ghEnv(m.git.scratch, m.proxyURL(), runOptions{}), m.git.scratch, m.git.world)
83 if err != nil {
84 fail(http.StatusInternalServerError, err)
85 return true
86 }
87 ctx, cancel := context.WithCancel(r.Context())
88 defer cancel()
89 cmd := exec.CommandContext(ctx, "git", "http-backend")
90 cmd.Dir = m.git.world
91 cmd.Env = append(env,
92 "GIT_PROJECT_ROOT="+filepath.Dir(filepath.Dir(repoDir)),
93 "GIT_HTTP_EXPORT_ALL=1",

Callers 1

ServeHTTPMethod · 0.95

Calls 15

recordMethod · 0.95
proxyURLMethod · 0.95
isAPIHostFunction · 0.85
parseGitRepoFunction · 0.85
gitRepoPathFunction · 0.85
gitEnvironmentFunction · 0.85
ghEnvFunction · 0.85
processExitCodeFunction · 0.85
JoinMethod · 0.80
containCommandFunction · 0.70
GetMethod · 0.65
QueryMethod · 0.65

Tested by

no test coverage detected