git-difftool--helper.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/sh
  2. # git-difftool--helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher.
  3. # This script is typically launched by using the 'git difftool'
  4. # convenience command.
  5. #
  6. # Copyright (c) 2009, 2010 David Aguilar
  7. TOOL_MODE=diff
  8. . git-mergetool--lib
  9. # difftool.prompt controls the default prompt/no-prompt behavior
  10. # and is overridden with $GIT_DIFFTOOL*_PROMPT.
  11. should_prompt () {
  12. prompt_merge=$(git config --bool mergetool.prompt || echo true)
  13. prompt=$(git config --bool difftool.prompt || echo $prompt_merge)
  14. if test "$prompt" = true
  15. then
  16. test -z "$GIT_DIFFTOOL_NO_PROMPT"
  17. else
  18. test -n "$GIT_DIFFTOOL_PROMPT"
  19. fi
  20. }
  21. # Indicates that --extcmd=... was specified
  22. use_ext_cmd () {
  23. test -n "$GIT_DIFFTOOL_EXTCMD"
  24. }
  25. launch_merge_tool () {
  26. # Merged is the filename as it appears in the work tree
  27. # Local is the contents of a/filename
  28. # Remote is the contents of b/filename
  29. # Custom merge tool commands might use $BASE so we provide it
  30. MERGED="$1"
  31. LOCAL="$2"
  32. REMOTE="$3"
  33. BASE="$1"
  34. # $LOCAL and $REMOTE are temporary files so prompt
  35. # the user with the real $MERGED name before launching $merge_tool.
  36. if should_prompt
  37. then
  38. printf "\nViewing (%s/%s): '%s'\n" "$GIT_DIFF_PATH_COUNTER" \
  39. "$GIT_DIFF_PATH_TOTAL" "$MERGED"
  40. if use_ext_cmd
  41. then
  42. printf "Launch '%s' [Y/n]? " \
  43. "$GIT_DIFFTOOL_EXTCMD"
  44. else
  45. printf "Launch '%s' [Y/n]? " "$merge_tool"
  46. fi
  47. read ans || return
  48. if test "$ans" = n
  49. then
  50. return
  51. fi
  52. fi
  53. if use_ext_cmd
  54. then
  55. export BASE
  56. eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
  57. else
  58. run_merge_tool "$merge_tool"
  59. fi
  60. }
  61. if ! use_ext_cmd
  62. then
  63. if test -n "$GIT_DIFF_TOOL"
  64. then
  65. merge_tool="$GIT_DIFF_TOOL"
  66. else
  67. merge_tool="$(get_merge_tool)"
  68. fi
  69. fi
  70. if test -n "$GIT_DIFFTOOL_DIRDIFF"
  71. then
  72. LOCAL="$1"
  73. REMOTE="$2"
  74. run_merge_tool "$merge_tool" false
  75. else
  76. # Launch the merge tool on each path provided by 'git diff'
  77. while test $# -gt 6
  78. do
  79. launch_merge_tool "$1" "$2" "$5"
  80. status=$?
  81. if test $status -ge 126
  82. then
  83. # Command not found (127), not executable (126) or
  84. # exited via a signal (>= 128).
  85. exit $status
  86. fi
  87. if test "$status" != 0 &&
  88. test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
  89. then
  90. exit $status
  91. fi
  92. shift 7
  93. done
  94. fi
  95. exit 0