{"record":{"id":"103dbd5ddc7355f7","repo":"alibaba/open-code-review","slug":"s-is-not-a-git-repository","errorCode":null,"errorMessage":"%s is not a git repository","messagePattern":"(.+?) is not a git repository","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/shared.go","lineNumber":151,"sourceCode":"func resolveWorkingDir(input string, requireGit bool) (string, bool, error) {\n\tif input == \"\" {\n\t\twd, err := os.Getwd()\n\t\tif err != nil {\n\t\t\treturn \"\", false, fmt.Errorf(\"get working directory: %w\", err)\n\t\t}\n\t\tinput = wd\n\t}\n\tabsPath, err := filepath.Abs(input)\n\tif err != nil {\n\t\treturn \"\", false, fmt.Errorf(\"resolve absolute path: %w\", err)\n\t}\n\tif _, statErr := os.Stat(absPath); statErr != nil {\n\t\treturn \"\", false, fmt.Errorf(\"stat %s: %w\", absPath, statErr)\n\t}\n\tout, err := runGitCmd(absPath, \"rev-parse\", \"--git-dir\")\n\tisGit := err == nil && len(out) > 0\n\tif !isGit && requireGit {\n\t\treturn \"\", false, fmt.Errorf(\"%s is not a git repository\", absPath)\n\t}\n\t// #287: git reports diff and `git show HEAD:<path>` paths relative to the\n\t// repository root, not the current directory. When `ocr review` runs from a\n\t// subdirectory of a monorepo, anchor RepoDir at the git top-level so those\n\t// root-relative paths resolve for both disk reads and git-show reads.\n\t// requireGit is true only for the review path; scan (requireGit=false) keeps\n\t// the CWD so its `git ls-files` walk stays scoped to the subdirectory.\n\tif isGit && requireGit {\n\t\t// runGitCmdStdout captures stdout only so git stderr notices can't\n\t\t// pollute the resolved path. --show-toplevel fails (or is empty) when\n\t\t// there is no work tree — e.g. a bare repo, where --git-dir succeeds so\n\t\t// isGit is true. Fail loudly there instead of silently reusing the\n\t\t// subdir, which would reproduce the #287 root-relative-path bug.\n\t\ttop, topErr := runGitCmdStdout(absPath, \"rev-parse\", \"--show-toplevel\")\n\t\tt := strings.TrimSpace(string(top))\n\t\tif topErr != nil || t == \"\" {\n\t\t\treturn \"\", false, fmt.Errorf(\"%s is a git repository without a work tree (bare repo?); cannot resolve its top level for review\", absPath)\n\t\t}","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/shared.go#L133-L169","documentation":"When the target directory exists but `git rev-parse --git-dir` fails (or returns nothing) and requireGit is true, resolveWorkingDir rejects it with `<path> is not a git repository`. The review path requires a git repo because it sources diffs and HEAD content via git; the scan path (requireGit=false) tolerates non-repos instead.","triggerScenarios":"Running `ocr review` in a plain directory with no .git, inside a .git directory itself, in a worktree with corrupt git metadata, or with GIT_DIR misconfigured so rev-parse fails.","commonSituations":"Forgotten clone/downloaded ZIP of a repo (no .git); running from a random project folder; corrupted .git after a failed checkout; CI artifact extraction without git metadata; GIT_DIR/GIT_WORK_TREE env overrides pointing elsewhere.","solutions":["Run ocr review from inside a cloned git repository, or pass --dir pointing at one","If the code came from a ZIP, git clone it instead so .git exists","Check and unset stray GIT_DIR/GIT_WORK_TREE environment variables","Repair or re-clone the repository if .git is corrupted"],"exampleFix":"// before\ncd ~/downloads/myrepo-zip && ocr review   # no .git\n// after\ngit clone https://host/myrepo.git && cd myrepo && ocr review","handlingStrategy":"validation","validationCode":"out, err := exec.Command(\"git\", \"-C\", dir, \"rev-parse\", \"--git-dir\").Output()\nif err != nil || len(bytes.TrimSpace(out)) == 0 {\n\treturn fmt.Errorf(\"%s is not a git repository; clone it first\", dir)\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n\tif strings.Contains(err.Error(), \"not a git repository\") {\n\t\t// guide user to git clone instead of failing blind\n\t}\n\treturn err\n}","preventionTips":["Never review code extracted from ZIP archives without .git","Unset GIT_DIR/GIT_WORK_TREE overrides before running","Clone repositories rather than copying working trees"],"tags":["go","git","cli"],"backgroundTag":"not-a-git-repository","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}