Skip to content

Commit a63ca1c

Browse files
committed
Merge branch 'pr-53-find-git-dir'
2 parents 2124b2e + b7dfe39 commit a63ca1c

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

mgitstatus

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ ID="$(id -n -u)"
168168
# infinitely deep
169169
FIND_OPTS=""
170170
if [ "$DEPTH" -ne 0 ]; then
171+
# 1 lvl deeper since we're searching for the .git directory inside the project
172+
((DEPTH=$DEPTH+1))
171173
FIND_OPTS="$FIND_OPTS -maxdepth $DEPTH"
172174
fi
173175
if [ "$NO_DEPTH" -eq 1 ]; then
@@ -178,15 +180,30 @@ if [ "$NO_DEPTH" -eq 1 ]; then
178180
fi
179181

180182

181-
# Go through positional arguments (DIRs) or '.' if no argumnets are given
183+
find_git_work_tree()(
184+
cd "$1"
185+
likely_gitdir="$PWD"
186+
worktree="$(git rev-parse --show-toplevel 2>/dev/null)"
187+
if [ -z "$worktree" ]; then
188+
# GIT_DIR doesn't know; guess:
189+
cd ..
190+
echo "$PWD"
191+
else
192+
# git told us, so we'll trust it
193+
echo "$worktree"
194+
return 0
195+
fi
196+
)
197+
198+
# Go through positional arguments (DIRs) or '.' if no arguments are given
182199
for DIR in "${@:-"."}"; do
183200
# We *want* to expand parameters, so disable shellcheck for this error:
184201
# shellcheck disable=SC2086
185-
find -L "$DIR" $FIND_OPTS -type d | while read -r PROJ_DIR
202+
find -L "$DIR" $FIND_OPTS -type d -name "*.git" -prune | while read -r GIT_DIR
186203
do
187-
GIT_DIR="$PROJ_DIR/.git"
188-
GIT_CONF="$PROJ_DIR/.git/config"
189-
204+
PROJ_DIR="$(find_git_work_tree "$GIT_DIR")"
205+
GIT_CONF="$GIT_DIR/config"
206+
190207
# Check if the repo is safe (https://github.blog/2022-04-12-git-security-vulnerability-announced/)
191208
if [ -d "$GIT_DIR" ]; then
192209
GIT_DIR_OWNER="$(ls -ld "$GIT_DIR" | awk 'NR==1 {print $3}')"
@@ -220,11 +237,11 @@ for DIR in "${@:-"."}"; do
220237

221238
# Do a 'git fetch' if requested
222239
if [ "$DO_FETCH" -eq 1 ]; then
223-
git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" fetch -q >/dev/null
240+
git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" fetch -q >/dev/null
224241
fi
225242

226243
# Refresh the index, or we might get wrong results.
227-
git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" update-index -q --refresh >/dev/null 2>&1
244+
git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" update-index -q --refresh >/dev/null 2>&1
228245

229246
# Get current branch, if "-b" is specified
230247
if [ "$SHOW_CUR_BRANCH" -eq 1 ]; then
@@ -295,12 +312,10 @@ for DIR in "${@:-"."}"; do
295312
NEEDS_UPSTREAM_BRANCHES=$(printf "$NEEDS_UPSTREAM_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
296313

297314
# Find out if there are unstaged, uncommitted or untracked changes
298-
UNSTAGED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-index --quiet HEAD -- 2>/dev/null; echo $?)
299-
UNCOMMITTED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-files --quiet --ignore-submodules --; echo $?)
300-
UNTRACKED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" ls-files --exclude-standard --others)
301-
cd "$(dirname "$GIT_DIR")" || exit
302-
STASHES=$(git stash list | wc -l)
303-
cd "$OLDPWD" || exit
315+
UNSTAGED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" diff-index --quiet HEAD -- 2>/dev/null; echo $?)
316+
UNCOMMITTED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" diff-files --quiet --ignore-submodules --; echo $?)
317+
UNTRACKED=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" ls-files --exclude-standard --others)
318+
STASHES=$(git --work-tree "$PROJ_DIR" --git-dir "$GIT_DIR" stash list | wc -l)
304319

305320
[ $DEBUG -eq 1 ] && echo "UNSTAGED: $UNSTAGED"
306321
[ $DEBUG -eq 1 ] && echo "UNCOMMITTED: $UNCOMMITTED"

0 commit comments

Comments
 (0)