git-barepo

Tools for sharing git bare repositories
git clone git://git.meso-star.com/git-repo.git
Log | Files | Refs | README | LICENSE

test_barepo.sh (4119B)


      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 name="repo"
     32 tmpdir="$(mktemp -d "${TMPDIR:-/tmp}"/git_barepo_test_XXXXXX)"
     33 git="${tmpdir}/${name}.git"
     34 
     35 # Use the local git-publish
     36 export PATH="${PWD}:${PATH}"
     37 
     38 ########################################################################
     39 # Helper functions
     40 ########################################################################
     41 get_group() # file
     42 {
     43   # shellcheck disable=SC2012
     44   ls -dl "$1" | cut -d' ' -f4
     45 }
     46 
     47 get_group_perms() # file
     48 {
     49   # shellcheck disable=SC2012
     50   ls -dl "$1" | cut -d' ' -f1 | cut -c5-7
     51 }
     52 
     53 check_shared_repo() # repo, group
     54 {
     55   # Check git configuration
     56   cd "$1"
     57   _v="$(git config --local --get core.sharedRepository)"
     58   [ "${_v}" = "group" ]
     59   cd "${OLDPWD}"
     60 
     61   # Check file permissions
     62   find -L "$1" | while read -r i; do
     63     _group="$(get_group "${i}")"
     64     _perm="$(get_group_perms "${i}")"
     65 
     66     if [ -L "${i}" ]; then # Do not check symbolic links
     67       continue
     68     fi
     69 
     70     [ "${_group}" = "$2" ]
     71 
     72     # Check group's permissions
     73     if [ -d "${i}" ]; then
     74       [ "${_perm}" = "rws" ] # Directory
     75     else
     76       _p="$(printf '%s\n' "${_perm}" | cut -c1,2)"
     77       [ "${_p}" = "rw" ] # File
     78     fi
     79   done
     80 }
     81 
     82 check_private_repo()
     83 {
     84   # Check git configuration
     85   cd "$1"
     86   _v="$(git config --local --get core.sharedRepository)"
     87   [ -z "${_v}" ] || [ "${_v}" = "false" ]
     88   cd "${OLDPWD}"
     89 
     90   # There is no simple way to check the access permissions of a private
     91   # file, as we cannot make any assumptions about its default
     92   # permissions. It is still possible, but that would amount to
     93   # reimplementing what is already done in git-barepo. However, it seems
     94   # inappropriate to reimplement something that should be verify.
     95 }
     96 
     97 ########################################################################
     98 # The test
     99 ########################################################################
    100 git barepo "${git}" # Create bare repository
    101 
    102 # Check that a directory exists...
    103 [ -d "${git}" ]
    104 
    105 # ... and that it is a bare repository.
    106 cd "${git}"
    107 is_bare="$(git rev-parse --is-bare-repository)"
    108 [ "${is_bare}" = "true" ]
    109 cd "${OLDPWD}"
    110 
    111 # Check that it is not a dumb repository
    112 [ ! -e "${git}/hooks/post-update" ]
    113 
    114 # Create a link to the repository
    115 link="${tmpdir}/${name}_link.git"
    116 ln -s "${git}" "${link}"
    117 [ -L "${link}" ]
    118 
    119 git barepo -d "${link}"
    120 [ -e "${git}/hooks/post-update" ] # It should be now a dumb repository
    121 git barepo -D "${git}"
    122 [ ! -e "${link}/hooks/post-update" ] # It is no more a dumb repository
    123 
    124 # Find another group that is not the user's group, but to which the user
    125 # belongs. Fails if such group is not found, even though this situation
    126 # is perfectly valid from the system's perspective, because the test
    127 # cannot actually be performed.
    128 group="$(id -gn)"
    129 group="$(id -Gn | tr ' ' '\n' | grep -v "${group}" | head -n1)"
    130 [ -n "${group}" ]
    131 
    132 # Share the repository
    133 git barepo -s "${group}" "${git}"
    134 check_shared_repo "${git}" "${group}"
    135 
    136 # Privatise the repository
    137 git barepo -p "${git}"
    138 check_private_repo "${git}"
    139 
    140 # Update repository sharing via a symoblic link
    141 git barepo -s "${group}" "${link}"
    142 check_shared_repo "${git}" "${group}"
    143 check_shared_repo "${link}" "${group}"
    144 git barepo -p "${link}"
    145 check_private_repo "${git}"
    146 check_private_repo "${link}"