Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ankitsaini2609/cb1c2583bba94fa52d1de8c4dc72d284 to your computer and use it in GitHub Desktop.
Save ankitsaini2609/cb1c2583bba94fa52d1de8c4dc72d284 to your computer and use it in GitHub Desktop.

Revisions

  1. ankitsaini2609 revised this gist Aug 7, 2024. 1 changed file with 88 additions and 41 deletions.
    129 changes: 88 additions & 41 deletions global-pre-commit-hook-gitleaks-ankitsaini2609.sh
    Original file line number Diff line number Diff line change
    @@ -1,53 +1,100 @@
    #!/bin/bash
    python3 -m pip install pre-commit
    if [ `echo $?` -ne 0 ]; then
    pip install pre-commit
    ## V1.0.0
    ## pre-commit-hook.sh
    ## Authors: Ankit Saini

    echo -e "
    ###### # ###
    # # ##### ###### #### #### # # # # # ##### # # #### #### # # # # ## # #
    # # # # # # # # # ## ## ## ## # # # # # # # # # # # # # # # #
    ###### # # ##### ##### # # # # ## # # ## # # # ###### # # # # #### # # # # #
    # ##### # # # # # # # # # # # # # # # # # # # # # ### # #
    # # # # # # # # # # # # # # # # # # # # # # # # # ### # #
    # # # ###### #### #### # # # # # # # # #### #### # # ## ##### ### ### "


    echo ""; echo ""
    echo "Setting up the environment for pre-commit hook..."
    echo ""; echo ""

    if ! command -v python3 &> /dev/null; then
    echo "Python 3 is not installed. Please install Python3 by running 'brew install python3'"
    exit 1
    fi

    # Check if pre-commit is installed
    if ! command -v pre-commit &> /dev/null; then
    echo "pre-commit is not installed. Please install pre-commit by running 'pip3 install pre-commit'"
    exit 1
    fi

    echo ""; echo ""
    echo "Environment setup is done"
    echo ""; echo ""
    echo "Installing pre-commit hook"
    echo ""; echo ""

    # Checking whether git directory exists or not
    if [[ ! -d ~/.config/git/hooks ]]; then
    mkdir -p ~/.config/git/hooks
    fi

    #Checking for pre-commit file
    FILE=~/.config/git/hooks/pre-commit
    flag=0

    if [ -f "$FILE" ]; then
    string1="pre-commit run -c ~/.config/git/.pre-commit-config.yaml"
    search=`grep "${string1}" ~/.config/git/hooks/pre-commit`
    if [ "${search}" = "${string1}" ]; then
    FILE=~/.config/git/.pre-commit-config.yaml
    if [ -f "$FILE" ]; then
    string1="- repo: https://github.com/ankitsaini2609/gitleaks"
    search=`grep -- "${string1}" $HOME/.config/git/.pre-commit-config.yaml`
    if [ "${search}" = "${string1}" ]; then
    exit 0
    else
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF
    exit 0
    fi
    else
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF
    exit 0
    fi
    search=$(grep -F "${string1}" "$FILE")
    if [ -n "$search" ]; then
    echo "pre-commit file already contains the command."
    flag=1
    else
    flag=0
    fi
    fi
    fi

    mkdir -p ~/.config/git/hooks
    git config --global core.hooksPath ~/.config/git/hooks/
    cat << EOF >> ~/.config/git/hooks/pre-commit
    if [ "$flag" -eq 0 ]; then
    cat << "EOF" >> ~/.config/git/hooks/pre-commit
    #!/bin/bash
    # Find the root of the Git repository
    GIT_ROOT=$(git rev-parse --show-toplevel)
    directory_exists="$GIT_ROOT/.git/hooks/pre-commit"
    if [ -f "$directory_exists" ]; then
    echo "local pre-commit hooks exists. executing it first"
    bash $directory_exists
    fi
    pre-commit run -c ~/.config/git/.pre-commit-config.yaml
    EOF
    chmod +x ~/.config/git/hooks/pre-commit
    fi

    #Checking for pre-commit config file
    FILE=~/.config/git/.pre-commit-config.yaml
    flag=0

    if [ -f "$FILE" ]; then
    string1=" - repo: [email protected]:ankitsaini2609/oculus-pre-commit-hook.git"
    search=$(grep -Fx "$string1" "$FILE")
    if [ -n "$search" ]; then
    echo "pre-commit config file already contains the entry."
    flag=1
    else
    flag=0
    fi
    fi

    if [ "$flag" -eq 0 ]; then
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    repos:
    - repo: [email protected]:ankitsaini2609/oculus-pre-commit-hook.git
    rev: v1.0
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF
    - id: gitleaks
    EOF
    fi

    git config --global core.hooksPath ~/.config/git/hooks/
    chmod +x ~/.config/git/hooks/pre-commit
    echo "Done!!"
  2. ankitsaini2609 created this gist Feb 8, 2024.
    53 changes: 53 additions & 0 deletions global-pre-commit-hook-gitleaks-ankitsaini2609.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/bin/bash
    python3 -m pip install pre-commit
    if [ `echo $?` -ne 0 ]; then
    pip install pre-commit
    fi
    FILE=~/.config/git/hooks/pre-commit
    if [ -f "$FILE" ]; then
    string1="pre-commit run -c ~/.config/git/.pre-commit-config.yaml"
    search=`grep "${string1}" ~/.config/git/hooks/pre-commit`
    if [ "${search}" = "${string1}" ]; then
    FILE=~/.config/git/.pre-commit-config.yaml
    if [ -f "$FILE" ]; then
    string1="- repo: https://github.com/ankitsaini2609/gitleaks"
    search=`grep -- "${string1}" $HOME/.config/git/.pre-commit-config.yaml`
    if [ "${search}" = "${string1}" ]; then
    exit 0
    else
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF
    exit 0
    fi
    else
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF
    exit 0
    fi
    fi
    fi

    mkdir -p ~/.config/git/hooks
    git config --global core.hooksPath ~/.config/git/hooks/
    cat << EOF >> ~/.config/git/hooks/pre-commit
    #!/bin/bash
    pre-commit run -c ~/.config/git/.pre-commit-config.yaml
    EOF
    chmod +x ~/.config/git/hooks/pre-commit
    cat << EOF >> ~/.config/git/.pre-commit-config.yaml
    - repo: https://github.com/ankitsaini2609/gitleaks
    rev: main
    hooks:
    - id: gitleaks
    args: ['--report','/tmp/pre-commit-output.json', '--']
    EOF