blob: 07dfeb8df4bf910c5c79fb4c3dd3345ba4719da0 [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>"
David Aguilare5a15182013-02-23 16:50:12 -080021LONG_USAGE="usage: git merge-one-file $USAGE
Jonathan Niederae5bdda2009-11-09 09:04:53 -060022
23Blob ids and modes should be empty for missing files."
24
Jeff King6aaeca92011-04-29 18:24:32 -040025SUBDIRECTORY_OK=Yes
26. git-sh-setup
27cd_to_toplevel
28require_work_tree
29
Kevin Bracey333ea382013-03-24 14:26:23 +020030if test $# != 7
Jonathan Niederae5bdda2009-11-09 09:04:53 -060031then
32 echo "$LONG_USAGE"
33 exit 1
34fi
35
Linus Torvaldse3b4be72005-04-18 14:17:58 -070036case "${1:-.}${2:-.}${3:-.}" in
37#
Linus Torvalds98a96b02005-06-08 17:56:09 -070038# Deleted in both or deleted in one and unchanged in the other
James Bottomleye2b6a9d2005-04-23 20:50:10 -070039#
Linus Torvalds98a96b02005-06-08 17:56:09 -070040"$1.." | "$1.$1" | "$1$1.")
Kevin Bracey333ea382013-03-24 14:26:23 +020041 if test -n "$2"
42 then
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070043 echo "Removing $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070044 else
45 # read-tree checked that index matches HEAD already,
46 # so we know we do not have this path tracked.
47 # there may be an unrelated working tree file here,
Junio C Hamano561b0fb2007-07-08 14:42:05 -070048 # which we should just leave unmolested. Make sure
49 # we do not have it in the index, though.
50 exec git update-index --remove -- "$4"
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070051 fi
Kevin Bracey333ea382013-03-24 14:26:23 +020052 if test -f "$4"
53 then
Junio C Hamano397c7662005-11-19 19:50:44 -080054 rm -f -- "$4" &&
Mark Woodingf327dbc2006-04-13 22:01:24 +000055 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
Junio C Hamanoaacc15e2005-06-25 02:24:50 -070056 fi &&
Junio C Hamano5be60072007-07-02 22:52:14 -070057 exec git update-index --remove -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020058 ;;
59
Linus Torvalds839a7a02005-04-18 12:15:10 -070060#
Junio C Hamanoee281522005-05-01 23:53:32 -070061# Added in one.
Linus Torvalds839a7a02005-04-18 12:15:10 -070062#
Junio C Hamanoed93b442006-10-08 22:48:06 -070063".$2.")
64 # the other side did not add and we added so there is nothing
Junio C Hamano561b0fb2007-07-08 14:42:05 -070065 # to be done, except making the path merged.
66 exec git update-index --add --cacheinfo "$6" "$2" "$4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070067 ;;
68"..$3")
Linus Torvalds98a96b02005-06-08 17:56:09 -070069 echo "Adding $4"
Junio C Hamano29dc1332008-03-17 16:47:18 -070070 if test -f "$4"
71 then
Kevin Braceyd401acf2013-03-24 14:26:24 +020072 echo "ERROR: untracked $4 is overwritten by the merge." >&2
Junio C Hamanoed93b442006-10-08 22:48:06 -070073 exit 1
Junio C Hamano29dc1332008-03-17 16:47:18 -070074 fi
Junio C Hamano561b0fb2007-07-08 14:42:05 -070075 git update-index --add --cacheinfo "$7" "$3" "$4" &&
Junio C Hamano5be60072007-07-02 22:52:14 -070076 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020077 ;;
78
James Bottomleye2b6a9d2005-04-23 20:50:10 -070079#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080080# Added in both, identically (check for same permissions).
James Bottomleye2b6a9d2005-04-23 20:50:10 -070081#
Junio C Hamanoee281522005-05-01 23:53:32 -070082".$3$2")
Kevin Bracey333ea382013-03-24 14:26:23 +020083 if test "$6" != "$7"
84 then
Kevin Braceyd401acf2013-03-24 14:26:24 +020085 echo "ERROR: File $4 added identically in both branches," >&2
86 echo "ERROR: but permissions conflict $6->$7." >&2
James Bottomleye2b6a9d2005-04-23 20:50:10 -070087 exit 1
88 fi
Linus Torvalds98a96b02005-06-08 17:56:09 -070089 echo "Adding $4"
Junio C Hamano5be60072007-07-02 22:52:14 -070090 git update-index --add --cacheinfo "$6" "$2" "$4" &&
91 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020092 ;;
93
Linus Torvalds839a7a02005-04-18 12:15:10 -070094#
Junio C Hamanoee281522005-05-01 23:53:32 -070095# Modified in both, but differently.
Linus Torvalds839a7a02005-04-18 12:15:10 -070096#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080097"$1$2$3" | ".$2$3")
Junio C Hamano54dd99a2005-12-02 00:54:50 -080098
99 case ",$6,$7," in
100 *,120000,*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200101 echo "ERROR: $4: Not merging symbolic link changes." >&2
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800102 exit 1
103 ;;
Junio C Hamanoff72af02007-12-10 11:22:05 -0800104 *,160000,*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200105 echo "ERROR: $4: Not merging conflicting submodule changes." >&2
Junio C Hamanoff72af02007-12-10 11:22:05 -0800106 exit 1
107 ;;
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800108 esac
109
Kevin Bracey45491622013-03-13 03:12:21 +0200110 src1=$(git-unpack-file $2)
111 src2=$(git-unpack-file $3)
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800112 case "$1" in
113 '')
114 echo "Added $4 in both, but differently."
Kevin Bracey45491622013-03-13 03:12:21 +0200115 orig=$(git-unpack-file $2)
116 create_virtual_base "$orig" "$src2"
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800117 ;;
118 *)
Alex Riesen31f883d2006-01-05 12:46:16 +0100119 echo "Auto-merging $4"
Kevin Bracey45491622013-03-13 03:12:21 +0200120 orig=$(git-unpack-file $1)
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800121 ;;
122 esac
Petr Baudisec739622005-06-09 01:38:20 +0200123
Junio C Hamano5be60072007-07-02 22:52:14 -0700124 git merge-file "$src1" "$orig" "$src2"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700125 ret=$?
Alex Riesen718135e2009-04-29 23:40:50 +0200126 msg=
Junio C Hamano4fa5c052013-03-25 10:05:13 -0700127 if test $ret != 0 || test -z "$1"
Kevin Bracey333ea382013-03-24 14:26:23 +0200128 then
Alex Riesen718135e2009-04-29 23:40:50 +0200129 msg='content conflict'
Junio C Hamano4fa5c052013-03-25 10:05:13 -0700130 ret=1
Alex Riesen718135e2009-04-29 23:40:50 +0200131 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.
Jeff King6aaeca92011-04-29 18:24:32 -0400135 git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800136 rm -f -- "$orig" "$src1" "$src2"
Petr Baudis8544a6f2005-06-08 23:26:55 +0200137
Kevin Bracey333ea382013-03-24 14:26:23 +0200138 if test "$6" != "$7"
139 then
140 if test -n "$msg"
141 then
Alex Riesen718135e2009-04-29 23:40:50 +0200142 msg="$msg, "
143 fi
144 msg="${msg}permissions conflict: $5->$6,$7"
Linus Torvalds2a68a862005-06-08 11:40:59 -0700145 ret=1
James Bottomley0a9ea852005-04-18 19:55:19 -0700146 fi
Petr Baudis8544a6f2005-06-08 23:26:55 +0200147
Kevin Bracey333ea382013-03-24 14:26:23 +0200148 if test $ret != 0
149 then
Kevin Braceyd401acf2013-03-24 14:26:24 +0200150 echo "ERROR: $msg in $4" >&2
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*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200157 echo "ERROR: $4: Not handling case $1 -> $2 -> $3" >&2
Petr Baudisec739622005-06-09 01:38:20 +0200158 ;;
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700159esac
160exit 1