summaryrefslogtreecommitdiff
path: root/git-apply-patch-script
blob: dccad27061ca753a871891afed615c4d4a10c765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
# Copyright (C) 2005 Junio C Hamano
#
# Applying diff between two trees to the work tree can be
# done with the following single command:
#
# GIT_EXTERNAL_DIFF=git-apply-patch-script git-diff-tree -p $tree1 $tree2
#

case "$#" in
2)    exit 1 ;; # do not feed unmerged diff to me!
esac
name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
case "$mode1" in *7??) mode1=+x ;; *6??) mode1=-x ;; esac
case "$mode2" in *7??) mode2=+x ;; *6??) mode2=-x ;; esac

if test -f "$name.orig" || test -f "$name.rej"
then
    echo >&2 "Unresolved patch conflicts in the previous run found."
    exit 1
fi

case "$mode1,$mode2" in
.,?x)
    # newly created
    dir=$(dirname "$name")
    case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
	echo >&2 "cannot create leading path for $name."
	exit 1
    }
    if test -f "$name"
    then
	echo >&2 "file $name to be created already exists."
	exit 1
    fi
    cat "$tmp2" >"$name" || {
	echo >&2 "cannot create $name."
	exit 1
    } 
    case "$mode2" in
    +x)
	echo >&2 "created $name with mode +x."
	chmod "$mode2" "$name"
	;;
    -x)
	echo >&2 "created $name."
	;;
    esac
    git-update-cache --add -- "$name"
    ;;
?x,.)
    # deleted
    echo >&2 "deleted $name."
    rm -f "$name" || {
	echo >&2 "cannot remove $name";
	exit 1
    }
    git-update-cache --remove -- "$name"
    ;;
*)
    # changed
    dir=$(dirname "$name")
    case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
	echo >&2 "cannot create leading path for $name."
	exit 1
    }
    # This will say "patching ..." so we do not say anything outselves.
    diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" | patch -p1 || exit

    case "$mode1,$mode2" in
    "$mode2,$mode1") ;;
    *)
	echo >&2 "changing mode from $mode1 to $mode2."
	chmod "$mode2" "$name"
	;;
    esac
    git-update-cache -- "$name"
esac