patch_git.sh 2.7 KB

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