2 # Copyright (C) 2005 Junio C Hamano
4 # Applying diff between two trees to the work tree can be
5 # done with the following single command:
7 # GIT_EXTERNAL_DIFF=git-apply-patch-script git-diff-tree -p $tree1 $tree2
12 echo >&2 "cannot handle unmerged diff on path $1."
15 name
="$1" tmp1
="$2" hex1
="$3" mode1
="$4" tmp2
="$5" hex2
="$6" mode2
="$7"
33 case "$type1,$type2" in
36 dir
=$
(dirname "$name")
37 case "$dir" in '' | .
) ;; *) mkdir
-p "$dir" ;; esac ||
{
38 echo >&2 "cannot create leading path for $name."
43 echo >&2 "path $name to be created already exists."
48 # creating a regular file
49 cat "$tmp2" >"$name" ||
{
50 echo >&2 "cannot create a regular file $name."
55 echo >&2 "created a regular file $name with mode +x."
56 chmod "$mode2" "$name"
59 echo >&2 "created a regular file $name."
65 ln -s "$(cat "$tmp2")" "$name" ||
{
66 echo >&2 "cannot create a symbolic link $name."
69 echo >&2 "created a symbolic link $name."
72 echo >&2 "do not know how to create $name of type $type2."
75 git-update-cache
--add -- "$name" ;;
79 echo >&2 "cannot remove $name"
82 echo >&2 "deleted $name."
83 git-update-cache
--remove -- "$name" ;;
86 echo >&2 "cannot change a regular file $name and a symbolic link $name."
91 current
=$
(readlink
"$name") ||
{
92 echo >&2 "cannot read the target of the symbolic link $name."
95 original
=$
(cat "$tmp1")
97 test "$original" != "$current" ||
{
98 echo >&2 "cannot apply symbolic link target change ($original->$next) to $name which points to $current."
101 if test "$next" != "$current"
103 rm -f "$name" && ln -s "$next" "$name" ||
{
104 echo >&2 "cannot create symbolic link $name."
107 echo >&2 "changed symbolic target of $name."
108 git-update-cache
-- "$name"
114 echo >&2 "regular file $name to be patched does not exist."
117 dir
=$
(dirname "$name")
118 case "$dir" in '' | .
) ;; *) mkdir
-p "$dir";; esac ||
{
119 echo >&2 "cannot create leading path for $name."
122 tmp
=.git-apply-patch-$$
123 trap "rm -f $tmp-*" 0 1 2 3 15
125 # Be careful, in case "$tmp2" is borrowed path from the work tree
126 # we are looking at...
127 diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" >$tmp-patch
129 # This will say "patching ..." so we do not say anything outselves.
130 patch -p1 <$tmp-patch ||
exit
132 case "$mode1,$mode2" in
135 chmod "$mode2" "$name"
136 echo >&2 "changed mode from $mode1 to $mode2."
139 git-update-cache
-- "$name"