update-patches 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env mksh
  2. #-
  3. # Copyright (c) 2014
  4. # Thorsten Glaser <tg@mirbsd.org>
  5. # Copyright (c) 2006
  6. # Thorsten Glaser <tg@freewrt.org>
  7. #
  8. # Derived from the MirPorts Framework "update-patches" script:
  9. #
  10. # Copyright (c) 2003, 2004, 2005
  11. # Thorsten "mirabile" Glaser <tg@MirBSD.de>
  12. # Based upon code and idea (c) 2000
  13. # Marc Espie for the OpenBSD project. All rights reserved.
  14. #
  15. # Provided that these terms and disclaimer and all copyright notices
  16. # are retained or reproduced in an accompanying document, permission
  17. # is granted to deal in this work without restriction, including un-
  18. # limited rights to use, publicly perform, distribute, sell, modify,
  19. # merge, give away, or sublicence.
  20. #
  21. # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
  22. # the utmost extent permitted by applicable law, neither express nor
  23. # implied; without malicious intent or gross negligence. In no event
  24. # may a licensor, author or contributor be held liable for indirect,
  25. # direct, other damage, loss, or other issues arising in any way out
  26. # of dealing in the work, even if advised of the possibility of such
  27. # damage or existence of a defect, except proven that it results out
  28. # of said person's immediate fault when using the work as intended.
  29. do_diff() {
  30. local f1=$2/$1
  31. local f2=$3/$1
  32. if [[ ! -e $f1 ]]; then
  33. [[ -d ${f1%/*}/. ]] || mkdir -p "${f1%/*}"
  34. if [[ ! -s $f2 ]]; then
  35. cat <<EOF
  36. --- $f1 (non-existant)
  37. +++ $f2 (empty)
  38. @@ -0,0 +0,0 @@
  39. EOF
  40. return 0
  41. fi
  42. touch -t 197001010000.00 "$f1"
  43. fi
  44. diff -adup "$f1" "$f2"
  45. return $?
  46. }
  47. set -A accounted
  48. set -A edit
  49. TRANSFORM='sed s/[.+]/\\\\&/g'
  50. PATCHDIR=$CURDIR/patches
  51. EXTRADIR=$CURDIR/extra
  52. mkdir -p "$PATCHDIR"
  53. SUBDIST=${WRKDIST##${WRKDIR1}?(/)}
  54. if [[ -n $SUBDIST ]]; then
  55. mv "${WRKDIR1}.orig/${SUBDIST}" "${WRKDIR1}/${SUBDIST}.orig"
  56. D_BASE=${WRKDIR1}
  57. D_SUB=${SUBDIST}
  58. # D_SUBP=$D_SUB
  59. D_SUBP='[^/]*'
  60. D_CMP=$D_SUBP
  61. else
  62. # WRKSRC == WRKDIR
  63. D_BASE=$(dirname "${WRKDIR1}")
  64. D_SUB=$(basename "${WRKDIR1}")
  65. D_SUBP=$D_SUB
  66. D_CMP=
  67. fi
  68. ORGDIST=${D_BASE}/${D_SUB}.orig
  69. if [[ -e $WRKDIST/.patched-newfiles ]]; then
  70. touch "$ORGDIST/.patched-newfiles"
  71. patch_newfiles=1
  72. else
  73. patch_newfiles=0
  74. fi
  75. if [[ -e $WRKDIST/../.autoreconf_done ]]; then
  76. touch "$ORGDIST/.autoreconf_done"
  77. ignore_autoconf=1
  78. else
  79. ignore_autoconf=0
  80. fi
  81. DIFF_FLAGS="-adu -I \"^--- $(print -r -- "$D_SUBP.orig/" | $TRANSFORM)@@ .*\""
  82. DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(print -r -- "$D_SUBP/" | $TRANSFORM)@@ .*\""
  83. (cd "${WRKDIST}"; find . -type f -print0) |&
  84. while IFS= read -p -d '' -r file; do
  85. file=${file#./}
  86. #print -r -- "DEBUG: <$file>" >>/tmp/debug
  87. [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
  88. if (( ignore_autoconf )); then
  89. [[ $file = configure ]] && continue
  90. [[ $file = missing ]] && continue
  91. [[ $file = depcomp ]] && continue
  92. [[ $file = install-sh ]] && continue
  93. [[ $file = aclocal.m4 ]] && continue
  94. [[ $file = INSTALL ]] && continue
  95. [[ $file = config.h.in ]] && continue
  96. [[ ${file##*/} = Makefile.in ]] && continue
  97. fi
  98. cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
  99. print -ru2 -- "Processing ${file}..."
  100. # look in patchdir for an existing patchfile matching this
  101. cd "$PATCHDIR"
  102. for i in $PATCH_LIST; do
  103. # Ignore non-files, or old backup
  104. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  105. # Patch found. Is this the one?
  106. if grep "^[+-][+-][+-] $D_CMP[^/]*/$file " "$i" >/dev/null; then
  107. # Multiple files in the diff?
  108. if [[ $(grep -c "^--- $D_CMP" "$i") -gt 1 || \
  109. $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]]; then
  110. print -ru2 -- "Cannot process, $i contains patches"
  111. print -ru2 -- "to multiple files! Aborting."
  112. print -n 'FAIL\0'
  113. [[ -n $SUBDIST ]] && mv \
  114. "${WRKDIR1}/${SUBDIST}.orig" \
  115. "${WRKDIR1}.orig/${SUBDIST}"
  116. exit 0
  117. fi
  118. # Multiple diffs with this file?
  119. let n=0
  120. pflst=
  121. for j in $PATCH_LIST; do
  122. [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
  123. continue
  124. grep "^[+-][+-][+-] $D_CMP[^/]*/$file " \
  125. "$j" >/dev/null || continue
  126. let n++
  127. pflst="$pflst '$j'"
  128. done
  129. if (( n != 1 )); then
  130. print -ru2 -- "Cannot process, file $file"
  131. print -ru2 -- "is contained in multiple patches:"
  132. print -ru2 -- "$pflst"
  133. print -n 'FAIL\0'
  134. [[ -n $SUBDIST ]] && mv \
  135. "${WRKDIR1}/${SUBDIST}.orig" \
  136. "${WRKDIR1}.orig/${SUBDIST}"
  137. exit 0
  138. fi
  139. # No, process this patch
  140. accounted+=("$i")
  141. # found it, copy preamble before comparision
  142. ( sed -e "/^--- /,\$d" <"$i"; \
  143. cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
  144. ) >"$i.new"
  145. # did it change? mark it as changed
  146. tfile=$(print -r -- "$file" | $TRANSFORM)
  147. if eval diff "$(print -r -- "${DIFF_FLAGS}" | sed \
  148. "s#@@#${tfile}#g")" '"$i" "$i.new"' 1>&2; then
  149. rm "$i.new"
  150. else
  151. print -ru2 -- "Patch $i for $file updated"
  152. mv "$i" "$i.orig"
  153. mv "$i.new" "$i"
  154. edit+=("$i")
  155. fi
  156. continue 2
  157. fi
  158. done
  159. # Build a sensible name for the new patch file
  160. patchname=patch-${file//[\/.- ]/_}
  161. print -ru2 -- "No patch-* found for $file, creating $patchname"
  162. ( cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" ) >"$patchname"
  163. edit+=("$patchname")
  164. accounted+=("$patchname")
  165. done
  166. # Verify all patches accounted for
  167. cd "$PATCHDIR"
  168. for i in *; do
  169. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  170. grep '^\\ No newline at end of file' "$i" >/dev/null && \
  171. print -ru2 -- "*** Patch $i needs manual intervention"
  172. found=0
  173. for j in "${accounted[@]}"; do
  174. [[ $i = "$j" ]] || continue
  175. found=1
  176. break
  177. done
  178. (( found )) || print -ru2 -- "*** Patch $i not accounted for"
  179. done
  180. for i in "${edit[@]}"; do
  181. print -nr -- "$i"
  182. print -n '\0'
  183. done
  184. [[ -n $SUBDIST ]] && mv "${WRKDIR1}/${SUBDIST}.orig" "${WRKDIR1}.orig/${SUBDIST}"
  185. exit 0