Skip to content

Commit cee0800

Browse files
committed
Improve handling of temp files
1 parent 22c687c commit cee0800

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

git-fmt-diff

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# SPDX-License-Identifier: MIT
44
# Copyright 2023 Jorengarenar
55

6+
# globals {{{
7+
68
gc_prog_name="fmt-diff"
79

810
g_obj1=
@@ -17,6 +19,8 @@ gf_debug=
1719
gf_no_term=
1820
gf_use_color=
1921

22+
# }}}
23+
2024
# helpers {{{1
2125

2226
abspath () (
@@ -45,6 +49,16 @@ pat_list_to_case () (
4549
sed 's/\s\+/|/g'
4650
)
4751

52+
rm_ () (
53+
[ "$gf_debug" = 1 ] && return
54+
if [ "$1" = "-d" ]; then
55+
shift
56+
rmdir "$@"
57+
else
58+
rm "$@"
59+
fi
60+
)
61+
4862
sed_esc () (
4963
if [ "$1" = "-p" ]; then
5064
printf '%s\n' "$2" | sed -e 's/[]\/$*.^[]/\\&/g'
@@ -53,6 +67,12 @@ sed_esc () (
5367
fi
5468
)
5569

70+
setup_env () {
71+
TMPDIR="${TMPDIR:-/tmp}"/git-"$gc_prog_name"
72+
mkdir -p "$TMPDIR"
73+
trap 'rm_ -d "$TMPDIR" 2> /dev/null' EXIT
74+
}
75+
5676
warn () (
5777
>&2 printf "%s: " "$(basename "$0")"
5878
# printf "\033[30m\033[103m"
@@ -254,30 +274,32 @@ git_changes_formatted () (
254274
old_fmt="$5"
255275

256276
merge () { git merge-file -p "$@"; }
277+
diff_fmt="$tempdir"/diff_fmt
257278
temp="$tempdir"/temp
258279

259280
if git diff -s "$cur_fmt" "$old_fmt"; then
260281
merge --our "$cur" "$old" "$cur_fmt" > "$temp"
261282
merge "$cur" "$temp" "$cur_fmt"
262-
[ "$gf_debug" != 1 ] && rm "$temp"
283+
rm_ "$temp"
263284
return
264285
fi
265286

266-
fmtcng="$tempdir"/fmtcng
267-
merge --their "$old" "$old_fmt" "$cur_fmt" > "$fmtcng"
287+
merge --their "$old" "$old_fmt" "$cur_fmt" > "$diff_fmt"
268288

269-
merge --our "$fmtcng" "$old" "$cur" > "$temp"
270-
if ! git diff -s "$fmtcng" "$temp"; then
271-
merge --their "$cur" "$fmtcng" "$cur_fmt" > "$temp"
272-
merge --their "$cur" "$temp" "$cur_fmt" > "$fmtcng"
289+
merge --our "$diff_fmt" "$old" "$cur" > "$temp"
290+
if ! git diff -s "$diff_fmt" "$temp"; then
291+
merge --their "$cur" "$diff_fmt" "$cur_fmt" > "$temp"
292+
merge --their "$cur" "$temp" "$cur_fmt" > "$diff_fmt"
273293
fi
274294

275295
merge --our "$old" "$old_fmt" "$cur" > "$temp"
276-
if ! git diff -s "$fmtcng" "$temp"; then
277-
cat "$fmtcng"
296+
if ! git diff -s "$diff_fmt" "$temp"; then
297+
echo "$diff_fmt"
298+
else
299+
rm_ "$diff_fmt"
278300
fi
279301

280-
[ "$gf_debug" != 1 ] && rm "$fmtcng" "$temp"
302+
rm_ "$temp"
281303
)
282304

283305
git_diff () (
@@ -344,7 +366,6 @@ process_file () (
344366
old="$tempdir"/old
345367
cur_fmt="$tempdir"/cur_fmt
346368
old_fmt="$tempdir"/old_fmt
347-
fmt="$tempdir"/fmt
348369

349370
git_retrieve_file_from_obj "$obj1" "$old_name" > "$old"
350371
git_retrieve_file_from_obj "$obj2" "$cur_name" > "$cur"
@@ -357,20 +378,22 @@ process_file () (
357378
if ! git diff -s "$cur" "$cur_fmt"; then
358379
sh -c "$formatter" < "$old" > "$old_fmt" 2> /dev/null
359380

360-
git_changes_formatted "$tempdir" "$cur" "$old" "$cur_fmt" "$old_fmt" > "$fmt"
381+
diff_fmt="$(git_changes_formatted \
382+
"$tempdir" \
383+
"$cur" "$old" "$cur_fmt" "$old_fmt" \
384+
)"
361385

362-
if [ -s "$fmt" ]; then
363-
git_diff "$cur" "$fmt" "$cur_name"
386+
if [ -n "$diff_fmt" ]; then
387+
git_diff "$cur" "$diff_fmt" "$cur_name"
388+
rm_ "$diff_fmt"
364389
fi
365390

366-
[ "$gf_debug" != 1 ] && rm "$old_fmt" "$fmt"
391+
rm_ "$old_fmt"
367392
fi
368393

369394
debug "$cur_name => $tempdir"
370-
if [ "$gf_debug" != 1 ]; then
371-
rm "$old" "$cur" "$cur_fmt"
372-
rmdir "$tempdir"
373-
fi
395+
rm_ "$old" "$cur" "$cur_fmt"
396+
rm_ -d "$tempdir"
374397
)
375398

376399
# running {{{1
@@ -475,6 +498,7 @@ main () (
475498
obj2="$2"
476499

477500
outfile="$(mktemp)"
501+
debug "OUTFILE: $outfile"
478502

479503
files="$(list_files "$obj1" "$obj2")"
480504

@@ -489,10 +513,11 @@ main () (
489513

490514
sh -c "$(git_conf_get pager)" < "$outfile"
491515

492-
rm "$outfile"
516+
rm_ "$outfile"
493517
)
494518

495519
# }}}1
496520

521+
setup_env
497522
parse_options "$@"
498523
main "$g_obj1" "$g_obj2"

0 commit comments

Comments
 (0)