children.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. set -eu -o pipefail
  3. # "bashbrew children" can't work with "--uniq" and "bashbrew build" will build the entire "tag group", so we need to filter the output to just uniq values
  4. : "${BASHBREW:=bashbrew}"
  5. IFS=$'\n'
  6. set -- $("$BASHBREW" list --uniq --repos --build-order --apply-constraints "$@")
  7. # \o/ https://github.com/docker-library/official-images/commit/9e57342714f99074ec205eea668c8b73aada36ec
  8. comm -13 \
  9. <("$BASHBREW" list "$@" | sort -u) \
  10. <("$BASHBREW" children --apply-constraints "$@" | sort -u) \
  11. | xargs --no-run-if-empty "$BASHBREW" list --build-order --apply-constraints --uniq
  12. exit 0
  13. children=( $("$BASHBREW" children --apply-constraints "$@") )
  14. [ "${#children[@]}" -gt 0 ] || exit 0
  15. # just repo names so we can get the right build-order for all of their tags
  16. childrenRepos=( $(echo "${children[*]}" | cut -d: -f1 | sort -u) )
  17. # all uniq tags from all repos which have relevant children, in proper build order
  18. childrenReposUniq=( $("$BASHBREW" list --uniq --build-order --apply-constraints "${childrenRepos[@]}") )
  19. # the canonical ("uniq") versions of the children we're after (same as the values now in "childrenReposUniq")
  20. # use "comm" to suppress "$@" from the list of children we care about
  21. childrenUniq=(
  22. $(
  23. comm -13 \
  24. <(
  25. "$BASHBREW" list --uniq "$@" \
  26. | sort -u
  27. ) \
  28. <(
  29. "$BASHBREW" list --uniq "${children[@]}" \
  30. | sort -u
  31. )
  32. )
  33. )
  34. [ "${#childrenUniq[@]}" -gt 0 ] || exit 0
  35. unset IFS
  36. # create a lookup table of whether we should return a particular tag
  37. declare -A wantChild=()
  38. for child in "${childrenUniq[@]}"; do
  39. wantChild["$child"]=1
  40. done
  41. # loop over the canonical build order and print out tags we want :)
  42. for child in "${childrenReposUniq[@]}"; do
  43. [ "${wantChild[$child]:-}" ] || continue
  44. echo "$child"
  45. done
  46. # note that we can't use "comm" by itself here because "childrenUniq" and "childrenReposUniq" are not in the same order, which "comm" requires