Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save VehpuS/1dbe91a5d2a1c30aa83382db03f75ee2 to your computer and use it in GitHub Desktop.
Save VehpuS/1dbe91a5d2a1c30aa83382db03f75ee2 to your computer and use it in GitHub Desktop.

Revisions

  1. @guysmoilov guysmoilov revised this gist Mar 16, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ IFS='
    echo "Checking staged file sizes"
    shouldFail=false
    for file in $( git diff-index --cached --name-only $against ); do
    file_size=$( ls -la $file | awk '{ print $5 }')
    file_size=$(([ ! -f $file ] && echo 0) || (ls -la $file | awk '{ print $5 }'))
    if [ "$file_size" -gt "$limit" ]; then
    echo File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB
    shouldFail=true
  2. @guysmoilov guysmoilov revised this gist Dec 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ done

    if $shouldFail
    then
    echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under 10MB!!!
    echo If you really need to commit this file, you can override the size limit by setting the GIT_FILE_SIZE_LIMIT environment variable, e.g. GIT_FILE_SIZE_LIMIT=42000000 for 42MB. Or, commit with the --no-verify switch to skip the check entirely, you naughty boy!
    echo Commit aborted
    exit 1;
    fi
  3. @guysmoilov guysmoilov revised this gist Dec 26, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Git pre-commit hook for large files.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,12 @@ Of course, you can just download it directly to the hooks in an existing git rep
    ## Installation
    `curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/install.sh | bash`

    ## Configuration
    The default limit is max 5MB per file. If you feel that your commit is a special case, you can always override the limit with:
    ```
    GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB"
    ```

    ## Contents
    * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself
    * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory.
  4. @guysmoilov guysmoilov revised this gist Dec 26, 2019. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,11 @@
    # This is a pre-commit hook that ensures attempts to commit files that are
    # are larger than $limit to your _local_ repo fail, with a helpful error message.

    # You can override the default limit of 5MB by supplying the environment variable:
    # GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB"

    # Maximum file size limit in bytes
    limit=5000000 # 5MB
    limit=${GIT_FILE_SIZE_LIMIT:-5000000} # Default 5MB
    limitInMB=$(( $limit / 10**6 ))

    # Move to the repo root so git files paths make sense
  5. @guysmoilov guysmoilov revised this gist Dec 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Git pre-commit hook for large files.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ a script is attached to add this hook to all git repos you create / clone in the
    Of course, you can just download it directly to the hooks in an existing git repo.

    ## Installation
    `curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh | bash`
    `curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/install.sh | bash`

    ## Contents
    * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself
  6. @guysmoilov guysmoilov revised this gist Dec 26, 2019. 2 changed files with 4 additions and 4 deletions.
    6 changes: 3 additions & 3 deletions Git pre-commit hook for large files.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    This hook warns you before you accidentally commit large files to git.
    It's very hard to reverse such an accidental commit, so it's better to prevent it in advance.

    Since you will likely want [this script](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) to run in all your git repos,
    Since you will likely want [this script](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) to run in all your git repos,
    a script is attached to add this hook to all git repos you create / clone in the future.

    Of course, you can just download it directly to the hooks in an existing git repo.
    @@ -11,8 +11,8 @@ Of course, you can just download it directly to the hooks in an existing git rep
    `curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh | bash`

    ## Contents
    * [pre-commit](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) : The hook itself
    * [install.sh](eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh) : Installs this hook to your git template directory.
    * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself
    * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory.

    ## Credits
    Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e
    2 changes: 1 addition & 1 deletion install.sh
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ if [ -f "$PRECOMMIT_HOOK" ]; then
    fi

    echo "Downloading the hook to $PRECOMMIT_HOOK"
    curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null
    curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null

    echo "Making it executable"
    chmod +x "$PRECOMMIT_HOOK"
  7. @guysmoilov guysmoilov created this gist Dec 26, 2019.
    20 changes: 20 additions & 0 deletions Git pre-commit hook for large files.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # Git pre-commit hook for large files
    This hook warns you before you accidentally commit large files to git.
    It's very hard to reverse such an accidental commit, so it's better to prevent it in advance.

    Since you will likely want [this script](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) to run in all your git repos,
    a script is attached to add this hook to all git repos you create / clone in the future.

    Of course, you can just download it directly to the hooks in an existing git repo.

    ## Installation
    `curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh | bash`

    ## Contents
    * [pre-commit](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) : The hook itself
    * [install.sh](eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh) : Installs this hook to your git template directory.

    ## Credits
    Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e

    [Help from this stackoverflow question](https://stackoverflow.com/questions/2293498/applying-a-git-post-commit-hook-to-all-current-and-future-repos)
    37 changes: 37 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/sh
    set -e
    echo "Starting install script..."

    SET_GIT_TEMPLATE_DIR=false
    EXISTING_TEMPLATE=$(git config --global init.templateDir || echo "")
    if [ -z "$EXISTING_TEMPLATE" ]; then
    echo "Creating a new global git template dir at ~/.git_template"
    mkdir ~/.git_template
    EXISTING_TEMPLATE="$(cd ~; pwd -P)/.git_template"
    SET_GIT_TEMPLATE_DIR=true
    else
    EXISTING_TEMPLATE="$(eval cd $(dirname "$EXISTING_TEMPLATE"); pwd -P)/$(basename "$EXISTING_TEMPLATE")"
    echo "Using existing git template dir: $EXISTING_TEMPLATE"
    fi

    HOOKS_DIR="$EXISTING_TEMPLATE/hooks"
    PRECOMMIT_HOOK="$HOOKS_DIR/pre-commit"
    echo "Creating hooks dir if it doesn't already exist: $HOOKS_DIR"
    mkdir -p "$HOOKS_DIR"
    if [ -f "$PRECOMMIT_HOOK" ]; then
    echo "Cannot install hook as it's already defined: '$PRECOMMIT_HOOK'" >&2
    exit 1
    fi

    echo "Downloading the hook to $PRECOMMIT_HOOK"
    curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null

    echo "Making it executable"
    chmod +x "$PRECOMMIT_HOOK"

    if [ "$SET_GIT_TEMPLATE_DIR" = true ]; then
    echo "Defining ~/.git_template as the global git template dir"
    git config --global init.templateDir '~/.git_template'
    fi

    echo -e "\nDone! Any future git repo created in this user profile will contain the hook\n"
    42 changes: 42 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/bin/sh

    # This is a pre-commit hook that ensures attempts to commit files that are
    # are larger than $limit to your _local_ repo fail, with a helpful error message.

    # Maximum file size limit in bytes
    limit=5000000 # 5MB
    limitInMB=$(( $limit / 10**6 ))

    # Move to the repo root so git files paths make sense
    repo_root=$( git rev-parse --show-toplevel )
    cd $repo_root

    empty_tree=$( git hash-object -t tree /dev/null )

    if git rev-parse --verify HEAD > /dev/null 2>&1
    then
    against=HEAD
    else
    against="$empty_tree"
    fi

    # Set split so that for loop below can handle spaces in file names by splitting on line breaks
    IFS='
    '

    echo "Checking staged file sizes"
    shouldFail=false
    for file in $( git diff-index --cached --name-only $against ); do
    file_size=$( ls -la $file | awk '{ print $5 }')
    if [ "$file_size" -gt "$limit" ]; then
    echo File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB
    shouldFail=true
    fi
    done

    if $shouldFail
    then
    echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under 10MB!!!
    echo Commit aborted
    exit 1;
    fi