patch_git.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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) 2021 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. elif [ -e .git/rebase-apply ]; then
  35. git am --abort
  36. fi
  37. i=0
  38. patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
  39. while [ -d $patch_tmp ]; do
  40. let "i++"
  41. patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
  42. done
  43. mkdir -p $patch_tmp
  44. patch_series=$(printf "%04d" $i)
  45. git commit --allow-empty --no-signoff --no-gpg-sign \
  46. --author="OpenADK <wbx@openadk.org>" \
  47. -m "OpenADK patch marker: $patch_series"
  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. patchname="$(basename -s .gz -s .bz -s .bz2 -s .zip -s .Z -s .patch $i)"
  71. {
  72. if ! grep -q '^Subject: ' ${i}; then
  73. echo "From: OpenADK <wbx@openadk.org>"
  74. echo "Subject: [PATCH] ${patchname#[0-9]*-}"
  75. echo ""
  76. fi
  77. ${uncomp} ${i}
  78. } >${targetdir}/${patch_tmp}/${patchname}.patch
  79. cd $patchdir
  80. done
  81. # no patches to apply? bail out
  82. [ -e ${targetdir}/${patch_tmp}/__patchfiles__ ] || {
  83. rmdir ${targetdir}/${patch_tmp}
  84. exit 0
  85. }
  86. # provide backwards compatibility to old style using 'patch' program
  87. # XXX: this is unsafe and should be dropped at some point
  88. am_opts="-C1"
  89. cd ${wd}
  90. realpath $patchdir >${targetdir}/${patch_tmp}/__patchdir__
  91. cd ${targetdir}
  92. git am $am_opts ${patch_tmp}/*.patch
  93. if [ $? != 0 ] ; then
  94. echo "git-am failed! Please fix patches!"
  95. exit 1
  96. fi