Guest User

Untitled

a guest
Jan 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #! /usr/bin/python2.7
  2.  
  3. import sys
  4. import fileinput
  5. import subprocess
  6.  
  7.  
  8. class RejectError(Exception):
  9.  
  10. code = 255
  11. msg = ""
  12.  
  13. def reject(self):
  14. if self.msg:
  15. sys.stdout.write(self.msg)
  16. sys.exit(self.code)
  17.  
  18.  
  19. class ProtectMasterBranchError(RejectError):
  20.  
  21. code = 1
  22. msg = "branch master is protected, no one can delete or force push"
  23.  
  24.  
  25. class Git(object):
  26.  
  27. @staticmethod
  28. def is_null_commit(commit_id):
  29. return True if commit_id == '0' * len(commit_id) else False
  30.  
  31. @staticmethod
  32. def is_force_push(old_commit_id, new_commit_id):
  33. merge_base = subprocess.check_output(
  34. "git merge-base %s %s" % (old_commit_id, new_commit_id),
  35. shell=True,
  36. )
  37. return old_commit_id != merge_base
  38.  
  39.  
  40. def protect_master_branch(ref, old_commit_id, new_commit_id):
  41. if ref != 'refs/heads/master':
  42. return
  43. if Git.is_null_commit(old_commit_id):
  44. return
  45. if Git.is_null_commit(new_commit_id):
  46. raise ProtectMasterBranchError()
  47. if Git.is_force_push(old_commit_id, new_commit_id):
  48. raise ProtectMasterBranchError()
  49.  
  50.  
  51. def main():
  52. try:
  53. for line in fileinput.input():
  54. old_commit_id, new_commit_id, ref = line.split()
  55. protect_master_branch(ref, old_commit_id, new_commit_id)
  56. except RejectError as error:
  57. error.reject()
  58.  
  59. main()
Add Comment
Please, Sign In to add comment