Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # This program resolves merge conflicts in git |
| 4 | # |
| 5 | # Copyright (c) 2006 Theodore Y. Ts'o |
David Aguilar | 8827b3a | 2016-10-07 16:58:03 -0700 | [diff] [blame] | 6 | # Copyright (c) 2009-2016 David Aguilar |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 7 | # |
| 8 | # This file is licensed under the GPL v2, or a later version |
Josh Triplett | 2571ac6 | 2007-06-05 21:24:19 -0700 | [diff] [blame] | 9 | # at the discretion of Junio C Hamano. |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 10 | # |
| 11 | |
Denton Liu | 063f2bd | 2018-10-24 12:25:31 -0400 | [diff] [blame] | 12 | USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [-g|--gui|--no-gui] [-O<orderfile>] [file to merge] ...' |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 13 | SUBDIRECTORY_OK=Yes |
Charles Bailey | 7bfb7c3 | 2014-10-11 01:39:37 -0700 | [diff] [blame] | 14 | NONGIT_OK=Yes |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 15 | OPTIONS_SPEC= |
David Aguilar | 21d0ba7 | 2009-04-08 00:17:20 -0700 | [diff] [blame] | 16 | TOOL_MODE=merge |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 17 | . git-sh-setup |
David Aguilar | 21d0ba7 | 2009-04-08 00:17:20 -0700 | [diff] [blame] | 18 | . git-mergetool--lib |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 19 | |
| 20 | # Returns true if the mode reflects a symlink |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 21 | is_symlink () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 22 | test "$1" = 120000 |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 23 | } |
| 24 | |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 25 | is_submodule () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 26 | test "$1" = 160000 |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 29 | local_present () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 30 | test -n "$local_mode" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 31 | } |
| 32 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 33 | remote_present () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 34 | test -n "$remote_mode" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 37 | base_present () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 38 | test -n "$base_mode" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 39 | } |
| 40 | |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 41 | mergetool_tmpdir_init () { |
| 42 | if test "$(git config --bool mergetool.writeToTemp)" != true |
| 43 | then |
| 44 | MERGETOOL_TMPDIR=. |
| 45 | return 0 |
| 46 | fi |
| 47 | if MERGETOOL_TMPDIR=$(mktemp -d -t "git-mergetool-XXXXXX" 2>/dev/null) |
| 48 | then |
| 49 | return 0 |
| 50 | fi |
| 51 | die "error: mktemp is needed when 'mergetool.writeToTemp' is true" |
| 52 | } |
| 53 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 54 | cleanup_temp_files () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 55 | if test "$1" = --save-backup |
| 56 | then |
| 57 | rm -rf -- "$MERGED.orig" |
| 58 | test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig" |
| 59 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" |
| 60 | else |
| 61 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP" |
| 62 | fi |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 63 | if test "$MERGETOOL_TMPDIR" != "." |
| 64 | then |
| 65 | rmdir "$MERGETOOL_TMPDIR" |
| 66 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 69 | describe_file () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 70 | mode="$1" |
| 71 | branch="$2" |
| 72 | file="$3" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 73 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 74 | printf " {%s}: " "$branch" |
| 75 | if test -z "$mode" |
| 76 | then |
| 77 | echo "deleted" |
| 78 | elif is_symlink "$mode" |
| 79 | then |
| 80 | echo "a symbolic link -> '$(cat "$file")'" |
| 81 | elif is_submodule "$mode" |
| 82 | then |
| 83 | echo "submodule commit $file" |
| 84 | elif base_present |
| 85 | then |
| 86 | echo "modified file" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 87 | else |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 88 | echo "created file" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 89 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 92 | resolve_symlink_merge () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 93 | while true |
| 94 | do |
| 95 | printf "Use (l)ocal or (r)emote, or (a)bort? " |
| 96 | read ans || return 1 |
| 97 | case "$ans" in |
| 98 | [lL]*) |
| 99 | git checkout-index -f --stage=2 -- "$MERGED" |
| 100 | git add -- "$MERGED" |
| 101 | cleanup_temp_files --save-backup |
| 102 | return 0 |
| 103 | ;; |
| 104 | [rR]*) |
| 105 | git checkout-index -f --stage=3 -- "$MERGED" |
| 106 | git add -- "$MERGED" |
| 107 | cleanup_temp_files --save-backup |
| 108 | return 0 |
| 109 | ;; |
| 110 | [aA]*) |
| 111 | return 1 |
| 112 | ;; |
| 113 | esac |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 114 | done |
| 115 | } |
| 116 | |
| 117 | resolve_deleted_merge () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 118 | while true |
| 119 | do |
| 120 | if base_present |
| 121 | then |
| 122 | printf "Use (m)odified or (d)eleted file, or (a)bort? " |
| 123 | else |
| 124 | printf "Use (c)reated or (d)eleted file, or (a)bort? " |
| 125 | fi |
| 126 | read ans || return 1 |
| 127 | case "$ans" in |
| 128 | [mMcC]*) |
| 129 | git add -- "$MERGED" |
David Aguilar | a298604 | 2016-03-09 23:13:59 -0800 | [diff] [blame] | 130 | if test "$merge_keep_backup" = "true" |
| 131 | then |
| 132 | cleanup_temp_files --save-backup |
| 133 | else |
| 134 | cleanup_temp_files |
| 135 | fi |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 136 | return 0 |
| 137 | ;; |
| 138 | [dD]*) |
| 139 | git rm -- "$MERGED" > /dev/null |
| 140 | cleanup_temp_files |
| 141 | return 0 |
| 142 | ;; |
| 143 | [aA]*) |
David Aguilar | a298604 | 2016-03-09 23:13:59 -0800 | [diff] [blame] | 144 | if test "$merge_keep_temporaries" = "false" |
| 145 | then |
| 146 | cleanup_temp_files |
| 147 | fi |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 148 | return 1 |
| 149 | ;; |
| 150 | esac |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 151 | done |
| 152 | } |
| 153 | |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 154 | resolve_submodule_merge () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 155 | while true |
| 156 | do |
| 157 | printf "Use (l)ocal or (r)emote, or (a)bort? " |
| 158 | read ans || return 1 |
| 159 | case "$ans" in |
| 160 | [lL]*) |
| 161 | if ! local_present |
| 162 | then |
| 163 | if test -n "$(git ls-tree HEAD -- "$MERGED")" |
| 164 | then |
| 165 | # Local isn't present, but it's a subdirectory |
| 166 | git ls-tree --full-name -r HEAD -- "$MERGED" | |
| 167 | git update-index --index-info || exit $? |
| 168 | else |
| 169 | test -e "$MERGED" && mv -- "$MERGED" "$BACKUP" |
| 170 | git update-index --force-remove "$MERGED" |
| 171 | cleanup_temp_files --save-backup |
| 172 | fi |
| 173 | elif is_submodule "$local_mode" |
| 174 | then |
| 175 | stage_submodule "$MERGED" "$local_sha1" |
| 176 | else |
| 177 | git checkout-index -f --stage=2 -- "$MERGED" |
| 178 | git add -- "$MERGED" |
| 179 | fi |
| 180 | return 0 |
| 181 | ;; |
| 182 | [rR]*) |
| 183 | if ! remote_present |
| 184 | then |
| 185 | if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")" |
| 186 | then |
| 187 | # Remote isn't present, but it's a subdirectory |
| 188 | git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" | |
| 189 | git update-index --index-info || exit $? |
| 190 | else |
| 191 | test -e "$MERGED" && mv -- "$MERGED" "$BACKUP" |
| 192 | git update-index --force-remove "$MERGED" |
| 193 | fi |
| 194 | elif is_submodule "$remote_mode" |
| 195 | then |
| 196 | ! is_submodule "$local_mode" && |
| 197 | test -e "$MERGED" && |
| 198 | mv -- "$MERGED" "$BACKUP" |
| 199 | stage_submodule "$MERGED" "$remote_sha1" |
| 200 | else |
| 201 | test -e "$MERGED" && mv -- "$MERGED" "$BACKUP" |
| 202 | git checkout-index -f --stage=3 -- "$MERGED" |
| 203 | git add -- "$MERGED" |
| 204 | fi |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 205 | cleanup_temp_files --save-backup |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 206 | return 0 |
| 207 | ;; |
| 208 | [aA]*) |
| 209 | return 1 |
| 210 | ;; |
| 211 | esac |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 212 | done |
| 213 | } |
| 214 | |
| 215 | stage_submodule () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 216 | path="$1" |
| 217 | submodule_sha1="$2" |
| 218 | mkdir -p "$path" || |
| 219 | die "fatal: unable to create directory for module at $path" |
| 220 | # Find $path relative to work tree |
| 221 | work_tree_root=$(cd_to_toplevel && pwd) |
| 222 | work_rel_path=$(cd "$path" && |
| 223 | GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix |
| 224 | ) |
| 225 | test -n "$work_rel_path" || |
| 226 | die "fatal: unable to get path of module $path relative to work tree" |
| 227 | git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die |
Jonathon Mah | ff7f089 | 2011-04-13 03:00:48 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Charles Bailey | 0ec7b6c | 2009-01-21 22:57:48 +0000 | [diff] [blame] | 230 | checkout_staged_file () { |
Johannes Sixt | 8b01465 | 2019-06-12 18:33:47 +0200 | [diff] [blame] | 231 | tmpfile="$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" && |
| 232 | tmpfile=${tmpfile%%' '*} |
Charles Bailey | 0ec7b6c | 2009-01-21 22:57:48 +0000 | [diff] [blame] | 233 | |
Elia Pinto | 1cb4937 | 2014-06-06 07:55:51 -0700 | [diff] [blame] | 234 | if test $? -eq 0 && test -n "$tmpfile" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 235 | then |
| 236 | mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3" |
| 237 | else |
| 238 | >"$3" |
| 239 | fi |
Charles Bailey | 0ec7b6c | 2009-01-21 22:57:48 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Seth House | 98ea309 | 2021-02-09 13:07:10 -0700 | [diff] [blame] | 242 | hide_resolved () { |
| 243 | git merge-file --ours -q -p "$LOCAL" "$BASE" "$REMOTE" >"$LCONFL" |
| 244 | git merge-file --theirs -q -p "$LOCAL" "$BASE" "$REMOTE" >"$RCONFL" |
| 245 | mv -- "$LCONFL" "$LOCAL" |
| 246 | mv -- "$RCONFL" "$REMOTE" |
| 247 | } |
| 248 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 249 | merge_file () { |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 250 | MERGED="$1" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 251 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 252 | f=$(git ls-files -u -- "$MERGED") |
| 253 | if test -z "$f" |
| 254 | then |
| 255 | if test ! -f "$MERGED" |
| 256 | then |
| 257 | echo "$MERGED: file not found" |
| 258 | else |
| 259 | echo "$MERGED: file does not need merging" |
| 260 | fi |
| 261 | return 1 |
| 262 | fi |
| 263 | |
Johannes Sixt | 8b01465 | 2019-06-12 18:33:47 +0200 | [diff] [blame] | 264 | # extract file extension from the last path component |
| 265 | case "${MERGED##*/}" in |
| 266 | *.*) |
| 267 | ext=.${MERGED##*.} |
| 268 | BASE=${MERGED%"$ext"} |
| 269 | ;; |
| 270 | *) |
David Aguilar | eab335c | 2014-10-10 01:19:47 -0700 | [diff] [blame] | 271 | BASE=$MERGED |
| 272 | ext= |
Johannes Sixt | 8b01465 | 2019-06-12 18:33:47 +0200 | [diff] [blame] | 273 | esac |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 274 | |
Seth House | de8dafb | 2021-02-09 13:07:11 -0700 | [diff] [blame] | 275 | initialize_merge_tool "$merge_tool" || return |
| 276 | |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 277 | mergetool_tmpdir_init |
| 278 | |
| 279 | if test "$MERGETOOL_TMPDIR" != "." |
| 280 | then |
| 281 | # If we're using a temporary directory then write to the |
| 282 | # top-level of that directory. |
| 283 | BASE=${BASE##*/} |
| 284 | fi |
| 285 | |
| 286 | BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext" |
| 287 | LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext" |
Seth House | 98ea309 | 2021-02-09 13:07:10 -0700 | [diff] [blame] | 288 | LCONFL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_LCONFL_$$$ext" |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 289 | REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext" |
Seth House | 98ea309 | 2021-02-09 13:07:10 -0700 | [diff] [blame] | 290 | RCONFL="$MERGETOOL_TMPDIR/${BASE}_REMOTE_RCONFL_$$$ext" |
David Aguilar | 8f0cb41 | 2014-10-11 10:04:45 -0700 | [diff] [blame] | 291 | BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 292 | |
Johannes Sixt | 7e6d6f7 | 2019-06-12 18:33:48 +0200 | [diff] [blame] | 293 | base_mode= local_mode= remote_mode= |
| 294 | |
| 295 | # here, $IFS is just a LF |
| 296 | for line in $f |
| 297 | do |
| 298 | mode=${line%% *} # 1st word |
| 299 | sha1=${line#"$mode "} |
| 300 | sha1=${sha1%% *} # 2nd word |
| 301 | case "${line#$mode $sha1 }" in # remainder |
| 302 | '1 '*) |
| 303 | base_mode=$mode |
| 304 | ;; |
| 305 | '2 '*) |
| 306 | local_mode=$mode local_sha1=$sha1 |
| 307 | ;; |
| 308 | '3 '*) |
| 309 | remote_mode=$mode remote_sha1=$sha1 |
| 310 | ;; |
| 311 | esac |
| 312 | done |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 313 | |
| 314 | if is_submodule "$local_mode" || is_submodule "$remote_mode" |
| 315 | then |
| 316 | echo "Submodule merge conflict for '$MERGED':" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 317 | describe_file "$local_mode" "local" "$local_sha1" |
| 318 | describe_file "$remote_mode" "remote" "$remote_sha1" |
| 319 | resolve_submodule_merge |
| 320 | return |
| 321 | fi |
| 322 | |
David Aguilar | faaab8d | 2016-03-09 23:13:58 -0800 | [diff] [blame] | 323 | if test -f "$MERGED" |
| 324 | then |
| 325 | mv -- "$MERGED" "$BACKUP" |
| 326 | cp -- "$BACKUP" "$MERGED" |
| 327 | fi |
| 328 | # Create a parent directory to handle delete/delete conflicts |
| 329 | # where the base's directory no longer exists. |
| 330 | mkdir -p "$(dirname "$MERGED")" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 331 | |
| 332 | checkout_staged_file 1 "$MERGED" "$BASE" |
| 333 | checkout_staged_file 2 "$MERGED" "$LOCAL" |
| 334 | checkout_staged_file 3 "$MERGED" "$REMOTE" |
| 335 | |
Seth House | 9d9cf23 | 2021-02-09 13:07:12 -0700 | [diff] [blame] | 336 | # hideResolved preferences hierarchy. |
| 337 | global_config="mergetool.hideResolved" |
| 338 | tool_config="mergetool.${merge_tool}.hideResolved" |
| 339 | |
| 340 | if enabled=$(git config --type=bool "$tool_config") |
| 341 | then |
| 342 | # The user has a specific preference for a specific tool and no |
| 343 | # other preferences should override that. |
| 344 | : ; |
| 345 | elif enabled=$(git config --type=bool "$global_config") |
| 346 | then |
| 347 | # The user has a general preference for all tools. |
| 348 | # |
| 349 | # 'true' means the user likes the feature so we should use it |
| 350 | # where possible but tool authors can still override. |
| 351 | # |
| 352 | # 'false' means the user doesn't like the feature so we should |
| 353 | # not use it anywhere. |
| 354 | if test "$enabled" = true && hide_resolved_enabled |
| 355 | then |
| 356 | enabled=true |
| 357 | else |
| 358 | enabled=false |
| 359 | fi |
| 360 | else |
Jonathan Nieder | b2a51c1 | 2021-03-13 00:38:48 -0800 | [diff] [blame] | 361 | # The user does not have a preference. Default to disabled. |
| 362 | enabled=false |
Seth House | 9d9cf23 | 2021-02-09 13:07:12 -0700 | [diff] [blame] | 363 | fi |
| 364 | |
| 365 | if test "$enabled" = true |
Seth House | 98ea309 | 2021-02-09 13:07:10 -0700 | [diff] [blame] | 366 | then |
| 367 | hide_resolved |
| 368 | fi |
| 369 | |
Elia Pinto | 1cb4937 | 2014-06-06 07:55:51 -0700 | [diff] [blame] | 370 | if test -z "$local_mode" || test -z "$remote_mode" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 371 | then |
| 372 | echo "Deleted merge conflict for '$MERGED':" |
| 373 | describe_file "$local_mode" "local" "$LOCAL" |
| 374 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 375 | resolve_deleted_merge |
David Aguilar | faaab8d | 2016-03-09 23:13:58 -0800 | [diff] [blame] | 376 | status=$? |
| 377 | rmdir -p "$(dirname "$MERGED")" 2>/dev/null |
| 378 | return $status |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 379 | fi |
| 380 | |
| 381 | if is_symlink "$local_mode" || is_symlink "$remote_mode" |
| 382 | then |
| 383 | echo "Symbolic link merge conflict for '$MERGED':" |
| 384 | describe_file "$local_mode" "local" "$LOCAL" |
| 385 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 386 | resolve_symlink_merge |
| 387 | return |
| 388 | fi |
| 389 | |
| 390 | echo "Normal merge conflict for '$MERGED':" |
| 391 | describe_file "$local_mode" "local" "$LOCAL" |
| 392 | describe_file "$remote_mode" "remote" "$REMOTE" |
Felipe Contreras | 4ecc63d | 2014-04-20 19:17:34 -0500 | [diff] [blame] | 393 | if test "$guessed_merge_tool" = true || test "$prompt" = true |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 394 | then |
| 395 | printf "Hit return to start merge resolution tool (%s): " "$merge_tool" |
| 396 | read ans || return 1 |
| 397 | fi |
| 398 | |
| 399 | if base_present |
| 400 | then |
| 401 | present=true |
Theodore Ts'o | ce5b6d7 | 2007-03-27 18:00:03 -0400 | [diff] [blame] | 402 | else |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 403 | present=false |
Charles Bailey | 162eba8 | 2008-12-12 21:48:41 +0000 | [diff] [blame] | 404 | fi |
| 405 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 406 | if ! run_merge_tool "$merge_tool" "$present" |
| 407 | then |
| 408 | echo "merge of $MERGED failed" 1>&2 |
| 409 | mv -- "$BACKUP" "$MERGED" |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 410 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 411 | if test "$merge_keep_temporaries" = "false" |
| 412 | then |
| 413 | cleanup_temp_files |
| 414 | fi |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 415 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 416 | return 1 |
| 417 | fi |
| 418 | |
| 419 | if test "$merge_keep_backup" = "true" |
| 420 | then |
| 421 | mv -- "$BACKUP" "$MERGED.orig" |
| 422 | else |
| 423 | rm -- "$BACKUP" |
| 424 | fi |
| 425 | |
| 426 | git add -- "$MERGED" |
| 427 | cleanup_temp_files |
| 428 | return 0 |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 431 | prompt_after_failed_merge () { |
| 432 | while true |
| 433 | do |
Nikola Forró | cce076e | 2016-04-12 16:44:20 +0200 | [diff] [blame] | 434 | printf "Continue merging other unresolved paths [y/n]? " |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 435 | read ans || return 1 |
| 436 | case "$ans" in |
| 437 | [yY]*) |
| 438 | return 0 |
| 439 | ;; |
| 440 | [nN]*) |
| 441 | return 1 |
| 442 | ;; |
| 443 | esac |
| 444 | done |
Charles Bailey | b0169d8 | 2008-12-12 21:48:40 +0000 | [diff] [blame] | 445 | } |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 446 | |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 447 | print_noop_and_exit () { |
| 448 | echo "No files need merging" |
| 449 | exit 0 |
| 450 | } |
| 451 | |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 452 | main () { |
| 453 | prompt=$(git config --bool mergetool.prompt) |
Tao Klerks | 42943b9 | 2023-03-18 15:27:43 +0000 | [diff] [blame] | 454 | GIT_MERGETOOL_GUI= |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 455 | guessed_merge_tool=false |
David Aguilar | 654311b | 2016-10-07 17:01:30 -0700 | [diff] [blame] | 456 | orderfile= |
Charles Bailey | 7bfb7c3 | 2014-10-11 01:39:37 -0700 | [diff] [blame] | 457 | |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 458 | while test $# != 0 |
| 459 | do |
| 460 | case "$1" in |
| 461 | --tool-help=*) |
| 462 | TOOL_MODE=${1#--tool-help=} |
| 463 | show_tool_help |
| 464 | ;; |
| 465 | --tool-help) |
| 466 | show_tool_help |
| 467 | ;; |
| 468 | -t|--tool*) |
| 469 | case "$#,$1" in |
| 470 | *,*=*) |
Johannes Sixt | 8b01465 | 2019-06-12 18:33:47 +0200 | [diff] [blame] | 471 | merge_tool=${1#*=} |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 472 | ;; |
| 473 | 1,*) |
| 474 | usage ;; |
| 475 | *) |
| 476 | merge_tool="$2" |
| 477 | shift ;; |
| 478 | esac |
| 479 | ;; |
Denton Liu | 063f2bd | 2018-10-24 12:25:31 -0400 | [diff] [blame] | 480 | --no-gui) |
Denton Liu | 05fb872 | 2019-04-29 02:21:08 -0400 | [diff] [blame] | 481 | GIT_MERGETOOL_GUI=false |
Denton Liu | 063f2bd | 2018-10-24 12:25:31 -0400 | [diff] [blame] | 482 | ;; |
| 483 | -g|--gui) |
Denton Liu | 05fb872 | 2019-04-29 02:21:08 -0400 | [diff] [blame] | 484 | GIT_MERGETOOL_GUI=true |
Denton Liu | 063f2bd | 2018-10-24 12:25:31 -0400 | [diff] [blame] | 485 | ;; |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 486 | -y|--no-prompt) |
| 487 | prompt=false |
| 488 | ;; |
| 489 | --prompt) |
| 490 | prompt=true |
| 491 | ;; |
David Aguilar | 654311b | 2016-10-07 17:01:30 -0700 | [diff] [blame] | 492 | -O*) |
Richard Hansen | c1b0d3a | 2017-01-10 15:42:01 -0500 | [diff] [blame] | 493 | orderfile="${1#-O}" |
David Aguilar | 654311b | 2016-10-07 17:01:30 -0700 | [diff] [blame] | 494 | ;; |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 495 | --) |
| 496 | shift |
| 497 | break |
| 498 | ;; |
| 499 | -*) |
| 500 | usage |
| 501 | ;; |
| 502 | *) |
| 503 | break |
| 504 | ;; |
| 505 | esac |
| 506 | shift |
| 507 | done |
| 508 | |
| 509 | git_dir_init |
| 510 | require_work_tree |
| 511 | |
Felipe Contreras | 4ecc63d | 2014-04-20 19:17:34 -0500 | [diff] [blame] | 512 | if test -z "$merge_tool" |
| 513 | then |
Tao Klerks | 42943b9 | 2023-03-18 15:27:43 +0000 | [diff] [blame] | 514 | merge_tool=$(get_merge_tool) |
| 515 | subshell_exit_status=$? |
| 516 | if test $subshell_exit_status = 1 |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 517 | then |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 518 | guessed_merge_tool=true |
Tao Klerks | 42943b9 | 2023-03-18 15:27:43 +0000 | [diff] [blame] | 519 | elif test $subshell_exit_status -gt 1 |
| 520 | then |
| 521 | exit $subshell_exit_status |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 522 | fi |
Felipe Contreras | 4ecc63d | 2014-04-20 19:17:34 -0500 | [diff] [blame] | 523 | fi |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 524 | merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)" |
| 525 | merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 526 | |
Richard Hansen | d0e0cfe | 2017-01-10 15:42:02 -0500 | [diff] [blame] | 527 | prefix=$(git rev-parse --show-prefix) || exit 1 |
| 528 | cd_to_toplevel |
| 529 | |
| 530 | if test -n "$orderfile" |
| 531 | then |
| 532 | orderfile=$( |
| 533 | git rev-parse --prefix "$prefix" -- "$orderfile" | |
| 534 | sed -e 1d |
| 535 | ) |
| 536 | fi |
| 537 | |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 538 | if test $# -eq 0 && test -e "$GIT_DIR/MERGE_RR" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 539 | then |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 540 | set -- $(git rerere remaining) |
| 541 | if test $# -eq 0 |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 542 | then |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 543 | print_noop_and_exit |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 544 | fi |
Richard Hansen | d0e0cfe | 2017-01-10 15:42:02 -0500 | [diff] [blame] | 545 | elif test $# -ge 0 |
| 546 | then |
| 547 | # rev-parse provides the -- needed for 'set' |
| 548 | eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 549 | fi |
Charles Bailey | b0169d8 | 2008-12-12 21:48:40 +0000 | [diff] [blame] | 550 | |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 551 | files=$(git -c core.quotePath=false \ |
David Aguilar | 654311b | 2016-10-07 17:01:30 -0700 | [diff] [blame] | 552 | diff --name-only --diff-filter=U \ |
Richard Hansen | c1b0d3a | 2017-01-10 15:42:01 -0500 | [diff] [blame] | 553 | ${orderfile:+"-O$orderfile"} -- "$@") |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 554 | |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 555 | if test -z "$files" |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 556 | then |
David Aguilar | 57937f7 | 2016-10-07 16:58:05 -0700 | [diff] [blame] | 557 | print_noop_and_exit |
Junio C Hamano | f8750a0 | 2012-08-22 22:33:15 -0700 | [diff] [blame] | 558 | fi |
Jonathon Mah | 3e8e691 | 2011-09-15 19:12:10 -0700 | [diff] [blame] | 559 | |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 560 | printf "Merging:\n" |
| 561 | printf "%s\n" "$files" |
| 562 | |
| 563 | rc=0 |
Nicholas Guriev | d651a54 | 2018-08-13 08:09:29 +0300 | [diff] [blame] | 564 | set -- $files |
| 565 | while test $# -ne 0 |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 566 | do |
| 567 | printf "\n" |
Nicholas Guriev | d651a54 | 2018-08-13 08:09:29 +0300 | [diff] [blame] | 568 | if ! merge_file "$1" |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 569 | then |
| 570 | rc=1 |
Nicholas Guriev | d651a54 | 2018-08-13 08:09:29 +0300 | [diff] [blame] | 571 | test $# -ne 1 && prompt_after_failed_merge || exit 1 |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 572 | fi |
Nicholas Guriev | d651a54 | 2018-08-13 08:09:29 +0300 | [diff] [blame] | 573 | shift |
David Aguilar | 08221e3 | 2016-10-07 16:58:04 -0700 | [diff] [blame] | 574 | done |
| 575 | |
| 576 | exit $rc |
| 577 | } |
| 578 | |
| 579 | main "$@" |