summaryrefslogtreecommitdiff
path: root/scripts/patch_git.sh
blob: 40653a5760ccbe1b9c9d960e79407796c0068a51 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
#
# Patch sources using git-am, aligning things to use git-format-patch for
# update-patches.
#
# (c) 2021 Phil Sutter <phil@nwl.cc>
#
# Based on the classic patch.sh, written by:
#
# (c) 2006, 2007 Thorsten Glaser <tg@freewrt.org>
# (c) 2002 Erik Andersen <andersen@codepoet.org>

[[ -n $BASH_VERSION ]] && shopt -s extglob

# Set directories from arguments, or use defaults.
targetdir=${1-.}
patchdir=${2-../patches}
patchpattern=${3-*}

if [ ! -d "${targetdir}" ] ; then
    echo "Aborting.  '${targetdir}' is not a directory."
    exit 1
fi
if [ ! -d "${patchdir}" ] ; then
    echo "Aborting.  '${patchdir}' is not a directory."
    exit 0
fi

wd=$(pwd)

cd "${targetdir}"
if [ ! -d .git ]; then
    # drop leftover .gitignores in non-git sources, they
    # might prevent us from patching 'temporary' files
    # which are still present in the tarball.
    find . -name .gitignore -delete
    git init
    git add .
elif [ -e .git/rebase-apply ]; then
    git am --abort
fi

i=0
patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
while [ -d $patch_tmp ]; do
	let "i++"
	patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
done
mkdir -p $patch_tmp
patch_series=$(printf "%04d" $i)

git commit --allow-empty --no-signoff --no-gpg-sign \
    --author="OpenADK <wbx@openadk.org>" \
    -m "OpenADK patch marker: $patch_series"

cd $wd
cd $patchdir
for i in $(eval echo ${patchpattern}); do
    test -e "$i" || continue
    i=$patchdir/$i
    cd $wd
    case $i in
	*.gz)
	type="gzip"; uncomp="gunzip -dc"; ;;
	*.bz)
	type="bzip"; uncomp="bunzip -dc"; ;;
	*.bz2)
	type="bzip2"; uncomp="bunzip2 -dc"; ;;
	*.zip)
	type="zip"; uncomp="unzip -d"; ;;
	*.Z)
	type="compress"; uncomp="uncompress -c"; ;;
	*)
	type="plaintext"; uncomp="cat"; ;;
    esac
    [ -d "${i}" ] && echo "Ignoring subdirectory ${i}" && continue
    echo "$(basename $i)" >>${targetdir}/${patch_tmp}/__patchfiles__
    patchname="$(basename -s .gz -s .bz -s .bz2 -s .zip -s .Z -s .patch $i)"
    {
        if ! grep -q '^Subject: ' ${i}; then
            echo "From: OpenADK <wbx@openadk.org>"
            echo "Subject: [PATCH] ${patchname#[0-9]*-}"
            echo ""
        fi
        ${uncomp} ${i}
    } >${targetdir}/${patch_tmp}/${patchname}.patch
    cd $patchdir
done

# no patches to apply? bail out
[ -e ${targetdir}/${patch_tmp}/__patchfiles__ ] || {
	rmdir ${targetdir}/${patch_tmp}
	exit 0
}

# provide backwards compatibility to old style using 'patch' program
# XXX: this is unsafe and should be dropped at some point
am_opts="-C1"

cd ${wd}
realpath $patchdir >${targetdir}/${patch_tmp}/__patchdir__
cd ${targetdir}
git am $am_opts ${patch_tmp}/*.patch
if [ $? != 0 ] ; then
    echo "git-am failed! Please fix patches!"
    exit 1
fi