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.

checkoutSite.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. # Author: Martin Stockhammer <martin_s@apache.org>
  21. # Date: 2018-11-03
  22. #
  23. # This script runs a sparse git clone of a remote repository and
  24. # initializes the git configuration.
  25. #
  26. # It is mainly used for site content creation, because the main archiva-web-content repository
  27. # is rather large and we don't want to checkout the complete data.
  28. #
  29. SITE_DIR=".site-content"
  30. GIT_REMOTE=""
  31. GIT_USER=$(git config user.name)
  32. GIT_EMAIL=$(git config user.email)
  33. GIT_PATTERN_FILE="git-sparse-checkout-pattern"
  34. GIT_PATTERN_DEST=".git/info/sparse-checkout"
  35. MY_PWD=$(pwd)
  36. CLONE=1
  37. FORCE=1
  38. MODULE_DIR="${MY_PWD}"
  39. PATTERN=""
  40. BRANCH="master"
  41. while [ ! -z "$1" ]; do
  42. case "$1" in
  43. -f)
  44. FORCE=0
  45. shift
  46. ;;
  47. -d)
  48. shift
  49. SITE_DIR="$1"
  50. shift
  51. ;;
  52. -p)
  53. shift
  54. if [ -z "${PATTERN}" ]; then
  55. PATTERN="${1}"
  56. else
  57. PATTERN="${PATTERN}\n${1}"
  58. fi
  59. shift
  60. ;;
  61. -m)
  62. shift
  63. MODULE_DIR="$1"
  64. shift
  65. ;;
  66. -b)
  67. shift
  68. BRANCH="$1"
  69. shift
  70. ;;
  71. *)
  72. GIT_REMOTE="$1"
  73. shift
  74. ;;
  75. esac
  76. done
  77. print_usage() {
  78. echo "checkoutRepo [-m MODULE_DIR] [-d SITE_DIR] [-f] GIT_URL"
  79. echo " -m: The module directory where the pattern file can be found and the site dir will be created."
  80. echo " -d SITE_DIR: Use the given directory for checkout"
  81. echo " -f: Force clone, even if directory exists"
  82. }
  83. if [ ! -f "${MODULE_DIR}/pom.xml" ]; then
  84. echo "Looks like the working directory is not a valid dir. No pom.xml found."
  85. exit 1
  86. fi
  87. cd "${MODULE_DIR}" || { echo "Could not change to module directory ${MODULE_DIR}"; exit 1; }
  88. if [ -z "$GIT_REMOTE" ]; then
  89. print_usage
  90. exit 1
  91. fi
  92. if [ "${GIT_REMOTE:0:8}" == "scm:git:" ]; then
  93. GIT_REMOTE="${GIT_REMOTE:8}"
  94. fi
  95. if [ -d "${SITE_DIR}" ]; then
  96. if [ ! -d "${SITE_DIR}/.git" ]; then
  97. echo "Directory ${SITE_DIR} exist already, but is not a git clone. Aborting."
  98. exit 1
  99. elif [ "$FORCE" -eq 0 ]; then
  100. CLONE=0
  101. fi
  102. else
  103. CLONE=0
  104. fi
  105. if [ $CLONE -eq 0 ]; then
  106. git clone "${GIT_REMOTE}" "${SITE_DIR}" --no-checkout
  107. if [ $? -ne 0 ]; then
  108. echo "Git clone failed"
  109. exit 1
  110. fi
  111. fi
  112. cd "${SITE_DIR}" || { echo "Could not change to site dir ${SITE_DIR}"; exit 1; }
  113. git checkout "${BRANCH}"
  114. git config core.sparsecheckout true
  115. git config user.name "${GIT_USER}"
  116. git config user.email "${GIT_EMAIL}"
  117. if [ ! -z "${PATTERN}" ]; then
  118. echo -e "${PATTERN}" >"${GIT_PATTERN_DEST}"
  119. elif [ -f "../${GIT_PATTERN_FILE}" ]; then
  120. cp "../${GIT_PATTERN_FILE}" "${GIT_PATTERN_DEST}"
  121. fi
  122. git checkout --
  123. cd "${MY_PWD}"