mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-07-09 17:28:58 +00:00
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.
31 lines
979 B
Go
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
|
|
}
|