open-code-review/cmd/opencodereview/git.go
kite 13f81f24f6 fix(cmd): read git top-level via stdout-only helper
Resolve the repo top level through runGitCmdStdout (cmd.Output) instead of
CombinedOutput so git stderr notices (permission warnings, deprecations,
config hints) cannot pollute the resolved path. Add a regression test that a
bare repo (no work tree) fails loudly on the review path rather than silently
falling back to the invocation dir.
2026-07-06 19:19:30 +08:00

31 lines
979 B
Go

package main
import (
"fmt"
"os/exec"
"strings"
)
func runGitCmd(repoDir string, args ...string) ([]byte, error) {
fullArgs := append([]string{"-C", repoDir}, args...)
cmd := exec.Command("git", fullArgs...)
return cmd.CombinedOutput()
}
// runGitCmdStdout runs git and returns stdout only. Unlike runGitCmd it keeps
// stderr separate, so git notices (permission warnings, deprecations, config
// hints) cannot pollute output that is consumed as data — e.g. the path from
// `git rev-parse --show-toplevel`.
func runGitCmdStdout(repoDir string, args ...string) ([]byte, error) {
fullArgs := append([]string{"-C", repoDir}, args...)
cmd := exec.Command("git", fullArgs...)
return cmd.Output()
}
func getCommitMessage(repoDir, commit string) (string, error) {
out, err := runGitCmd(repoDir, "log", "-1", "--format=%B", "--end-of-options", commit)
if err != nil {
return "", fmt.Errorf("git log failed: %w", err)
}
return strings.TrimSpace(string(out)), nil
}