Skip to content

Commit

Permalink
;bin: git: make ensure_git more robust
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
larsks committed Jan 2, 2025
1 parent 4c46502 commit c911f9c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions bin/hledger-git
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c911f9c

Please sign in to comment.