apply-zephyr-patches.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env bash
  2. # Applies this app's local patches on top of the shared Zephyr checkout, so
  3. # that checkout stays a clean, reusable resource for other projects while
  4. # still carrying the changes this app needs. Run once after a fresh
  5. # `west update`, or after pulling upstream changes into the shared tree.
  6. #
  7. # Patches are tracked in zephyr-patches/ as plain `git format-patch`
  8. # output; applied with `git am` so they land as real commits (reviewable,
  9. # revertable) rather than a silent working-tree diff.
  10. set -euo pipefail
  11. ZEPHYR_BASE="${ZEPHYR_BASE:-./zephyrproject/zephyr}"
  12. PATCH_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/zephyr-patches"
  13. if [ ! -d "$ZEPHYR_BASE/.git" ]; then
  14. echo "error: $ZEPHYR_BASE is not a git checkout" >&2
  15. exit 1
  16. fi
  17. cd "$ZEPHYR_BASE"
  18. if [ -n "$(git status --porcelain)" ]; then
  19. echo "error: $ZEPHYR_BASE has uncommitted changes - commit, stash, or" >&2
  20. echo " reset it before applying patches, so it's clear which" >&2
  21. echo " commits came from these patches vs. local hacking." >&2
  22. exit 1
  23. fi
  24. for patch in "$PATCH_DIR"/*.patch; do
  25. # git mailinfo is the same header parser `git am` itself uses, so it
  26. # correctly strips "[PATCH]"/"[PATCH 1/2]" prefixes and un-wraps a
  27. # subject line that spans multiple header lines - a plain sed/grep
  28. # on "Subject:" got this wrong for a >1-patch series and silently
  29. # skipped every patch without applying them (grep -qF against an
  30. # empty extracted subject matches any line).
  31. msgfile=$(mktemp)
  32. bodyfile=$(mktemp)
  33. subject=$(git mailinfo "$msgfile" "$bodyfile" < "$patch" | sed -n 's/^Subject: //p')
  34. rm -f "$msgfile" "$bodyfile"
  35. if [ -z "$subject" ]; then
  36. echo "error: could not extract a Subject from $(basename "$patch")" >&2
  37. exit 1
  38. fi
  39. history=$(git log --oneline)
  40. if grep -qF "$subject" <<< "$history"; then
  41. echo "skip (already applied): $subject"
  42. continue
  43. fi
  44. echo "applying: $(basename "$patch")"
  45. git am "$patch"
  46. done
  47. echo "done - $ZEPHYR_BASE now carries $(ls "$PATCH_DIR"/*.patch | wc -l | tr -d ' ') local patch(es)."