Split "git-pull-script" into two parts
[git/git.git] / git-apply-patch-script
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
11 2) exit 1 ;; # do not feed unmerged diff to me!
12 esac
13 name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
14 case "$mode1" in *7??) mode1=+x ;; *6??) mode1=-x ;; esac
15 case "$mode2" in *7??) mode2=+x ;; *6??) mode2=-x ;; esac
16
17 if test -f "$name.orig" || test -f "$name.rej"
18 then
19 echo >&2 "Unresolved patch conflicts in the previous run found."
20 exit 1
21 fi
22
23 case "$mode1,$mode2" in
24 .,?x)
25 # newly created
26 dir=$(dirname "$name")
27 case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
28 echo >&2 "cannot create leading path for $name."
29 exit 1
30 }
31 if test -f "$name"
32 then
33 echo >&2 "file $name to be created already exists."
34 exit 1
35 fi
36 cat "$tmp2" >"$name" || {
37 echo >&2 "cannot create $name."
38 exit 1
39 }
40 case "$mode2" in
41 +x)
42 echo >&2 "created $name with mode +x."
43 chmod "$mode2" "$name"
44 ;;
45 -x)
46 echo >&2 "created $name."
47 ;;
48 esac
49 git-update-cache --add -- "$name"
50 ;;
51 ?x,.)
52 # deleted
53 echo >&2 "deleted $name."
54 rm -f "$name" || {
55 echo >&2 "cannot remove $name";
56 exit 1
57 }
58 git-update-cache --remove -- "$name"
59 ;;
60 *)
61 # changed
62 dir=$(dirname "$name")
63 case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
64 echo >&2 "cannot create leading path for $name."
65 exit 1
66 }
67 # This will say "patching ..." so we do not say anything outselves.
68 diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" | patch -p1 || exit
69
70 case "$mode1,$mode2" in
71 "$mode2,$mode1") ;;
72 *)
73 echo >&2 "changing mode from $mode1 to $mode2."
74 chmod "$mode2" "$name"
75 ;;
76 esac
77 git-update-cache -- "$name"
78 esac