blob: f6d9852d2f618393412016cc81c73b1e8362a59d [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.")
Jeff King72fac662015-10-26 17:39:39 -040041 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
Kevin Bracey333ea382013-03-24 14:26:23 +020049 if test -n "$2"
50 then
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070051 echo "Removing $4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070052 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,
Junio C Hamano561b0fb2007-07-08 14:42:05 -070056 # 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"
Linus Torvaldscdcb0ed2005-08-29 22:36:16 -070059 fi
Kevin Bracey333ea382013-03-24 14:26:23 +020060 if test -f "$4"
61 then
Junio C Hamano397c7662005-11-19 19:50:44 -080062 rm -f -- "$4" &&
Mark Woodingf327dbc2006-04-13 22:01:24 +000063 rmdir -p "$(expr "z$4" : 'z\(.*\)/')" 2>/dev/null || :
Junio C Hamanoaacc15e2005-06-25 02:24:50 -070064 fi &&
Junio C Hamano5be60072007-07-02 22:52:14 -070065 exec git update-index --remove -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020066 ;;
67
Linus Torvalds839a7a02005-04-18 12:15:10 -070068#
Junio C Hamanoee281522005-05-01 23:53:32 -070069# Added in one.
Linus Torvalds839a7a02005-04-18 12:15:10 -070070#
Junio C Hamanoed93b442006-10-08 22:48:06 -070071".$2.")
72 # the other side did not add and we added so there is nothing
Junio C Hamano561b0fb2007-07-08 14:42:05 -070073 # to be done, except making the path merged.
74 exec git update-index --add --cacheinfo "$6" "$2" "$4"
Junio C Hamanoed93b442006-10-08 22:48:06 -070075 ;;
76"..$3")
Linus Torvalds98a96b02005-06-08 17:56:09 -070077 echo "Adding $4"
Junio C Hamano29dc1332008-03-17 16:47:18 -070078 if test -f "$4"
79 then
Kevin Braceyd401acf2013-03-24 14:26:24 +020080 echo "ERROR: untracked $4 is overwritten by the merge." >&2
Junio C Hamanoed93b442006-10-08 22:48:06 -070081 exit 1
Junio C Hamano29dc1332008-03-17 16:47:18 -070082 fi
Junio C Hamano561b0fb2007-07-08 14:42:05 -070083 git update-index --add --cacheinfo "$7" "$3" "$4" &&
Junio C Hamano5be60072007-07-02 22:52:14 -070084 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +020085 ;;
86
James Bottomleye2b6a9d2005-04-23 20:50:10 -070087#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -080088# Added in both, identically (check for same permissions).
James Bottomleye2b6a9d2005-04-23 20:50:10 -070089#
Junio C Hamanoee281522005-05-01 23:53:32 -070090".$3$2")
Kevin Bracey333ea382013-03-24 14:26:23 +020091 if test "$6" != "$7"
92 then
Kevin Braceyd401acf2013-03-24 14:26:24 +020093 echo "ERROR: File $4 added identically in both branches," >&2
94 echo "ERROR: but permissions conflict $6->$7." >&2
James Bottomleye2b6a9d2005-04-23 20:50:10 -070095 exit 1
96 fi
Linus Torvalds98a96b02005-06-08 17:56:09 -070097 echo "Adding $4"
Junio C Hamano5be60072007-07-02 22:52:14 -070098 git update-index --add --cacheinfo "$6" "$2" "$4" &&
99 exec git checkout-index -u -f -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +0200100 ;;
101
Linus Torvalds839a7a02005-04-18 12:15:10 -0700102#
Junio C Hamanoee281522005-05-01 23:53:32 -0700103# Modified in both, but differently.
Linus Torvalds839a7a02005-04-18 12:15:10 -0700104#
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800105"$1$2$3" | ".$2$3")
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800106
107 case ",$6,$7," in
108 *,120000,*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200109 echo "ERROR: $4: Not merging symbolic link changes." >&2
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800110 exit 1
111 ;;
Junio C Hamanoff72af02007-12-10 11:22:05 -0800112 *,160000,*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200113 echo "ERROR: $4: Not merging conflicting submodule changes." >&2
Junio C Hamanoff72af02007-12-10 11:22:05 -0800114 exit 1
115 ;;
Junio C Hamano54dd99a2005-12-02 00:54:50 -0800116 esac
117
Michael Forney974ce802017-08-04 23:49:05 -0700118 src1=$(git unpack-file $2)
119 src2=$(git unpack-file $3)
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800120 case "$1" in
121 '')
122 echo "Added $4 in both, but differently."
brian m. carlson7882fa22018-05-02 00:26:10 +0000123 orig=$(git unpack-file $(git hash-object /dev/null))
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800124 ;;
125 *)
Alex Riesen31f883d2006-01-05 12:46:16 +0100126 echo "Auto-merging $4"
Michael Forney974ce802017-08-04 23:49:05 -0700127 orig=$(git unpack-file $1)
Junio C Hamanof7d24bb2005-11-07 22:03:46 -0800128 ;;
129 esac
Petr Baudisec739622005-06-09 01:38:20 +0200130
Junio C Hamano5be60072007-07-02 22:52:14 -0700131 git merge-file "$src1" "$orig" "$src2"
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700132 ret=$?
Alex Riesen718135e2009-04-29 23:40:50 +0200133 msg=
Junio C Hamano4fa5c052013-03-25 10:05:13 -0700134 if test $ret != 0 || test -z "$1"
Kevin Bracey333ea382013-03-24 14:26:23 +0200135 then
Alex Riesen718135e2009-04-29 23:40:50 +0200136 msg='content conflict'
Junio C Hamano4fa5c052013-03-25 10:05:13 -0700137 ret=1
Alex Riesen718135e2009-04-29 23:40:50 +0200138 fi
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800139
140 # Create the working tree file, using "our tree" version from the
141 # index, and then store the result of the merge.
Jeff King6aaeca92011-04-29 18:24:32 -0400142 git checkout-index -f --stage=2 -- "$4" && cat "$src1" >"$4" || exit 1
Junio C Hamanob539c5e2005-12-07 00:50:33 -0800143 rm -f -- "$orig" "$src1" "$src2"
Petr Baudis8544a6f2005-06-08 23:26:55 +0200144
Kevin Bracey333ea382013-03-24 14:26:23 +0200145 if test "$6" != "$7"
146 then
147 if test -n "$msg"
148 then
Alex Riesen718135e2009-04-29 23:40:50 +0200149 msg="$msg, "
150 fi
151 msg="${msg}permissions conflict: $5->$6,$7"
Linus Torvalds2a68a862005-06-08 11:40:59 -0700152 ret=1
James Bottomley0a9ea852005-04-18 19:55:19 -0700153 fi
Petr Baudis8544a6f2005-06-08 23:26:55 +0200154
Kevin Bracey333ea382013-03-24 14:26:23 +0200155 if test $ret != 0
156 then
Kevin Braceyd401acf2013-03-24 14:26:24 +0200157 echo "ERROR: $msg in $4" >&2
James Bottomleye2b6a9d2005-04-23 20:50:10 -0700158 exit 1
159 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700160 exec git update-index -- "$4"
Petr Baudisec739622005-06-09 01:38:20 +0200161 ;;
162
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700163*)
Kevin Braceyd401acf2013-03-24 14:26:24 +0200164 echo "ERROR: $4: Not handling case $1 -> $2 -> $3" >&2
Petr Baudisec739622005-06-09 01:38:20 +0200165 ;;
Linus Torvaldse3b4be72005-04-18 14:17:58 -0700166esac
167exit 1