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 |
| 6 | # |
| 7 | # This file is licensed under the GPL v2, or a later version |
Josh Triplett | 2571ac6 | 2007-06-05 21:24:19 -0700 | [diff] [blame] | 8 | # at the discretion of Junio C Hamano. |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 9 | # |
| 10 | |
| 11 | USAGE='[--tool=tool] [file to merge] ...' |
| 12 | SUBDIRECTORY_OK=Yes |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 13 | OPTIONS_SPEC= |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 14 | . git-sh-setup |
| 15 | require_work_tree |
Junio C Hamano | 769f398 | 2007-09-27 14:41:23 -0700 | [diff] [blame] | 16 | prefix=$(git rev-parse --show-prefix) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 17 | |
| 18 | # Returns true if the mode reflects a symlink |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 19 | is_symlink () { |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 20 | test "$1" = 120000 |
| 21 | } |
| 22 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 23 | local_present () { |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 24 | test -n "$local_mode" |
| 25 | } |
| 26 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 27 | remote_present () { |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 28 | test -n "$remote_mode" |
| 29 | } |
| 30 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 31 | base_present () { |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 32 | test -n "$base_mode" |
| 33 | } |
| 34 | |
| 35 | cleanup_temp_files () { |
| 36 | if test "$1" = --save-backup ; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 37 | mv -- "$BACKUP" "$MERGED.orig" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 38 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" |
| 39 | else |
| 40 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP" |
| 41 | fi |
| 42 | } |
| 43 | |
Theodore Ts'o | 262c981 | 2007-03-29 06:55:11 -0400 | [diff] [blame] | 44 | describe_file () { |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 45 | mode="$1" |
| 46 | branch="$2" |
| 47 | file="$3" |
| 48 | |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 49 | printf " {%s}: " "$branch" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 50 | if test -z "$mode"; then |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 51 | echo "deleted" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 52 | elif is_symlink "$mode" ; then |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 53 | echo "a symbolic link -> '$(cat "$file")'" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 54 | else |
| 55 | if base_present; then |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 56 | echo "modified" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 57 | else |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 58 | echo "created" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 59 | fi |
| 60 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | resolve_symlink_merge () { |
Theodore Ts'o | d1dc695 | 2007-03-29 06:46:59 -0400 | [diff] [blame] | 65 | while true; do |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 66 | printf "Use (l)ocal or (r)emote, or (a)bort? " |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 67 | read ans |
| 68 | case "$ans" in |
| 69 | [lL]*) |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 70 | git checkout-index -f --stage=2 -- "$MERGED" |
| 71 | git add -- "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 72 | cleanup_temp_files --save-backup |
| 73 | return |
| 74 | ;; |
Theodore Ts'o | 5a174f1 | 2007-03-29 09:48:31 -0400 | [diff] [blame] | 75 | [rR]*) |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 76 | git checkout-index -f --stage=3 -- "$MERGED" |
| 77 | git add -- "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 78 | cleanup_temp_files --save-backup |
| 79 | return |
| 80 | ;; |
Theodore Ts'o | 5a174f1 | 2007-03-29 09:48:31 -0400 | [diff] [blame] | 81 | [aA]*) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 82 | exit 1 |
| 83 | ;; |
| 84 | esac |
| 85 | done |
| 86 | } |
| 87 | |
| 88 | resolve_deleted_merge () { |
Theodore Ts'o | d1dc695 | 2007-03-29 06:46:59 -0400 | [diff] [blame] | 89 | while true; do |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 90 | if base_present; then |
| 91 | printf "Use (m)odified or (d)eleted file, or (a)bort? " |
| 92 | else |
| 93 | printf "Use (c)reated or (d)eleted file, or (a)bort? " |
| 94 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 95 | read ans |
| 96 | case "$ans" in |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 97 | [mMcC]*) |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 98 | git add -- "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 99 | cleanup_temp_files --save-backup |
| 100 | return |
| 101 | ;; |
Theodore Ts'o | 5a174f1 | 2007-03-29 09:48:31 -0400 | [diff] [blame] | 102 | [dD]*) |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 103 | git rm -- "$MERGED" > /dev/null |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 104 | cleanup_temp_files |
| 105 | return |
| 106 | ;; |
Theodore Ts'o | 5a174f1 | 2007-03-29 09:48:31 -0400 | [diff] [blame] | 107 | [aA]*) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 108 | exit 1 |
| 109 | ;; |
| 110 | esac |
| 111 | done |
| 112 | } |
| 113 | |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 114 | check_unchanged () { |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 115 | if test "$MERGED" -nt "$BACKUP" ; then |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 116 | status=0; |
| 117 | else |
| 118 | while true; do |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 119 | echo "$MERGED seems unchanged." |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 120 | printf "Was the merge successful? [y/n] " |
| 121 | read answer < /dev/tty |
| 122 | case "$answer" in |
| 123 | y*|Y*) status=0; break ;; |
| 124 | n*|N*) status=1; break ;; |
| 125 | esac |
| 126 | done |
| 127 | fi |
| 128 | } |
| 129 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 130 | merge_file () { |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 131 | MERGED="$1" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 132 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 133 | f=`git ls-files -u -- "$MERGED"` |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 134 | if test -z "$f" ; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 135 | if test ! -f "$MERGED" ; then |
| 136 | echo "$MERGED: file not found" |
Theodore Ts'o | ce5b6d7 | 2007-03-27 18:00:03 -0400 | [diff] [blame] | 137 | else |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 138 | echo "$MERGED: file does not need merging" |
Theodore Ts'o | ce5b6d7 | 2007-03-27 18:00:03 -0400 | [diff] [blame] | 139 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 140 | exit 1 |
| 141 | fi |
| 142 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 143 | ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')" |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 144 | BACKUP="./$MERGED.BACKUP.$ext" |
| 145 | LOCAL="./$MERGED.LOCAL.$ext" |
| 146 | REMOTE="./$MERGED.REMOTE.$ext" |
| 147 | BASE="./$MERGED.BASE.$ext" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 148 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 149 | mv -- "$MERGED" "$BACKUP" |
| 150 | cp -- "$BACKUP" "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 151 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 152 | base_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'` |
| 153 | local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'` |
| 154 | remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'` |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 155 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 156 | base_present && git cat-file blob ":1:$prefix$MERGED" >"$BASE" 2>/dev/null |
| 157 | local_present && git cat-file blob ":2:$prefix$MERGED" >"$LOCAL" 2>/dev/null |
| 158 | remote_present && git cat-file blob ":3:$prefix$MERGED" >"$REMOTE" 2>/dev/null |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 159 | |
| 160 | if test -z "$local_mode" -o -z "$remote_mode"; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 161 | echo "Deleted merge conflict for '$MERGED':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 162 | describe_file "$local_mode" "local" "$LOCAL" |
| 163 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 164 | resolve_deleted_merge |
| 165 | return |
| 166 | fi |
| 167 | |
| 168 | if is_symlink "$local_mode" || is_symlink "$remote_mode"; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 169 | echo "Symbolic link merge conflict for '$MERGED':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 170 | describe_file "$local_mode" "local" "$LOCAL" |
| 171 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 172 | resolve_symlink_merge |
| 173 | return |
| 174 | fi |
| 175 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 176 | echo "Normal merge conflict for '$MERGED':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 177 | describe_file "$local_mode" "local" "$LOCAL" |
| 178 | describe_file "$remote_mode" "remote" "$REMOTE" |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 179 | printf "Hit return to start merge resolution tool (%s): " "$merge_tool" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 180 | read ans |
| 181 | |
| 182 | case "$merge_tool" in |
| 183 | kdiff3) |
| 184 | if base_present ; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 185 | ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \ |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 186 | -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 187 | else |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 188 | ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \ |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 189 | -o "$MERGED" "$LOCAL" "$REMOTE" > /dev/null 2>&1) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 190 | fi |
| 191 | status=$? |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 192 | ;; |
| 193 | tkdiff) |
| 194 | if base_present ; then |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 195 | "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 196 | else |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 197 | "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 198 | fi |
| 199 | status=$? |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 200 | ;; |
James Bowes | 9cec653 | 2007-03-18 22:11:54 -0400 | [diff] [blame] | 201 | meld|vimdiff) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 202 | touch "$BACKUP" |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 203 | "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE" |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 204 | check_unchanged |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 205 | ;; |
Dan McGee | 730b5b4 | 2007-06-05 21:19:47 -0400 | [diff] [blame] | 206 | gvimdiff) |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 207 | touch "$BACKUP" |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 208 | "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE" |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 209 | check_unchanged |
| 210 | ;; |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 211 | xxdiff) |
| 212 | touch "$BACKUP" |
| 213 | if base_present ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 214 | "$merge_tool_path" -X --show-merged-pane \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 215 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ |
| 216 | -R 'Accel.Search: "Ctrl+F"' \ |
| 217 | -R 'Accel.SearchForward: "Ctrl-G"' \ |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 218 | --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 219 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 220 | "$merge_tool_path" -X --show-merged-pane \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 221 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ |
| 222 | -R 'Accel.Search: "Ctrl+F"' \ |
| 223 | -R 'Accel.SearchForward: "Ctrl-G"' \ |
Patrick Higgins | 641dba4 | 2008-06-16 17:33:41 -0600 | [diff] [blame] | 224 | --merged-file "$MERGED" "$LOCAL" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 225 | fi |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 226 | check_unchanged |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 227 | ;; |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 228 | opendiff) |
| 229 | touch "$BACKUP" |
| 230 | if base_present; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 231 | "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 232 | else |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 233 | "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 234 | fi |
| 235 | check_unchanged |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 236 | ;; |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 237 | ecmerge) |
| 238 | touch "$BACKUP" |
| 239 | if base_present; then |
Sebastian Schuberth | f4e9f78 | 2008-05-06 12:53:56 +0200 | [diff] [blame] | 240 | "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED" |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 241 | else |
Sebastian Schuberth | f4e9f78 | 2008-05-06 12:53:56 +0200 | [diff] [blame] | 242 | "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED" |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 243 | fi |
| 244 | check_unchanged |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 245 | ;; |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 246 | emerge) |
| 247 | if base_present ; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 248 | "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 249 | else |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 250 | "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 251 | fi |
| 252 | status=$? |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 253 | ;; |
Charles Bailey | 964473a | 2008-02-21 23:31:12 +0000 | [diff] [blame] | 254 | *) |
| 255 | if test -n "$merge_tool_cmd"; then |
| 256 | if test "$merge_tool_trust_exit_code" = "false"; then |
| 257 | touch "$BACKUP" |
| 258 | ( eval $merge_tool_cmd ) |
| 259 | check_unchanged |
| 260 | else |
| 261 | ( eval $merge_tool_cmd ) |
| 262 | status=$? |
| 263 | fi |
| 264 | fi |
| 265 | ;; |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 266 | esac |
| 267 | if test "$status" -ne 0; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 268 | echo "merge of $MERGED failed" 1>&2 |
| 269 | mv -- "$BACKUP" "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 270 | exit 1 |
| 271 | fi |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 272 | |
| 273 | if test "$merge_keep_backup" = "true"; then |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 274 | mv -- "$BACKUP" "$MERGED.orig" |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 275 | else |
| 276 | rm -- "$BACKUP" |
| 277 | fi |
| 278 | |
Charles Bailey | b3ea27e | 2008-02-21 23:30:34 +0000 | [diff] [blame] | 279 | git add -- "$MERGED" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 280 | cleanup_temp_files |
| 281 | } |
| 282 | |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 283 | while test $# != 0 |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 284 | do |
| 285 | case "$1" in |
| 286 | -t|--tool*) |
| 287 | case "$#,$1" in |
| 288 | *,*=*) |
| 289 | merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'` |
| 290 | ;; |
| 291 | 1,*) |
| 292 | usage ;; |
| 293 | *) |
| 294 | merge_tool="$2" |
| 295 | shift ;; |
| 296 | esac |
| 297 | ;; |
| 298 | --) |
| 299 | break |
| 300 | ;; |
| 301 | -*) |
| 302 | usage |
| 303 | ;; |
| 304 | *) |
| 305 | break |
| 306 | ;; |
| 307 | esac |
| 308 | shift |
| 309 | done |
| 310 | |
Charles Bailey | 964473a | 2008-02-21 23:31:12 +0000 | [diff] [blame] | 311 | valid_custom_tool() |
| 312 | { |
| 313 | merge_tool_cmd="$(git config mergetool.$1.cmd)" |
| 314 | test -n "$merge_tool_cmd" |
| 315 | } |
| 316 | |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 317 | valid_tool() { |
| 318 | case "$1" in |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 319 | kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge) |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 320 | ;; # happy |
| 321 | *) |
Charles Bailey | 964473a | 2008-02-21 23:31:12 +0000 | [diff] [blame] | 322 | if ! valid_custom_tool "$1"; then |
| 323 | return 1 |
| 324 | fi |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 325 | ;; |
| 326 | esac |
| 327 | } |
| 328 | |
| 329 | init_merge_tool_path() { |
| 330 | merge_tool_path=`git config mergetool.$1.path` |
| 331 | if test -z "$merge_tool_path" ; then |
| 332 | case "$1" in |
| 333 | emerge) |
| 334 | merge_tool_path=emacs |
| 335 | ;; |
| 336 | *) |
| 337 | merge_tool_path=$1 |
| 338 | ;; |
| 339 | esac |
| 340 | fi |
| 341 | } |
| 342 | |
| 343 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 344 | if test -z "$merge_tool"; then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 345 | merge_tool=`git config merge.tool` |
Steffen Prohaska | d279fc1 | 2007-10-18 12:25:34 +0200 | [diff] [blame] | 346 | if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then |
Theodore Ts'o | d6678c2 | 2007-03-18 22:30:10 -0400 | [diff] [blame] | 347 | echo >&2 "git config option merge.tool set to unknown tool: $merge_tool" |
| 348 | echo >&2 "Resetting to default..." |
| 349 | unset merge_tool |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 350 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 351 | fi |
| 352 | |
| 353 | if test -z "$merge_tool" ; then |
Theodore Ts'o | 301ac38 | 2007-06-10 11:17:30 -0400 | [diff] [blame] | 354 | if test -n "$DISPLAY"; then |
| 355 | merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff" |
| 356 | if test -n "$GNOME_DESKTOP_SESSION_ID" ; then |
| 357 | merge_tool_candidates="meld $merge_tool_candidates" |
| 358 | fi |
| 359 | if test "$KDE_FULL_SESSION" = "true"; then |
| 360 | merge_tool_candidates="kdiff3 $merge_tool_candidates" |
| 361 | fi |
| 362 | fi |
| 363 | if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then |
| 364 | merge_tool_candidates="$merge_tool_candidates emerge" |
| 365 | fi |
| 366 | if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then |
| 367 | merge_tool_candidates="$merge_tool_candidates vimdiff" |
| 368 | fi |
| 369 | merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff" |
| 370 | echo "merge tool candidates: $merge_tool_candidates" |
| 371 | for i in $merge_tool_candidates; do |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 372 | init_merge_tool_path $i |
| 373 | if type "$merge_tool_path" > /dev/null 2>&1; then |
Theodore Ts'o | 301ac38 | 2007-06-10 11:17:30 -0400 | [diff] [blame] | 374 | merge_tool=$i |
| 375 | break |
| 376 | fi |
| 377 | done |
| 378 | if test -z "$merge_tool" ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 379 | echo "No known merge resolution program available." |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 380 | exit 1 |
| 381 | fi |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 382 | else |
| 383 | if ! valid_tool "$merge_tool"; then |
| 384 | echo >&2 "Unknown merge_tool $merge_tool" |
| 385 | exit 1 |
| 386 | fi |
| 387 | |
| 388 | init_merge_tool_path "$merge_tool" |
| 389 | |
Charles Bailey | 44c36d1 | 2008-02-21 23:30:02 +0000 | [diff] [blame] | 390 | merge_keep_backup="$(git config --bool merge.keepBackup || echo true)" |
| 391 | |
Charles Bailey | 964473a | 2008-02-21 23:31:12 +0000 | [diff] [blame] | 392 | if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 393 | echo "The merge tool $merge_tool is not available as '$merge_tool_path'" |
| 394 | exit 1 |
| 395 | fi |
Charles Bailey | 964473a | 2008-02-21 23:31:12 +0000 | [diff] [blame] | 396 | |
| 397 | if ! test -z "$merge_tool_cmd"; then |
| 398 | merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)" |
| 399 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 400 | fi |
| 401 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 402 | |
| 403 | if test $# -eq 0 ; then |
| 404 | files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u` |
| 405 | if test -z "$files" ; then |
| 406 | echo "No files need merging" |
| 407 | exit 0 |
| 408 | fi |
Rogan Dawes | 2e8fd78 | 2008-01-07 09:37:38 +0200 | [diff] [blame] | 409 | echo Merging the files: "$files" |
| 410 | git ls-files -u | |
| 411 | sed -e 's/^[^ ]* //' | |
| 412 | sort -u | |
| 413 | while IFS= read i |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 414 | do |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 415 | printf "\n" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 416 | merge_file "$i" < /dev/tty > /dev/tty |
| 417 | done |
| 418 | else |
| 419 | while test $# -gt 0; do |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 420 | printf "\n" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 421 | merge_file "$1" |
| 422 | shift |
| 423 | done |
| 424 | fi |
| 425 | exit 0 |