Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Fetch the PID of git commit command just ran
- PID=$(ps aux | grep "git commit" | tail -1 | awk '{print $2}')
- # If unable to fetch the PID, give it another try
- if [[ -z "$PID" ]]; then
- echo "unable to get process id, please try again.."
- exit 1
- fi
- # Fetch the command used
- COMMAND=$(ps -p "$PID" -o args)
- # At times, unable to fetch the command used from PID
- # Checking Line counts and exits
- COMMAND_LINE_COUNT=$(ps -p "$PID" -o args | wc -l)
- if [[ $COMMAND_LINE_COUNT -eq 1 ]]; then
- echo "unable to fetch the command, please try again.."
- exit 1
- fi
- COMMAND=$(echo "$COMMAND" | tail -1)
- # Print the Command observed
- # echo $COMMAND
- # Variable to store if the command is ammend
- is_ammend=false
- # Check if command is amend by regex match
- if [[ $COMMAND == *"amend"* ]]; then
- is_ammend=true
- fi
- # If the issued command is ammend, validate the current author and previous commit author
- if [[ $is_ammend = "true" ]]; then
- echo "Looks like you are ammending the code.. "
- PREV_AUTHOR=$(git log --pretty=format:"%an" | head -1)
- CURRENT_AUTHOR=$(git config --global user.name)
- if [ "$PREV_AUTHOR" != "$CURRENT_AUTHOR" ]; then
- echo "Current Author $CURRENT_AUTHOR"
- echo "Previous Author $PREV_AUTHOR"
- echo "ammends to other's commit is not supported..."
- exit 1
- fi
- fi
- exit 0
Add Comment
Please, Sign In to add comment