Guest User

Untitled

a guest
Jan 12th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. import argparse
  5. import re
  6. import subprocess
  7.  
  8.  
  9. NORMAL_BRANCH_NAME_LINE_PREFIX = "# On branch "
  10. MESSAGE_BRANCH_NAME_LINE_PREFIX = "["
  11. TICKET_NUMBER_REGEX = r'(\d+)-'
  12.  
  13.  
  14. if __name__ == '__main__':
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('commit_msg_file')
  17. parser.add_argument('commit_source', nargs='?')
  18. parser.add_argument('sha1', nargs='?')
  19. args = parser.parse_args()
  20.  
  21. if args.commit_source in ('merge', 'squash', 'commit'):
  22. sys.exit()
  23.  
  24. content = None
  25. ticket_number = None
  26.  
  27. branch_name_line_prefix = MESSAGE_BRANCH_NAME_LINE_PREFIX \
  28. if args.commit_source == 'message' \
  29. else NORMAL_BRANCH_NAME_LINE_PREFIX
  30.  
  31. with open(args.commit_msg_file) as f:
  32. branch_name = subprocess.check_output(
  33. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('utf8')
  34. match = re.match(TICKET_NUMBER_REGEX, branch_name)
  35. if match:
  36. f.seek(0)
  37. content = f.read()
  38. ticket_number = match.group(1)
  39.  
  40. if content is not None and ticket_number is not None:
  41. if args.commit_source == 'message':
  42. ticket_number_suffix = " "
  43. else:
  44. ticket_number_suffix = " \n"
  45. content = "{}{}{}".format(ticket_number, ticket_number_suffix, content)
  46. with open(args.commit_msg_file, 'w') as f:
  47. f.write(content)
Add Comment
Please, Sign In to add comment