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 echo >&2 "cannot handle rename diff between $1 and $8 yet."
18 name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
36 case "$type1,$type2" in
39 dir=$(dirname "$name")
40 case "$dir" in '' | .) ;; *) mkdir -p "$dir" ;; esac || {
41 echo >&2 "cannot create leading path for $name."
46 echo >&2 "path $name to be created already exists."
51 # creating a regular file
52 cat "$tmp2" >"$name" || {
53 echo >&2 "cannot create a regular file $name."
58 echo >&2 "created a regular file $name with mode +x."
59 chmod "$mode2" "$name"
62 echo >&2 "created a regular file $name."
68 ln -s "$(cat "$tmp2")" "$name" || {
69 echo >&2 "cannot create a symbolic link $name."
72 echo >&2 "created a symbolic link $name."
75 echo >&2 "do not know how to create $name of type $type2."
78 git-update-cache --add -- "$name" ;;
82 echo >&2 "cannot remove $name"
85 echo >&2 "deleted $name."
86 git-update-cache --remove -- "$name" ;;
89 echo >&2 "cannot change a regular file $name and a symbolic link $name."
94 current=$(readlink "$name") || {
95 echo >&2 "cannot read the target of the symbolic link $name."
98 original=$(cat "$tmp1")
100 test "$original" != "$current" || {
101 echo >&2 "cannot apply symbolic link target change ($original->$next) to $name which points to $current."
104 if test "$next" != "$current"
106 rm -f "$name" && ln -s "$next" "$name" || {
107 echo >&2 "cannot create symbolic link $name."
110 echo >&2 "changed symbolic target of $name."
111 git-update-cache -- "$name"
117 echo >&2 "regular file $name to be patched does not exist."
120 dir=$(dirname "$name")
121 case "$dir" in '' | .) ;; *) mkdir -p "$dir";; esac || {
122 echo >&2 "cannot create leading path for $name."
125 tmp=.git-apply-patch-$$
126 trap "rm -f $tmp-*" 0 1 2 3 15
128 # Be careful, in case "$tmp2" is borrowed path from the work tree
129 # we are looking at...
130 diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" >$tmp-patch
132 # This will say "patching ..." so we do not say anything outselves.
133 patch -p1 <$tmp-patch || exit
135 case "$mode1,$mode2" in
138 chmod "$mode2" "$name"
139 echo >&2 "changed mode from $mode1 to $mode2."
142 git-update-cache -- "$name"