alibaba/open-code-review · error

git log failed: %w

Error message

git log failed: %w

What it means

getCommitMessage runs `git log -1 --format=%B --end-of-options <commit>` via runGitCmd and wraps any git failure as "git log failed: <cause>". The %w wrap keeps the exit status/error from the underlying git process, so the caller can detect invalid or unknown commits.

Source

Thrown at cmd/opencodereview/git.go:30

func runGitCmd(repoDir string, args ...string) ([]byte, error) {
	fullArgs := append([]string{"-C", repoDir}, args...)
	cmd := exec.Command("git", fullArgs...)
	return cmd.CombinedOutput()
}

// runGitCmdStdout is like runGitCmd but returns stdout only. Use it when the
// output is consumed as data (e.g. a resolved path) so git's stderr warnings
// (permissions, deprecations, config notices) can't pollute the result.
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
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Check the wrapped cause — 'unknown revision' means the commit is absent from this repo
  2. Run `git log -1 <commit>` manually in repoDir to reproduce
  3. Fetch the missing commit (git fetch origin <sha>) or unshallow the clone
  4. Verify repoDir points at the correct working repository

Example fix

// before
msg, err := getCommitMessage(repo, "deadbeefdeadbeef") // unknown revision
// after
if out, err := runGitCmd(repo, "cat-file", "-t", commit); err != nil || strings.TrimSpace(out) != "commit" {
    return "", fmt.Errorf("commit %s not found in repository", commit)
}
msg, err := getCommitMessage(repo, commit)
Defensive patterns

Strategy: try-catch

Validate before calling

func commitExists(dir, sha string) bool {
    out, err := runGitCmd(dir, "rev-parse", "--verify", "--quiet", sha+"^{commit}")
    return err == nil && out != ""
}

Try / catch

msg, err := getCommitMessage(repoDir, commit)
if err != nil {
    var ee *exec.ExitError
    if errors.As(err, &ee) && strings.Contains(string(ee.Stderr), "unknown revision") {
        return "", fmt.Errorf("commit %s not found: run 'git fetch' first", commit)
    }
    return "", err
}

Prevention

When it happens

Trigger: resolveBackground asked for the message of a commit that does not exist in the repo (e.g. a SHA from another repo, an abbreviated hash that matches nothing, or `HEAD` in an empty repo).

Common situations: Passing a full SHA from a fork after history was rewritten; shallow clone missing the commit; typo in a branch/commit name; running outside the repo (repoDir wrong).

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/b1acc5295559d7df. Report an issue: GitHub.