blob: 7d62d7902cb1d7cd2749091b86d532306bea89b3 [file] [log] [blame]
Linus Torvalds839a7a02005-04-18 12:15:10 -07001#!/bin/sh
2#
Petr Baudisec739622005-06-09 01:38:20 +02003# Copyright (c) Linus Torvalds, 2005
4#
5# This is the git per-file merge script, called with
Linus Torvalds839a7a02005-04-18 12:15:10 -07006#
Linus Torvaldse3b4be72005-04-18 14:17:58 -07007# $1 - original file SHA1 (or empty)
8# $2 - file in branch1 SHA1 (or empty)
9# $3 - file in branch2 SHA1 (or empty)
Linus Torvalds839a7a02005-04-18 12:15:10 -070010# $4 - pathname in repository
Pavel Roskin82e5a822006-07-10 01:50:18 -040011# $5 - original file mode (or empty)
Junio C Hamanoee281522005-05-01 23:53:32 -070012# $6 - file in branch1 mode (or empty)
13# $7 - file in branch2 mode (or empty)
Linus Torvalds839a7a02005-04-18 12:15:10 -070014#
Linus Torvaldse3b4be72005-04-18 14:17:58 -070015# Handle some trivial cases.. The _really_ trivial cases have
Junio C Hamano0fc65a42005-04-29 16:25:05 -070016# been handled already by git-read-tree, but that one doesn't
Petr Baudisec739622005-06-09 01:38:20 +020017# do any merges that might change the tree layout.
James Bottomley0a9ea852005-04-18 19:55:19 -070018
Linus Torvaldse3b4be72005-04-18 14:17:58 -070019case "${1:-.}${2:-.}${3:-.}" in
20#
Linus Torvalds98a96b02005-06-08 17:56:09 -070021# Deleted in both or deleted in one and unchanged in the other
James Bottomleye2b6a9d2005-04-23 20:50:10 -070022#
Linus Torvalds98a96b02005-06-08 17:56:09 -070023"$1.." | "$1.$1" | "$1$1.")
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070024 if [ "$2" ]; then
25 echo "Removing $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070026 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,
30 # which we should just leave unmolested.
31 exit 0
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070032 fi
Petr Baudis0b124bb2005-07-29 11:00:45 +020033 if test -f "$4"; then
Junio C Hamano397c7662005-11-19 19:50:44 -080034 rm -f -- "$4" &&
Mark Woodingf327dbc2006-04-13 22:01:24 +000035 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
Junio C Hamanoaacc15e2005-06-25 02:24:50 -070036 fi &&
Junio C Hamano215a7ad2005-09-07 17:26:23 -070037 exec git-update-index --remove -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020038 ;;
39
Linus Torvalds839a7a02005-04-18 12:15:10 -070040#
Junio C Hamanoee281522005-05-01 23:53:32 -070041# Added in one.
Linus Torvalds839a7a02005-04-18 12:15:10 -070042#
Junio C Hamanoed93b442006-10-08 22:48:06 -070043".$2.")
44 # the other side did not add and we added so there is nothing
45 # to be done.
46 ;;
47"..$3")
Linus Torvalds98a96b02005-06-08 17:56:09 -070048 echo "Adding $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070049 test -f "$4" || {
50 echo "ERROR: untracked $4 is overwritten by the merge."
51 exit 1
52 }
Junio C Hamano215a7ad2005-09-07 17:26:23 -070053 git-update-index --add --cacheinfo "$6$7" "$2$3" "$4" &&
54 exec git-checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020055 ;;
56
James Bottomleye2b6a9d2005-04-23 20:50:10 -070057#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080058# Added in both, identically (check for same permissions).
James Bottomleye2b6a9d2005-04-23 20:50:10 -070059#
Junio C Hamanoee281522005-05-01 23:53:32 -070060".$3$2")
James Bottomleye2b6a9d2005-04-23 20:50:10 -070061 if [ "$6" != "$7" ]; then
Junio C Hamanoee281522005-05-01 23:53:32 -070062 echo "ERROR: File $4 added identically in both branches,"
63 echo "ERROR: but permissions conflict $6->$7."
James Bottomleye2b6a9d2005-04-23 20:50:10 -070064 exit 1
65 fi
Linus Torvalds98a96b02005-06-08 17:56:09 -070066 echo "Adding $4"
Junio C Hamano215a7ad2005-09-07 17:26:23 -070067 git-update-index --add --cacheinfo "$6" "$2" "$4" &&
68 exec git-checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020069 ;;
70
Linus Torvalds839a7a02005-04-18 12:15:10 -070071#
Junio C Hamanoee281522005-05-01 23:53:32 -070072# Modified in both, but differently.
Linus Torvalds839a7a02005-04-18 12:15:10 -070073#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080074"$1$2$3" | ".$2$3")
Junio C Hamano54dd99a2005-12-02 00:54:50 -080075
76 case ",$6,$7," in
77 *,120000,*)
78 echo "ERROR: $4: Not merging symbolic link changes."
79 exit 1
80 ;;
81 esac
82
Junio C Hamanocb93c192005-11-09 20:38:33 -080083 src2=`git-unpack-file $3`
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080084 case "$1" in
85 '')
86 echo "Added $4 in both, but differently."
Junio C Hamanocb93c192005-11-09 20:38:33 -080087 # This extracts OUR file in $orig, and uses git-apply to
88 # remove lines that are unique to ours.
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080089 orig=`git-unpack-file $2`
Junio C Hamanofd66dbf2005-11-10 19:30:23 -080090 sz0=`wc -c <"$orig"`
Junio C Hamanocb93c192005-11-09 20:38:33 -080091 diff -u -La/$orig -Lb/$orig $orig $src2 | git-apply --no-add
Junio C Hamanofd66dbf2005-11-10 19:30:23 -080092 sz1=`wc -c <"$orig"`
93
94 # If we do not have enough common material, it is not
95 # worth trying two-file merge using common subsections.
96 expr "$sz0" \< "$sz1" \* 2 >/dev/null || : >$orig
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080097 ;;
98 *)
Alex Riesen31f883d2006-01-05 12:46:16 +010099 echo "Auto-merging $4"
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800100 orig=`git-unpack-file $1`
101 ;;
102 esac
Petr Baudisec739622005-06-09 01:38:20 +0200103
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800104 # Be careful for funny filename such as "-L" in "$4", which
105 # would confuse "merge" greatly.
106 src1=`git-unpack-file $2`
Johannes Schindelincaba1392006-12-22 03:20:55 +0100107 git-merge-file "$src1" "$orig" "$src2"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700108 ret=$?
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800109
110 # Create the working tree file, using "our tree" version from the
111 # index, and then store the result of the merge.
112 git-checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4"
113 rm -f -- "$orig" "$src1" "$src2"
Petr Baudis8544a6f2005-06-08 23:26:55 +0200114
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700115 if [ "$6" != "$7" ]; then
Petr Baudisec739622005-06-09 01:38:20 +0200116 echo "ERROR: Permissions conflict: $5->$6,$7."
Linus Torvalds2a68a862005-06-08 11:40:59 -0700117 ret=1
James Bottomley0a9ea852005-04-18 19:55:19 -0700118 fi
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800119 if [ "$1" = '' ]; then
120 ret=1
121 fi
Petr Baudis8544a6f2005-06-08 23:26:55 +0200122
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700123 if [ $ret -ne 0 ]; then
Alex Riesen31f883d2006-01-05 12:46:16 +0100124 echo "ERROR: Merge conflict in $4"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700125 exit 1
126 fi
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700127 exec git-update-index -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +0200128 ;;
129
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700130*)
Petr Baudisec739622005-06-09 01:38:20 +0200131 echo "ERROR: $4: Not handling case $1 -> $2 -> $3"
132 ;;
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700133esac
134exit 1