test_publish_multi.sh (6536B)
1 #!/bin/sh 2 3 # Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 set -ex 19 20 die() 21 { 22 rm -rf "${tmpdir}" # cleanup temporary files 23 return "${1:-1}" # return status code (default is 1) 24 } 25 26 # Configure signal processing 27 trap 'die $?' EXIT 28 trap 'die 1' TERM INT 29 30 # Working directory 31 tmpdir="$(mktemp -d "${TMPDIR:-/tmp}"/git_publish_test_XXXXXX)" 32 gitdir="${tmpdir}/git" 33 srvdir="${tmpdir}/srv" 34 35 mkdir -p "${gitdir}" "${srvdir}/www" "${srvdir}/git" 36 37 # Use the local git-publish 38 export PATH="${PWD}:${PATH}" 39 40 ######################################################################## 41 # Helper functions 42 ######################################################################## 43 create_git_bare_repo() # name 44 { 45 _repo="${tmpdir}/$1" 46 _git="${gitdir}/$1.git" 47 48 mkdir -p "${_git}" 49 git init --bare --initial-branch=master "${_git}" 1>&2 50 51 # Create the repository content 52 mkdir -p "${_repo}" 53 printf 'Repository %s\n' "$1" > "${_repo}/README.txt" 54 cksum "${_repo}/README.txt" | cut -d' ' -f1 > "${_repo}/cookie.txt" 55 56 # Push the content to the git bare repository 57 cd "${_repo}" 58 { 59 git init --initial-branch=master 60 git config --local user.name "Mme Lambda" 61 git config --local user.email "mme@lambda.fr" 62 git add README.txt cookie.txt 63 git commit -m "Test content for publishing multiple repositories" 64 git remote add origin "file://${_git}" 65 git push origin master 66 } 1>&2 67 cd "${OLDPWD}" 68 69 # Output the value of the repository's cookie 70 cat "${_repo}/cookie.txt" 71 72 rm -rf "${_repo}" # Clean up 73 } 74 75 check_git_content() # repo-url, cookie 76 { 77 _name=$(basename "$1" ".git") 78 git clone "$1" "${tmpdir}/${_name}" 79 80 [ -f "${tmpdir}/${_name}/README.txt" ] 81 [ -f "${tmpdir}/${_name}/cookie.txt" ] 82 83 _cookie="$(cat "${tmpdir}/${_name}/cookie.txt")" 84 [ "${_cookie}" = "$2" ] 85 86 rm -rf "${tmpdir:?}/${_name}" 87 } 88 89 check_www_content() # repo 90 { 91 [ -f "${srvdir}/www/index.html" ] 92 [ -d "${srvdir}/www/$1" ] 93 [ -f "${srvdir}/www/$1/index.html" ] 94 } 95 96 ######################################################################## 97 # Setup repository 98 ######################################################################## 99 cookie1="$(create_git_bare_repo "repo01")" 100 cookie2="$(create_git_bare_repo "repo02")" 101 cookie3="$(create_git_bare_repo "repo03")" 102 103 ######################################################################## 104 # The tests 105 ######################################################################## 106 git publish \ 107 -g "${srvdir}/git" \ 108 -w "${srvdir}/www" \ 109 -u "http://mme.lambda.fr" \ 110 "${gitdir}"/*.git 111 112 # The published repositories are symlinks 113 [ -L "${srvdir}/git/repo01.git" ] 114 [ -L "${srvdir}/git/repo02.git" ] 115 [ -L "${srvdir}/git/repo03.git" ] 116 117 check_git_content "file://${srvdir}/git/repo01.git" "${cookie1}" 118 check_git_content "file://${srvdir}/git/repo02.git" "${cookie2}" 119 check_git_content "file://${srvdir}/git/repo03.git" "${cookie3}" 120 check_www_content "repo01" 121 check_www_content "repo02" 122 check_www_content "repo03" 123 124 # From now on, use environment variables 125 export GIT_PUBLISH_DIR_GIT="${srvdir}/git" 126 export GIT_PUBLISH_DIR_WWW="${srvdir}/www" 127 export GIT_PUBLISH_BASE_URL="http://mme.lambda.fr" 128 129 # Unpublish one repository 130 git publish -d "${gitdir}"/repo01.git 131 [ ! -e "${srvdir}/git/repo01.git" ] 132 [ ! -e "${srvdir}/www/repo01" ] 133 [ -d "${gitdir}/repo01.git" ] && [ ! -L "${gitdir}/repo01.git" ] 134 [ -d "${gitdir}/repo01.git" ] && [ ! -L "${gitdir}/repo01.git" ] 135 [ -f "${srvdir}/www/index.html" ] # There is still published repositories 136 137 # Unpublished the remaining repositories 138 git publish -d "${gitdir}"/repo0[2,3].git 139 [ ! -e "${srvdir}/git/repo02.git" ] 140 [ ! -e "${srvdir}/git/repo03.git" ] 141 [ -d "${gitdir}/repo02.git" ] && [ ! -L "${gitdir}/repo02.git" ] 142 [ -d "${gitdir}/repo03.git" ] && [ ! -L "${gitdir}/repo03.git" ] 143 [ ! -e "${srvdir}/www/index.html" ] # No more index since no more repo 144 145 # Publish the repositories by moving the source repositories 146 git publish -m "${gitdir}/"*.git 147 148 # The published repositories are symlinks 149 [ -d "${srvdir}/git/repo01.git" ] && [ ! -L "${srvdir}/git/repo01.git" ] 150 [ -d "${srvdir}/git/repo02.git" ] && [ ! -L "${srvdir}/git/repo02.git" ] 151 [ -d "${srvdir}/git/repo03.git" ] && [ ! -L "${srvdir}/git/repo03.git" ] 152 153 # The original repositories are symlinks 154 [ -L "${gitdir}/repo01.git" ] 155 [ -L "${gitdir}/repo02.git" ] 156 [ -L "${gitdir}/repo03.git" ] 157 158 check_git_content "file://${srvdir}/git/repo01.git" "${cookie1}" 159 check_git_content "file://${srvdir}/git/repo02.git" "${cookie2}" 160 check_git_content "file://${srvdir}/git/repo03.git" "${cookie3}" 161 check_git_content "file://${gitdir}/repo01.git" "${cookie1}" 162 check_git_content "file://${gitdir}/repo02.git" "${cookie2}" 163 check_git_content "file://${gitdir}/repo03.git" "${cookie3}" 164 check_www_content "repo01" 165 check_www_content "repo02" 166 check_www_content "repo03" 167 168 # Unpublish all repositories 169 git publish -d "${gitdir}"/*.git 170 [ ! -e "${srvdir}/git/repo01.git" ] 171 [ ! -e "${srvdir}/git/repo02.git" ] 172 [ ! -e "${srvdir}/git/repo03.git" ] 173 [ ! -e "${srvdir}/www/index.html" ] # No more index since no more repo 174 175 # Check the original repositories are directories and remain valid after 176 # their unpublication 177 [ -d "${gitdir}/repo01.git" ] && [ ! -L "${gitdir}/repo01.git" ] 178 [ -d "${gitdir}/repo02.git" ] && [ ! -L "${gitdir}/repo02.git" ] 179 [ -d "${gitdir}/repo03.git" ] && [ ! -L "${gitdir}/repo03.git" ] 180 check_git_content "file://${gitdir}/repo01.git" "${cookie1}" 181 check_git_content "file://${gitdir}/repo02.git" "${cookie2}" 182 check_git_content "file://${gitdir}/repo03.git" "${cookie3}" 183 184 # Prevent the publication of repo02 185 touch "${gitdir}/repo02.git/publication_ban" 186 git publish "${gitdir}"/*.git 187 [ -d "${srvdir}/git/repo01.git" ] 188 [ ! -e "${srvdir}/git/repo02.git" ] 189 [ -d "${srvdir}/git/repo03.git" ] 190 191 # Unpublish repo01 and repo03 192 git publish -d "${gitdir}"/repo0[1,3].git 193 [ ! -e "${srvdir}/git/repo01.git" ] 194 [ ! -e "${srvdir}/git/repo02.git" ] 195 [ ! -e "${srvdir}/git/repo03.git" ]