Merge branch 'jm/maint-apply-detects-corrupt-patch-header' into maint
[git] / git-mergetool--lib.sh
1 #!/bin/sh
2 # git-mergetool--lib is a library for common merge tool functions
3 diff_mode() {
4         test "$TOOL_MODE" = diff
5 }
6
7 merge_mode() {
8         test "$TOOL_MODE" = merge
9 }
10
11 translate_merge_tool_path () {
12         echo "$1"
13 }
14
15 check_unchanged () {
16         if test "$MERGED" -nt "$BACKUP"
17         then
18                 status=0
19         else
20                 while true
21                 do
22                         echo "$MERGED seems unchanged."
23                         printf "Was the merge successful? [y/n] "
24                         read answer || return 1
25                         case "$answer" in
26                         y*|Y*) status=0; break ;;
27                         n*|N*) status=1; break ;;
28                         esac
29                 done
30         fi
31 }
32
33 valid_tool_config () {
34         if test -n "$(get_merge_tool_cmd "$1")"
35         then
36                 return 0
37         else
38                 return 1
39         fi
40 }
41
42 valid_tool () {
43         setup_tool "$1" || valid_tool_config "$1"
44 }
45
46 setup_tool () {
47         case "$1" in
48         vim*|gvim*)
49                 tool=vim
50                 ;;
51         *)
52                 tool="$1"
53                 ;;
54         esac
55         mergetools="$(git --exec-path)/mergetools"
56
57         # Load the default definitions
58         . "$mergetools/defaults"
59         if ! test -f "$mergetools/$tool"
60         then
61                 return 1
62         fi
63
64         # Load the redefined functions
65         . "$mergetools/$tool"
66
67         if merge_mode && ! can_merge
68         then
69                 echo "error: '$tool' can not be used to resolve merges" >&2
70                 exit 1
71         elif diff_mode && ! can_diff
72         then
73                 echo "error: '$tool' can only be used to resolve merges" >&2
74                 exit 1
75         fi
76         return 0
77 }
78
79 get_merge_tool_cmd () {
80         # Prints the custom command for a merge tool
81         merge_tool="$1"
82         if diff_mode
83         then
84                 echo "$(git config difftool.$merge_tool.cmd ||
85                         git config mergetool.$merge_tool.cmd)"
86         else
87                 echo "$(git config mergetool.$merge_tool.cmd)"
88         fi
89 }
90
91 # Entry point for running tools
92 run_merge_tool () {
93         # If GIT_PREFIX is empty then we cannot use it in tools
94         # that expect to be able to chdir() to its value.
95         GIT_PREFIX=${GIT_PREFIX:-.}
96         export GIT_PREFIX
97
98         merge_tool_path="$(get_merge_tool_path "$1")" || exit
99         base_present="$2"
100         status=0
101
102         # Bring tool-specific functions into scope
103         setup_tool "$1"
104
105         if merge_mode
106         then
107                 merge_cmd "$1"
108         else
109                 diff_cmd "$1"
110         fi
111         return $status
112 }
113
114 guess_merge_tool () {
115         if merge_mode
116         then
117                 tools="tortoisemerge"
118         else
119                 tools="kompare"
120         fi
121         if test -n "$DISPLAY"
122         then
123                 if test -n "$GNOME_DESKTOP_SESSION_ID"
124                 then
125                         tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
126                 else
127                         tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
128                 fi
129                 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
130         fi
131         case "${VISUAL:-$EDITOR}" in
132         *vim*)
133                 tools="$tools vimdiff emerge"
134                 ;;
135         *)
136                 tools="$tools emerge vimdiff"
137                 ;;
138         esac
139         echo >&2 "merge tool candidates: $tools"
140
141         # Loop over each candidate and stop when a valid merge tool is found.
142         for i in $tools
143         do
144                 merge_tool_path="$(translate_merge_tool_path "$i")"
145                 if type "$merge_tool_path" >/dev/null 2>&1
146                 then
147                         echo "$i"
148                         return 0
149                 fi
150         done
151
152         echo >&2 "No known merge resolution program available."
153         return 1
154 }
155
156 get_configured_merge_tool () {
157         # Diff mode first tries diff.tool and falls back to merge.tool.
158         # Merge mode only checks merge.tool
159         if diff_mode
160         then
161                 merge_tool=$(git config diff.tool || git config merge.tool)
162         else
163                 merge_tool=$(git config merge.tool)
164         fi
165         if test -n "$merge_tool" && ! valid_tool "$merge_tool"
166         then
167                 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
168                 echo >&2 "Resetting to default..."
169                 return 1
170         fi
171         echo "$merge_tool"
172 }
173
174 get_merge_tool_path () {
175         # A merge tool has been set, so verify that it's valid.
176         merge_tool="$1"
177         if ! valid_tool "$merge_tool"
178         then
179                 echo >&2 "Unknown merge tool $merge_tool"
180                 exit 1
181         fi
182         if diff_mode
183         then
184                 merge_tool_path=$(git config difftool."$merge_tool".path ||
185                                   git config mergetool."$merge_tool".path)
186         else
187                 merge_tool_path=$(git config mergetool."$merge_tool".path)
188         fi
189         if test -z "$merge_tool_path"
190         then
191                 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
192         fi
193         if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
194                 ! type "$merge_tool_path" >/dev/null 2>&1
195         then
196                 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
197                          "'$merge_tool_path'"
198                 exit 1
199         fi
200         echo "$merge_tool_path"
201 }
202
203 get_merge_tool () {
204         # Check if a merge tool has been configured
205         merge_tool="$(get_configured_merge_tool)"
206         # Try to guess an appropriate merge tool if no tool has been set.
207         if test -z "$merge_tool"
208         then
209                 merge_tool="$(guess_merge_tool)" || exit
210         fi
211         echo "$merge_tool"
212 }