import-backports.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. BASE=$1; shift
  3. usage() {
  4. echo "Usage: $0 NNN <file>..."
  5. exit 1
  6. }
  7. check_number() {
  8. case "$1" in
  9. [0-9][0-9][0-9]) return 0;;
  10. esac
  11. return 1;
  12. }
  13. patch_header()
  14. {
  15. awk '
  16. /^(---|\*\*\*|Index:)[ \t][^ \t]|^diff -/ \
  17. { exit }
  18. { print }
  19. '
  20. }
  21. strip_diffstat()
  22. {
  23. awk '
  24. /#? .* \| / \
  25. { eat = eat $0 "\n"
  26. next }
  27. /^#? .* files? changed(, .* insertions?\(\+\))?(, .* deletions?\(-\))?/ \
  28. { eat = ""
  29. next }
  30. { print eat $0
  31. eat = "" }
  32. '
  33. }
  34. strip_trailing_whitespace() {
  35. sed -e 's:[ '$'\t'']*$::'
  36. }
  37. fixup_header() {
  38. awk '
  39. /^From / { next }
  40. /^Subject: / {
  41. sub("Subject: \\[[^\]]*\\]", "Subject: [PATCH]")
  42. }
  43. { print }
  44. '
  45. }
  46. check_number "$BASE" || usage
  47. quilt series > /dev/null || {
  48. echo "Not in quilt directory"
  49. exit 2
  50. }
  51. get_next() {
  52. NEW=$BASE
  53. quilt series | while read CUR; do
  54. [ -n "$CUR" ] || break
  55. CUR=${CUR%%-*}
  56. check_number "$CUR" || continue
  57. [ "$CUR" -lt "$NEW" ] && continue
  58. [ "$CUR" -ge "$(($BASE + 100))" ] && continue
  59. NEW="$(($CUR + 1))"
  60. echo $NEW
  61. done | tail -n1
  62. }
  63. CUR=$(get_next)
  64. CUR="${CUR:-$BASE}"
  65. while [ -n "$1" ]; do
  66. FILE="$1"; shift
  67. NAME="$(basename $FILE)"
  68. NAME="${NAME#[0-9]*-}"
  69. echo -n "Processing patch $NAME: "
  70. [ -e "$FILE" ] || {
  71. echo "file $FILE not found"
  72. exit 1
  73. }
  74. grep -qE "$NAME$" patches/series && {
  75. echo "already applied"
  76. continue
  77. }
  78. quilt new "$CUR-$NAME" || exit 1
  79. patch_header < "$FILE" |
  80. strip_diffstat |
  81. strip_trailing_whitespace |
  82. fixup_header > "patches/$CUR-$NAME"
  83. quilt fold < "$FILE" || {
  84. cp "$FILE" ./cur_patch
  85. echo "patch $FILE failed to apply, copied to ./cur_patch"
  86. exit 1
  87. }
  88. quilt refresh -p ab --no-index --no-timestamps
  89. CUR="$(($CUR + 1))"
  90. done
  91. exit 0