{"record":{"id":"9afbbd433b99df64","repo":"mislav/hub","slug":"unable-to-determine-git-working-directory","errorCode":null,"errorMessage":"unable to determine git working directory","messagePattern":"unable to determine git working directory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"git/git.go","lineNumber":74,"sourceCode":"\t\tgitDir, err = filepath.Abs(gitDir)\n\t\tif err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\n\t\tgitDir = filepath.Clean(gitDir)\n\t}\n\n\tcachedDir = gitDir\n\treturn gitDir, nil\n}\n\nfunc WorkdirName() (string, error) {\n\ttoplevelCmd := gitCmd(\"rev-parse\", \"--show-toplevel\")\n\ttoplevelCmd.Stderr = nil\n\toutput, err := toplevelCmd.Output()\n\tdir := firstLine(output)\n\tif dir == \"\" {\n\t\treturn \"\", fmt.Errorf(\"unable to determine git working directory\")\n\t}\n\treturn dir, err\n}\n\nfunc HasFile(segments ...string) bool {\n\t// The blessed way to resolve paths within git dir since Git 2.5.0\n\tpathCmd := gitCmd(\"rev-parse\", \"-q\", \"--git-path\", filepath.Join(segments...))\n\tpathCmd.Stderr = nil\n\tif output, err := pathCmd.Output(); err == nil {\n\t\tif lines := outputLines(output); len(lines) == 1 {\n\t\t\tif _, err := os.Stat(lines[0]); err == nil {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t}\n\n\t// Fallback for older git versions\n\tdir, err := Dir()","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/mislav/hub/blob/5c547ed804368763064e51f3990851e267e88edd/git/git.go#L56-L92","documentation":"WorkdirName() runs `git rev-parse --show-toplevel` and throws when the command's first output line is empty, meaning no top-level working directory could be determined. Note the underlying err is still returned alongside this sentinel, and rev-parse also fails outside a repository.","triggerScenarios":"Calling git.WorkdirName() (via create, createIssue, pullRequest, transformRemoteArgs) outside a git repository, or in odd contexts where `--show-toplevel` prints nothing (e.g. bare repo, or in a .git directory itself where toplevel is unavailable).","commonSituations":"Running inside a bare repository; executing from within the .git directory; running in a non-repo directory; Windows/UTF-8 locale quirks historically made --show-toplevel fail on non-ASCII paths.","solutions":["Run from inside a regular (non-bare) git work tree","Do not run the tool from inside the .git directory itself","Verify with `git rev-parse --show-toplevel` manually","Check the accompanying err return for the underlying cause"],"exampleFix":"// before\ndir, err := git.WorkdirName()\n// after\ndir, err := git.WorkdirName()\nif err != nil {\n    return fmt.Errorf(\"run from inside a git work tree: %w\", err)\n}\nif dir == \"\" {\n    return fmt.Errorf(\"empty workdir, not a git work tree\")\n}","handlingStrategy":"validation","validationCode":"out, err := exec.Command(\"git\", \"rev-parse\", \"--show-toplevel\").Output()\nif err != nil || len(strings.TrimSpace(string(out))) == 0 {\n    return fmt.Errorf(\"not inside a git work tree; cd into a checked-out repository\")\n}","typeGuard":null,"tryCatchPattern":"dir, err := git.WorkdirName()\nif err != nil || dir == \"\" {\n    return fmt.Errorf(\"unable to determine working directory; run inside a non-bare git work tree\")\n}","preventionTips":["Do not run the tool inside bare repos or .git directories","cd to the repo root before invoking repo-dependent operations","Verify toplevel resolution in CI before running the tool","Check the paired err return for the real cause"],"tags":["git","working-directory","repository"],"backgroundTag":"not-a-git-repository","analyzedSha":"5c547ed804368763064e51f3990851e267e88edd","analyzedAt":"2026-09-01T03:34:15.525Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}