detectGitRef attempts to resolve the current git branch and remote origin URL from the given working directory. These are sent to the control plane so it can look up PR/diff status via the GitHub API without SSHing into the workspace. Failures are silently ignored since this is best-effort.
(workingDirectory string)
| 25 | // without SSHing into the workspace. Failures are silently ignored |
| 26 | // since this is best-effort. |
| 27 | func detectGitRef(workingDirectory string) (branch string, remoteOrigin string) { |
| 28 | run := func(args ...string) string { |
| 29 | //nolint:gosec |
| 30 | cmd := exec.Command(args[0], args[1:]...) |
| 31 | if workingDirectory != "" { |
| 32 | cmd.Dir = workingDirectory |
| 33 | } |
| 34 | out, err := cmd.Output() |
| 35 | if err != nil { |
| 36 | return "" |
| 37 | } |
| 38 | return strings.TrimSpace(string(out)) |
| 39 | } |
| 40 | branch = run("git", "rev-parse", "--abbrev-ref", "HEAD") |
| 41 | remoteOrigin = run("git", "config", "--get", "remote.origin.url") |
| 42 | return branch, remoteOrigin |
| 43 | } |
| 44 | |
| 45 | // gitAskpass is used by the Coder agent to automatically authenticate |
| 46 | // with Git providers based on a hostname. |
no test coverage detected