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