{"record":{"id":"d3cd24d36ae5531b","repo":"alibaba/open-code-review","slug":"git-show-s-s-w","errorCode":null,"errorMessage":"git show %s:%s: %w","messagePattern":"git show (.+?):(.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tool/filereader.go","lineNumber":126,"sourceCode":"\t\t\treturn fullPath, nil\n\t\t}\n\t\treturn \"\", fmt.Errorf(\"resolve file %q: %w\", path, err)\n\t}\n\tif !pathutil.WithinBase(repoRoot, resolvedPath) {\n\t\treturn \"\", fmt.Errorf(\"file path %q is outside repository\", path)\n\t}\n\treturn resolvedPath, nil\n}\n\nfunc (fr *FileReader) readFromGitShow(parentCtx context.Context, path string) (string, error) {\n\tctx, cancel := context.WithTimeout(parentCtx, 30*time.Second)\n\tdefer cancel()\n\n\targs := []string{\"-c\", \"core.quotepath=false\", \"show\", \"--end-of-options\", fr.Ref + \":\" + path}\n\tif fr.Runner != nil {\n\t\toutput, err := fr.Runner.Output(ctx, fr.RepoDir, args...)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"git show %s:%s: %w\", fr.Ref, path, err)\n\t\t}\n\t\treturn string(output), nil\n\t}\n\n\tcmd := exec.CommandContext(ctx, \"git\", args...)\n\tcmd.Dir = fr.RepoDir\n\toutput, err := cmd.Output()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"git show %s:%s: %w\", fr.Ref, path, err)\n\t}\n\treturn string(output), nil\n}\n\n// ReadLines returns a window of lines from the file plus the total line count.\n// startLine is 1-based; maxLines is the maximum number of lines to collect.\nfunc (fr *FileReader) ReadLines(ctx context.Context, path string, startLine, maxLines int) ([]string, int, error) {\n\tswitch fr.Mode {\n\tcase ModeWorkspace:","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/internal/tool/filereader.go#L108-L144","documentation":"Wraps a failure from `git show <Ref>:<path>` executed through the injected gitcmd.Runner in readFromGitShow (internal/tool/filereader.go:126). FileReader.Read uses this in ModeRange/ModeCommit to fetch file content at a specific ref. The wrapped error typically comes from git itself: unknown ref, path not present at that ref, or a non-repository working directory. The Runner branch runs when fr.Runner != nil.","triggerScenarios":"FileReader.Read(ctx, path) with ModeRange or ModeCommit and a non-nil Runner, when the git subprocess exits non-zero: bad ref (e.g. wrong --to or --commit value), path that did not exist at that ref, RepoDir not a git repo, context timeout (30s), or git binary missing/unusable through the Runner.","commonSituations":"Passing a short branch name that does not exist locally after a shallow/partial clone; reading a file added in the working tree but absent at the reviewed commit; reviewing a commit that was garbage-collected or belongs to another remote not fetched; running the tool outside a git repository.","solutions":["Verify the ref exists: run `git cat-file -t <ref>` in RepoDir; fetch missing refs with `git fetch origin <ref>`.","Confirm the file exists at that ref: `git show <ref>:<path>` manually; if not, use the correct historical path or a different ref.","Ensure FileReader.RepoDir points at the root of a valid git repository.","If the context timed out, check for a hung git (credential prompt, lock file) and increase the timeout or pre-authenticate.","Check Runner configuration (git binary path, env) if the same command works manually."],"exampleFix":"// before: ref not fetched locally\nfr := &FileReader{Ref: \"origin/feature\", Mode: ModeRange, RepoDir: dir, Runner: r}\n// after: fetch the ref first\nexec.Command(\"git\", \"-C\", dir, \"fetch\", \"origin\", \"feature\").Run()","handlingStrategy":"validation","validationCode":"// validate ref and path before calling Read\nif err := exec.Command(\"git\", \"-C\", repoDir, \"cat-file\", \"-e\", ref+\":\"+path).Run(); err != nil {\n    // file does not exist at ref: skip or fix ref/path beforehand\n}\nif err := exec.Command(\"git\", \"-C\", repoDir, \"rev-parse\", \"--verify\", ref+\"^{commit}\").Run(); err != nil {\n    // ref unknown: fetch it first\n}","typeGuard":"func isGitShowErr(err error) bool {\n    return err != nil && strings.HasPrefix(err.Error(), \"git show \")\n}","tryCatchPattern":"content, err := fr.Read(ctx, path)\nif err != nil {\n    var ee *exec.ExitError\n    if isGitShowErr(err) && errors.As(err, &ee) {\n        // ref/path missing at ref: degrade gracefully\n        return placeholderForMissingFile(path)\n    }\n    return err\n}","preventionTips":["Fetch all reviewed refs before reading (--to/--commit targets included)","Validate ref+path with git cat-file -e before Read","Run the tool inside a complete (non-shallow) clone of the relevant history","Set fr.Runner to a configured gitcmd.Runner for consistent git handling"],"tags":["git","subprocess","git-show","ref-not-found"],"backgroundTag":"git-show-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}