blob: d067894bf45fd0a50513e196ea2a5e671d901681 [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
Jonathan Niederae5bdda2009-11-09 09:04:53 -060019USAGE='<orig blob> <our blob> <their blob> <path>'
20USAGE="$USAGE <orig mode> <our mode> <their mode>"
21LONG_USAGE="Usage: git merge-one-file $USAGE
22
23Blob ids and modes should be empty for missing files."
24
25if ! test "$#" -eq 7
26then
27 echo "$LONG_USAGE"
28 exit 1
29fi
30
Linus Torvaldse3b4be72005-04-18 14:17:58 -070031case "${1:-.}${2:-.}${3:-.}" in
32#
Linus Torvalds98a96b02005-06-08 17:56:09 -070033# Deleted in both or deleted in one and unchanged in the other
James Bottomleye2b6a9d2005-04-23 20:50:10 -070034#
Linus Torvalds98a96b02005-06-08 17:56:09 -070035"$1.." | "$1.$1" | "$1$1.")
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070036 if [ "$2" ]; then
37 echo "Removing $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070038 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 Hamano561b0fb2007-07-08 14:42:05 -070042 # 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 Torvaldscdcb0ed2005-08-29 22:36:16 -070045 fi
Petr Baudis0b124bb2005-07-29 11:00:45 +020046 if test -f "$4"; then
Junio C Hamano397c7662005-11-19 19:50:44 -080047 rm -f -- "$4" &&
Mark Woodingf327dbc2006-04-13 22:01:24 +000048 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
Junio C Hamanoaacc15e2005-06-25 02:24:50 -070049 fi &&
Junio C Hamano5be60072007-07-02 22:52:14 -070050 exec git update-index --remove -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020051 ;;
52
Linus Torvalds839a7a02005-04-18 12:15:10 -070053#
Junio C Hamanoee281522005-05-01 23:53:32 -070054# Added in one.
Linus Torvalds839a7a02005-04-18 12:15:10 -070055#
Junio C Hamanoed93b442006-10-08 22:48:06 -070056".$2.")
57 # the other side did not add and we added so there is nothing
Junio C Hamano561b0fb2007-07-08 14:42:05 -070058 # to be done, except making the path merged.
59 exec git update-index --add --cacheinfo "$6" "$2" "$4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070060 ;;
61"..$3")
Linus Torvalds98a96b02005-06-08 17:56:09 -070062 echo "Adding $4"
Junio C Hamano29dc1332008-03-17 16:47:18 -070063 if test -f "$4"
64 then
Junio C Hamanoed93b442006-10-08 22:48:06 -070065 echo "ERROR: untracked $4 is overwritten by the merge."
66 exit 1
Junio C Hamano29dc1332008-03-17 16:47:18 -070067 fi
Junio C Hamano561b0fb2007-07-08 14:42:05 -070068 git update-index --add --cacheinfo "$7" "$3" "$4" &&
Junio C Hamano5be60072007-07-02 22:52:14 -070069 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020070 ;;
71
James Bottomleye2b6a9d2005-04-23 20:50:10 -070072#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080073# Added in both, identically (check for same permissions).
James Bottomleye2b6a9d2005-04-23 20:50:10 -070074#
Junio C Hamanoee281522005-05-01 23:53:32 -070075".$3$2")
James Bottomleye2b6a9d2005-04-23 20:50:10 -070076 if [ "$6" != "$7" ]; then
Junio C Hamanoee281522005-05-01 23:53:32 -070077 echo "ERROR: File $4 added identically in both branches,"
78 echo "ERROR: but permissions conflict $6->$7."
James Bottomleye2b6a9d2005-04-23 20:50:10 -070079 exit 1
80 fi
Linus Torvalds98a96b02005-06-08 17:56:09 -070081 echo "Adding $4"
Junio C Hamano5be60072007-07-02 22:52:14 -070082 git update-index --add --cacheinfo "$6" "$2" "$4" &&
83 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020084 ;;
85
Linus Torvalds839a7a02005-04-18 12:15:10 -070086#
Junio C Hamanoee281522005-05-01 23:53:32 -070087# Modified in both, but differently.
Linus Torvalds839a7a02005-04-18 12:15:10 -070088#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080089"$1$2$3" | ".$2$3")
Junio C Hamano54dd99a2005-12-02 00:54:50 -080090
91 case ",$6,$7," in
92 *,120000,*)
93 echo "ERROR: $4: Not merging symbolic link changes."
94 exit 1
95 ;;
Junio C Hamanoff72af02007-12-10 11:22:05 -080096 *,160000,*)
97 echo "ERROR: $4: Not merging conflicting submodule changes."
98 exit 1
99 ;;
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800100 esac
101
Junio C Hamanocb93c192005-11-09 20:38:33 -0800102 src2=`git-unpack-file $3`
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800103 case "$1" in
104 '')
105 echo "Added $4 in both, but differently."
Junio C Hamano5be60072007-07-02 22:52:14 -0700106 # This extracts OUR file in $orig, and uses git apply to
Junio C Hamanocb93c192005-11-09 20:38:33 -0800107 # remove lines that are unique to ours.
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800108 orig=`git-unpack-file $2`
Junio C Hamanofd66dbf2005-11-10 19:30:23 -0800109 sz0=`wc -c <"$orig"`
Junio C Hamano5be60072007-07-02 22:52:14 -0700110 diff -u -La/$orig -Lb/$orig $orig $src2 | git apply --no-add
Junio C Hamanofd66dbf2005-11-10 19:30:23 -0800111 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 Hamanof7d24bb2005-11-07 22:03:46 -0800116 ;;
117 *)
Alex Riesen31f883d2006-01-05 12:46:16 +0100118 echo "Auto-merging $4"
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800119 orig=`git-unpack-file $1`
120 ;;
121 esac
Petr Baudisec739622005-06-09 01:38:20 +0200122
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800123 # Be careful for funny filename such as "-L" in "$4", which
124 # would confuse "merge" greatly.
125 src1=`git-unpack-file $2`
Junio C Hamano5be60072007-07-02 22:52:14 -0700126 git merge-file "$src1" "$orig" "$src2"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700127 ret=$?
Alex Riesen718135e2009-04-29 23:40:50 +0200128 msg=
129 if [ $ret -ne 0 ]; then
130 msg='content conflict'
131 fi
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800132
133 # Create the working tree file, using "our tree" version from the
134 # index, and then store the result of the merge.
Junio C Hamano5be60072007-07-02 22:52:14 -0700135 git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4"
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800136 rm -f -- "$orig" "$src1" "$src2"
Petr Baudis8544a6f2005-06-08 23:26:55 +0200137
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700138 if [ "$6" != "$7" ]; then
Alex Riesen718135e2009-04-29 23:40:50 +0200139 if [ -n "$msg" ]; then
140 msg="$msg, "
141 fi
142 msg="${msg}permissions conflict: $5->$6,$7"
Linus Torvalds2a68a862005-06-08 11:40:59 -0700143 ret=1
James Bottomley0a9ea852005-04-18 19:55:19 -0700144 fi
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800145 if [ "$1" = '' ]; then
146 ret=1
147 fi
Petr Baudis8544a6f2005-06-08 23:26:55 +0200148
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700149 if [ $ret -ne 0 ]; then
Alex Riesen718135e2009-04-29 23:40:50 +0200150 echo "ERROR: $msg in $4"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700151 exit 1
152 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700153 exec git update-index -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +0200154 ;;
155
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700156*)
Petr Baudisec739622005-06-09 01:38:20 +0200157 echo "ERROR: $4: Not handling case $1 -> $2 -> $3"
158 ;;
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700159esac
160exit 1