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/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))