libnfs: update to 2.0.0
[openadk.git] / scripts / patch_git.sh
blob36a2d6af80e0244270ca5908fa73ff5b6229b358
1 #!/usr/bin/env bash
3 # Patch sources using git-am, aligning things to use git-format-patch for
4 # update-patches.
6 # (c) 2016 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 git commit -a --allow-empty \
40 --author="OpenADK <wbx@openadk.org>" \
41 -m "OpenADK patch marker: 0000"
43 [ -e .git/rebase-apply ] && \
44 git am --abort
46 i=1
47 patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
48 while [ -d $patch_tmp ]; do
49 let "i++"
50 patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
51 done
52 mkdir -p $patch_tmp
53 patch_series=$(printf "%04d" $i)
55 cd $wd
56 cd $patchdir
57 for i in $(eval echo ${patchpattern}); do
58 test -e "$i" || continue
59 i=$patchdir/$i
60 cd $wd
61 case $i in
62 *.gz)
63 type="gzip"; uncomp="gunzip -dc"; ;;
64 *.bz)
65 type="bzip"; uncomp="bunzip -dc"; ;;
66 *.bz2)
67 type="bzip2"; uncomp="bunzip2 -dc"; ;;
68 *.zip)
69 type="zip"; uncomp="unzip -d"; ;;
70 *.Z)
71 type="compress"; uncomp="uncompress -c"; ;;
73 type="plaintext"; uncomp="cat"; ;;
74 esac
75 [ -d "${i}" ] && echo "Ignoring subdirectory ${i}" && continue
76 echo "$(basename $i)" >>${targetdir}/${patch_tmp}/__patchfiles__
77 fake_hdr=""
78 patchname="$(basename -s .gz -s .bz -s .bz2 -s .zip -s .Z -s .patch $i)"
79 if ! grep -q '^Subject: ' ${i}; then
80 fake_hdr="From: OpenADK <wbx@openadk.org>\nSubject: [PATCH] ${patchname#[0-9]*-}\n\n"
82 { echo -en $fake_hdr; ${uncomp} ${i}; } >${targetdir}/${patch_tmp}/${patchname}.patch
83 cd $patchdir
84 done
86 # no patches to apply? bail out
87 [ -e ${targetdir}/${patch_tmp}/__patchfiles__ ] || {
88 rmdir ${targetdir}/${patch_tmp}
89 exit 0
92 # provide backwards compatibility to old style using 'patch' program
93 # XXX: this is unsafe and should be dropped at some point
94 am_opts="-C1"
96 realpath $patchdir >${targetdir}/${patch_tmp}/__patchdir__
97 cd ${targetdir}
98 git am $am_opts ${patch_tmp}/*.patch
99 if [ $? != 0 ] ; then
100 echo "git-am failed! Please fix patches!"
101 exit 1
103 git commit -a --allow-empty \
104 --author="OpenADK <wbx@openadk.org>" \
105 -m "OpenADK patch marker: $patch_series"