1
0

update-patches 4.8 KB

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