diff-pr.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. shopt -s dotglob
  4. # make sure we can GTFO
  5. trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT
  6. # if bashbrew is missing, bail early with a sane error
  7. bashbrew --version > /dev/null
  8. usage() {
  9. cat <<-EOUSAGE
  10. usage: $0 [PR number] [repo[:tag]]
  11. ie: $0 1024
  12. $0 9001 debian php django
  13. EOUSAGE
  14. }
  15. # TODO flags parsing
  16. allFiles=
  17. listTarballContents=1
  18. findCopies='20%'
  19. uninterestingTarballContent=(
  20. # "config_diff_2017_01_07.log"
  21. 'var/log/YaST2/'
  22. # "ks-script-mqmz_080.log"
  23. # "ks-script-ycfq606i.log"
  24. 'var/log/anaconda/'
  25. # "2016-12-20/"
  26. 'var/lib/yum/history/'
  27. 'var/lib/dnf/history/'
  28. # "a/f8c032d2be757e1a70f00336b55c434219fee230-acl-2.2.51-12.el7-x86_64/var_uuid"
  29. 'var/lib/yum/yumdb/'
  30. 'var/lib/dnf/yumdb/'
  31. # "b42ff584.0"
  32. 'etc/pki/tls/rootcerts/'
  33. # "09/401f736622f2c9258d14388ebd47900bbab126"
  34. 'usr/lib/.build-id/'
  35. )
  36. # prints "$2$1$3$1...$N"
  37. join() {
  38. local sep="$1"; shift
  39. local out; printf -v out "${sep//%/%%}%s" "$@"
  40. echo "${out#$sep}"
  41. }
  42. uninterestingTarballGrep="^([.]?/)?($(join '|' "${uninterestingTarballContent[@]}"))"
  43. if [ "$#" -eq 0 ]; then
  44. usage >&2
  45. exit 1
  46. fi
  47. pull="$1" # PR number
  48. shift
  49. diffDir="$(readlink -f "$BASH_SOURCE")"
  50. diffDir="$(dirname "$diffDir")"
  51. tempDir="$(mktemp -d)"
  52. trap "rm -rf '$tempDir'" EXIT
  53. cd "$tempDir"
  54. git clone --quiet \
  55. https://github.com/docker-library/official-images.git \
  56. oi
  57. if [ "$pull" != '0' ]; then
  58. git -C oi fetch --quiet \
  59. origin "pull/$pull/merge":refs/heads/pull
  60. else
  61. git -C oi fetch --quiet --update-shallow \
  62. "$diffDir" HEAD:refs/heads/pull
  63. fi
  64. if [ "$#" -eq 0 ]; then
  65. images="$(git -C oi/library diff --name-only HEAD...pull -- .)"
  66. [ -n "$images" ] || exit 0
  67. images="$(xargs -n1 basename <<<"$images")"
  68. set -- $images
  69. fi
  70. export BASHBREW_CACHE="${BASHBREW_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/bashbrew}"
  71. export BASHBREW_LIBRARY="$PWD/oi/library"
  72. : "${BASHBREW_ARCH:=amd64}" # TODO something smarter with arches
  73. export BASHBREW_ARCH
  74. # "bashbrew cat" template for duplicating something like "bashbrew list --uniq" but with architectures too
  75. archesListTemplate='
  76. {{- range $e := $.Entries -}}
  77. {{- range .Architectures -}}
  78. {{- $.RepoName -}}:{{- $e.Tags | last -}}
  79. {{- " @ " -}}
  80. {{- . -}}
  81. {{- "\n" -}}
  82. {{- end -}}
  83. {{- end -}}
  84. '
  85. # ... and SharedTags
  86. sharedTagsListTemplate='
  87. {{- range $group := .Manifest.GetSharedTagGroups -}}
  88. {{- range $tag := $group.SharedTags -}}
  89. {{- join ":" $.RepoName $tag -}}
  90. {{- " -- " -}}
  91. {{- range $i, $e := $group.Entries -}}
  92. {{- if gt $i 0 -}}
  93. {{- ", " -}}
  94. {{- end -}}
  95. {{- join ":" $.RepoName ($e.Tags | last) -}}
  96. {{- end -}}
  97. {{- "\n" -}}
  98. {{- end -}}
  99. {{- end -}}
  100. '
  101. # TODO something less hacky than "git archive" hackery, like a "bashbrew archive" or "bashbrew context" or something
  102. template='
  103. tempDir="$(mktemp -d)"
  104. {{- "\n" -}}
  105. {{- range $.Entries -}}
  106. {{- $arch := .HasArchitecture arch | ternary arch (.Architectures | first) -}}
  107. {{- $froms := $.ArchDockerFroms $arch . -}}
  108. {{- $outDir := join "_" $.RepoName (.Tags | last) -}}
  109. git -C "$BASHBREW_CACHE/git" archive --format=tar
  110. {{- " " -}}
  111. {{- "--prefix=" -}}
  112. {{- $outDir -}}
  113. {{- "/" -}}
  114. {{- " " -}}
  115. {{- .ArchGitCommit $arch -}}
  116. {{- ":" -}}
  117. {{- $dir := .ArchDirectory $arch -}}
  118. {{- (eq $dir ".") | ternary "" $dir -}}
  119. {{- "\n" -}}
  120. mkdir -p "$tempDir/{{- $outDir -}}" && echo "{{- .ArchFile $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-dockerfile-name"
  121. {{- "\n" -}}
  122. {{- end -}}
  123. tar -cC "$tempDir" . && rm -rf "$tempDir"
  124. '
  125. copy-tar() {
  126. local src="$1"; shift
  127. local dst="$1"; shift
  128. if [ "$allFiles" ]; then
  129. mkdir -p "$dst"
  130. cp -al "$src"/*/ "$dst/"
  131. return
  132. fi
  133. local d dockerfiles=()
  134. for d in "$src"/*/.bashbrew-dockerfile-name; do
  135. local bf; bf="$(< "$d")"
  136. local dDir; dDir="$(dirname "$d")"
  137. dockerfiles+=( "$dDir/$bf" )
  138. if [ "$bf" = 'Dockerfile' ]; then
  139. # if "Dockerfile.builder" exists, let's check that too (busybox, hello-world)
  140. if [ -f "$dDir/$bf.builder" ]; then
  141. dockerfiles+=( "$dDir/$bf.builder" )
  142. fi
  143. fi
  144. rm "$d" # remove the ".bashbrew-dockerfile-name" file we created
  145. done
  146. for d in "${dockerfiles[@]}"; do
  147. local dDir; dDir="$(dirname "$d")"
  148. local dDirName; dDirName="$(basename "$dDir")"
  149. local IFS=$'\n'
  150. local dBase; dBase="$(basename "$d")"
  151. local copyAddContext; copyAddContext="$(awk '
  152. toupper($1) == "COPY" || toupper($1) == "ADD" {
  153. for (i = 2; i < NF; i++) {
  154. if ($i ~ /^--from=/) {
  155. next
  156. }
  157. if ($i !~ /^--chown=/) {
  158. print $i
  159. }
  160. }
  161. }
  162. ' "$d")"
  163. local files=(
  164. "$dBase"
  165. $copyAddContext
  166. # some extra files which are likely interesting if they exist, but no big loss if they do not
  167. ' .dockerignore' # will be used automatically by "docker build"
  168. ' *.manifest' # debian/ubuntu "package versions" list
  169. ' *.ks' # fedora "kickstart" (rootfs build script)
  170. ' build*.txt' # ubuntu "build-info.txt", debian "build-command.txt"
  171. # usefulness yet to be proven:
  172. #' *.log'
  173. #' {MD5,SHA1,SHA256}SUMS'
  174. #' *.{md5,sha1,sha256}'
  175. # (the space prefix is removed below and is used to ignore non-matching globs so that bad "Dockerfile" entries appropriately lead to failure)
  176. )
  177. unset IFS
  178. mkdir -p "$dst/$dDirName"
  179. local f origF failureMatters
  180. for origF in "${files[@]}"; do
  181. f="${origF# }" # trim off leading space (indicates we don't care about failure)
  182. [ "$f" = "$origF" ] && failureMatters=1 || failureMatters=
  183. local globbed
  184. # "find: warning: -path ./xxx/ will not match anything because it ends with /."
  185. local findGlobbedPath="${f%/}"
  186. findGlobbedPath="${findGlobbedPath#./}"
  187. local globbedStr; globbedStr="$(cd "$dDir" && find -path "./$findGlobbedPath")"
  188. local -a globbed=( $globbedStr )
  189. if [ "${#globbed[@]}" -eq 0 ]; then
  190. globbed=( "$f" )
  191. fi
  192. local g
  193. for g in "${globbed[@]}"; do
  194. if [ -z "$failureMatters" ] && [ ! -e "$dDir/$g" ]; then
  195. continue
  196. fi
  197. local gDir; gDir="$(dirname "$dst/$dDirName/$g")"
  198. mkdir -p "$gDir"
  199. cp -alT "$dDir/$g" "$dst/$dDirName/$g"
  200. if [ "$listTarballContents" ]; then
  201. case "$g" in
  202. *.tar.*|*.tgz)
  203. if [ -s "$dst/$dDirName/$g" ]; then
  204. tar -tf "$dst/$dDirName/$g" \
  205. | grep -vE "$uninterestingTarballGrep" \
  206. | sed -e 's!^[.]/!!' \
  207. | sort \
  208. > "$dst/$dDirName/$g 'tar -t'"
  209. fi
  210. ;;
  211. esac
  212. fi
  213. done
  214. done
  215. done
  216. }
  217. mkdir temp
  218. git -C temp init --quiet
  219. git -C temp config user.name 'Bogus'
  220. git -C temp config user.email 'bogus@bogus'
  221. bashbrew list "$@" 2>>temp/_bashbrew.err | sort -uV > temp/_bashbrew-list || :
  222. bashbrew cat --format "$archesListTemplate" "$@" 2>>temp/_bashbrew.err | sort -V > temp/_bashbrew-arches || :
  223. bashbrew cat --format "$sharedTagsListTemplate" "$@" 2>>temp/_bashbrew.err | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
  224. for image; do
  225. if script="$(bashbrew cat -f "$template" "$image")"; then
  226. mkdir tar
  227. ( eval "$script" | tar -xiC tar )
  228. copy-tar tar temp
  229. rm -rf tar
  230. fi
  231. done
  232. git -C temp add . || :
  233. git -C temp commit --quiet --allow-empty -m 'initial' || :
  234. git -C oi checkout --quiet pull
  235. git -C temp rm --quiet -rf . || :
  236. bashbrew list "$@" 2>>temp/_bashbrew.err | sort -uV > temp/_bashbrew-list || :
  237. bashbrew cat --format "$archesListTemplate" "$@" 2>>temp/_bashbrew.err | sort -V > temp/_bashbrew-arches || :
  238. bashbrew cat --format "$sharedTagsListTemplate" "$@" 2>>temp/_bashbrew.err | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
  239. script="$(bashbrew cat -f "$template" "$@")"
  240. mkdir tar
  241. ( eval "$script" | tar -xiC tar )
  242. copy-tar tar temp
  243. rm -rf tar
  244. git -C temp add .
  245. git -C temp diff \
  246. --find-copies-harder \
  247. --find-copies="$findCopies" \
  248. --find-renames="$findCopies" \
  249. --ignore-blank-lines \
  250. --ignore-space-at-eol \
  251. --ignore-space-change \
  252. --irreversible-delete \
  253. --minimal \
  254. --staged