t4038: Remove non-portable '-a' option passed to test_cmp
[git/git.git] / git-merge-one-file.sh
CommitLineData
839a7a06
LT
1#!/bin/sh
2#
ec73962d
PB
3# Copyright (c) Linus Torvalds, 2005
4#
5# This is the git per-file merge script, called with
839a7a06 6#
e3b4be7f
LT
7# $1 - original file SHA1 (or empty)
8# $2 - file in branch1 SHA1 (or empty)
9# $3 - file in branch2 SHA1 (or empty)
839a7a06 10# $4 - pathname in repository
82e5a82f 11# $5 - original file mode (or empty)
ee28152d
JH
12# $6 - file in branch1 mode (or empty)
13# $7 - file in branch2 mode (or empty)
839a7a06 14#
e3b4be7f 15# Handle some trivial cases.. The _really_ trivial cases have
5be60078 16# been handled already by git read-tree, but that one doesn't
ec73962d 17# do any merges that might change the tree layout.
0a9ea850 18
ae5bdda3
JN
19USAGE='<orig blob> <our blob> <their blob> <path>'
20USAGE="$USAGE <orig mode> <our mode> <their mode>"
e5a1518e 21LONG_USAGE="usage: git merge-one-file $USAGE
ae5bdda3
JN
22
23Blob ids and modes should be empty for missing files."
24
6aaeca90
JK
25SUBDIRECTORY_OK=Yes
26. git-sh-setup
27cd_to_toplevel
28require_work_tree
29
333ea38d 30if test $# != 7
ae5bdda3
JN
31then
32 echo "$LONG_USAGE"
33 exit 1
34fi
35
e3b4be7f 36case "${1:-.}${2:-.}${3:-.}" in
839a7a06 37#
98a96b00 38# Deleted in both or deleted in one and unchanged in the other
839a7a06 39#
98a96b00 40"$1.." | "$1.$1" | "$1$1.")
72fac66b
JK
41 if { test -z "$6" && test "$5" != "$7"; } ||
42 { test -z "$7" && test "$5" != "$6"; }
43 then
44 echo "ERROR: File $4 deleted on one branch but had its" >&2
45 echo "ERROR: permissions changed on the other." >&2
46 exit 1
47 fi
48
333ea38d
KB
49 if test -n "$2"
50 then
cdcb0ed4 51 echo "Removing $4"
ed93b449
JH
52 else
53 # read-tree checked that index matches HEAD already,
54 # so we know we do not have this path tracked.
55 # there may be an unrelated working tree file here,
561b0fbb
JH
56 # which we should just leave unmolested. Make sure
57 # we do not have it in the index, though.
58 exec git update-index --remove -- "$4"
cdcb0ed4 59 fi
333ea38d
KB
60 if test -f "$4"
61 then
397c7669 62 rm -f -- "$4" &&
f327dbce 63 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
aacc15ec 64 fi &&
5be60078 65 exec git update-index --remove -- "$4"
ec73962d
PB
66 ;;
67
839a7a06 68#
ee28152d 69# Added in one.
839a7a06 70#
ed93b449
JH
71".$2.")
72 # the other side did not add and we added so there is nothing
561b0fbb
JH
73 # to be done, except making the path merged.
74 exec git update-index --add --cacheinfo "$6" "$2" "$4"
ed93b449
JH
75 ;;
76"..$3")
98a96b00 77 echo "Adding $4"
29dc1331
JH
78 if test -f "$4"
79 then
d401acf7 80 echo "ERROR: untracked $4 is overwritten by the merge." >&2
ed93b449 81 exit 1
29dc1331 82 fi
561b0fbb 83 git update-index --add --cacheinfo "$7" "$3" "$4" &&
5be60078 84 exec git checkout-index -u -f -- "$4"
ec73962d
PB
85 ;;
86
e2b6a9d0 87#
f7d24bbe 88# Added in both, identically (check for same permissions).
e2b6a9d0 89#
ee28152d 90".$3$2")
333ea38d
KB
91 if test "$6" != "$7"
92 then
d401acf7
KB
93 echo "ERROR: File $4 added identically in both branches," >&2
94 echo "ERROR: but permissions conflict $6->$7." >&2
e2b6a9d0
JB
95 exit 1
96 fi
98a96b00 97 echo "Adding $4"
5be60078
JH
98 git update-index --add --cacheinfo "$6" "$2" "$4" &&
99 exec git checkout-index -u -f -- "$4"
ec73962d
PB
100 ;;
101
e3b4be7f 102#
ee28152d 103# Modified in both, but differently.
e3b4be7f 104#
f7d24bbe 105"$1$2$3" | ".$2$3")
54dd99a1
JH
106
107 case ",$6,$7," in
108 *,120000,*)
d401acf7 109 echo "ERROR: $4: Not merging symbolic link changes." >&2
54dd99a1
JH
110 exit 1
111 ;;
ff72af00 112 *,160000,*)
d401acf7 113 echo "ERROR: $4: Not merging conflicting submodule changes." >&2
ff72af00
JH
114 exit 1
115 ;;
54dd99a1
JH
116 esac
117
974ce807
MF
118 src1=$(git unpack-file $2)
119 src2=$(git unpack-file $3)
f7d24bbe
JH
120 case "$1" in
121 '')
122 echo "Added $4 in both, but differently."
7882fa22 123 orig=$(git unpack-file $(git hash-object /dev/null))
f7d24bbe
JH
124 ;;
125 *)
31f883d1 126 echo "Auto-merging $4"
974ce807 127 orig=$(git unpack-file $1)
f7d24bbe
JH
128 ;;
129 esac
ec73962d 130
5be60078 131 git merge-file "$src1" "$orig" "$src2"
e2b6a9d0 132 ret=$?
718135e3 133 msg=
4fa5c059 134 if test $ret != 0 || test -z "$1"
333ea38d 135 then
718135e3 136 msg='content conflict'
4fa5c059 137 ret=1
718135e3 138 fi
b539c5e8
JH
139
140 # Create the working tree file, using "our tree" version from the
141 # index, and then store the result of the merge.
6aaeca90 142 git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1
b539c5e8 143 rm -f -- "$orig" "$src1" "$src2"
8544a6f1 144
333ea38d
KB
145 if test "$6" != "$7"
146 then
147 if test -n "$msg"
148 then
718135e3
AR
149 msg="$msg, "
150 fi
151 msg="${msg}permissions conflict: $5->$6,$7"
2a68a865 152 ret=1
e2b6a9d0 153 fi
8544a6f1 154
333ea38d
KB
155 if test $ret != 0
156 then
d401acf7 157 echo "ERROR: $msg in $4" >&2
0a9ea850
JB
158 exit 1
159 fi
5be60078 160 exec git update-index -- "$4"
ec73962d
PB
161 ;;
162
e3b4be7f 163*)
d401acf7 164 echo "ERROR: $4: Not handling case $1 -> $2 -> $3" >&2
ec73962d 165 ;;
e3b4be7f
LT
166esac
167exit 1