Skip to content

Instantly share code, notes, and snippets.

@Allanfs
Created February 20, 2021 19:02
Show Gist options
  • Save Allanfs/0108bdb726585ee033d201838fdf8b83 to your computer and use it in GitHub Desktop.
Save Allanfs/0108bdb726585ee033d201838fdf8b83 to your computer and use it in GitHub Desktop.
Hook para adicionar informação na mensagem do commit
#!/usr/bin/python3.8
import sys, re
from subprocess import check_output
commit_msg_filepath = sys.argv[1]
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
regex = rb'[0-9]+$'
if re.search(regex, branch):
issue = str(re.findall(regex, branch)[0], 'utf-8')
with open(commit_msg_filepath, 'r+') as fh:
commit_msg = fh.read()
fh.seek(0, 0)
fh.write('%s\n[%s]' % (commit_msg, issue)) # ID no final da msg do commit
# fh.write('[%s] %s' % (issue, commit_msg)) # ID no começo da msg do commit
elif branch != 'master' and branch != 'dev':
print('Branch protegida:', str(branch, 'utf-8'))
sys.exit(1)
@Allanfs
Copy link
Author

Allanfs commented Feb 20, 2021

O hook acima é ideal para branches no padrão que terminam com números. Exemplo: c_teste_de_hook_12345

Algumas informações a mais:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment