update-patches 4.9 KB

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