update-patches 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. fi
  78. DIFF_FLAGS="-adu -I \"^--- $(print -r -- "$D_SUBP.orig/" | $TRANSFORM)@@ .*\""
  79. DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(print -r -- "$D_SUBP/" | $TRANSFORM)@@ .*\""
  80. (cd "${WRKDIST}"; find . -type f -print0) |&
  81. while IFS= read -p -d '' -r file; do
  82. file=${file#./}
  83. [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
  84. for i in $DIFF_IGNOREFILES; do
  85. [[ $file = $i ]] && continue 2
  86. done
  87. cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
  88. print -ru2 -- "Processing ${file}..."
  89. # look in patchdir for an existing patchfile matching this
  90. cd "$PATCHDIR"
  91. for i in $PATCH_LIST; do
  92. # Ignore non-files, or old backup
  93. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  94. # Patch found. Is this the one?
  95. if grep "^[+-][+-][+-] $D_CMP[^/]*/$file " "$i" >/dev/null; then
  96. # Multiple files in the diff?
  97. if [[ $(grep -c "^--- $D_CMP" "$i") -gt 1 || \
  98. $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]]; then
  99. print -ru2 -- "Cannot process, $i contains patches"
  100. print -ru2 -- "to multiple files! Aborting."
  101. print -n 'FAIL\0'
  102. [[ -n $SUBDIST ]] && mv \
  103. "${WRKDIR1}/${SUBDIST}.orig" \
  104. "${WRKDIR1}.orig/${SUBDIST}"
  105. exit 0
  106. fi
  107. # Multiple diffs with this file?
  108. let n=0
  109. pflst=
  110. for j in $PATCH_LIST; do
  111. [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
  112. continue
  113. grep "^[+-][+-][+-] $D_CMP[^/]*/$file " \
  114. "$j" >/dev/null || continue
  115. let n++
  116. pflst="$pflst '$j'"
  117. done
  118. if (( n != 1 )); then
  119. print -ru2 -- "Cannot process, file $file"
  120. print -ru2 -- "is contained in multiple patches:"
  121. print -ru2 -- "$pflst"
  122. print -n 'FAIL\0'
  123. [[ -n $SUBDIST ]] && mv \
  124. "${WRKDIR1}/${SUBDIST}.orig" \
  125. "${WRKDIR1}.orig/${SUBDIST}"
  126. exit 0
  127. fi
  128. # No, process this patch
  129. accounted+=("$i")
  130. # found it, copy preamble before comparision
  131. ( sed -e "/^--- /,\$d" <"$i"; \
  132. cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
  133. ) >"$i.new"
  134. # did it change? mark it as changed
  135. tfile=$(print -r -- "$file" | $TRANSFORM)
  136. if eval diff "$(print -r -- "${DIFF_FLAGS}" | sed \
  137. "s#@@#${tfile}#g")" '"$i" "$i.new"' 1>&2; then
  138. rm "$i.new"
  139. else
  140. print -ru2 -- "Patch $i for $file updated"
  141. mv "$i" "$i.orig"
  142. mv "$i.new" "$i"
  143. edit+=("$i")
  144. fi
  145. continue 2
  146. fi
  147. done
  148. # Build a sensible name for the new patch file
  149. patchname=patch-${file//[\/.- ]/_}
  150. print -ru2 -- "No patch-* found for $file, creating $patchname"
  151. ( cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" ) >"$patchname"
  152. edit+=("$patchname")
  153. accounted+=("$patchname")
  154. done
  155. # Verify all patches accounted for
  156. cd "$PATCHDIR"
  157. for i in *; do
  158. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  159. grep '^\\ No newline at end of file' "$i" >/dev/null && \
  160. print -ru2 -- "*** Patch $i needs manual intervention"
  161. found=0
  162. for j in "${accounted[@]}"; do
  163. [[ $i = "$j" ]] || continue
  164. found=1
  165. break
  166. done
  167. (( found )) || print -ru2 -- "*** Patch $i not accounted for"
  168. done
  169. for i in "${edit[@]}"; do
  170. print -nr -- "$i"
  171. print -n '\0'
  172. done
  173. [[ -n $SUBDIST ]] && mv "${WRKDIR1}/${SUBDIST}.orig" "${WRKDIR1}.orig/${SUBDIST}"
  174. exit 0