Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """ commit-msg hook for git.
  3.  
  4. Hooks checks and alters commit message content based
  5. on given rules.
  6.  
  7. This uses concept of project tagging, if there environmental variable
  8. PROJECT_TAGS defined or hard coded TAGS variable then it takes
  9. it's value as possible tag words.
  10.  
  11. Example:
  12. PROJECT_TAGS=ISSUES, anotherissue
  13.  
  14. Commits themselves should use convention::
  15.  
  16. 'TAG-1234: Commit message'
  17.  
  18. To us branch as basis for commit message creation. Then they should be in format::
  19.  
  20. TAG-1234_some_optional_text
  21.  
  22.  
  23. This should be installed as commit-msg into $GIT_DIR/hooks/ folder. Like
  24. my_project/.git/hooks/commit-msg
  25. """
  26. import sys
  27. import os
  28. import subprocess
  29.  
  30.  
  31. TAGS = None
  32.  
  33.  
  34. def get_tags():
  35. """ get project tags from PROJECT_TAGS environmental variable
  36. """
  37. tags = TAGS or os.getenv('PROJECT_TAGS', '')
  38. return tags.split(',')
  39.  
  40.  
  41. def get_current_branch_name():
  42. return subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
  43.  
  44.  
  45. def _get_commit_message_file():
  46. # The first argument is the filename where lies commit message
  47. return open(sys.argv[1], 'r+')
  48.  
  49.  
  50. def read_commit_message():
  51. """ Read original commit message from file
  52. """
  53. with _get_commit_message_file() as f:
  54. return f.read().strip()
  55.  
  56.  
  57. def write_commit_message(message):
  58. """ Write new commit message to file
  59. """
  60. with _get_commit_message_file() as f:
  61. f.seek(0)
  62. f.write(message)
  63. f.truncate()
  64.  
  65.  
  66. def parse_tag(message):
  67. """ parse possible tag from message
  68. """
  69. return message.split('-')[0].strip()
  70.  
  71.  
  72. def parse_tag_and_num_from_branch(branch_name):
  73. return branch_name.split('_')[0].strip()
  74.  
  75.  
  76. def parse_tag_from_tagnum(tagnum):
  77. return tagnum.split('-')[0].strip()
  78.  
  79.  
  80. def is_tagged(message):
  81. """ check if commit message has already a tag in it.
  82. """
  83. tags = get_tags()
  84. possible_tag = parse_tag(message)
  85. return any(possible_tag == tag for tag in tags)
  86.  
  87.  
  88. def get_tagnum_from_branch():
  89. tags = get_tags()
  90. branch_name = get_current_branch_name()
  91. branch_tagnum = parse_tag_and_num_from_branch(branch_name)
  92.  
  93. if parse_tag_from_tagnum(branch_tagnum) in tags:
  94. return branch_tagnum
  95.  
  96.  
  97. def write_tagnum(message, tag=None):
  98. """ write tagnum to commit message.
  99.  
  100. add tag if exists
  101. """
  102. if tag is not None:
  103. write_commit_message("{}: {}".format(tag, message))
  104.  
  105.  
  106. def alter_message(message):
  107. """ Alter message content if needed
  108.  
  109. tag is not added if it cannot be parsed from branch,
  110. or it already exists.
  111. """
  112. if not is_tagged(message):
  113. write_tagnum(message, get_tagnum_from_branch())
  114.  
  115.  
  116. def run():
  117. message = read_commit_message()
  118. alter_message(message)
  119.  
  120.  
  121. if __name__ == '__main__':
  122. try:
  123. run()
  124. sys.exit(0)
  125. except Exception as e:
  126. print(str(e))
  127. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement