Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 3 | # Copyright (c) Linus Torvalds, 2005 |
| 4 | # |
| 5 | # This is the git per-file merge script, called with |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 6 | # |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 7 | # $1 - original file SHA1 (or empty) |
| 8 | # $2 - file in branch1 SHA1 (or empty) |
| 9 | # $3 - file in branch2 SHA1 (or empty) |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 10 | # $4 - pathname in repository |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 11 | # $5 - original file mode (or empty) |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 12 | # $6 - file in branch1 mode (or empty) |
| 13 | # $7 - file in branch2 mode (or empty) |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 14 | # |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 15 | # Handle some trivial cases.. The _really_ trivial cases have |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 16 | # been handled already by git read-tree, but that one doesn't |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 17 | # do any merges that might change the tree layout. |
James Bottomley | 0a9ea85 | 2005-04-18 19:55:19 -0700 | [diff] [blame] | 18 | |
Jonathan Nieder | ae5bdda | 2009-11-09 09:04:53 -0600 | [diff] [blame] | 19 | USAGE='<orig blob> <our blob> <their blob> <path>' |
| 20 | USAGE="$USAGE <orig mode> <our mode> <their mode>" |
David Aguilar | e5a1518 | 2013-02-23 16:50:12 -0800 | [diff] [blame] | 21 | LONG_USAGE="usage: git merge-one-file $USAGE |
Jonathan Nieder | ae5bdda | 2009-11-09 09:04:53 -0600 | [diff] [blame] | 22 | |
| 23 | Blob ids and modes should be empty for missing files." |
| 24 | |
Jeff King | 6aaeca9 | 2011-04-29 18:24:32 -0400 | [diff] [blame] | 25 | SUBDIRECTORY_OK=Yes |
| 26 | . git-sh-setup |
| 27 | cd_to_toplevel |
| 28 | require_work_tree |
| 29 | |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 30 | if test $# != 7 |
Jonathan Nieder | ae5bdda | 2009-11-09 09:04:53 -0600 | [diff] [blame] | 31 | then |
| 32 | echo "$LONG_USAGE" |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 36 | case "${1:-.}${2:-.}${3:-.}" in |
| 37 | # |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 38 | # Deleted in both or deleted in one and unchanged in the other |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 39 | # |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 40 | "$1.." | "$1.$1" | "$1$1.") |
Jeff King | 72fac66 | 2015-10-26 17:39:39 -0400 | [diff] [blame] | 41 | if { test -z "$6" && test "$5" != "$7"; } || |
| 42 | { test -z "$7" && test "$5" != "$6"; } |
| 43 | then |
| 44 | echo "ERROR: File $4 deleted on one branch but had its" >&2 |
| 45 | echo "ERROR: permissions changed on the other." >&2 |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 49 | if test -n "$2" |
| 50 | then |
Linus Torvalds | cdcb0ed | 2005-08-29 22:36:16 -0700 | [diff] [blame] | 51 | echo "Removing $4" |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 52 | else |
| 53 | # read-tree checked that index matches HEAD already, |
| 54 | # so we know we do not have this path tracked. |
| 55 | # there may be an unrelated working tree file here, |
Junio C Hamano | 561b0fb | 2007-07-08 14:42:05 -0700 | [diff] [blame] | 56 | # which we should just leave unmolested. Make sure |
| 57 | # we do not have it in the index, though. |
| 58 | exec git update-index --remove -- "$4" |
Linus Torvalds | cdcb0ed | 2005-08-29 22:36:16 -0700 | [diff] [blame] | 59 | fi |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 60 | if test -f "$4" |
| 61 | then |
Junio C Hamano | 397c766 | 2005-11-19 19:50:44 -0800 | [diff] [blame] | 62 | rm -f -- "$4" && |
Mark Wooding | f327dbc | 2006-04-13 22:01:24 +0000 | [diff] [blame] | 63 | rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || : |
Junio C Hamano | aacc15e | 2005-06-25 02:24:50 -0700 | [diff] [blame] | 64 | fi && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 65 | exec git update-index --remove -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 66 | ;; |
| 67 | |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 68 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 69 | # Added in one. |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 70 | # |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 71 | ".$2.") |
| 72 | # the other side did not add and we added so there is nothing |
Junio C Hamano | 561b0fb | 2007-07-08 14:42:05 -0700 | [diff] [blame] | 73 | # to be done, except making the path merged. |
| 74 | exec git update-index --add --cacheinfo "$6" "$2" "$4" |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 75 | ;; |
| 76 | "..$3") |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 77 | echo "Adding $4" |
Junio C Hamano | 29dc133 | 2008-03-17 16:47:18 -0700 | [diff] [blame] | 78 | if test -f "$4" |
| 79 | then |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 80 | echo "ERROR: untracked $4 is overwritten by the merge." >&2 |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 81 | exit 1 |
Junio C Hamano | 29dc133 | 2008-03-17 16:47:18 -0700 | [diff] [blame] | 82 | fi |
Junio C Hamano | 561b0fb | 2007-07-08 14:42:05 -0700 | [diff] [blame] | 83 | git update-index --add --cacheinfo "$7" "$3" "$4" && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 84 | exec git checkout-index -u -f -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 85 | ;; |
| 86 | |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 87 | # |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 88 | # Added in both, identically (check for same permissions). |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 89 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 90 | ".$3$2") |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 91 | if test "$6" != "$7" |
| 92 | then |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 93 | echo "ERROR: File $4 added identically in both branches," >&2 |
| 94 | echo "ERROR: but permissions conflict $6->$7." >&2 |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 95 | exit 1 |
| 96 | fi |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 97 | echo "Adding $4" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 98 | git update-index --add --cacheinfo "$6" "$2" "$4" && |
| 99 | exec git checkout-index -u -f -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 100 | ;; |
| 101 | |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 102 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 103 | # Modified in both, but differently. |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 104 | # |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 105 | "$1$2$3" | ".$2$3") |
Junio C Hamano | 54dd99a | 2005-12-02 00:54:50 -0800 | [diff] [blame] | 106 | |
| 107 | case ",$6,$7," in |
| 108 | *,120000,*) |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 109 | echo "ERROR: $4: Not merging symbolic link changes." >&2 |
Junio C Hamano | 54dd99a | 2005-12-02 00:54:50 -0800 | [diff] [blame] | 110 | exit 1 |
| 111 | ;; |
Junio C Hamano | ff72af0 | 2007-12-10 11:22:05 -0800 | [diff] [blame] | 112 | *,160000,*) |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 113 | echo "ERROR: $4: Not merging conflicting submodule changes." >&2 |
Junio C Hamano | ff72af0 | 2007-12-10 11:22:05 -0800 | [diff] [blame] | 114 | exit 1 |
| 115 | ;; |
Junio C Hamano | 54dd99a | 2005-12-02 00:54:50 -0800 | [diff] [blame] | 116 | esac |
| 117 | |
Michael Forney | 974ce80 | 2017-08-04 23:49:05 -0700 | [diff] [blame] | 118 | src1=$(git unpack-file $2) |
| 119 | src2=$(git unpack-file $3) |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 120 | case "$1" in |
| 121 | '') |
| 122 | echo "Added $4 in both, but differently." |
brian m. carlson | 7882fa2 | 2018-05-02 00:26:10 +0000 | [diff] [blame] | 123 | orig=$(git unpack-file $(git hash-object /dev/null)) |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 124 | ;; |
| 125 | *) |
Alex Riesen | 31f883d | 2006-01-05 12:46:16 +0100 | [diff] [blame] | 126 | echo "Auto-merging $4" |
Michael Forney | 974ce80 | 2017-08-04 23:49:05 -0700 | [diff] [blame] | 127 | orig=$(git unpack-file $1) |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 128 | ;; |
| 129 | esac |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 130 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 131 | git merge-file "$src1" "$orig" "$src2" |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 132 | ret=$? |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 133 | msg= |
Junio C Hamano | 4fa5c05 | 2013-03-25 10:05:13 -0700 | [diff] [blame] | 134 | if test $ret != 0 || test -z "$1" |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 135 | then |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 136 | msg='content conflict' |
Junio C Hamano | 4fa5c05 | 2013-03-25 10:05:13 -0700 | [diff] [blame] | 137 | ret=1 |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 138 | fi |
Junio C Hamano | b539c5e | 2005-12-07 00:50:33 -0800 | [diff] [blame] | 139 | |
| 140 | # Create the working tree file, using "our tree" version from the |
| 141 | # index, and then store the result of the merge. |
Jeff King | 6aaeca9 | 2011-04-29 18:24:32 -0400 | [diff] [blame] | 142 | git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1 |
Junio C Hamano | b539c5e | 2005-12-07 00:50:33 -0800 | [diff] [blame] | 143 | rm -f -- "$orig" "$src1" "$src2" |
Petr Baudis | 8544a6f | 2005-06-08 23:26:55 +0200 | [diff] [blame] | 144 | |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 145 | if test "$6" != "$7" |
| 146 | then |
| 147 | if test -n "$msg" |
| 148 | then |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 149 | msg="$msg, " |
| 150 | fi |
| 151 | msg="${msg}permissions conflict: $5->$6,$7" |
Linus Torvalds | 2a68a86 | 2005-06-08 11:40:59 -0700 | [diff] [blame] | 152 | ret=1 |
James Bottomley | 0a9ea85 | 2005-04-18 19:55:19 -0700 | [diff] [blame] | 153 | fi |
Petr Baudis | 8544a6f | 2005-06-08 23:26:55 +0200 | [diff] [blame] | 154 | |
Kevin Bracey | 333ea38 | 2013-03-24 14:26:23 +0200 | [diff] [blame] | 155 | if test $ret != 0 |
| 156 | then |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 157 | echo "ERROR: $msg in $4" >&2 |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 158 | exit 1 |
| 159 | fi |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 160 | exec git update-index -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 161 | ;; |
| 162 | |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 163 | *) |
Kevin Bracey | d401acf | 2013-03-24 14:26:24 +0200 | [diff] [blame] | 164 | echo "ERROR: $4: Not handling case $1 -> $2 -> $3" >&2 |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 165 | ;; |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 166 | esac |
| 167 | exit 1 |