Skip to content

Instantly share code, notes, and snippets.

@eikevons
Last active June 26, 2023 13:20
Show Gist options
  • Select an option

  • Save eikevons/6c4d490bcf6d36422ae624f6aa01bf0d to your computer and use it in GitHub Desktop.

Select an option

Save eikevons/6c4d490bcf6d36422ae624f6aa01bf0d to your computer and use it in GitHub Desktop.

Revisions

  1. eikevons renamed this gist Jun 26, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. eikevons created this gist Jun 25, 2023.
    34 changes: 34 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/bin/sh

    # Maximum file size limit in MiB
    limitMiB=${GIT_MAX_MIB:-5} # Default 5MiB
    if [ $limitMiB -le 0 ]; then
    exit 0
    fi

    # Go to repo root folder
    cd "$(git rev-parse --show-toplevel)"

    git diff-index --cached --name-only HEAD | {
    fail=0
    while read file; do
    sizeMiB=$(( $(stat -c '%s' "$file") / 1024 / 1024 ))
    if [ $sizeMiB -gt $limitMiB ]; then
    if [ $fail = 0 ]; then
    echo "Some files exceed size limit of $limitMiB MiB:" 1>&2
    echo 1>&2

    fail=1
    fi
    echo "$file ($sizeMiB MiB)" 1>&2
    fi
    done
    if [ "$fail" = 1 ];then
    echo 1>&2
    echo "(Use 'GIT_MAX_MIB=... git commit ...' to use a different limit. GIT_MAX_MIB=0 disables this hook.)" 1>&2
    exit 1
    fi
    }
    fail=$?

    exit $fail