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


                • #9
                  I think the problem is here:
                  size=$(du -b "$REPOS"/db/transactions/"$TXN".txn | cut -f 1)

                  I mean, I donīt get the proper value. It seems that thereīs another way to get the size of a commit, and this way does not work. I see the values using your script, but the values are not the same than the "file size" of the files I want to upload to subversion. It doesnt get the REAL value.

                  Any idea to get the commit size?

                  Comment


                  • #10
                    Perhaps change the "du -b ..." to be "du -sb ..." - you need the size of the entire tree?!

                    Comment


                    • #11
                      I tryed both but no one worked. I would like to get the size of files bigger than 100MB, and block them to avoid multiple bigger files in the repository. Thats the goal of my script, but it does not work... it should be any way to get size of files

                      Comment


                      • #12
                        The "-s" option to the "du" command will calculate the size of the entire tree (rather than a list of 1 line per directory underneath - which will confuse your script).

                        Comment


                        • #13
                          Im using du -sb to get size, still not work. It does not get the proper size of commits. Always get 188K or similar values, but it doesnt get the real size of the commit. I donīt care if it works individual or entire tree, just would like to make it work...

                          Comment


                          • #14
                            Hereīs the result of your script... Iīm not getting the size of commit at end.

                            root@localhost hooks]# cat /tmp/mylog.19428
                            + echo 'files in the TXN are:'
                            files in the TXN are:
                            + find /repositorio/prueba/db/transactions/3-9.txn -type f -a -print0
                            + xargs -0 ls -ld
                            -rw-rw-r--. 1 user user 101 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/7053669113ea0207878c1dab0902b46984528a83
                            -rw-rw-r--. 1 user user 231 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/changes
                            -rw-rw-r--. 1 user user 4 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/next-ids
                            -rw-rw-r--. 1 user user 92 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node.0-1.0
                            -rw-rw-r--. 1 user user 184 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node.0-1.0.children
                            -rw-rw-r--. 1 user user 82 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node.0.0
                            -rw-rw-r--. 1 user user 68 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node.0.0.children
                            -rw-rw-r--. 1 user user 218 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node._1.0
                            -rw-rw-r--. 1 user user 53 Oct 7 09:31 /repositorio/prueba/db/transactions/3-9.txn/node._1.0.props
                            -rw-rw-r--. 1 user user 266 Oct 7 09:30 /repositorio/prueba/db/transactions/3-9.txn/props

                            Comment


                            • #15
                              I just would like to create a script which I can block people who is trying to upload files greather than 200MB... I canīt reach the way to get it

                              Comment

                              Working...
                              X