{"record":{"id":"c58ac722ce7eaa6d","repo":"plandex-ai/plandex","slug":"error-getting-current-git-branch-for-dir-s-err","errorCode":null,"errorMessage":"error getting current git branch for dir: %s, err: %v","messagePattern":"error getting current git branch for dir: (.+?), err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/git.go","lineNumber":441,"sourceCode":"\t}\n\treturn nil\n\n}\n\nfunc gitCheckoutBranch(repoDir, branch string) error {\n\tlog.Printf(\"[Git] gitCheckoutBranch - repoDir: %s, branch: %s\", repoDir, branch)\n\tif err := gitRemoveIndexLockFileIfExists(repoDir); err != nil {\n\t\treturn fmt.Errorf(\"error removing lock file before checkout: %v\", err)\n\t}\n\n\t// get current branch and only checkout if it's not the same\n\t// trying to check out the same branch will result in an error\n\tvar out bytes.Buffer\n\tcmd := exec.Command(\"git\", \"-C\", repoDir, \"branch\", \"--show-current\")\n\tcmd.Stdout = &out\n\terr := cmd.Run()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error getting current git branch for dir: %s, err: %v\", repoDir, err)\n\t}\n\n\tcurrentBranch := strings.TrimSpace(out.String())\n\tlog.Printf(\"[Git] gitCheckoutBranch - currentBranch: %s\", currentBranch)\n\n\tif currentBranch == branch {\n\t\tlog.Printf(\"[Git] gitCheckoutBranch - already on branch %s, skipping\", branch)\n\t\treturn nil\n\t}\n\n\tlog.Println(\"[Git] gitCheckoutBranch - checking out branch:\", branch)\n\tres, err := exec.Command(\"git\", \"-C\", repoDir, \"checkout\", branch).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error checking out git branch for dir: %s, err: %v, output: %s\", repoDir, err, string(res))\n\t}\n\treturn nil\n}\n","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/git.go#L423-L459","documentation":"gitCheckoutBranch first queries the currently checked-out branch with `git -C repoDir branch --show-current` so it can no-op when the target branch is already active (checking out the same branch would error). If that query command itself fails to run or exits non-zero, this error wraps the directory and underlying error. Callers include lockRepoDB, so this can block repo locking flows.","triggerScenarios":"exec of `git branch --show-current` fails: git not installed/not on PATH, repoDir does not exist or is not a git repo, permission denied, or the process environment cannot exec git (missing libs, sandbox).","commonSituations":"Detached HEAD state (show-current prints empty — not an error, but check surrounding logic); repo directory deleted or moved; git binary missing in container images; path permission issues after volume remounts.","solutions":["Run `git -C <repoDir> branch --show-current` manually as the server user to see the raw error.","Verify repoDir exists and contains .git; recreate or repair the repo if missing.","Install/repair git and ensure PATH for the service process includes it.","Note: on a detached HEAD this command exits 0 with empty output — if you also see [367]-style empty results, handle the empty currentBranch case rather than assuming a failure."],"exampleFix":"// before\ncmd := exec.Command(\"git\", \"-C\", repoDir, \"branch\", \"--show-current\")\ncmd.Stdout = &out\nerr := cmd.Run()\n// after (fail fast, clearer diagnostics)\nif _, err := exec.LookPath(\"git\"); err != nil {\n    return fmt.Errorf(\"git not found in PATH: %w\", err)\n}\nif _, err := os.Stat(filepath.Join(repoDir, \".git\")); err != nil {\n    return fmt.Errorf(\"not a git repo (%s): %w\", repoDir, err)\n}","handlingStrategy":"validation","validationCode":"func ensureCheckoutable(repoDir string) error {\n    if _, err := exec.LookPath(\"git\"); err != nil {\n        return fmt.Errorf(\"git not in PATH\")\n    }\n    fi, err := os.Stat(filepath.Join(repoDir, \".git\"))\n    if err != nil {\n        return fmt.Errorf(\"%s is not a git repo: %w\", repoDir, err)\n    }\n    _ = fi\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := gitCheckoutBranch(repoDir, branch); err != nil {\n    if strings.Contains(err.Error(), \"error getting current git branch\") {\n        // environment/repo problem, not a branch problem: check git + repo dir, then retry once\n        log.Printf(\"cannot query current branch in %s: %v\", repoDir, err)\n    }\n}","preventionTips":["Install git in all deployment images and verify PATH for the service user.","Assert repo directories exist and contain .git before any git operation.","Watch for detached-HEAD states: `branch --show-current` returns empty there; handle empty output explicitly.","Keep repo dirs on stable mounts with consistent ownership."],"tags":["git","branch","exec"],"backgroundTag":"git-command-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}