Complete prototype of git_config_from_parameters()
[git/platforms.git] / git-mergetool.sh
blobb52a7410bcb7b37dce0f4d6213dddedd2c1e42e7
1 #!/bin/sh
3 # This program resolves merge conflicts in git
5 # Copyright (c) 2006 Theodore Y. Ts'o
7 # This file is licensed under the GPL v2, or a later version
8 # at the discretion of Junio C Hamano.
11 USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 OPTIONS_SPEC=
14 TOOL_MODE=merge
15 . git-sh-setup
16 . git-mergetool--lib
17 require_work_tree
19 # Returns true if the mode reflects a symlink
20 is_symlink () {
21 test "$1" = 120000
24 local_present () {
25 test -n "$local_mode"
28 remote_present () {
29 test -n "$remote_mode"
32 base_present () {
33 test -n "$base_mode"
36 cleanup_temp_files () {
37 if test "$1" = --save-backup ; then
38 mv -- "$BACKUP" "$MERGED.orig"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
40 else
41 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
45 describe_file () {
46 mode="$1"
47 branch="$2"
48 file="$3"
50 printf " {%s}: " "$branch"
51 if test -z "$mode"; then
52 echo "deleted"
53 elif is_symlink "$mode" ; then
54 echo "a symbolic link -> '$(cat "$file")'"
55 else
56 if base_present; then
57 echo "modified"
58 else
59 echo "created"
65 resolve_symlink_merge () {
66 while true; do
67 printf "Use (l)ocal or (r)emote, or (a)bort? "
68 read ans
69 case "$ans" in
70 [lL]*)
71 git checkout-index -f --stage=2 -- "$MERGED"
72 git add -- "$MERGED"
73 cleanup_temp_files --save-backup
74 return 0
76 [rR]*)
77 git checkout-index -f --stage=3 -- "$MERGED"
78 git add -- "$MERGED"
79 cleanup_temp_files --save-backup
80 return 0
82 [aA]*)
83 return 1
85 esac
86 done
89 resolve_deleted_merge () {
90 while true; do
91 if base_present; then
92 printf "Use (m)odified or (d)eleted file, or (a)bort? "
93 else
94 printf "Use (c)reated or (d)eleted file, or (a)bort? "
96 read ans
97 case "$ans" in
98 [mMcC]*)
99 git add -- "$MERGED"
100 cleanup_temp_files --save-backup
101 return 0
103 [dD]*)
104 git rm -- "$MERGED" > /dev/null
105 cleanup_temp_files
106 return 0
108 [aA]*)
109 return 1
111 esac
112 done
115 checkout_staged_file () {
116 tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
118 if test $? -eq 0 -a -n "$tmpfile" ; then
119 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
123 merge_file () {
124 MERGED="$1"
126 f=$(git ls-files -u -- "$MERGED")
127 if test -z "$f" ; then
128 if test ! -f "$MERGED" ; then
129 echo "$MERGED: file not found"
130 else
131 echo "$MERGED: file does not need merging"
133 return 1
136 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
137 BACKUP="./$MERGED.BACKUP.$ext"
138 LOCAL="./$MERGED.LOCAL.$ext"
139 REMOTE="./$MERGED.REMOTE.$ext"
140 BASE="./$MERGED.BASE.$ext"
142 mv -- "$MERGED" "$BACKUP"
143 cp -- "$BACKUP" "$MERGED"
145 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
146 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
147 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
149 base_present && checkout_staged_file 1 "$MERGED" "$BASE"
150 local_present && checkout_staged_file 2 "$MERGED" "$LOCAL"
151 remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
153 if test -z "$local_mode" -o -z "$remote_mode"; then
154 echo "Deleted merge conflict for '$MERGED':"
155 describe_file "$local_mode" "local" "$LOCAL"
156 describe_file "$remote_mode" "remote" "$REMOTE"
157 resolve_deleted_merge
158 return
161 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
162 echo "Symbolic link merge conflict for '$MERGED':"
163 describe_file "$local_mode" "local" "$LOCAL"
164 describe_file "$remote_mode" "remote" "$REMOTE"
165 resolve_symlink_merge
166 return
169 echo "Normal merge conflict for '$MERGED':"
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 if "$prompt" = true; then
173 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
174 read ans
177 if base_present; then
178 present=true
179 else
180 present=false
183 if ! run_merge_tool "$merge_tool" "$present"; then
184 echo "merge of $MERGED failed" 1>&2
185 mv -- "$BACKUP" "$MERGED"
187 if test "$merge_keep_temporaries" = "false"; then
188 cleanup_temp_files
191 return 1
194 if test "$merge_keep_backup" = "true"; then
195 mv -- "$BACKUP" "$MERGED.orig"
196 else
197 rm -- "$BACKUP"
200 git add -- "$MERGED"
201 cleanup_temp_files
202 return 0
205 prompt=$(git config --bool mergetool.prompt || echo true)
207 while test $# != 0
209 case "$1" in
210 -t|--tool*)
211 case "$#,$1" in
212 *,*=*)
213 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
215 1,*)
216 usage ;;
218 merge_tool="$2"
219 shift ;;
220 esac
222 -y|--no-prompt)
223 prompt=false
225 --prompt)
226 prompt=true
229 shift
230 break
233 usage
236 break
238 esac
239 shift
240 done
242 prompt_after_failed_merge() {
243 while true; do
244 printf "Continue merging other unresolved paths (y/n) ? "
245 read ans
246 case "$ans" in
248 [yY]*)
249 return 0
252 [nN]*)
253 return 1
255 esac
256 done
259 if test -z "$merge_tool"; then
260 merge_tool=$(get_merge_tool "$merge_tool") || exit
262 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
263 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
265 last_status=0
266 rollup_status=0
268 if test $# -eq 0 ; then
269 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
270 if test -z "$files" ; then
271 echo "No files need merging"
272 exit 0
274 echo Merging the files: "$files"
275 git ls-files -u |
276 sed -e 's/^[^ ]* //' |
277 sort -u |
278 while IFS= read i
280 if test $last_status -ne 0; then
281 prompt_after_failed_merge < /dev/tty || exit 1
283 printf "\n"
284 merge_file "$i" < /dev/tty > /dev/tty
285 last_status=$?
286 if test $last_status -ne 0; then
287 rollup_status=1
289 done
290 else
291 while test $# -gt 0; do
292 if test $last_status -ne 0; then
293 prompt_after_failed_merge || exit 1
295 printf "\n"
296 merge_file "$1"
297 last_status=$?
298 if test $last_status -ne 0; then
299 rollup_status=1
301 shift
302 done
305 exit $rollup_status