From e67abd55be81e92f439db7dc439359bde9ab08a3 Mon Sep 17 00:00:00 2001 From: Shreyas Joshi Date: Wed, 17 Jun 2026 22:40:08 +0530 Subject: [PATCH 1/2] fix: repository detection from subdirectories Replace os.Stat('.git') with git rev-parse --is-inside-work-tree so lrc hooks install/uninstall --local works from any subdirectory. Rename IsGitRepositoryCurrentDir to IsGitRepository to match function purpose. Add E2E subdirectory tests. --- gitops/config.go | 7 ++-- internal/appcore/hooks_management.go | 2 +- issue.md | 53 ++++++++++++++++++++++++++++ tests/worktree-hooks.sh | 24 +++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 issue.md diff --git a/gitops/config.go b/gitops/config.go index 5abb0d3..9c83a5f 100644 --- a/gitops/config.go +++ b/gitops/config.go @@ -1,13 +1,14 @@ package gitops import ( - "os" "os/exec" "strings" ) -func IsGitRepositoryCurrentDir() bool { - _, err := os.Stat(".git") +// IsGitRepository checks if the current working directory is inside a Git repository. +func IsGitRepository() bool { + cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree") + err := cmd.Run() return err == nil } diff --git a/internal/appcore/hooks_management.go b/internal/appcore/hooks_management.go index aa08b56..eb64d97 100644 --- a/internal/appcore/hooks_management.go +++ b/internal/appcore/hooks_management.go @@ -716,7 +716,7 @@ func runHooksStatus(c *cli.Context) error { // isGitRepository checks if current directory is in a git repository func isGitRepository() bool { - return gitops.IsGitRepositoryCurrentDir() + return gitops.IsGitRepository() } // installHook installs or updates a hook with lrc managed section diff --git a/issue.md b/issue.md new file mode 100644 index 0000000..e91db27 --- /dev/null +++ b/issue.md @@ -0,0 +1,53 @@ +### Pre-flight checks + +- [x] I searched existing issues and discussions. +- [x] I read CONTRIBUTING.md. +- [x] This is not a private security vulnerability report. + +--- + +### Current behavior + +When running `lrc hooks install --local` or `lrc hooks uninstall --local` from a nested subdirectory of a Git repository, the CLI exits with: + +``` +Error: not in a git repository (no .git directory found) +``` + +Developers must be at the repository root containing `.git` to manage hooks locally, which breaks the expected workflow when working in subdirectories. + +### Expected behavior + +`git-lrc` should detect the enclosing Git repository when invoked from any subdirectory. `lrc hooks install --local` and `lrc hooks uninstall --local` should succeed when run from any path inside a Git worktree. + +### Steps to reproduce + +1. Build and install: `make build-local && lrc hooks install` +2. Create a subdirectory: `mkdir -p subdir && cd subdir` +3. Try local install: `lrc hooks install --local` +4. Observe: + + ``` + Error: not in a git repository (no .git directory found) + ``` + +### Environment + +- lrc version: Dev / main +- OS: Linux +- Shell: bash +- Git version: any + +### Additional context + +**Root cause** + +The function `IsGitRepositoryCurrentDir()` in `gitops/config.go` checks for `.git` using `os.Stat(".git")`, which only looks at the current working directory. When invoked from a subdirectory, `.git` is not found even though the directory is inside a valid Git worktree. + +**Naming bug** + +The name says "check current dir for .git" but the actual purpose is "check whether we are inside any Git repository." Per project convention (AGENTS.md): names must match function meaning. + +**Proposed fix** + +Replace `os.Stat(".git")` with `git rev-parse --is-inside-work-tree`, rename `IsGitRepositoryCurrentDir` → `IsGitRepository`, and drop the unused `"os"` import. diff --git a/tests/worktree-hooks.sh b/tests/worktree-hooks.sh index ad9a5f1..14cfe8f 100644 --- a/tests/worktree-hooks.sh +++ b/tests/worktree-hooks.sh @@ -272,6 +272,30 @@ for hook in pre-commit prepare-commit-msg commit-msg post-commit; do assert_file_not_contains_or_missing "local worktree uninstall removes managed section: $hook" "# BEGIN lrc managed section - DO NOT EDIT" "$WT_LOCAL_HOOKS_DIR/$hook" done +bold "" +bold "══ Local Hook Management In Subdirectories ════════════════════" + +mkdir -p "$LOCAL_REPO_DIR/subdir" +cd "$LOCAL_REPO_DIR/subdir" + +lrc hooks install --local >/dev/null +STATUS_OUTPUT_SUBDIR="$(lrc hooks status 2>&1)" + +for hook in pre-commit prepare-commit-msg commit-msg post-commit; do + assert_file_contains "subdirectory install adds managed section: $hook" "# BEGIN lrc managed section - DO NOT EDIT" "$WT_LOCAL_HOOKS_DIR/$hook" +done + +assert_contains "subdirectory status reports common-dir hooks path" "hooksPath: $WT_LOCAL_HOOKS_DIR" "$STATUS_OUTPUT_SUBDIR" +assert_contains "subdirectory status reports worktree root" "repo: $LOCAL_REPO_DIR" "$STATUS_OUTPUT_SUBDIR" + +lrc hooks uninstall --local >/dev/null + +for hook in pre-commit prepare-commit-msg commit-msg post-commit; do + assert_file_not_contains_or_missing "subdirectory uninstall removes managed section: $hook" "# BEGIN lrc managed section - DO NOT EDIT" "$WT_LOCAL_HOOKS_DIR/$hook" +done + +cd "$LOCAL_REPO_DIR" + bold "" bold "══ Results ═══════════════════════════════════════════════════" TOTAL=$((PASS + FAIL)) From 0edc2874cfc1b96c40063b4fc8b3bd3bea9d9c95 Mon Sep 17 00:00:00 2001 From: Shreyas Joshi Date: Wed, 17 Jun 2026 22:42:37 +0530 Subject: [PATCH 2/2] remove issue.md from repo (PR collateral) --- issue.md | 53 ----------------------------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 issue.md diff --git a/issue.md b/issue.md deleted file mode 100644 index e91db27..0000000 --- a/issue.md +++ /dev/null @@ -1,53 +0,0 @@ -### Pre-flight checks - -- [x] I searched existing issues and discussions. -- [x] I read CONTRIBUTING.md. -- [x] This is not a private security vulnerability report. - ---- - -### Current behavior - -When running `lrc hooks install --local` or `lrc hooks uninstall --local` from a nested subdirectory of a Git repository, the CLI exits with: - -``` -Error: not in a git repository (no .git directory found) -``` - -Developers must be at the repository root containing `.git` to manage hooks locally, which breaks the expected workflow when working in subdirectories. - -### Expected behavior - -`git-lrc` should detect the enclosing Git repository when invoked from any subdirectory. `lrc hooks install --local` and `lrc hooks uninstall --local` should succeed when run from any path inside a Git worktree. - -### Steps to reproduce - -1. Build and install: `make build-local && lrc hooks install` -2. Create a subdirectory: `mkdir -p subdir && cd subdir` -3. Try local install: `lrc hooks install --local` -4. Observe: - - ``` - Error: not in a git repository (no .git directory found) - ``` - -### Environment - -- lrc version: Dev / main -- OS: Linux -- Shell: bash -- Git version: any - -### Additional context - -**Root cause** - -The function `IsGitRepositoryCurrentDir()` in `gitops/config.go` checks for `.git` using `os.Stat(".git")`, which only looks at the current working directory. When invoked from a subdirectory, `.git` is not found even though the directory is inside a valid Git worktree. - -**Naming bug** - -The name says "check current dir for .git" but the actual purpose is "check whether we are inside any Git repository." Per project convention (AGENTS.md): names must match function meaning. - -**Proposed fix** - -Replace `os.Stat(".git")` with `git rev-parse --is-inside-work-tree`, rename `IsGitRepositoryCurrentDir` → `IsGitRepository`, and drop the unused `"os"` import.