update-patches 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env bash
  2. # $Id: update-patches 24 2008-08-31 14:56:13Z wbx $
  3. # $MirOS: ports/infrastructure/scripts/update-patches,v 1.5 2006/06/15 19:18:43 tg Exp $
  4. #-
  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. [[ -n $BASH_VERSION ]] && shopt -s extglob
  30. do_diff()
  31. {
  32. local f1=$2/$1
  33. local f2=$3/$1
  34. if [[ ! -e $f1 ]]; then
  35. [[ -d ${f1%/*}/. ]] || mkdir -p ${f1%/*}
  36. if [[ ! -s $f2 ]]; then
  37. cat <<EOF
  38. --- $f1 (non-existant)
  39. +++ $f2 (empty)
  40. @@ -0,0 +0,0 @@
  41. EOF
  42. return 0
  43. fi
  44. touch -t 197001010000.00 "$f1"
  45. fi
  46. diff -adup "$f1" "$f2"
  47. return $?
  48. }
  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. [[ -d $EXTRADIR ]] && \
  70. (cd $EXTRADIR; find . -print0) | \
  71. (cd $WRKDIST; pax -rw -0 -d $ORGDIST/) >/dev/null 2>&1
  72. if [[ -e $WRKDIST/.patched-newfiles ]]; then
  73. touch $ORGDIST/.patched-newfiles
  74. patch_newfiles=1
  75. else
  76. patch_newfiles=0
  77. fi
  78. DIFF_FLAGS="-adu -I \"^--- $(echo $D_SUBP.orig/ | $TRANSFORM)@@ .*\""
  79. DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(echo $D_SUBP/ | $TRANSFORM)@@ .*\""
  80. for file in $(cd ${WRKDIST}; find . -type f | sed 's#^\./##'); do
  81. [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
  82. cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
  83. echo "Processing ${file}..." >&2
  84. # look in patchdir for an existing patchfile matching this
  85. cd $PATCHDIR
  86. for i in $PATCH_LIST; do
  87. # Ignore non-files, or old backup
  88. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  89. # Patch found. Is this the one?
  90. if grep "^[+-][+-][+-] $D_CMP[^/]*/$file " "$i" >/dev/null; then
  91. # Multiple files in the diff?
  92. if [ $(grep -c "^--- $D_CMP" "$i") -gt 1 -o \
  93. $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]; then
  94. echo "Cannot process, $i contains patches" >&2
  95. echo "to multiple files! Aborting." >&2
  96. echo FAIL
  97. [[ -n $SUBDIST ]] && mv \
  98. ${WRKDIR1}/${SUBDIST}.orig \
  99. ${WRKDIR1}.orig/${SUBDIST}
  100. exit 0
  101. fi
  102. # Multiple diffs with this file?
  103. let n=0
  104. pflst=
  105. for j in $PATCH_LIST; do
  106. [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
  107. continue
  108. grep "^[+-][+-][+-] $D_CMP[^/]*/$file " \
  109. "$j" >/dev/null || continue
  110. let n++
  111. pflst="$pflst '$j'"
  112. done
  113. if (( n != 1 )); then
  114. echo "Cannot process, file $file" >&2
  115. echo "is contained in multiple patches:" >&2
  116. echo "$pflst" >&2
  117. echo FAIL
  118. [[ -n $SUBDIST ]] && mv \
  119. ${WRKDIR1}/${SUBDIST}.orig \
  120. ${WRKDIR1}.orig/${SUBDIST}
  121. exit 0
  122. fi
  123. # No, process this patch
  124. accounted="$accounted $i"
  125. # found it, copy preamble before comparision
  126. ( sed -e "/^--- /,\$d" <$i; \
  127. cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
  128. ) >"$i.new"
  129. # did it change ? mark it as changed
  130. tfile="$(echo "$file" | $TRANSFORM)"
  131. if eval diff "$(echo "${DIFF_FLAGS}" \
  132. | sed "s#@@#${tfile}#g")" \
  133. "$i" "$i.new" 1>&2; then
  134. rm "$i.new"
  135. else
  136. echo "Patch $i for $file updated" >&2
  137. mv "$i" "$i.orig"
  138. mv "$i.new" "$i"
  139. edit="$edit $i"
  140. fi
  141. continue 2
  142. fi
  143. done
  144. # Build a sensible name for the new patch file
  145. patchname=patch-$(echo "$file" | sed -e 's#[/. ]#_#g')
  146. echo "No patch-* found for $file, creating $patchname" >&2
  147. ( echo '$Id: update-patches 24 2008-08-31 14:56:13Z wbx $'; \
  148. cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
  149. ) >$patchname
  150. edit="$edit $patchname"
  151. accounted="$accounted $patchname"
  152. done
  153. # Verify all patches accounted for
  154. cd $PATCHDIR
  155. for i in *; do
  156. [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
  157. grep '^\\ No newline at end of file' $i >/dev/null \
  158. && echo "*** Patch $i needs manual intervention" >&2
  159. [[ $accounted != *@($i)* ]] \
  160. && echo "*** Patch $i not accounted for" >&2
  161. done
  162. echo $edit
  163. [[ -n $SUBDIST ]] && mv ${WRKDIR1}/${SUBDIST}.orig ${WRKDIR1}.orig/${SUBDIST}
  164. exit 0