On 30 May 2019, at 3:12, Andreas Stieger wrote:

I'm looking to update my precommit hook to reject commits that are larger than x megabytes.

The proposed transaction is passed as a parameter to the hook.
Hand that to svnlook (svnlook -t $TXN changed), and iterate over all entries.
Filter out deletions and directory changes.
For each file get size (svnlook -t $TXN filesize) and sum it up.
Evaluate and return hook as required.

Very late followup here :), but my coworker finally did this and here it is in case it can help anyone else:

```
# Detect and reject very large commits
# Total and maximum commit size in bytes
MAX_COMMIT_SIZE=500000000

# For each item in the commit transaction, filter out deletions, get the size and sum it up: # 'svnlook changed' output starts with a status code (?, A, D, U, _U, UU) followed by the paths of files that were added/removed/changed # Out of this output only two filepaths should be removed, deletions (status code 'D') and folders (filepath ending with '/') SVN_STAT_OUTPUT=`${SVNLOOK} changed -t "${TRANSACTION}" "${REPOS_PATH}" | egrep $'^[^D]' `

echo $SVN_STAT_OUTPUT | cut -c 2- | while read item
do
# Get size of each item, in theory all items are exclusively files or folder # Note the '2> /dev/null' this is used to ignore stderr output from trying to get the size of a folder.. perhaps not the most elegant way of handling errors # However since ITEM_SIZE will not increase if given a folder as parameter, simply ignoring stderr output handles the situation just fine. ITEM_SIZE=$(${SVNLOOK} filesize -t "${TRANSACTION}" "${REPOS_PATH}" "${item}" )

    sum=$((ITEM_SIZE + total))
    echo "$sum" > /tmp/svn_sum.txt

done

TOTAL=`cat /tmp/svn_sum.txt`

if [ ${TOTAL} -gt ${MAX_COMMIT_SIZE} ]; then
echo "Commit blocked! Your commit size is $TOTAL bytes, the maximum size is $MAX_COMMIT_SIZE bytes." 1>&2
    exit 4
fi

# All checks passed, so allow the commit.
exit 0
```

Reply via email to