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 |
| 37 | mv -- "$BACKUP" "$path.orig" |
| 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]*) |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 70 | git checkout-index -f --stage=2 -- "$path" |
| 71 | git add -- "$path" |
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]*) |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 76 | git checkout-index -f --stage=3 -- "$path" |
| 77 | git add -- "$path" |
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]*) |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 98 | git add -- "$path" |
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]*) |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 103 | git rm -- "$path" > /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 () { |
| 115 | if test "$path" -nt "$BACKUP" ; then |
| 116 | status=0; |
| 117 | else |
| 118 | while true; do |
| 119 | echo "$path seems unchanged." |
| 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 | |
| 130 | save_backup () { |
| 131 | if test "$status" -eq 0; then |
| 132 | mv -- "$BACKUP" "$path.orig" |
| 133 | fi |
| 134 | } |
| 135 | |
| 136 | remove_backup () { |
| 137 | if test "$status" -eq 0; then |
| 138 | rm "$BACKUP" |
| 139 | fi |
| 140 | } |
| 141 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 142 | merge_file () { |
| 143 | path="$1" |
| 144 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 145 | f=`git ls-files -u -- "$path"` |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 146 | if test -z "$f" ; then |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 147 | if test ! -f "$path" ; then |
Theodore Ts'o | ce5b6d7 | 2007-03-27 18:00:03 -0400 | [diff] [blame] | 148 | echo "$path: file not found" |
| 149 | else |
| 150 | echo "$path: file does not need merging" |
| 151 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 152 | exit 1 |
| 153 | fi |
| 154 | |
Pini Reznik | c3d51cd | 2007-12-05 09:19:13 +0200 | [diff] [blame] | 155 | ext="$$$(expr "$path" : '.*\(\.[^/]*\)$')" |
| 156 | BACKUP="$path.BACKUP.$ext" |
| 157 | LOCAL="$path.LOCAL.$ext" |
| 158 | REMOTE="$path.REMOTE.$ext" |
| 159 | BASE="$path.BASE.$ext" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 160 | |
| 161 | mv -- "$path" "$BACKUP" |
| 162 | cp -- "$BACKUP" "$path" |
| 163 | |
| 164 | base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'` |
| 165 | local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'` |
| 166 | remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'` |
| 167 | |
Junio C Hamano | 769f398 | 2007-09-27 14:41:23 -0700 | [diff] [blame] | 168 | base_present && git cat-file blob ":1:$prefix$path" >"$BASE" 2>/dev/null |
| 169 | local_present && git cat-file blob ":2:$prefix$path" >"$LOCAL" 2>/dev/null |
| 170 | remote_present && git cat-file blob ":3:$prefix$path" >"$REMOTE" 2>/dev/null |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 171 | |
| 172 | if test -z "$local_mode" -o -z "$remote_mode"; then |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 173 | echo "Deleted merge conflict for '$path':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 174 | describe_file "$local_mode" "local" "$LOCAL" |
| 175 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 176 | resolve_deleted_merge |
| 177 | return |
| 178 | fi |
| 179 | |
| 180 | if is_symlink "$local_mode" || is_symlink "$remote_mode"; then |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 181 | echo "Symbolic link merge conflict for '$path':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 182 | describe_file "$local_mode" "local" "$LOCAL" |
| 183 | describe_file "$remote_mode" "remote" "$REMOTE" |
| 184 | resolve_symlink_merge |
| 185 | return |
| 186 | fi |
| 187 | |
Theodore Ts'o | 27090aa | 2007-03-29 11:39:46 -0400 | [diff] [blame] | 188 | echo "Normal merge conflict for '$path':" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 189 | describe_file "$local_mode" "local" "$LOCAL" |
| 190 | describe_file "$remote_mode" "remote" "$REMOTE" |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 191 | printf "Hit return to start merge resolution tool (%s): " "$merge_tool" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 192 | read ans |
| 193 | |
| 194 | case "$merge_tool" in |
| 195 | kdiff3) |
| 196 | if base_present ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 197 | ("$merge_tool_path" --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 198 | -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) |
| 199 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 200 | ("$merge_tool_path" --auto --L1 "$path (Local)" --L2 "$path (Remote)" \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 201 | -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1) |
| 202 | fi |
| 203 | status=$? |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 204 | remove_backup |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 205 | ;; |
| 206 | tkdiff) |
| 207 | if base_present ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 208 | "$merge_tool_path" -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 209 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 210 | "$merge_tool_path" -o "$path" -- "$LOCAL" "$REMOTE" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 211 | fi |
| 212 | status=$? |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 213 | save_backup |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 214 | ;; |
James Bowes | 9cec653 | 2007-03-18 22:11:54 -0400 | [diff] [blame] | 215 | meld|vimdiff) |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 216 | touch "$BACKUP" |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 217 | "$merge_tool_path" -- "$LOCAL" "$path" "$REMOTE" |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 218 | check_unchanged |
| 219 | save_backup |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 220 | ;; |
Dan McGee | 730b5b4 | 2007-06-05 21:19:47 -0400 | [diff] [blame] | 221 | gvimdiff) |
| 222 | touch "$BACKUP" |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 223 | "$merge_tool_path" -f -- "$LOCAL" "$path" "$REMOTE" |
Dan McGee | 730b5b4 | 2007-06-05 21:19:47 -0400 | [diff] [blame] | 224 | check_unchanged |
| 225 | save_backup |
| 226 | ;; |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 227 | xxdiff) |
| 228 | touch "$BACKUP" |
| 229 | if base_present ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 230 | "$merge_tool_path" -X --show-merged-pane \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 231 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ |
| 232 | -R 'Accel.Search: "Ctrl+F"' \ |
| 233 | -R 'Accel.SearchForward: "Ctrl-G"' \ |
| 234 | --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE" |
| 235 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 236 | "$merge_tool_path" -X --show-merged-pane \ |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 237 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ |
| 238 | -R 'Accel.Search: "Ctrl+F"' \ |
| 239 | -R 'Accel.SearchForward: "Ctrl-G"' \ |
| 240 | --merged-file "$path" -- "$LOCAL" "$REMOTE" |
| 241 | fi |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 242 | check_unchanged |
| 243 | save_backup |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 244 | ;; |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 245 | opendiff) |
| 246 | touch "$BACKUP" |
| 247 | if base_present; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 248 | "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 249 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 250 | "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$path" | cat |
Theodore Ts'o | 365cf97 | 2007-03-29 10:03:17 -0400 | [diff] [blame] | 251 | fi |
| 252 | check_unchanged |
| 253 | save_backup |
| 254 | ;; |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 255 | ecmerge) |
| 256 | touch "$BACKUP" |
| 257 | if base_present; then |
| 258 | "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --mode=merge3 --to="$path" |
| 259 | else |
| 260 | "$merge_tool_path" "$LOCAL" "$REMOTE" --mode=merge2 --to="$path" |
| 261 | fi |
| 262 | check_unchanged |
| 263 | save_backup |
| 264 | ;; |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 265 | emerge) |
| 266 | if base_present ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 267 | "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 268 | else |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 269 | "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$path")" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 270 | fi |
| 271 | status=$? |
Theodore Ts'o | ddc0c49 | 2007-03-29 09:39:59 -0400 | [diff] [blame] | 272 | save_backup |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 273 | ;; |
| 274 | esac |
| 275 | if test "$status" -ne 0; then |
| 276 | echo "merge of $path failed" 1>&2 |
| 277 | mv -- "$BACKUP" "$path" |
| 278 | exit 1 |
| 279 | fi |
| 280 | git add -- "$path" |
| 281 | cleanup_temp_files |
| 282 | } |
| 283 | |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 284 | while test $# != 0 |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 285 | do |
| 286 | case "$1" in |
| 287 | -t|--tool*) |
| 288 | case "$#,$1" in |
| 289 | *,*=*) |
| 290 | merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'` |
| 291 | ;; |
| 292 | 1,*) |
| 293 | usage ;; |
| 294 | *) |
| 295 | merge_tool="$2" |
| 296 | shift ;; |
| 297 | esac |
| 298 | ;; |
| 299 | --) |
| 300 | break |
| 301 | ;; |
| 302 | -*) |
| 303 | usage |
| 304 | ;; |
| 305 | *) |
| 306 | break |
| 307 | ;; |
| 308 | esac |
| 309 | shift |
| 310 | done |
| 311 | |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 312 | valid_tool() { |
| 313 | case "$1" in |
Steffen Prohaska | ca8e6b7 | 2007-10-17 19:16:12 +0200 | [diff] [blame] | 314 | kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge) |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 315 | ;; # happy |
| 316 | *) |
| 317 | return 1 |
| 318 | ;; |
| 319 | esac |
| 320 | } |
| 321 | |
| 322 | init_merge_tool_path() { |
| 323 | merge_tool_path=`git config mergetool.$1.path` |
| 324 | if test -z "$merge_tool_path" ; then |
| 325 | case "$1" in |
| 326 | emerge) |
| 327 | merge_tool_path=emacs |
| 328 | ;; |
| 329 | *) |
| 330 | merge_tool_path=$1 |
| 331 | ;; |
| 332 | esac |
| 333 | fi |
| 334 | } |
| 335 | |
| 336 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 337 | if test -z "$merge_tool"; then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 338 | merge_tool=`git config merge.tool` |
Steffen Prohaska | d279fc1 | 2007-10-18 12:25:34 +0200 | [diff] [blame] | 339 | if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then |
Theodore Ts'o | d6678c2 | 2007-03-18 22:30:10 -0400 | [diff] [blame] | 340 | echo >&2 "git config option merge.tool set to unknown tool: $merge_tool" |
| 341 | echo >&2 "Resetting to default..." |
| 342 | unset merge_tool |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 343 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 344 | fi |
| 345 | |
| 346 | if test -z "$merge_tool" ; then |
Theodore Ts'o | 301ac38 | 2007-06-10 11:17:30 -0400 | [diff] [blame] | 347 | if test -n "$DISPLAY"; then |
| 348 | merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff" |
| 349 | if test -n "$GNOME_DESKTOP_SESSION_ID" ; then |
| 350 | merge_tool_candidates="meld $merge_tool_candidates" |
| 351 | fi |
| 352 | if test "$KDE_FULL_SESSION" = "true"; then |
| 353 | merge_tool_candidates="kdiff3 $merge_tool_candidates" |
| 354 | fi |
| 355 | fi |
| 356 | if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then |
| 357 | merge_tool_candidates="$merge_tool_candidates emerge" |
| 358 | fi |
| 359 | if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then |
| 360 | merge_tool_candidates="$merge_tool_candidates vimdiff" |
| 361 | fi |
| 362 | merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff" |
| 363 | echo "merge tool candidates: $merge_tool_candidates" |
| 364 | for i in $merge_tool_candidates; do |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 365 | init_merge_tool_path $i |
| 366 | if type "$merge_tool_path" > /dev/null 2>&1; then |
Theodore Ts'o | 301ac38 | 2007-06-10 11:17:30 -0400 | [diff] [blame] | 367 | merge_tool=$i |
| 368 | break |
| 369 | fi |
| 370 | done |
| 371 | if test -z "$merge_tool" ; then |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 372 | echo "No known merge resolution program available." |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 373 | exit 1 |
| 374 | fi |
Steffen Prohaska | e3fa2c7 | 2007-10-17 19:16:11 +0200 | [diff] [blame] | 375 | else |
| 376 | if ! valid_tool "$merge_tool"; then |
| 377 | echo >&2 "Unknown merge_tool $merge_tool" |
| 378 | exit 1 |
| 379 | fi |
| 380 | |
| 381 | init_merge_tool_path "$merge_tool" |
| 382 | |
| 383 | if ! type "$merge_tool_path" > /dev/null 2>&1; then |
| 384 | echo "The merge tool $merge_tool is not available as '$merge_tool_path'" |
| 385 | exit 1 |
| 386 | fi |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 387 | fi |
| 388 | |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 389 | |
| 390 | if test $# -eq 0 ; then |
| 391 | files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u` |
| 392 | if test -z "$files" ; then |
| 393 | echo "No files need merging" |
| 394 | exit 0 |
| 395 | fi |
Rogan Dawes | 2e8fd78 | 2008-01-07 09:37:38 +0200 | [diff] [blame] | 396 | echo Merging the files: "$files" |
| 397 | git ls-files -u | |
| 398 | sed -e 's/^[^ ]* //' | |
| 399 | sort -u | |
| 400 | while IFS= read i |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 401 | do |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 402 | printf "\n" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 403 | merge_file "$i" < /dev/tty > /dev/tty |
| 404 | done |
| 405 | else |
| 406 | while test $# -gt 0; do |
Theodore Ts'o | 20fa04e | 2007-03-27 12:12:22 -0400 | [diff] [blame] | 407 | printf "\n" |
Theodore Ts'o | c4b4a5a | 2007-03-06 00:05:16 -0500 | [diff] [blame] | 408 | merge_file "$1" |
| 409 | shift |
| 410 | done |
| 411 | fi |
| 412 | exit 0 |