Difference between revisions of "Flake8 Git Pre-Commit Hook"

From crowdresearch
Jump to: navigation, search
(Created page with "From your project's root directory, create a new file .git/hooks/pre-commit Add below code to it. Remember to update VIRTUAL_ENV with path for virtualenv (normally it is $H...")
 
 
Line 1: Line 1:
 
From your project's root directory, create a new file  
 
From your project's root directory, create a new file  
 
.git/hooks/pre-commit  
 
.git/hooks/pre-commit  
 +
 +
    nano .git/hooks/pre-commit
  
 
Add below code to it. Remember to update VIRTUAL_ENV with path for virtualenv (normally it is $HOME/.virtualenvs/<environ-name>)  
 
Add below code to it. Remember to update VIRTUAL_ENV with path for virtualenv (normally it is $HOME/.virtualenvs/<environ-name>)  
Line 18: Line 20:
 
     done
 
     done
 
     unset VIRTUAL_ENV
 
     unset VIRTUAL_ENV
 +
 +
Now create hook file for flake
 +
.git/hooks/pre-commit.d/flake.py
 +
    nano .git/hooks/pre-commit.d/flake.py
 +
 +
Add below code to it
 +
 +
    #!/usr/bin/env python
 +
    import sys
 +
    from flake8.hooks import git_hook, get_git_param
 +
    # `get_git_param` will retrieve configuration from your local git config and
 +
    # then fall back to using the environment variables that the hook has always
 +
    # supported.
 +
    # For example, to set the complexity, you'll need to do:
 +
    #  git config flake8.complexity 10
 +
    COMPLEXITY = get_git_param('FLAKE8_COMPLEXITY', 12)
 +
    STRICT = get_git_param('FLAKE8_STRICT', True)
 +
    IGNORE = get_git_param('FLAKE8_IGNORE', None)
 +
    LAZY = get_git_param('FLAKE8_LAZY', False)
 +
    if __name__ == '__main__':
 +
        sys.exit(git_hook(
 +
            complexity=COMPLEXITY,
 +
            strict=STRICT,
 +
            ignore=IGNORE,
 +
            lazy=LAZY
 +
          ))

Latest revision as of 07:31, 22 January 2016

From your project's root directory, create a new file .git/hooks/pre-commit

   nano .git/hooks/pre-commit 

Add below code to it. Remember to update VIRTUAL_ENV with path for virtualenv (normally it is $HOME/.virtualenvs/<environ-name>)

   #!/bin/bash
   export VIRTUAL_ENV=~/.virtualenvs/crowd
   if [ -n $VIRTUAL_ENV ]; then
       PATH=$VIRTUAL_ENV/bin:$PATH
   fi
   for f in .git/hooks/pre-commit.d/*; do
       if [ -x "$f" ]; then
           if ! "$f"; then
               echo "COMMIT FAILED!";
               exit 1
           fi
       fi
   done
   unset VIRTUAL_ENV

Now create hook file for flake .git/hooks/pre-commit.d/flake.py

   nano .git/hooks/pre-commit.d/flake.py

Add below code to it

   #!/usr/bin/env python
   import sys
   from flake8.hooks import git_hook, get_git_param
   # `get_git_param` will retrieve configuration from your local git config and
   # then fall back to using the environment variables that the hook has always
   # supported.
   # For example, to set the complexity, you'll need to do:
   #   git config flake8.complexity 10
   COMPLEXITY = get_git_param('FLAKE8_COMPLEXITY', 12)
   STRICT = get_git_param('FLAKE8_STRICT', True)
   IGNORE = get_git_param('FLAKE8_IGNORE', None)
   LAZY = get_git_param('FLAKE8_LAZY', False)
   if __name__ == '__main__':
        sys.exit(git_hook(
            complexity=COMPLEXITY,
            strict=STRICT,
            ignore=IGNORE,
            lazy=LAZY
         ))