Announcement

Collapse
No announcement yet.

SVN pre-commit hook

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SVN pre-commit hook

    Hello all,

    I'm trying to build a pre-commit script which I can control the commits of the repository. I want to block all commits bigger than 100 MB. I was trying it with that code, but it doesn't work.

    #!/bin/ksh
    REPOS="$1"
    TXN="$2"

    size=$(du -b -s "$REPOS"/db/transactions/"$TXN".txn | cut -f 1)
    if [[ $size -gt 104857600 ]]; then
    size=$(du -h -s "$REPOS"/db/transactions/"$TXN".txn | cut -f 1)
    echo "Your changeset requires a disk space ($size) exceeding our 100MB limit">&2
    exit 1
    fi


    I also tryed another ways to get size of commit, but no one worked(if thatīs the problem):
    #size=$(svnlook cat $REPOS -t $TXN | wc -c)
    #size=$(svnlook filesize -t $TXN $REPOS $f)
    Any idea?

    Thank you very much!
    Last edited by davidvazqueztds; 09-05-2019, 08:04 AM.

  • #2
    Strange to use Ksh in this day and age of Bash but the commands you've shown shouldn't be any different.

    One thing that should be done is to verify that the PATH is set properly. When writing hooks scripts it's always best to set a PATH that is simple and covers all of the necessary tools since you can't depend on a reasonable PATH being set when the hook is run. For instance, did the inherited PATH enable access to the "svnlook" command? How are you debugging this?

    Comment


    • #3
      Thanks for reply.

      Im using Ksh, but I can also use bash, sintax is similar and I also tryed with Bash, but it didnīt work. PATH i think is working. The $REPOS has it.
      As you see, Im not using svn look, I just tryed with different ways to calculate size but noone worked for me.
      Also I dont know how to debug it...

      What should I do?

      Using bash:
      #!/bin/bash

      REPOS="$1"
      TXN="$2"

      size=$(du -b "$REPOS"/db/transactions/"$TXN".txn | cut -f 1)

      if [ $size -gt "204800" ]; then #250MB en bytes
      size=$(du -b "$REPOS"/db/transactions/"$TXN".txn | cut -f 1)
      echo "Your changeset requires a disk space ($size) Kb exceeding our 250MB limit">&2
      exit 1
      else
      exit 0
      fi
      Last edited by davidvazqueztds; 09-11-2019, 01:08 PM.

      Comment


      • #4
        In general, the default PATH environment variable should be able to find "du", "svnlook", etc. since they're all normally in "/usr/bin" - but I don't assume that's where you've put it.

        Yes, $REPOS is a path so that should be ok.

        Have you debugged this at all? One debug technique would be to:[LIST=1][*]Always exit non-zero so that nothing works until you've squared things away.[*]Output STDOUT/STDERR for all into a log file in "/tmp". This can be done by:[/LIST][INDENT]#!/bin/bash --noprofile
        REPOS="$1"
        TXN="$2"

        for x in 1; do
        set -x
        size=$(...)
        echo files in the TXN are:
        find "${REPOS}/db/transactions/${TXN}.txn" -type f -a -print0 | xargs -0 ls -ld
        done > /tmp/mylog.$$ 2>&1[/INDENT]


        That output might help you analyze what's going on... For instance, are files compressed?
        Last edited by DougR; 09-13-2019, 12:10 PM.

        Comment


        • #5
          I will check your code.. thank you.

          Files could be or not be compressed. mostly of them are not. They are .doc,xls,csv,pictures and some .zip
          But the thing is to avoid people to upload .zip higher than 100MB

          I think the problem should be getting the size of the committed files, because script should work. Commands work separately. Just maybe therés another way to calculate the size of a commit.

          Comment


          • #6
            I tryed your code, hereīs the result:

            [root@localhost tmp]# cat mylog.85476
            ++ du -b /repositorio/tecdesoft/db/transactions/84507-1t9x.txn
            ++ cut -f 1
            + size=159876
            + echo files in the TXN are:
            files in the TXN are:
            + find /repositorio/tecdesoft/db/transactions/84507-1t9x.txn -type -f -a -print0
            + xargs -0 ls -ld
            find: Arguments to -type should contain only one letter
            dr-xr-xr-x. 19 root root 257 Jul 11 15:50 .

            Im trying to commit files about 1,25MB (exactly 1310720 bytes). When I try du -b file.pdf the result is exactly 1310720, totally different to 159876 which I see in log.
            So maybe thatīs not the way to obtain the commit size.
            Any idea?

            Comment


            • #7
              That ways does not work:
              #size=$(svnlook cat $REPOS -t $TXN | wc -c)
              #size=$(svnlook filesize -t $TXN $REPOS $f)
              fail...

              Comment


              • #8
                So, I had a typo above in the "find" command-line. I've just edited my post and fixed it up. The error from the "find" command was quite explicit so easy to diagnose. Just in case it isn't obvious here's the code again:

                for x in 1; do
                set -x
                echo "files in the TXN are:"
                find "${REPOS}/db/transactions/${TXN}.txn" -type f -a -print0 | xargs -0 ls -ld
                done > /tmp/mylog.$$ 2>&

                Update your hook and try again please.

                Comment

                Working...
                X