Code

Make sure we error out if we can't remove a file on automatic merges.
[git.git] / git-merge-one-file-script
1 #!/bin/sh
2 #
3 # This is the git merge script, called with
4 #
5 #   $1 - original file SHA1 (or empty)
6 #   $2 - file in branch1 SHA1 (or empty)
7 #   $3 - file in branch2 SHA1 (or empty)
8 #   $4 - pathname in repository
9 #   $5 - orignal file mode (or empty)
10 #   $6 - file in branch1 mode (or empty)
11 #   $7 - file in branch2 mode (or empty)
12 #
13 # Handle some trivial cases.. The _really_ trivial cases have
14 # been handled already by git-read-tree, but that one doesn't
15 # do any merges that migth change the tree layout.
17 case "${1:-.}${2:-.}${3:-.}" in
18 #
19 # Deleted in both.
20 #
21 "$1..")
22         echo "WARNING: $4 is removed in both branches."
23         echo "WARNING: This is a potential rename conflict."
24         rm -f -- "$4" || exit 1
25         exec git-update-cache --remove -- "$4" ;;
26 #
27 # Deleted in one and unchanged in the other.
28 #
29 "$1.." | "$1.$1" | "$1$1.")
30         echo "Removing $4"
31         rm -f -- "$4" || exit 1
32         exec git-update-cache --remove -- "$4" ;;
33 #
34 # Added in one.
35 #
36 ".$2." | "..$3" )
37         case "$6$7" in *7??) mode=+x;; *) mode=-x;; esac
38         echo "Adding $4 with perm $mode."
39         git-cat-file blob "$2$3" > "$4"
40         chmod $mode -- "$4"
41         exec git-update-cache --add -- "$4" ;;
42 #
43 # Added in both (check for same permissions).
44 #
45 ".$3$2")
46         if [ "$6" != "$7" ]; then
47                 echo "ERROR: File $4 added identically in both branches,"
48                 echo "ERROR: but permissions conflict $6->$7."
49                 exit 1
50         fi
51         case "$6" in *7??) mode=+x;; *) mode=-x;; esac
52         echo "Adding $4 with perm $mode"
53         git-cat-file blob "$2" > "$4"
54         chmod $mode -- "$4"
55         exec git-update-cache --add -- "$4" ;;
56 #
57 # Modified in both, but differently.
58 #
59 "$1$2$3")
60         echo "Auto-merging $4."
61         orig=`git-unpack-file $1`
62         src1=`git-unpack-file $2`
63         src2=`git-unpack-file $3`
64         merge -p "$src1" "$orig" "$src2" > "$4"
65         ret=$?
66         rm -f -- "$orig" "$src1" "$src2"
68         if [ "$6" != "$7" ]; then
69                 echo "ERROR: Permissions $5->$6->$7 don't match."
70                 ret=1
71         fi
72         case "$6" in *7??) mode=+x;; *) mode=-x;; esac
73         chmod "$mode" "$4"
75         if [ $ret -ne 0 ]; then
76                 # Reset the index to the first branch, making
77                 # git-diff-file useful
78                 git-update-cache --add --cacheinfo "$6" "$2" "$4"
79                 echo "ERROR: Merge conflict in $4."
80                 exit 1
81         fi
82         exec git-update-cache --add -- "$4" ;;
83 *)
84         echo "ERROR: Not handling case $4: $1 -> $2 -> $3" ;;
85 esac
86 exit 1