1
0

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