]>
Commit | Line | Data |
---|---|---|
5d2f8b27 JH |
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 | ||
10 | case "$#" in | |
e4f5b8c6 | 11 | 1) |
fc54a9c3 JH |
12 | echo >&2 "cannot handle unmerged diff on path $1." |
13 | exit 1 ;; | |
427dcb4b | 14 | 8 | 9) |
5c97558c JH |
15 | echo >&2 "cannot handle rename diff between $1 and $8 yet." |
16 | exit 1 ;; | |
5d2f8b27 JH |
17 | esac |
18 | name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7" | |
5d2f8b27 | 19 | |
fc54a9c3 JH |
20 | type1=f |
21 | case "$mode1" in | |
22 | *120???) type1=l ;; | |
23 | *1007??) mode1=+x ;; | |
24 | *1006??) mode1=-x ;; | |
25 | .) type1=- ;; | |
26 | esac | |
27 | ||
28 | type2=f | |
29 | case "$mode2" in | |
30 | *120???) type2=l ;; | |
31 | *1007??) mode2=+x ;; | |
32 | *1006??) mode2=-x ;; | |
33 | .) type2=- ;; | |
34 | esac | |
35 | ||
36 | case "$type1,$type2" in | |
5d2f8b27 | 37 | |
fc54a9c3 | 38 | -,?) |
6fa28064 | 39 | dir=$(dirname "$name") |
fc54a9c3 | 40 | case "$dir" in '' | .) ;; *) mkdir -p "$dir" ;; esac || { |
6fa28064 JH |
41 | echo >&2 "cannot create leading path for $name." |
42 | exit 1 | |
43 | } | |
fc54a9c3 | 44 | if test -e "$name" |
8a9d32b7 | 45 | then |
fc54a9c3 | 46 | echo >&2 "path $name to be created already exists." |
8a9d32b7 JH |
47 | exit 1 |
48 | fi | |
fc54a9c3 JH |
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 | |
5d2f8b27 | 65 | ;; |
fc54a9c3 JH |
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 | |
6fa28064 | 77 | esac |
fc54a9c3 JH |
78 | git-update-cache --add -- "$name" ;; |
79 | ||
80 | ?,-) | |
6fa28064 | 81 | rm -f "$name" || { |
fc54a9c3 JH |
82 | echo >&2 "cannot remove $name" |
83 | exit 1 | |
84 | } | |
85 | echo >&2 "deleted $name." | |
86 | git-update-cache --remove -- "$name" ;; | |
87 | ||
88 | l,f|f,l) | |
89 | echo >&2 "cannot change a regular file $name and a symbolic link $name." | |
90 | exit 1 ;; | |
91 | ||
92 | l,l) | |
93 | # symlink to symlink | |
94 | current=$(readlink "$name") || { | |
95 | echo >&2 "cannot read the target of the symbolic link $name." | |
6fa28064 JH |
96 | exit 1 |
97 | } | |
fc54a9c3 JH |
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 | ||
114 | f,f) | |
6fa28064 | 115 | # changed |
fc54a9c3 JH |
116 | test -e "$name" || { |
117 | echo >&2 "regular file $name to be patched does not exist." | |
118 | exit 1 | |
119 | } | |
6fa28064 | 120 | dir=$(dirname "$name") |
fc54a9c3 | 121 | case "$dir" in '' | .) ;; *) mkdir -p "$dir";; esac || { |
6fa28064 JH |
122 | echo >&2 "cannot create leading path for $name." |
123 | exit 1 | |
124 | } | |
fc54a9c3 JH |
125 | tmp=.git-apply-patch-$$ |
126 | trap "rm -f $tmp-*" 0 1 2 3 15 | |
6fa28064 | 127 | |
fc54a9c3 JH |
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 | |
6fa28064 JH |
135 | case "$mode1,$mode2" in |
136 | "$mode2,$mode1") ;; | |
5d2f8b27 | 137 | *) |
6fa28064 | 138 | chmod "$mode2" "$name" |
fc54a9c3 | 139 | echo >&2 "changed mode from $mode1 to $mode2." |
6fa28064 | 140 | ;; |
5d2f8b27 | 141 | esac |
6fa28064 | 142 | git-update-cache -- "$name" |
fc54a9c3 | 143 | |
6fa28064 | 144 | esac |