Skip to content

Commit

Permalink
Merge 1fd403a into 61fe2bd
Browse files Browse the repository at this point in the history
  • Loading branch information
lovato committed Apr 8, 2019
2 parents 61fe2bd + 1fd403a commit 3e03fe9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
6 changes: 4 additions & 2 deletions .hooks4git.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scripts]
flake8 = flake8 --max-line-length=120 --exclude .git,build,dist,.env,.venv
flake8 = flake8 --max-line-length=119 --exclude .git,build,dist
nosetests = nosetests --with-coverage
pytest = python -m pytest tests
; https://www.npmjs.com/package/travis-lint
Expand All @@ -8,10 +8,12 @@ travis_linter = travis-lint .travis.yml
md_linter = markdownlint README.md
bandit = bandit hooks4git
checkbranch = scripts/check_branch_name ^(feature|bugfix|hotfix)\/.+
black = black . --line-length=119 --check -t py36

[hooks.pre-commit.scripts]
checkbranch = checkbranch
check.py = flake8
check = flake8
; check = black

[hooks.pre-push.scripts]
tests = pytest
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Example section for pre-commit, for Python:

```bash
[scripts]
flake8 = flake8 --max-line-length=120 --exclude .git,build,dist,.env,.venv
flake8 = flake8 --max-line-length=119 --exclude .git,build,dist,.env,.venv
nosetests = nosetests --with-coverage

[hooks.pre-commit.scripts]
Expand All @@ -84,7 +84,7 @@ Note: All scripts you add here need to be available on your PATH for execution.

#### Built-in Scripts

Currently, there is only one available built-in script, called 'check_branch_name.sh'. If you want to use, just follow the exemple on the default .ini file, on sub-section 'checkbranch'. This is the way to trigger built-in scripts, prefixing them with 'scripts/'. On 0.1 release, I was using a '_' character for built-in scripts, but that caused so many headaches, mainly when trying to make this work inside GitBash for windows (ok, that was because I was actually trying to call a bat file ... then I gave up).
Currently, there is only one available built-in script, called 'check_branch_name.sh'. If you want to use, just follow the exemple on the default .ini file, on sub-section 'checkbranch'. This is the way to trigger built-in scripts, prefixing them with 'scripts/'. On 0.1 release, I was using a '_' character for built-in scripts, but that caused so many headaches, mainly when trying to make this work inside GitBash for windows (ok, that was because I was actually trying to call a bat file ... then I gave it up).

### Output

Expand All @@ -94,7 +94,7 @@ Here is a sample output for a Python configuration, with Flake8 (black and white
———————————————————————————————————————————————————————————————————————————————
hooks4git v0.2.x :: Pre-Commit :: hook triggered
———————————————————————————————————————————————————————————————————————————————
STEP | $ flake8 --max-line-length=120 --exclude .git,__pycache__,build,dist
STEP | $ flake8 --max-line-length=119 --exclude .git,__pycache__,build,dist
OUT | None
PASS | 'flake8' step executed successfully
———————————————————————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -123,6 +123,8 @@ See list of [contributors](../../graphs/contributors) who participated in this p

- Support for Windows with GitBash
- Added docker scripts for quick clean machine testing environment
- Better exception handling when user configures duplicate sections by mistake
- Changed default max line length example to 119 instead of 120

### 0.1.x

Expand Down
5 changes: 3 additions & 2 deletions hooks4git/.hooks4git.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[scripts]
echo = echo Wheeee! Running a hook!
flake8 = flake8 --max-line-length=120 --exclude .git,build,dist,.env,.venv
flake8 = flake8 --max-line-length=119 --exclude .git,build,dist,.env,.venv
black = black . --line-length=119 --check
pytest = python -m pytest tests
eslint = eslint -f checkstyle index.js > checkstyle-result.xml
checkbranch = scripts/check_branch_name ^(feature|bugfix|hotfix)\/.+
checkbranch = scripts/check_branch_name ^(feature|bugfix|hotfix|fix)\/.+

[hooks.pre-commit.scripts]
msg = echo
Expand Down
45 changes: 28 additions & 17 deletions hooks4git/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,13 @@ def main(cmd):
git_root = system('git', 'rev-parse', '--show-toplevel')[1].replace('\n', '')
configfile = "%s/.hooks4git.ini" % git_root
config = configparser.ConfigParser()
cfg = {}
exception_message = ""
try:
config.read(configfile)
cfg = ini_as_dict(config)
except Exception as e: # noqa
cfg = []
exception_message = str(e)

global steps_executed
steps_executed = 0
Expand All @@ -289,26 +291,35 @@ def main(cmd):
scripts = cfg.get('scripts', {})
hook = cfg.get('hooks.%s.scripts' % cmd, {})
commands = hook.keys()
if len(commands) > 0:
# If section is ommited, app outputs absolutelly nothing to stdout
if 'hooks.'+cmd.lower()+'.scripts' in config.sections():
divider()
title = "hooks4git v%s :: %s :: hook triggered" % (__version__, cmd.title())
title = Fore.YELLOW + Style.BRIGHT + title + Style.RESET_ALL
print(title)
for command_item in commands:
# if len(commands) == 0:
# print("Somehow, nothing to do...")
if len(exception_message) > 0:
divider()
print("Oops! " + exception_message)
divider()
exit(1)
divider()
steps_executed += 1
files = []
# if cmd == 'pre-commit':
# files = get_changed_files()
command = scripts[hook[command_item]]
result = execute(command.split()[0], files, command.split()[1:])
if result[0] != 0:
no_fails = False
style = Fore.RED + Style.BRIGHT
out('FAIL', "%s'%s/%s' step failed to execute %s" % (style, command_item, hook[command_item], Style.RESET_ALL)) # noqa
else:
style = Fore.GREEN
out('PASS', "%s'%s/%s' step executed successfully %s" % (style, command_item, hook[command_item], Style.RESET_ALL)) # noqa
for command_item in commands:
steps_executed += 1
files = []
# if cmd == 'pre-commit':
# files = get_changed_files()
command = scripts[hook[command_item]]
result = execute(command.split()[0], files, command.split()[1:])
if result[0] != 0:
no_fails = False
style = Fore.RED + Style.BRIGHT
out('FAIL', "%s'%s/%s' step failed to execute %s" % (style, command_item, hook[command_item], Style.RESET_ALL)) # noqa
else:
style = Fore.GREEN
out('PASS', "%s'%s/%s' step executed successfully %s" % (style, command_item, hook[command_item], Style.RESET_ALL)) # noqa
divider()
return no_fails
except Exception as e: # noqa
out('ERR!', str(e), color=Fore.RED)
Expand Down Expand Up @@ -357,7 +368,7 @@ def divider():

def report():
if steps_executed > 0:
divider()
# divider()
end_time = datetime.datetime.now()
out('STEPS', '%s were executed' % steps_executed, color=Fore.BLUE)
out('TIME', 'Execution took ' + str(end_time - start_time), color=Fore.BLUE)
Expand Down
15 changes: 10 additions & 5 deletions hooks4git/hooks4git
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ def parse_args(args):
Returns:
:obj:`argparse.Namespace`: command line parameters namespace
"""
parser = argparse.ArgumentParser(description="hooks4git")
parser = argparse.ArgumentParser(description="Extensible Hook System for GIT")

if len(sys.argv) < 2:
parser.print_usage()
sys.exit(1)

parser.add_argument(
"-v",
"--version",
action="version",
version="" # "hooks4git {ver}".format(ver=__version__),
version="hooks4git {ver}".format(ver=__version__),
)
parser.add_argument(
"--init",
dest="init",
action="store_const",
help="Install hooks4git on current repository",
help="Install hooks4git on current repository (.git/hooks)",
const=True,
)
parser.add_argument(
"-t", "--trigger", dest="git_hook", help="Select which hook to trigger"
"-t", "--trigger", dest="git_hook", help="Select which hook to trigger manually"
)
return parser.parse_args(args)

Expand All @@ -43,6 +48,7 @@ def main(args):
args ([str]): command line parameter list
"""
args = parse_args(args)

if args.init:
Exec.add_hooks()
else:
Expand All @@ -53,7 +59,6 @@ def main(args):
def run():
"""Entry point for console_scripts
"""
print("hooks4git v" + __version__)
main(sys.argv[1:])


Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pytest
urllib3==1.22
mock
flake8
bandit
bandit
black

0 comments on commit 3e03fe9

Please sign in to comment.