blob: 9ee3f804520eadb322de3d0ea70a2bf9f092b089 [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 Hamano5be60072007-07-02 22:52:14 -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,
Junio C Hamano561b0fb2007-07-08 14:42:05 -070030 # 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 Torvaldscdcb0ed2005-08-29 22:36:16 -070033 fi
Petr Baudis0b124bb2005-07-29 11:00:45 +020034 if test -f "$4"; then
Junio C Hamano397c7662005-11-19 19:50:44 -080035 rm -f -- "$4" &&
Mark Woodingf327dbc2006-04-13 22:01:24 +000036 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
Junio C Hamanoaacc15e2005-06-25 02:24:50 -070037 fi &&
Junio C Hamano5be60072007-07-02 22:52:14 -070038 exec git update-index --remove -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020039 ;;
40
Linus Torvalds839a7a02005-04-18 12:15:10 -070041#
Junio C Hamanoee281522005-05-01 23:53:32 -070042# Added in one.
Linus Torvalds839a7a02005-04-18 12:15:10 -070043#
Junio C Hamanoed93b442006-10-08 22:48:06 -070044".$2.")
45 # the other side did not add and we added so there is nothing
Junio C Hamano561b0fb2007-07-08 14:42:05 -070046 # to be done, except making the path merged.
47 exec git update-index --add --cacheinfo "$6" "$2" "$4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070048 ;;
49"..$3")
Linus Torvalds98a96b02005-06-08 17:56:09 -070050 echo "Adding $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070051 test -f "$4" || {
52 echo "ERROR: untracked $4 is overwritten by the merge."
53 exit 1
54 }
Junio C Hamano561b0fb2007-07-08 14:42:05 -070055 git update-index --add --cacheinfo "$7" "$3" "$4" &&
Junio C Hamano5be60072007-07-02 22:52:14 -070056 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020057 ;;
58
James Bottomleye2b6a9d2005-04-23 20:50:10 -070059#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080060# Added in both, identically (check for same permissions).
James Bottomleye2b6a9d2005-04-23 20:50:10 -070061#
Junio C Hamanoee281522005-05-01 23:53:32 -070062".$3$2")
James Bottomleye2b6a9d2005-04-23 20:50:10 -070063 if [ "$6" != "$7" ]; then
Junio C Hamanoee281522005-05-01 23:53:32 -070064 echo "ERROR: File $4 added identically in both branches,"
65 echo "ERROR: but permissions conflict $6->$7."
James Bottomleye2b6a9d2005-04-23 20:50:10 -070066 exit 1
67 fi
Linus Torvalds98a96b02005-06-08 17:56:09 -070068 echo "Adding $4"
Junio C Hamano5be60072007-07-02 22:52:14 -070069 git update-index --add --cacheinfo "$6" "$2" "$4" &&
70 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020071 ;;
72
Linus Torvalds839a7a02005-04-18 12:15:10 -070073#
Junio C Hamanoee281522005-05-01 23:53:32 -070074# Modified in both, but differently.
Linus Torvalds839a7a02005-04-18 12:15:10 -070075#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080076"$1$2$3" | ".$2$3")
Junio C Hamano54dd99a2005-12-02 00:54:50 -080077
78 case ",$6,$7," in
79 *,120000,*)
80 echo "ERROR: $4: Not merging symbolic link changes."
81 exit 1
82 ;;
Junio C Hamanoff72af02007-12-10 11:22:05 -080083 *,160000,*)
84 echo "ERROR: $4: Not merging conflicting submodule changes."
85 exit 1
86 ;;
Junio C Hamano54dd99a2005-12-02 00:54:50 -080087 esac
88
Junio C Hamanocb93c192005-11-09 20:38:33 -080089 src2=`git-unpack-file $3`
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080090 case "$1" in
91 '')
92 echo "Added $4 in both, but differently."
Junio C Hamano5be60072007-07-02 22:52:14 -070093 # This extracts OUR file in $orig, and uses git apply to
Junio C Hamanocb93c192005-11-09 20:38:33 -080094 # remove lines that are unique to ours.
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080095 orig=`git-unpack-file $2`
Junio C Hamanofd66dbf2005-11-10 19:30:23 -080096 sz0=`wc -c <"$orig"`
Junio C Hamano5be60072007-07-02 22:52:14 -070097 diff -u -La/$orig -Lb/$orig $orig $src2 | git apply --no-add
Junio C Hamanofd66dbf2005-11-10 19:30:23 -080098 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 Hamanof7d24bb2005-11-07 22:03:46 -0800103 ;;
104 *)
Alex Riesen31f883d2006-01-05 12:46:16 +0100105 echo "Auto-merging $4"
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800106 orig=`git-unpack-file $1`
107 ;;
108 esac
Petr Baudisec739622005-06-09 01:38:20 +0200109
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800110 # Be careful for funny filename such as "-L" in "$4", which
111 # would confuse "merge" greatly.
112 src1=`git-unpack-file $2`
Junio C Hamano5be60072007-07-02 22:52:14 -0700113 git merge-file "$src1" "$orig" "$src2"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700114 ret=$?
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800115
116 # Create the working tree file, using "our tree" version from the
117 # index, and then store the result of the merge.
Junio C Hamano5be60072007-07-02 22:52:14 -0700118 git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4"
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800119 rm -f -- "$orig" "$src1" "$src2"
Petr Baudis8544a6f2005-06-08 23:26:55 +0200120
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700121 if [ "$6" != "$7" ]; then
Petr Baudisec739622005-06-09 01:38:20 +0200122 echo "ERROR: Permissions conflict: $5->$6,$7."
Linus Torvalds2a68a862005-06-08 11:40:59 -0700123 ret=1
James Bottomley0a9ea852005-04-18 19:55:19 -0700124 fi
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800125 if [ "$1" = '' ]; then
126 ret=1
127 fi
Petr Baudis8544a6f2005-06-08 23:26:55 +0200128
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700129 if [ $ret -ne 0 ]; then
Alex Riesen31f883d2006-01-05 12:46:16 +0100130 echo "ERROR: Merge conflict in $4"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700131 exit 1
132 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700133 exec git update-index -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +0200134 ;;
135
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700136*)
Petr Baudisec739622005-06-09 01:38:20 +0200137 echo "ERROR: $4: Not handling case $1 -> $2 -> $3"
138 ;;
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700139esac
140exit 1