x11vnc: fix compile error
[openadk.git] / scripts / patch_git.sh
blob40653a5760ccbe1b9c9d960e79407796c0068a51
1 #!/usr/bin/env bash
3 # Patch sources using git-am, aligning things to use git-format-patch for
4 # update-patches.
6 # (c) 2021 Phil Sutter <phil@nwl.cc>
8 # Based on the classic patch.sh, written by:
10 # (c) 2006, 2007 Thorsten Glaser <tg@freewrt.org>
11 # (c) 2002 Erik Andersen <andersen@codepoet.org>
13 [[ -n $BASH_VERSION ]] && shopt -s extglob
15 # Set directories from arguments, or use defaults.
16 targetdir=${1-.}
17 patchdir=${2-../patches}
18 patchpattern=${3-*}
20 if [ ! -d "${targetdir}" ] ; then
21 echo "Aborting. '${targetdir}' is not a directory."
22 exit 1
24 if [ ! -d "${patchdir}" ] ; then
25 echo "Aborting. '${patchdir}' is not a directory."
26 exit 0
29 wd=$(pwd)
31 cd "${targetdir}"
32 if [ ! -d .git ]; then
33 # drop leftover .gitignores in non-git sources, they
34 # might prevent us from patching 'temporary' files
35 # which are still present in the tarball.
36 find . -name .gitignore -delete
37 git init
38 git add .
39 elif [ -e .git/rebase-apply ]; then
40 git am --abort
43 i=0
44 patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
45 while [ -d $patch_tmp ]; do
46 let "i++"
47 patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
48 done
49 mkdir -p $patch_tmp
50 patch_series=$(printf "%04d" $i)
52 git commit --allow-empty --no-signoff --no-gpg-sign \
53 --author="OpenADK <wbx@openadk.org>" \
54 -m "OpenADK patch marker: $patch_series"
56 cd $wd
57 cd $patchdir
58 for i in $(eval echo ${patchpattern}); do
59 test -e "$i" || continue
60 i=$patchdir/$i
61 cd $wd
62 case $i in
63 *.gz)
64 type="gzip"; uncomp="gunzip -dc"; ;;
65 *.bz)
66 type="bzip"; uncomp="bunzip -dc"; ;;
67 *.bz2)
68 type="bzip2"; uncomp="bunzip2 -dc"; ;;
69 *.zip)
70 type="zip"; uncomp="unzip -d"; ;;
71 *.Z)
72 type="compress"; uncomp="uncompress -c"; ;;
74 type="plaintext"; uncomp="cat"; ;;
75 esac
76 [ -d "${i}" ] && echo "Ignoring subdirectory ${i}" && continue
77 echo "$(basename $i)" >>${targetdir}/${patch_tmp}/__patchfiles__
78 patchname="$(basename -s .gz -s .bz -s .bz2 -s .zip -s .Z -s .patch $i)"
80 if ! grep -q '^Subject: ' ${i}; then
81 echo "From: OpenADK <wbx@openadk.org>"
82 echo "Subject: [PATCH] ${patchname#[0-9]*-}"
83 echo ""
85 ${uncomp} ${i}
86 } >${targetdir}/${patch_tmp}/${patchname}.patch
87 cd $patchdir
88 done
90 # no patches to apply? bail out
91 [ -e ${targetdir}/${patch_tmp}/__patchfiles__ ] || {
92 rmdir ${targetdir}/${patch_tmp}
93 exit 0
96 # provide backwards compatibility to old style using 'patch' program
97 # XXX: this is unsafe and should be dropped at some point
98 am_opts="-C1"
100 cd ${wd}
101 realpath $patchdir >${targetdir}/${patch_tmp}/__patchdir__
102 cd ${targetdir}
103 git am $am_opts ${patch_tmp}/*.patch
104 if [ $? != 0 ] ; then
105 echo "git-am failed! Please fix patches!"
106 exit 1