patch_git.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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=$(realpath ${2-../patches})
  16. patchpattern=${3-*}
  17. git_commit_marker() { # (num)
  18. git commit --allow-empty --no-signoff --no-gpg-sign \
  19. --author="OpenADK <wbx@openadk.org>" \
  20. -m "OpenADK patch marker: $1"
  21. }
  22. if [ ! -d "${targetdir}" ] ; then
  23. echo "Aborting. '${targetdir}' is not a directory."
  24. exit 1
  25. fi
  26. wd=$(pwd)
  27. cd "${targetdir}"
  28. if [ ! -d .git ]; then
  29. # drop leftover .gitignores in non-git sources, they
  30. # might prevent us from patching 'temporary' files
  31. # which are still present in the tarball.
  32. find . -name .gitignore -delete
  33. git init
  34. git add .
  35. git_commit_marker $(printf "%04d" 0)
  36. elif [ -e .git/rebase-apply ]; then
  37. git am --abort
  38. fi
  39. i=0
  40. patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
  41. while [ -d $patch_tmp ]; do
  42. let "i++"
  43. patch_tmp=$(printf ".git/patch_tmp/%04d" $i)
  44. done
  45. mkdir -p $patch_tmp
  46. patch_series=$(printf "%04d" $i)
  47. if [ $i -gt 0 ]; then
  48. git_commit_marker $patch_series
  49. fi
  50. echo "$patchdir" >${targetdir}/${patch_tmp}/__patchdir__
  51. if [ ! -d "${patchdir}" ] ; then
  52. echo "Aborting. '${patchdir}' is not a directory."
  53. exit 0
  54. fi
  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"; ;;
  72. *)
  73. type="plaintext"; uncomp="cat"; ;;
  74. esac
  75. [ -d "${i}" ] && echo "Ignoring subdirectory ${i}" && continue
  76. echo "$(basename $i)" >>${targetdir}/${patch_tmp}/__patchfiles__
  77. patchname="$(basename -s .gz -s .bz -s .bz2 -s .zip -s .Z -s .patch $i)"
  78. {
  79. if ! grep -q '^Subject: ' ${i}; then
  80. echo "From: OpenADK <wbx@openadk.org>"
  81. echo "Subject: [PATCH] ${patchname#[0-9]*-}"
  82. echo ""
  83. fi
  84. ${uncomp} ${i}
  85. } >${targetdir}/${patch_tmp}/${patchname}.patch
  86. cd $patchdir
  87. done
  88. # no patches to apply? bail out
  89. [ -e ${targetdir}/${patch_tmp}/__patchfiles__ ] || {
  90. rmdir ${targetdir}/${patch_tmp}
  91. exit 0
  92. }
  93. # provide backwards compatibility to old style using 'patch' program
  94. # XXX: this is unsafe and should be dropped at some point
  95. am_opts="-C1"
  96. cd ${targetdir}
  97. git am $am_opts ${patch_tmp}/*.patch
  98. if [ $? != 0 ] ; then
  99. echo "git-am failed! Please fix patches!"
  100. exit 1
  101. fi