How to make a git pre-commit code check?

22.8k views Asked by At

First question... is it even possible to accomplish this with git? :)

What I want is this:

Sometimes I switch one variable in my code to true (localMode = true;) for my own debugging purposes. But this should never be commited. I should only commit code with the variable set to false. And of course sometimes I forget to make this change. Is it possible for git to somehow stop or warn me if I am about to commit the 'wrong' code?

UPD:
Thanks for the help! I ended up with the following shell script:

#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ $(echo "$FILE" | grep -E "^.+main\-controller\.js$") ]]; then
    content=$(<"$FILE")
    if [[ $(echo "$content" | grep -E "rootScope\.localMode = true") ]]; then   
        echo -e "\e[1;31m\tCommit contains localMode set to true.\e[0m" >&2
        exit 1
    fi
fi
done
4

There are 4 answers

1
sangheestyle On BEST ANSWER

What about the following link?

http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/

I think you can just add some regular expression to detect localMode = true into the sample code at the link.

0
dcastro On

Yes, you can use a pre-commit hook.

Just drop a shell script named pre-commit (with no extension) inside your ".git/hooks" folder with the logic to check your variable and either:

  • change it to false and continue with the commit or
  • print a message telling the user to correct the value manually, and exit with a non-zero code to abort the commit

The hooks folder should contain a few samples, such as "pre-commit.sample", which you might find helpful.

From the docs:

The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify. You can do things like check for code style (run lint or something equivalent), check for trailing whitespace (the default hook does exactly this), or check for appropriate documentation on new methods.

0
kubanczyk On

Here is my take based on an earlier answer:

#! /bin/bash
#
# This is a git hook. It exits with non-zero status and thus aborts
# a running `git commit` if the processed files contain undesirable PATTERNS.
#
# To enable this hook, rename this file to .git/hooks/pre-commit and run:
#   chmod a+x .git/hooks/pre-commit
# 
# To make it global:
#   git config --global core.hooksPath ~/git-central-hooks
#
# Source: https://stackoverflow.com/questions/26992576/how-to-make-a-git-pre-commit-code-check
# 
# Known problem:
# If you encounter error `declare: -A: invalid option` or similar upgrade bash 
# version to v4. For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
#

# Declare empty arrays
declare -A PATTERNS
declare -a errors

# Customize it:  ['your grep pattern'] ===> "Error message when found"
PATTERNS['^[<>|=]{4,}']="You've got leftover CONFLICT markers"
PATTERNS['FIXME']="You've got FIXME hanging (consider changing to TODO)"

while read file 
do
  for elem in ${!PATTERNS[*]}
  do
    if git show :0:"$file" | grep -Eq "$elem"
    then
        errors+=( "${PATTERNS[${elem}]} in file '$file'" )
    fi
  done
# The post-loop expression generates only filenames added (A) or modified (M)
done < <( git diff --staged --name-only --diff-filter=AM --no-color --unified=0 )

# Print errors
for error in "${errors[@]}"
do
  echo -e "\033[1;31m${error}\033[0m"
  # Mac OS only: use auditable speech
  # author=$(git config --get user.name)
  # which -s say && say -v Samantha -r 250 "$author $error"
done

# Fail if there is an error
if [[ ${#errors[@]} -ne 0 ]]
then
  exit 1
fi
0
Maxime Brehin On

This is an update for people who want to find alternatives to previous solutions.

There is plenty of resources now to check contents using git pre-commit hook.

The most "famous" is probably https://pre-commit.com/

Git hooks are sometimes hard to maintain and share (even if git 2.9 introduced the core.hooksPath config that make it easier).

I mostly work with JavaScript and I found a popular module named Husky that manages shareable hooks through a projet. I like it because it's integrated, shared and configured in my projects through my package.json.

I also tried to find a complementary module to check my contents before commiting but I found nothing satisfying. I wanted something similar with a shared configuration (in package.json) like this:

"precommit-checks": [
  {
    "filter": "\\.js$",
    "nonBlocking": "true",
    "message": "You’ve got leftover `console.log`",
    "regex": "console\\.log"
  },
  {
    "message": "You’ve got leftover conflict markers",
    "regex": "/^[<>|=]{4,}/m"
  },
  {
    "message": "You have unfinished devs",
    "nonBlocking": "true",
    "regex": "(?:FIXME|TODO)"
  }
]

I finally made my own: git-precommit-checks. You can try it if you want, otherwise you can still look for alternatives (especially if you don't work with JS).

If you still want to check your contents using a bash script, you can try a more generic approach and loop over an array of searched patterns that should stop your commit.

#! /bin/bash
# If you encounter any error like `declare: -A: invalid option`
# then you'll have to upgrade bash version to v4.
# For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/

# Hash using its key as a search Regex, and its value as associated error message
declare -A PATTERNS;
PATTERNS['^[<>|=]{4,}']="You've got leftover conflict markers";
PATTERNS['focus:\s*true']="You've got a focused spec";

# Declare empty errors array
declare -a errors;

# Loop over staged files and check for any specific pattern listed in PATTERNS keys
# Filter only added (A), copied (C), modified (M) files
for file in $(git diff --staged --name-only --diff-filter=ACM --no-color --unified=0); do
  for elem in ${!PATTERNS[*]} ; do
    { git show :0:"$file" | grep -Eq ${elem}; } || continue;
    errors+=("${PATTERNS[${elem}]} in ${file}…");
  done
done

# Print errors
# author=$(git config --get user.name)
for error in "${errors[@]}"; do
  echo -e "\033[1;31m${error}\033[0m"
  # Mac OS only: use auditable speech
  # which -s say && say -v Samantha -r 250 "$author $error"
done

# If there is any error, then stop commit creation
if ! [ ${#errors[@]} -eq 0 ]; then
  exit 1
fi