From c911f9c0adfe416bff2a0cde7f51aff02c3636fe Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Thu, 2 Jan 2025 13:45:23 -0500 Subject: [PATCH] ;bin: git: make ensure_git more robust Instead of manually checking for a .git directory, ask git to check whether or not we are in a work tree using 'git rev-parse --is-inside-work-tree'. This takes care of things like being in a subdirectory of a work tree, or someone having GIT_DIR pointing somewhere other than .git, etc. --- bin/hledger-git | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bin/hledger-git b/bin/hledger-git index 3911edccfdd..16607801ae5 100755 --- a/bin/hledger-git +++ b/bin/hledger-git @@ -62,12 +62,11 @@ EOF fi } -# TODO: also look in parent directories ensure_git_repo() { - if [[ ! -d "$DIR/.git" ]]; then - $GIT init "$DIR" - echo "Created git repo in $DIR" - fi + if [[ $($GIT rev-parse --is-inside-work-tree 2> /dev/null) != true ]]; then + $GIT init "$DIR" + echo "Created git repo in $DIR" + fi } # * Commands