pagure_block_unsigned.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. """Pagure specific hook to block commit not having a 'Signed-off-by'
  3. statement.
  4. """
  5. from __future__ import print_function, unicode_literals
  6. import os
  7. import sys
  8. if 'PAGURE_CONFIG' not in os.environ \
  9. and os.path.exists('/etc/pagure/pagure.cfg'):
  10. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  11. import pagure # noqa: E402
  12. import pagure.exceptions # noqa: E402
  13. import pagure.lib.link # noqa: E402
  14. import pagure.ui.plugins # noqa: E402
  15. _config = pagure.config.config
  16. abspath = os.path.abspath(os.environ['GIT_DIR'])
  17. def run_as_pre_receive_hook():
  18. for line in sys.stdin:
  19. if _config.get('HOOK_DEBUG', False):
  20. print(line)
  21. (oldrev, newrev, refname) = line.strip().split(' ', 2)
  22. if _config.get('HOOK_DEBUG', False):
  23. print(' -- Old rev')
  24. print(oldrev)
  25. print(' -- New rev')
  26. print(newrev)
  27. print(' -- Ref name')
  28. print(refname)
  29. if set(newrev) == set(['0']):
  30. print("Deleting a reference/branch, so we won't run the "
  31. "hook to block unsigned commits")
  32. return
  33. commits = pagure.lib.git.get_revs_between(
  34. oldrev, newrev, abspath, refname)
  35. for commit in commits:
  36. if _config.get('HOOK_DEBUG', False):
  37. print('Processing commit: %s' % commit)
  38. signed = False
  39. for line in pagure.lib.git.read_git_lines(
  40. ['log', '--no-walk', commit], abspath):
  41. if line.lower().strip().startswith('signed-off-by'):
  42. signed = True
  43. break
  44. if _config.get('HOOK_DEBUG', False):
  45. print(' - Commit: %s is signed: %s' % (commit, signed))
  46. if not signed:
  47. print("Commit %s is not signed" % commit)
  48. sys.exit(1)
  49. def main(args):
  50. run_as_pre_receive_hook()
  51. if __name__ == '__main__':
  52. main(sys.argv[1:])