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

Function cmdGitSSH

script/ghmock/gittransport.go:162–235  ·  view source on GitHub ↗

cmdGitSSH replaces the SSH transport, not the Git protocol. Git's requested upload/receive-pack runs against the corresponding local bare repository. The command string is parsed as data; it is never passed to a shell.

(args []string, stdin io.Reader, stdout, stderr io.Writer)

Source from the content-addressed store, hash-verified

160// upload/receive-pack runs against the corresponding local bare repository.
161// The command string is parsed as data; it is never passed to a shell.
162func cmdGitSSH(args []string, stdin io.Reader, stdout, stderr io.Writer) error {
163 for len(args) > 0 && strings.HasPrefix(args[0], "-") {
164 switch args[0] {
165 case "-p", "-o", "-i", "-F":
166 if len(args) < 2 {
167 return fmt.Errorf("missing value for SSH option %s", args[0])
168 }
169 args = args[2:]
170 case "-4", "-6", "-T":
171 args = args[1:]
172 default:
173 return fmt.Errorf("unsupported SSH option %q in ghmock transport", args[0])
174 }
175 }
176 if len(args) != 2 {
177 return fmt.Errorf("ghmock SSH transport expects a host and a Git service")
178 }
179 host := args[0]
180 if _, after, ok := strings.Cut(host, "@"); ok {
181 host = after
182 }
183 service, quoted, ok := strings.Cut(args[1], " ")
184 if !ok || (service != "git-upload-pack" && service != "git-receive-pack") ||
185 len(quoted) < 2 || quoted[0] != '\'' || quoted[len(quoted)-1] != '\'' {
186 return fmt.Errorf("unsupported SSH command %q in ghmock transport", args[1])
187 }
188 path := strings.TrimPrefix(quoted[1:len(quoted)-1], "/")
189 host, repo, err := parseGitRepo(host + "/" + path)
190 if err != nil {
191 return err
192 }
193 world, logPath := os.Getenv("GHMOCK_GIT_WORLD"), os.Getenv("GHMOCK_GIT_LOG")
194 if world == "" || logPath == "" {
195 return fmt.Errorf("ghmock SSH transport requires a Git run environment")
196 }
197 entry := loggedRequest{
198 Time: time.Now().UTC(), Method: "SSH", Host: host, Path: "/" + repo + ".git",
199 Name: "SSH " + host + "/" + repo + ".git", Git: &gitRequest{Service: service},
200 }
201 dir := gitRepoPath(world, host, repo)
202 deadline := time.Now().Add(defaultTimeout)
203 if value := os.Getenv("GHMOCK_GIT_DEADLINE"); value != "" {
204 deadline, err = time.Parse(time.RFC3339Nano, value)
205 if err != nil {
206 return fmt.Errorf("invalid Git transport deadline: %w", err)
207 }
208 }
209 ctx, cancel := context.WithDeadline(context.Background(), deadline)
210 defer cancel()
211 entry.Git.ID = fmt.Sprintf("%d-%d", os.Getpid(), entry.Time.UnixNano())
212 entry.Git.ExitCode = -1
213 entry.Git.Error = "SSH request did not complete"
214 if err := appendJSONL(logPath, []loggedRequest{entry}); err != nil {
215 return fmt.Errorf("recording Git SSH request: %w", err)
216 }
217 cmd := exec.CommandContext(ctx, "git", strings.TrimPrefix(service, "git-"), dir)
218 cmd.Env = os.Environ()
219 cmd.Stdin, cmd.Stdout = stdin, stdout

Callers 1

runMainFunction · 0.85

Calls 10

parseGitRepoFunction · 0.85
gitRepoPathFunction · 0.85
appendJSONLFunction · 0.85
processExitCodeFunction · 0.85
JoinMethod · 0.80
ErrorfMethod · 0.65
AddMethod · 0.65
RunMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected