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.

142 lines
4.0 KiB

2 years ago
  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. if [[ ${options[@]} ]]; then
  55. msg2 "$(gettext "Updating %s %s repo (params: %s)...")" "${repo}" "git" "${options[*]}"
  56. else
  57. msg2 "$(gettext "Updating %s %s repo...")" "${repo}" "git"
  58. fi
  59. if ! git fetch ${options[*]} -p; then
  60. # only warn on failure to allow offline builds
  61. warning "$(gettext "Failure while updating %s %s repo")" "${repo}" "git"
  62. fi
  63. fi
  64. }
  65. extract_git() {
  66. local netfile=$1 tagname
  67. local fragment=$(get_uri_fragment "$netfile")
  68. local repo=$(get_filename "$netfile")
  69. local dir=$(get_filepath "$netfile")
  70. [[ -z "$dir" ]] && dir="$SRCDEST/$(get_filename "$netfile")"
  71. msg2 "$(gettext "Creating working copy of %s %s repo...")" "${repo}" "git"
  72. pushd "$srcdir" &>/dev/null
  73. local updating=0
  74. if [[ -d "${dir##*/}" ]]; then
  75. updating=1
  76. cd_safe "${dir##*/}"
  77. if ! git fetch; then
  78. error "$(gettext "Failure while updating working copy of %s %s repo")" "${repo}" "git"
  79. plain "$(gettext "Aborting...")"
  80. exit 1
  81. fi
  82. cd_safe "$srcdir"
  83. echo "${dir##*/}"
  84. elif ! git clone "$dir" "${dir##*/}"; then
  85. error "$(gettext "Failure while creating working copy of %s %s repo")" "${repo}" "git"
  86. plain "$(gettext "Aborting...")"
  87. exit 1
  88. fi
  89. cd_safe "${dir##*/}"
  90. local ref=origin/HEAD
  91. if [[ -n $fragment ]]; then
  92. case ${fragment%%=*} in
  93. commit|tag)
  94. ref=${fragment##*=}
  95. ;;
  96. branch)
  97. ref=origin/${fragment##*=}
  98. ;;
  99. *)
  100. error "$(gettext "Unrecognized reference: %s")" "${fragment}"
  101. plain "$(gettext "Aborting...")"
  102. exit 1
  103. esac
  104. fi
  105. if [[ ${fragment%%=*} = tag ]]; then
  106. tagname="$(git tag -l --format='%(tag)' "$ref")"
  107. if [[ -n $tagname && $tagname != $ref ]]; then
  108. error "$(gettext "Failure while checking out version %s, the git tag has been forged")" "$ref"
  109. plain "$(gettext "Aborting...")"
  110. exit 1
  111. fi
  112. fi
  113. if [[ $ref != "origin/HEAD" ]] || (( updating )) ; then
  114. if ! git checkout --force --no-track -B makepkg $ref; then
  115. error "$(gettext "Failure while creating working copy of %s %s repo")" "${repo}" "git"
  116. plain "$(gettext "Aborting...")"
  117. exit 1
  118. fi
  119. fi
  120. popd &>/dev/null
  121. }