checkout.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env sh
  2. # Copyright 2018-2020 Docker Inc.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. checkout() (
  13. set -ex
  14. SRC="$1"
  15. REF="$2"
  16. REF_FETCH="$REF"
  17. # if ref is branch or tag, retrieve its canonical form
  18. REF=$(git -C "$SRC" ls-remote --refs --heads --tags origin "$REF" | awk '{print $2}')
  19. if [ -n "$REF" ]; then
  20. # if branch or tag then create it locally too
  21. REF_FETCH="$REF:$REF"
  22. else
  23. REF="FETCH_HEAD"
  24. fi
  25. git -C "$SRC" fetch --update-head-ok --depth 1 origin "$REF_FETCH"
  26. git -C "$SRC" checkout -q "$REF"
  27. )
  28. # Only execute checkout function above if this file is executed, not sourced from another script
  29. prog=checkout.sh # needs to be in sync with this file's name
  30. if [ "$(basename -- $0)" = "$prog" ]; then
  31. checkout $*
  32. fi