Skip to content

Commit 2950cbd

Browse files
committed
Merge branch 'js/mergetool-optim'
"git mergetool" and its tests now spawn fewer subprocesses. * js/mergetool-optim: mergetool: use shell variable magic instead of `awk` mergetool: dissect strings with shell variable magic instead of `expr` t7610-mergetool: use test_cmp instead of test $(cat file) = $txt t7610-mergetool: do not place pipelines headed by `yes` in subshells
2 parents 069874c + 7e6d6f7 commit 2950cbd

File tree

2 files changed

+208
-146
lines changed

2 files changed

+208
-146
lines changed

git-mergetool.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ stage_submodule () {
228228
}
229229

230230
checkout_staged_file () {
231-
tmpfile=$(expr \
232-
"$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
233-
: '\([^ ]*\) ')
231+
tmpfile="$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" &&
232+
tmpfile=${tmpfile%%' '*}
234233

235234
if test $? -eq 0 && test -n "$tmpfile"
236235
then
@@ -255,13 +254,16 @@ merge_file () {
255254
return 1
256255
fi
257256

258-
if BASE=$(expr "$MERGED" : '\(.*\)\.[^/]*$')
259-
then
260-
ext=$(expr "$MERGED" : '.*\(\.[^/]*\)$')
261-
else
257+
# extract file extension from the last path component
258+
case "${MERGED##*/}" in
259+
*.*)
260+
ext=.${MERGED##*.}
261+
BASE=${MERGED%"$ext"}
262+
;;
263+
*)
262264
BASE=$MERGED
263265
ext=
264-
fi
266+
esac
265267

266268
mergetool_tmpdir_init
267269

@@ -277,15 +279,30 @@ merge_file () {
277279
REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
278280
BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
279281

280-
base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
281-
local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
282-
remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
282+
base_mode= local_mode= remote_mode=
283+
284+
# here, $IFS is just a LF
285+
for line in $f
286+
do
287+
mode=${line%% *} # 1st word
288+
sha1=${line#"$mode "}
289+
sha1=${sha1%% *} # 2nd word
290+
case "${line#$mode $sha1 }" in # remainder
291+
'1 '*)
292+
base_mode=$mode
293+
;;
294+
'2 '*)
295+
local_mode=$mode local_sha1=$sha1
296+
;;
297+
'3 '*)
298+
remote_mode=$mode remote_sha1=$sha1
299+
;;
300+
esac
301+
done
283302

284303
if is_submodule "$local_mode" || is_submodule "$remote_mode"
285304
then
286305
echo "Submodule merge conflict for '$MERGED':"
287-
local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
288-
remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
289306
describe_file "$local_mode" "local" "$local_sha1"
290307
describe_file "$remote_mode" "remote" "$remote_sha1"
291308
resolve_submodule_merge
@@ -406,7 +423,7 @@ main () {
406423
-t|--tool*)
407424
case "$#,$1" in
408425
*,*=*)
409-
merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
426+
merge_tool=${1#*=}
410427
;;
411428
1,*)
412429
usage ;;

0 commit comments

Comments
 (0)