Merge with Linus' current tree
[alt-git.git] / git-apply-patch-script
blob6261fd85f29f6ee1441e530978ccc5374271cc6e
1 #!/bin/sh
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
10 case "$#" in
12 echo >&2 "cannot handle unmerged diff on path $1."
13 exit 1 ;;
14 8 | 9)
15 echo >&2 "cannot handle rename diff between $1 and $8 yet."
16 exit 1 ;;
17 esac
18 name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
20 type1=f
21 case "$mode1" in
22 *120???) type1=l ;;
23 *1007??) mode1=+x ;;
24 *1006??) mode1=-x ;;
25 .) type1=- ;;
26 esac
28 type2=f
29 case "$mode2" in
30 *120???) type2=l ;;
31 *1007??) mode2=+x ;;
32 *1006??) mode2=-x ;;
33 .) type2=- ;;
34 esac
36 case "$type1,$type2" in
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
44 if test -e "$name"
45 then
46 echo >&2 "path $name to be created already exists."
47 exit 1
49 case "$type2" in
51 # creating a regular file
52 cat "$tmp2" >"$name" || {
53 echo >&2 "cannot create a regular file $name."
54 exit 1
56 case "$mode2" in
57 +x)
58 echo >&2 "created a regular file $name with mode +x."
59 chmod "$mode2" "$name"
61 -x)
62 echo >&2 "created a regular file $name."
64 esac
67 # creating a symlink
68 ln -s "$(cat "$tmp2")" "$name" || {
69 echo >&2 "cannot create a symbolic link $name."
70 exit 1
72 echo >&2 "created a symbolic link $name."
75 echo >&2 "do not know how to create $name of type $type2."
76 exit 1
77 esac
78 git-update-cache --add -- "$name" ;;
80 ?,-)
81 rm -f "$name" || {
82 echo >&2 "cannot remove $name"
83 exit 1
85 echo >&2 "deleted $name."
86 git-update-cache --remove -- "$name" ;;
88 l,f|f,l)
89 echo >&2 "cannot change a regular file $name and a symbolic link $name."
90 exit 1 ;;
92 l,l)
93 # symlink to symlink
94 current=$(readlink "$name") || {
95 echo >&2 "cannot read the target of the symbolic link $name."
96 exit 1
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
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
110 echo >&2 "changed symbolic target of $name."
111 git-update-cache -- "$name"
112 fi ;;
114 f,f)
115 # changed
116 test -e "$name" || {
117 echo >&2 "regular file $name to be patched does not exist."
118 exit 1
120 dir=$(dirname "$name")
121 case "$dir" in '' | .) ;; *) mkdir -p "$dir";; esac || {
122 echo >&2 "cannot create leading path for $name."
123 exit 1
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
134 rm -f $tmp-patch
135 case "$mode1,$mode2" in
136 "$mode2,$mode1") ;;
138 chmod "$mode2" "$name"
139 echo >&2 "changed mode from $mode1 to $mode2."
141 esac
142 git-update-cache -- "$name"
144 esac