aboutsummaryrefslogtreecommitdiffstats
path: root/unix/makepatch.sh
blob: 86804ab056bc39b5a5facbb573e621290ef07f59 (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
#!/bin/sh
#
# Automatically make the patches for the updated files by find
# the corresponding '*.orig' files through the given directory.
#
# Based on the 'makepatch' target of FreeBSD 'Mk/bsd.port.mk'.
#
#
# Aaron LI
# Created: 2015-10-13
# Updated: 2015-12-07
#
# ChangeLog:
# 2015-12-06:
#   * Drop option '-s' for `find`
# 2015-12-07:
#   * Replace 'workdir' with 'srcdir'
#   * Make find to search 'srcdir' instead of '.'
#

case "$1" in
    -[hH]*|--[hH]*)
        echo "Usage: $0 [srcdir] [patchdir]"
        echo ""
        echo "Both 'srcdir' and 'patchdir' default to be ."
        exit 1
        ;;
esac

SRCDIR=${1:-.}
PATCHDIR=${2:-.}

if [ ! -d "${PATCHDIR}" ]; then
    mkdir ${PATCHDIR}
fi

PATCH_PATH_SEPARATOR="_"

for f in `find ${SRCDIR}/ -type f -name '*.orig'`; do
    _SEEN="NO"
    ORIG=${f#./}
    NEW=${ORIG%.orig}
    if cmp -s ${ORIG} ${NEW}; then
        # The two files are identical
        continue
    fi
    PATCH=`echo ${NEW} | sed -e \
        "s|${PATCH_PATH_SEPARATOR}|&&|g" -e "s|/|${PATCH_PATH_SEPARATOR}|g"`
    OUT="${PATCHDIR}/patch-${PATCH}"
    if test -f "${OUT}"; then
        echo WARNING: patch already exists: \"${OUT}\"
    else
        echo diff -udp ${ORIG} ${NEW} '>' ${OUT}
        TZ=UTC diff -udp ${ORIG} ${NEW} | sed -e \
            '/^---/s|\.[0-9]* +0000$| UTC|' -e \
            '/^+++/s|\([[:blank:]][-0-9:.+]*\)*$||' > ${OUT}
    fi
done

exit 0