git-prefetch.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. repo="${1:-}"
  4. if [ -z "$repo" ]; then
  5. echo >&2 "usage: $0 repo"
  6. echo >&2 " ie: $0 hylang"
  7. exit 1
  8. fi
  9. # if we haven't set BASHBREW_LIBRARY explicitly (like Jenkins does, for example), don't trust the local library
  10. if [ -z "${BASHBREW_LIBRARY:-}" ]; then
  11. repo="https://github.com/docker-library/official-images/raw/master/library/$repo"
  12. fi
  13. : "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
  14. export BASHBREW_CACHE
  15. # see "generate-dockerfile-links-partial.tmpl" for where our arch-choosing complexity comes from
  16. templateHeader='{{- $archSpecific := getenv "ARCH_SPECIFIC_DOCS" -}}'
  17. templateArchVar='{{- $arch := $archSpecific | ternary arch ($e.HasArchitecture arch | ternary arch ($e.Architectures | first)) -}}'
  18. tags="$(bashbrew list --uniq "$repo" | cut -d: -f2-)"
  19. tags=( $tags )
  20. toFetch=()
  21. for tag in "${tags[@]}"; do
  22. if ! bashbrew cat --format '
  23. '"$templateHeader"'
  24. {{- $e := $.TagEntry -}}
  25. '"$templateArchVar"'
  26. {{- if $e.HasArchitecture $arch -}}
  27. {{- $froms := $.ArchDockerFroms $arch $e -}}
  28. {{- end -}}
  29. ' "$repo:$tag" &> /dev/null; then
  30. toFetch+=( "$repo:$tag" )
  31. fi
  32. done
  33. if [ "${#toFetch[@]}" -eq 0 ]; then
  34. exit 0
  35. fi
  36. bashbrewFetchTemplate='
  37. '"$templateHeader"'
  38. {{- range $i, $e := ($archSpecific | ternary (archFilter arch $.Entries) $.Entries) -}}
  39. '"$templateArchVar"'
  40. {{- join "/" "refs/tags" $arch $.RepoName ($e.Tags | first) -}}
  41. {{- ":" -}}
  42. {{- "\n" -}}
  43. {{- end -}}
  44. '
  45. bashbrew cat --format "$bashbrewFetchTemplate" "$repo" \
  46. | xargs --no-run-if-empty git -C "$BASHBREW_CACHE/git" fetch --quiet --no-tags https://github.com/docker-library/commit-warehouse.git