SVN pre-commit hook for avoiding changes to tags subdirectories

前端 未结 11 712
陌清茗
陌清茗 2020-11-28 04:25

Is there anybody who has clear instructions on how to add a pre-commit hook that avoids changes to tags subdirectories?

I already searched the internet quite a bit.

11条回答
  •  鱼传尺愫
    2020-11-28 04:58

    Since the 1st answer didn't prevent add/suppr files, and prevented new tags creation, and many other where incomplete or buggy, I reworked it

    Here is my pre-commit hook : Goals are :

    • Disallow commits on tags (file addition/suppression/updates)
    • Don't prevent creation of tags

    --------- file "pre-commit" (put in repositories hooks folder) ---------

    #!/bin/sh
    
    REPOS="$1"
    TXN="$2"
    
    SVNLOOK=/usr/bin/svnlook
    
    #Logs
    #$SVNLOOK changed -t "$TXN" "$REPOS" > /tmp/changes
    #echo "$TXN" > /tmp/txn
    #echo "$REPOS" > /tmp/repos
    
    # Committing to tags is not allowed
    # Forbidden changes are Update/Add/Delete.  /W = non alphanum char  Redirect is necessary to get the error message, since regular output is lost.
    # BUT, we must allow tag creation / suppression
    
    $SVNLOOK changed -t "$TXN" "$REPOS" | /bin/grep "^A\W.*tags\/[0-9._-]*\/." && /bin/echo "Commit to tags are NOT allowed ! (Admin custom rule)" 1>&2 && exit 101
    $SVNLOOK changed -t "$TXN" "$REPOS" | /bin/grep "^U\W.*tags\/[0-9._-]*\/." && /bin/echo "Commit to tags are NOT allowed ! (Admin custom rule)" 1>&2 && exit 102
    $SVNLOOK changed -t "$TXN" "$REPOS" | /bin/grep "^D\W.*tags\/[0-9._-]*\/." && /bin/echo "Commit to tags are NOT allowed ! (Admin custom rule)" 1>&2 && exit 104
    
    # All checks passed, so allow the commit.
    exit 0;
    

    --------- end of file "pre-commit" ---------

    Also, I made 2 shell scripts to copy my hook in every project of my svn : One to set a repo read only :

    --------- script "setOneRepoTagsReadOnly.sh" ---------

    #!/bin/sh
    
    cd /var/svn/repos/svn
    zeFileName=$1/hooks/pre-commit
    /bin/cp ./CUSTOM_HOOKS/pre-commit $zeFileName
    chown www-data:www-data $zeFileName
    chmod +x $zeFileName
    

    --------- end of file "setOneRepoTagsReadOnly.sh" ---------

    And one calling it for every repo, to make all of my repos read only :

    --------- file "makeTagsReadOnly.sh" ---------

    #!/bin/shs/svn                                                                                                                                                                         
    #Lists all repos, and adds the pre-commit hook to protect tags on each of them
    find /var/svn/repos/svn/ -maxdepth 1 -mindepth 1 -type d -execdir '/var/svn/repos/svn/setOneRepoTagsReadOnly.sh' \{\} \;
    

    --------- end of file "makeTagsReadOnly.sh" ---------

    I execute thoses scripts directly from the svn "root" (/var/svn/repos/svn, in my case). Btw, a cron task could be set to automatically modify new repos by executing thoses scripts daily

    Hope it helps.

提交回复
热议问题