1#!/bin/sh
2# Copyright (C) 2005 Junio C Hamano
3#
4# Applying diff between two trees to the work tree can be
5# done with the following single command:
6#
7# GIT_EXTERNAL_DIFF=git-apply-patch-script git-diff-tree -p $tree1 $tree2
8#
9
10case "$#" in
111)
12 echo >&2 "cannot handle unmerged diff on path $1."
13 exit 1 ;;
148 | 9)
15 echo >&2 "cannot handle rename diff between $1 and $8 yet."
16 exit 1 ;;
17esac
18name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
19
20type1=f
21case "$mode1" in
22*120???) type1=l ;;
23*1007??) mode1=+x ;;
24*1006??) mode1=-x ;;
25.) type1=- ;;
26esac
27
28type2=f
29case "$mode2" in
30*120???) type2=l ;;
31*1007??) mode2=+x ;;
32*1006??) mode2=-x ;;
33.) type2=- ;;
34esac
35
36case "$type1,$type2" in
37
38-,?)
39 dir=$(dirname "$name")
40 case "$dir" in '' | .) ;; *) mkdir -p "$dir" ;; esac || {
41 echo >&2 "cannot create leading path for $name."
42 exit 1
43 }
44 if test -e "$name"
45 then
46 echo >&2 "path $name to be created already exists."
47 exit 1
48 fi
49 case "$type2" in
50 f)
51 # creating a regular file
52 cat "$tmp2" >"$name" || {
53 echo >&2 "cannot create a regular file $name."
54 exit 1
55 }
56 case "$mode2" in
57 +x)
58 echo >&2 "created a regular file $name with mode +x."
59 chmod "$mode2" "$name"
60 ;;
61 -x)
62 echo >&2 "created a regular file $name."
63 ;;
64 esac
65 ;;
66 l)
67 # creating a symlink
68 ln -s "$(cat "$tmp2")" "$name" || {
69 echo >&2 "cannot create a symbolic link $name."
70 exit 1
71 }
72 echo >&2 "created a symbolic link $name."
73 ;;
74 *)
75 echo >&2 "do not know how to create $name of type $type2."
76 exit 1
77 esac
78 git-update-cache --add -- "$name" ;;
79
80?,-)
81 rm -f "$name" || {
82 echo >&2 "cannot remove $name"
83 exit 1
84 }
85 echo >&2 "deleted $name."
86 git-update-cache --remove -- "$name" ;;
87
88l,f|f,l)
89 echo >&2 "cannot change a regular file $name and a symbolic link $name."
90 exit 1 ;;
91
92l,l)
93 # symlink to symlink
94 current=$(readlink "$name") || {
95 echo >&2 "cannot read the target of the symbolic link $name."
96 exit 1
97 }
98 original=$(cat "$tmp1")
99 next=$(cat "$tmp2")
100 test "$original" != "$current" || {
101 echo >&2 "cannot apply symbolic link target change ($original->$next) to $name which points to $current."
102 exit 1
103 }
104 if test "$next" != "$current"
105 then
106 rm -f "$name" && ln -s "$next" "$name" || {
107 echo >&2 "cannot create symbolic link $name."
108 exit 1
109 }
110 echo >&2 "changed symbolic target of $name."
111 git-update-cache -- "$name"
112 fi ;;
113
114f,f)
115 # changed
116 test -e "$name" || {
117 echo >&2 "regular file $name to be patched does not exist."
118 exit 1
119 }
120 dir=$(dirname "$name")
121 case "$dir" in '' | .) ;; *) mkdir -p "$dir";; esac || {
122 echo >&2 "cannot create leading path for $name."
123 exit 1
124 }
125 tmp=.git-apply-patch-$$
126 trap "rm -f $tmp-*" 0 1 2 3 15
127
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
131
132 # This will say "patching ..." so we do not say anything outselves.
133 patch -p1 <$tmp-patch || exit
134 rm -f $tmp-patch
135 case "$mode1,$mode2" in
136 "$mode2,$mode1") ;;
137 *)
138 chmod "$mode2" "$name"
139 echo >&2 "changed mode from $mode1 to $mode2."
140 ;;
141 esac
142 git-update-cache -- "$name"
143
144esac