git-prefetch.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. {{- $from := $.ArchDockerFrom $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 := $.Entries -}}
  39. '"$templateArchVar"'
  40. {{- if $e.HasArchitecture $arch -}}
  41. {{- join "/" "refs/tags" $arch $.RepoName ($e.Tags | first) -}}
  42. {{- ":" -}}
  43. {{- "\n" -}}
  44. {{- end -}}
  45. {{- end -}}
  46. '
  47. bashbrew cat --format "$bashbrewFetchTemplate" "$repo" \
  48. | xargs --no-run-if-empty git -C "$BASHBREW_CACHE/git" fetch --quiet --no-tags https://github.com/docker-library/commit-warehouse.git