Useful CLI tools (bash) for Arch Linux administration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

  1. #!/usr/bin/bash
  2. #
  3. # git.sh - function for handling the download and "extraction" of Git sources
  4. #
  5. # Copyright (c) 2015-2018 Pacman Development Team <pacman-dev@archlinux.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. [[ -n "$LIBMAKEPKG_SOURCE_GIT_SH" ]] && return
  21. LIBMAKEPKG_SOURCE_GIT_SH=1
  22. LIBRARY=${LIBRARY:-'/usr/share/makepkg'}
  23. source "$LIBRARY/util/message.sh"
  24. source "$LIBRARY/util/pkgbuild.sh"
  25. download_git() {
  26. local netfile=$1
  27. local options=${@:2}
  28. local dir=$(get_filepath "$netfile")
  29. [[ -z "$dir" ]] && dir="$SRCDEST/$(get_filename "$netfile")"
  30. local repo=$(get_filename "$netfile")
  31. local url=$(get_url "$netfile")
  32. url=${url#git+}
  33. url=${url%%#*}
  34. url=${url%%\?*}
  35. if [[ ! -d "$dir" ]] || dir_is_empty "$dir" ; then
  36. if [[ ${options[@]} ]]; then
  37. msg2 "$(gettext "Cloning %s %s repo (params: %s)...")" "${repo}" "git" "${options[*]}"
  38. else
  39. msg2 "$(gettext "Cloning %s %s repo...")" "${repo}" "git"
  40. fi
  41. if ! git clone ${options[*]} --mirror "$url" "$dir"; then
  42. error "$(gettext "Failure while downloading %s %s repo")" "${repo}" "git"
  43. plain "$(gettext "Aborting...")"
  44. exit 1
  45. fi
  46. elif (( ! HOLDVER )); then
  47. cd_safe "$dir"
  48. # Make sure we are fetching the right repo
  49. if [[ "$url" != "$(git config --get remote.origin.url)" ]] ; then
  50. error "$(gettext "%s is not a clone of %s")" "$dir" "$url"
  51. plain "$(gettext "Aborting...")"
  52. exit 1
  53. fi
  54. msg2 "$(gettext "Updating %s %s repo...")" "${repo}" "git"
  55. if ! git fetch --all -p; then
  56. # only warn on failure to allow offline builds
  57. warning "$(gettext "Failure while updating %s %s repo")" "${repo}" "git"
  58. fi
  59. fi
  60. }
  61. extract_git() {
  62. local netfile=$1 tagname
  63. local fragment=$(get_uri_fragment "$netfile")
  64. local repo=$(get_filename "$netfile")
  65. local dir=$(get_filepath "$netfile")
  66. [[ -z "$dir" ]] && dir="$SRCDEST/$(get_filename "$netfile")"
  67. msg2 "$(gettext "Creating working copy of %s %s repo...")" "${repo}" "git"
  68. pushd "$srcdir" &>/dev/null
  69. local updating=0
  70. if [[ -d "${dir##*/}" ]]; then
  71. updating=1
  72. cd_safe "${dir##*/}"
  73. if ! git fetch; then
  74. error "$(gettext "Failure while updating working copy of %s %s repo")" "${repo}" "git"
  75. plain "$(gettext "Aborting...")"
  76. exit 1
  77. fi
  78. cd_safe "$srcdir"
  79. echo "${dir##*/}"
  80. elif ! git clone "$dir" "${dir##*/}"; then
  81. error "$(gettext "Failure while creating working copy of %s %s repo")" "${repo}" "git"
  82. plain "$(gettext "Aborting...")"
  83. exit 1
  84. fi
  85. cd_safe "${dir##*/}"
  86. local ref=origin/HEAD
  87. if [[ -n $fragment ]]; then
  88. case ${fragment%%=*} in
  89. commit|tag)
  90. ref=${fragment##*=}
  91. ;;
  92. branch)
  93. ref=origin/${fragment##*=}
  94. ;;
  95. *)
  96. error "$(gettext "Unrecognized reference: %s")" "${fragment}"
  97. plain "$(gettext "Aborting...")"
  98. exit 1
  99. esac
  100. fi
  101. if [[ ${fragment%%=*} = tag ]]; then
  102. tagname="$(git tag -l --format='%(tag)' "$ref")"
  103. if [[ -n $tagname && $tagname != $ref ]]; then
  104. error "$(gettext "Failure while checking out version %s, the git tag has been forged")" "$ref"
  105. plain "$(gettext "Aborting...")"
  106. exit 1
  107. fi
  108. fi
  109. if [[ $ref != "origin/HEAD" ]] || (( updating )) ; then
  110. if ! git checkout --force --no-track -B makepkg $ref; then
  111. error "$(gettext "Failure while creating working copy of %s %s repo")" "${repo}" "git"
  112. plain "$(gettext "Aborting...")"
  113. exit 1
  114. fi
  115. fi
  116. popd &>/dev/null
  117. }