Skip to content

Commit a46af59

Browse files
committed
Merge branch 'da/mergetool-temporary-directory'
Allow a temporary directory specified to be used while running "git mergetool" backend. * da/mergetool-temporary-directory: t7610-mergetool: add test cases for mergetool.writeToTemp mergetool: add an option for writing to a temporary directory
2 parents e96e98b + 688684e commit a46af59

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,12 @@ mergetool.keepTemporaries::
17681768
preserved, otherwise they will be removed after the tool has
17691769
exited. Defaults to `false`.
17701770

1771+
mergetool.writeToTemp::
1772+
Git writes temporary 'BASE', 'LOCAL', and 'REMOTE' versions of
1773+
conflicting files in the worktree by default. Git will attempt
1774+
to use a temporary directory for these files when set `true`.
1775+
Defaults to `false`.
1776+
17711777
mergetool.prompt::
17721778
Prompt before each invocation of the merge resolution program.
17731779

git-mergetool.sh

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ base_present () {
3737
test -n "$base_mode"
3838
}
3939

40+
mergetool_tmpdir_init () {
41+
if test "$(git config --bool mergetool.writeToTemp)" != true
42+
then
43+
MERGETOOL_TMPDIR=.
44+
return 0
45+
fi
46+
if MERGETOOL_TMPDIR=$(mktemp -d -t "git-mergetool-XXXXXX" 2>/dev/null)
47+
then
48+
return 0
49+
fi
50+
die "error: mktemp is needed when 'mergetool.writeToTemp' is true"
51+
}
52+
4053
cleanup_temp_files () {
4154
if test "$1" = --save-backup
4255
then
@@ -46,6 +59,10 @@ cleanup_temp_files () {
4659
else
4760
rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
4861
fi
62+
if test "$MERGETOOL_TMPDIR" != "."
63+
then
64+
rmdir "$MERGETOOL_TMPDIR"
65+
fi
4966
}
5067

5168
describe_file () {
@@ -235,10 +252,20 @@ merge_file () {
235252
BASE=$MERGED
236253
ext=
237254
fi
238-
BACKUP="./${BASE}_BACKUP_$$$ext"
239-
LOCAL="./${BASE}_LOCAL_$$$ext"
240-
REMOTE="./${BASE}_REMOTE_$$$ext"
241-
BASE="./${BASE}_BASE_$$$ext"
255+
256+
mergetool_tmpdir_init
257+
258+
if test "$MERGETOOL_TMPDIR" != "."
259+
then
260+
# If we're using a temporary directory then write to the
261+
# top-level of that directory.
262+
BASE=${BASE##*/}
263+
fi
264+
265+
BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
266+
LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
267+
REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
268+
BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
242269

243270
base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
244271
local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')

t/t7610-mergetool.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,27 @@ test_expect_success 'custom commands override built-ins' '
514514
git reset --hard master >/dev/null 2>&1
515515
'
516516

517+
test_expect_success 'filenames seen by tools start with ./' '
518+
git checkout -b test15 branch1 &&
519+
test_config mergetool.writeToTemp false &&
520+
test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
521+
test_config mergetool.myecho.trustExitCode true &&
522+
test_must_fail git merge master &&
523+
git mergetool --no-prompt --tool myecho -- both >actual &&
524+
grep ^\./both_LOCAL_ actual >/dev/null &&
525+
git reset --hard master >/dev/null 2>&1
526+
'
527+
528+
test_expect_success 'temporary filenames are used with mergetool.writeToTemp' '
529+
git checkout -b test16 branch1 &&
530+
test_config mergetool.writeToTemp true &&
531+
test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
532+
test_config mergetool.myecho.trustExitCode true &&
533+
test_must_fail git merge master &&
534+
git mergetool --no-prompt --tool myecho -- both >actual &&
535+
test_must_fail grep ^\./both_LOCAL_ actual >/dev/null &&
536+
grep /both_LOCAL_ actual >/dev/null &&
537+
git reset --hard master >/dev/null 2>&1
538+
'
539+
517540
test_done

0 commit comments

Comments
 (0)