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>" |
| 21 | LONG_USAGE="Usage: git merge-one-file $USAGE |
| 22 | |
| 23 | Blob ids and modes should be empty for missing files." |
| 24 | |
| 25 | if ! test "$#" -eq 7 |
| 26 | then |
| 27 | echo "$LONG_USAGE" |
| 28 | exit 1 |
| 29 | fi |
| 30 | |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 31 | case "${1:-.}${2:-.}${3:-.}" in |
| 32 | # |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 33 | # Deleted in both or deleted in one and unchanged in the other |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 34 | # |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 35 | "$1.." | "$1.$1" | "$1$1.") |
Linus Torvalds | cdcb0ed | 2005-08-29 22:36:16 -0700 | [diff] [blame] | 36 | if [ "$2" ]; then |
| 37 | echo "Removing $4" |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 38 | else |
| 39 | # read-tree checked that index matches HEAD already, |
| 40 | # so we know we do not have this path tracked. |
| 41 | # there may be an unrelated working tree file here, |
Junio C Hamano | 561b0fb | 2007-07-08 14:42:05 -0700 | [diff] [blame] | 42 | # which we should just leave unmolested. Make sure |
| 43 | # we do not have it in the index, though. |
| 44 | exec git update-index --remove -- "$4" |
Linus Torvalds | cdcb0ed | 2005-08-29 22:36:16 -0700 | [diff] [blame] | 45 | fi |
Petr Baudis | 0b124bb | 2005-07-29 11:00:45 +0200 | [diff] [blame] | 46 | if test -f "$4"; then |
Junio C Hamano | 397c766 | 2005-11-19 19:50:44 -0800 | [diff] [blame] | 47 | rm -f -- "$4" && |
Mark Wooding | f327dbc | 2006-04-13 22:01:24 +0000 | [diff] [blame] | 48 | rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || : |
Junio C Hamano | aacc15e | 2005-06-25 02:24:50 -0700 | [diff] [blame] | 49 | fi && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 50 | exec git update-index --remove -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 51 | ;; |
| 52 | |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 53 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 54 | # Added in one. |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 55 | # |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 56 | ".$2.") |
| 57 | # 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] | 58 | # to be done, except making the path merged. |
| 59 | exec git update-index --add --cacheinfo "$6" "$2" "$4" |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 60 | ;; |
| 61 | "..$3") |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 62 | echo "Adding $4" |
Junio C Hamano | 29dc133 | 2008-03-17 16:47:18 -0700 | [diff] [blame] | 63 | if test -f "$4" |
| 64 | then |
Junio C Hamano | ed93b44 | 2006-10-08 22:48:06 -0700 | [diff] [blame] | 65 | echo "ERROR: untracked $4 is overwritten by the merge." |
| 66 | exit 1 |
Junio C Hamano | 29dc133 | 2008-03-17 16:47:18 -0700 | [diff] [blame] | 67 | fi |
Junio C Hamano | 561b0fb | 2007-07-08 14:42:05 -0700 | [diff] [blame] | 68 | git update-index --add --cacheinfo "$7" "$3" "$4" && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 69 | exec git checkout-index -u -f -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 70 | ;; |
| 71 | |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 72 | # |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 73 | # Added in both, identically (check for same permissions). |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 74 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 75 | ".$3$2") |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 76 | if [ "$6" != "$7" ]; then |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 77 | echo "ERROR: File $4 added identically in both branches," |
| 78 | echo "ERROR: but permissions conflict $6->$7." |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 79 | exit 1 |
| 80 | fi |
Linus Torvalds | 98a96b0 | 2005-06-08 17:56:09 -0700 | [diff] [blame] | 81 | echo "Adding $4" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 82 | git update-index --add --cacheinfo "$6" "$2" "$4" && |
| 83 | exec git checkout-index -u -f -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 84 | ;; |
| 85 | |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 86 | # |
Junio C Hamano | ee28152 | 2005-05-01 23:53:32 -0700 | [diff] [blame] | 87 | # Modified in both, but differently. |
Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 88 | # |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 89 | "$1$2$3" | ".$2$3") |
Junio C Hamano | 54dd99a | 2005-12-02 00:54:50 -0800 | [diff] [blame] | 90 | |
| 91 | case ",$6,$7," in |
| 92 | *,120000,*) |
| 93 | echo "ERROR: $4: Not merging symbolic link changes." |
| 94 | exit 1 |
| 95 | ;; |
Junio C Hamano | ff72af0 | 2007-12-10 11:22:05 -0800 | [diff] [blame] | 96 | *,160000,*) |
| 97 | echo "ERROR: $4: Not merging conflicting submodule changes." |
| 98 | exit 1 |
| 99 | ;; |
Junio C Hamano | 54dd99a | 2005-12-02 00:54:50 -0800 | [diff] [blame] | 100 | esac |
| 101 | |
Junio C Hamano | cb93c19 | 2005-11-09 20:38:33 -0800 | [diff] [blame] | 102 | src2=`git-unpack-file $3` |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 103 | case "$1" in |
| 104 | '') |
| 105 | echo "Added $4 in both, but differently." |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 106 | # This extracts OUR file in $orig, and uses git apply to |
Junio C Hamano | cb93c19 | 2005-11-09 20:38:33 -0800 | [diff] [blame] | 107 | # remove lines that are unique to ours. |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 108 | orig=`git-unpack-file $2` |
Junio C Hamano | fd66dbf | 2005-11-10 19:30:23 -0800 | [diff] [blame] | 109 | sz0=`wc -c <"$orig"` |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 110 | diff -u -La/$orig -Lb/$orig $orig $src2 | git apply --no-add |
Junio C Hamano | fd66dbf | 2005-11-10 19:30:23 -0800 | [diff] [blame] | 111 | sz1=`wc -c <"$orig"` |
| 112 | |
| 113 | # If we do not have enough common material, it is not |
| 114 | # worth trying two-file merge using common subsections. |
| 115 | expr "$sz0" \< "$sz1" \* 2 >/dev/null || : >$orig |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 116 | ;; |
| 117 | *) |
Alex Riesen | 31f883d | 2006-01-05 12:46:16 +0100 | [diff] [blame] | 118 | echo "Auto-merging $4" |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 119 | orig=`git-unpack-file $1` |
| 120 | ;; |
| 121 | esac |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 122 | |
Junio C Hamano | b539c5e | 2005-12-07 00:50:33 -0800 | [diff] [blame] | 123 | # Be careful for funny filename such as "-L" in "$4", which |
| 124 | # would confuse "merge" greatly. |
| 125 | src1=`git-unpack-file $2` |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 126 | git merge-file "$src1" "$orig" "$src2" |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 127 | ret=$? |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 128 | msg= |
| 129 | if [ $ret -ne 0 ]; then |
| 130 | msg='content conflict' |
| 131 | fi |
Junio C Hamano | b539c5e | 2005-12-07 00:50:33 -0800 | [diff] [blame] | 132 | |
| 133 | # Create the working tree file, using "our tree" version from the |
| 134 | # index, and then store the result of the merge. |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 135 | git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" |
Junio C Hamano | b539c5e | 2005-12-07 00:50:33 -0800 | [diff] [blame] | 136 | rm -f -- "$orig" "$src1" "$src2" |
Petr Baudis | 8544a6f | 2005-06-08 23:26:55 +0200 | [diff] [blame] | 137 | |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 138 | if [ "$6" != "$7" ]; then |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 139 | if [ -n "$msg" ]; then |
| 140 | msg="$msg, " |
| 141 | fi |
| 142 | msg="${msg}permissions conflict: $5->$6,$7" |
Linus Torvalds | 2a68a86 | 2005-06-08 11:40:59 -0700 | [diff] [blame] | 143 | ret=1 |
James Bottomley | 0a9ea85 | 2005-04-18 19:55:19 -0700 | [diff] [blame] | 144 | fi |
Junio C Hamano | f7d24bb | 2005-11-07 22:03:46 -0800 | [diff] [blame] | 145 | if [ "$1" = '' ]; then |
| 146 | ret=1 |
| 147 | fi |
Petr Baudis | 8544a6f | 2005-06-08 23:26:55 +0200 | [diff] [blame] | 148 | |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 149 | if [ $ret -ne 0 ]; then |
Alex Riesen | 718135e | 2009-04-29 23:40:50 +0200 | [diff] [blame] | 150 | echo "ERROR: $msg in $4" |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 151 | exit 1 |
| 152 | fi |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 153 | exec git update-index -- "$4" |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 154 | ;; |
| 155 | |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 156 | *) |
Petr Baudis | ec73962 | 2005-06-09 01:38:20 +0200 | [diff] [blame] | 157 | echo "ERROR: $4: Not handling case $1 -> $2 -> $3" |
| 158 | ;; |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 159 | esac |
| 160 | exit 1 |