OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
6.8 KiB

  1. #!/bin/sh
  2. # --- T2-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # T2 SDE: misc/archive/compare.sh
  6. # Copyright (C) 2004 - 2006 The T2 SDE Project
  7. #
  8. # More information can be found in the files COPYING and README.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; version 2 of the License. A copy of the
  13. # GNU General Public License can be found in the file COPYING.
  14. # --- T2-COPYRIGHT-NOTE-END ---
  15. verbose=0
  16. quiet=0
  17. ignspace=0
  18. ignprio=0
  19. ignrepo=0
  20. dopatch=0
  21. source=
  22. targets=
  23. repositories=
  24. packages=
  25. function show_usage() {
  26. echo "usage: $0 [-q[q]] [-v] [-r] {action}"
  27. echo
  28. echo "action: [-p [-P] [-S]] <source> <target> [-repository <repo>|<packages>|]"
  29. echo " -3 <source> <target1> <target2> [-repository <repo>|<packages>|]"
  30. echo
  31. echo " -q: don't show packages with the same version"
  32. echo " -qq: don't show packages missing or with the same version"
  33. echo " -v: show extra info about the packages"
  34. echo " -r: don't show repository name"
  35. echo
  36. echo " -p: show patch to turn \$source into \$target"
  37. echo " -P: ignore [P]s of .desc files on patches"
  38. echo " -S: ignore spaces on patches"
  39. }
  40. # TODO: it would be great to port it to "-n <n>" instead of -3
  41. #
  42. while [ $# -gt 0 ]; do
  43. case "$1" in
  44. -v) verbose=1 ;;
  45. -r) ignrepo=1 ;;
  46. -q) quiet=1 ;;
  47. -qq) quiet=2 ;;
  48. -p) dopatch=1 ;;
  49. -P) ignprio=1 ;;
  50. -S) ignspace=1 ;;
  51. -3) source="$2"
  52. targets="$3 $4"
  53. shift 3 ;;
  54. -repository)
  55. shift
  56. repositories="$*"; set --
  57. ;;
  58. *) if [ "$targets" ]; then
  59. packages="$*"; set --
  60. elif [ "$source" ]; then
  61. targets="$1"
  62. else
  63. source="$1"
  64. fi
  65. esac
  66. shift
  67. done
  68. function remove_header() {
  69. # thanks you blindcoder :)
  70. #
  71. local here=0 count=1
  72. while read line ; do
  73. count=$(( ${count} + 1 ))
  74. [ "${line//COPYRIGHT-NOTE-END/}" != "${line}" ] && here=${count}
  75. done < $1
  76. tail -n +${here} $1
  77. }
  78. function show_nice_diff() {
  79. local diffopt=
  80. [ $ignspace -eq 1 ] && diffopt='-EBb'
  81. diff -u $diffopt "$1" "$2" | sed \
  82. -e 's,^--- .*,--- old/package/'"${3##*/package/}," \
  83. -e 's,^[\+][\+][\+] .*,+++ new/package/'"${3##*/package/},"
  84. }
  85. function diff_package() {
  86. local source="$1"
  87. local target="$2"
  88. local x= y=
  89. # files on source
  90. for x in $source/*; do
  91. if [ -d "$x/" ]; then
  92. diff_package $x $target/${x##*/}
  93. elif [[ "$x" = *.cache ]]; then
  94. continue
  95. elif [ -f "$x" ]; then
  96. remove_header $x > $$.source
  97. if [ -f $target/${x##*/} ]; then
  98. remove_header $target/${x##*/} > $$.target
  99. if [[ "$x" = *.desc ]]; then
  100. y=$( grep -e "^\[P\]" $x )
  101. [ "$y" -a $ignprio -eq 1 ] && sed -i -e "s,^\[P\] .*,$y," $$.target
  102. fi
  103. show_nice_diff $$.source $$.target $x
  104. rm $$.target
  105. else
  106. show_nice_diff $$.source /dev/null $x
  107. fi
  108. rm $$.source
  109. fi
  110. done
  111. # files only on target
  112. for x in $target/*; do
  113. if [ -d $x/ ]; then
  114. [ ! -d $source/${x#$target/} ] && diff_package $source/${x#$target/} $x
  115. elif [[ "$x" = *.cache ]]; then
  116. continue
  117. elif [ -f "$x" ]; then
  118. if [ ! -f "$source/${x#$target/}" ]; then
  119. remove_header $x > $$.target
  120. show_nice_diff /dev/null $$.target $source/${x#$target/}
  121. rm $$.target
  122. fi
  123. fi
  124. done
  125. }
  126. # grabdata confdir field
  127. function grabdata() {
  128. local confdir=$1
  129. local pkg=$2
  130. local field=$3
  131. local output=
  132. case "$field" in
  133. version) output=$( grabdata_desc $confdir/$pkg.desc $field ) ;;
  134. *) output=$( grabdata_cache $confdir/$pkg.cache $field ) ;;
  135. esac
  136. if [ -z "${output// /}" ]; then
  137. echo "UNKNOWN"
  138. else
  139. echo "$output"
  140. fi
  141. }
  142. function grabdata_desc() {
  143. local output=
  144. if [ -f "$1" ]; then
  145. case "$2" in
  146. version) output=$( grep -e "^\[V\]" $1 | cut -d' ' -f2- ) ;;
  147. esac
  148. fi
  149. echo "$output"
  150. }
  151. function grabdata_cache() {
  152. local output=
  153. if [ -f "$1" ]; then
  154. case "$2" in
  155. status) if grep -q -e "^\[.-ERROR\]" $1; then
  156. output=BROKEN
  157. else
  158. output=BUILT
  159. fi ;;
  160. size) output=$( grep -e "^\[SIZE\]" $1 | cut -d' ' -f2- ) ;;
  161. esac
  162. fi
  163. echo "$output"
  164. }
  165. function compare_package() {
  166. local source=$1
  167. local fullpkg=${1##*/package/}
  168. local pkg=${1##*/}
  169. local target= x= missing=0
  170. local info=
  171. local srcver= srcstatus= srcsize=
  172. local tgtver= tgtstatus= tgtsize=
  173. shift;
  174. [ $ignrepo -eq 1 ] && fullpkg=$pkg
  175. srcver=$( grabdata $source $pkg version )
  176. srcstatus=$( grabdata $source $pkg status )
  177. srcsize=$( grabdata $source $pkg size )
  178. tgtver=
  179. tgtstatus=
  180. tgtsize=
  181. for x; do
  182. target=$( echo $x/package/*/$pkg | head -n 1 )
  183. if [ -d "$target" ]; then
  184. tgtver="$tgtver:$( grabdata $target $pkg version )"
  185. tgtstatus="$tgtstatus:$( grabdata $target $pkg status )"
  186. tgtsize="$tgtsize:$( grabdata $target $pkg size )"
  187. else
  188. tgtver="$tgtver:MISSING"
  189. tgtstatus="$tgtstatus:MISSING"
  190. tgtsize="$tgtsize:MISSING"
  191. missing=1
  192. fi
  193. done
  194. tgtver="${tgtver#:}"
  195. tgtstatus="${tgtstatus#:}"
  196. tgtsize="${tgtsize#:}"
  197. # do we have different versions?
  198. #
  199. equalver=1 equalstatus=1
  200. IFS=':' ; for x in $tgtver; do
  201. [ "$x" != "$srcver" ] && equalver=0
  202. done
  203. IFS=':' ; for x in $tgtstatus; do
  204. [ "$x" != "$srcstatus" ] && equalstatus=0
  205. done
  206. # optimize version and status if they are the same
  207. #
  208. if [ $equalver -eq 1 ]; then
  209. version=$srcver
  210. else
  211. version="$srcver -> $tgtver"
  212. fi
  213. if [ $equalstatus -eq 1 ]; then
  214. status="$srcstatus"
  215. else
  216. status="$srcstatus -> $tgtstatus"
  217. fi
  218. # acording to verbosity level, what info should i show?
  219. #
  220. if [ $verbose -eq 0 ]; then
  221. info="($version)"
  222. else
  223. info="($version) ($status) ($srcsize -> $tgtsize)"
  224. fi
  225. if [ $missing -eq 1 ]; then
  226. if [ $quiet -le 1 ]; then
  227. # New - the package is not available at target tree
  228. echo "N $fullpkg $info" 1>&2
  229. [ $dopatch -eq 1 ] && diff_package $source $target
  230. fi
  231. elif [ $equalver -eq 1 ]; then
  232. if [ $quiet -eq 0 ]; then
  233. # Equal - the versions and cache status are the same on both trees
  234. echo "E $fullpkg $info" 1>&2
  235. [ $dopatch -eq 1 ] && diff_package $source $target
  236. fi
  237. else
  238. # Modified - the version on both trees is different
  239. echo "M $fullpkg $info" 1>&2
  240. [ $dopatch -eq 1 ] && diff_package $source $target
  241. fi
  242. }
  243. if [ -z "$source" -o -z "$targets" ]; then
  244. show_usage; exit 1
  245. fi
  246. allexist=1
  247. for x in $source $targets; do
  248. [ ! -d "$x/" ] && allexist=0; break
  249. done
  250. if [ $allexist -eq 1 ]; then
  251. echo -e "from: $source\nto..: $targets" 1>&2
  252. if [ "$repositories" ]; then
  253. for repo in $repositories; do
  254. for x in $source/package/$repo/*; do
  255. [ -d "$x" ] && ( compare_package $x $targets )
  256. done
  257. done
  258. elif [ "$packages" ]; then
  259. for pkg in $packages; do
  260. x=$( echo $source/package/*/$pkg | head -n 1 )
  261. [ -d "$x" ] && compare_package $x $targets
  262. done
  263. else
  264. for repo in $source/package/*; do
  265. for x in $repo/*; do
  266. [ -d "$x" ] && compare_package $x $targets
  267. done
  268. done
  269. fi
  270. else
  271. show_usage
  272. exit 1
  273. fi