git-p4: correct --prepare-p4-only instructions
[gitweb.git] / git-mergetool.sh
index 96a61ba6f470d7634552a42547de57f63fae8d81..ff050e58ff3b752113105ba3ea5e20c6b633aae9 100755 (executable)
 
 USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [file to merge] ...'
 SUBDIRECTORY_OK=Yes
+NONGIT_OK=Yes
 OPTIONS_SPEC=
 TOOL_MODE=merge
 . git-sh-setup
 . git-mergetool--lib
-require_work_tree
 
 # Returns true if the mode reflects a symlink
 is_symlink () {
@@ -37,6 +37,19 @@ base_present () {
        test -n "$base_mode"
 }
 
+mergetool_tmpdir_init () {
+       if test "$(git config --bool mergetool.writeToTemp)" != true
+       then
+               MERGETOOL_TMPDIR=.
+               return 0
+       fi
+       if MERGETOOL_TMPDIR=$(mktemp -d -t "git-mergetool-XXXXXX" 2>/dev/null)
+       then
+               return 0
+       fi
+       die "error: mktemp is needed when 'mergetool.writeToTemp' is true"
+}
+
 cleanup_temp_files () {
        if test "$1" = --save-backup
        then
@@ -46,6 +59,10 @@ cleanup_temp_files () {
        else
                rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
        fi
+       if test "$MERGETOOL_TMPDIR" != "."
+       then
+               rmdir "$MERGETOOL_TMPDIR"
+       fi
 }
 
 describe_file () {
@@ -235,10 +252,20 @@ merge_file () {
                BASE=$MERGED
                ext=
        fi
-       BACKUP="./${BASE}_BACKUP_$$$ext"
-       LOCAL="./${BASE}_LOCAL_$$$ext"
-       REMOTE="./${BASE}_REMOTE_$$$ext"
-       BASE="./${BASE}_BASE_$$$ext"
+
+       mergetool_tmpdir_init
+
+       if test "$MERGETOOL_TMPDIR" != "."
+       then
+               # If we're using a temporary directory then write to the
+               # top-level of that directory.
+               BASE=${BASE##*/}
+       fi
+
+       BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
+       LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
+       REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
+       BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
 
        base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
        local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
@@ -327,6 +354,10 @@ guessed_merge_tool=false
 while test $# != 0
 do
        case "$1" in
+       --tool-help=*)
+               TOOL_MODE=${1#--tool-help=}
+               show_tool_help
+               ;;
        --tool-help)
                show_tool_help
                ;;
@@ -378,6 +409,9 @@ prompt_after_failed_merge () {
        done
 }
 
+git_dir_init
+require_work_tree
+
 if test -z "$merge_tool"
 then
        # Check if a merge tool has been configured