Skip to content

Commit a23b1fd

Browse files
author
Jorengarenar
committed
Fix running script from subdirectories
1 parent 4eba794 commit a23b1fd

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

git-fmt-diff

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ gf_use_color=
1919

2020
# helpers {{{1
2121

22+
abspath() (
23+
if [ -d "$1" ]; then
24+
cd "$1" && pwd
25+
elif [ -f "$1" ]; then
26+
case "$1" in
27+
/*) echo "$1" ;;
28+
*/*) echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" ;;
29+
*) echo "$PWD/$1" ;;
30+
esac
31+
else
32+
warn "no such file or directory"
33+
fi
34+
)
35+
2236
debug () (
2337
[ "$3" != "--" ] && [ "$gf_debug" != 1 ] && return
2438
>&2 printf "[debug] %s @ %s:%d # " "$(basename "$0")" "$1" "$2"
@@ -276,26 +290,6 @@ git_diff () (
276290
-e "s/$(sed_esc -p "b$new")/$(sed_esc -s "b/$filename")/g"
277291
)
278292

279-
git_list_changed_files () (
280-
obj1="$1"
281-
obj2="$2"
282-
283-
[ "$obj1" = "HEAD" ] && obj1=
284-
285-
if ! git rev-parse --is-inside-work-tree 2> /dev/null 1>&2; then
286-
[ -s "$obj1" ] && echo "$obj1"
287-
return
288-
fi
289-
290-
if [ -e "$obj1" ] && ! git ls-files --error-unmatch "$obj1" 2> /dev/null 1>&2; then
291-
echo "$1"
292-
return
293-
fi
294-
295-
# shellcheck disable=SC2086
296-
git diff $g_cached_opt --diff-filter=ARMC --name-status $obj1 $obj2 | cut -f2-
297-
)
298-
299293
git_retrieve_file_from_obj () (
300294
obj="$1"
301295
file="$2"
@@ -373,6 +367,26 @@ process_file () (
373367

374368
# running {{{1
375369

370+
list_files () (
371+
obj1="$1"
372+
obj2="$2"
373+
374+
[ "$obj1" = "HEAD" ] && obj1=
375+
376+
if ! git rev-parse --is-inside-work-tree 2> /dev/null 1>&2; then
377+
[ -s "$obj1" ] && abspath "$obj1"
378+
return
379+
fi
380+
381+
if [ -e "$obj1" ] && ! git ls-files --error-unmatch "$obj1" 2> /dev/null 1>&2; then
382+
abspath "$obj1"
383+
return
384+
fi
385+
386+
# shellcheck disable=SC2086
387+
git diff $g_cached_opt --diff-filter=ARMC --name-status $obj1 $obj2 | cut -f2-
388+
)
389+
376390
usage () (
377391
true # TODO
378392
)
@@ -453,7 +467,12 @@ main () (
453467

454468
outfile="$(mktemp)"
455469

456-
git_list_changed_files "$obj1" "$obj2" | while read -r line; do
470+
files="$(list_files "$obj1" "$obj2")"
471+
472+
repo_root="$(git rev-parse --show-toplevel 2> /dev/null)"
473+
[ -n "$repo_root" ] && { cd "$repo_root" || exit 1; }
474+
475+
echo "$files" | while read -r line; do
457476
debug "$line"
458477
[ -e "$obj1" ] && obj1="HEAD"
459478
process_file "$obj1" "$obj2" "$line" >> "$outfile"

0 commit comments

Comments
 (0)