git_cl_hooks.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import os
  5. import re
  6. import subprocess
  7. import sys
  8. import breakpad
  9. import presubmit_support
  10. import scm
  11. def Backquote(cmd, cwd=None):
  12. """Like running `cmd` in a shell script."""
  13. return subprocess.Popen(cmd,
  14. cwd=cwd,
  15. stdout=subprocess.PIPE).communicate()[0].strip()
  16. class ChangeOptions:
  17. def __init__(self, commit=None, upstream_branch=None):
  18. self.commit = commit
  19. self.verbose = None
  20. self.default_presubmit = None
  21. self.may_prompt = None
  22. root = Backquote(['git', 'rev-parse', '--show-cdup'])
  23. if not root:
  24. root = "."
  25. absroot = os.path.abspath(root)
  26. if not root:
  27. raise Exception("Could not get root directory.")
  28. log = Backquote(['git', 'show', '--name-only',
  29. '--pretty=format:%H%n%s%n%n%b'])
  30. m = re.match(r'^(\w+)\n(.*)$', log, re.MULTILINE|re.DOTALL)
  31. if not m:
  32. raise Exception("Could not parse log message: %s" % log)
  33. name = m.group(1)
  34. description = m.group(2)
  35. files = scm.GIT.CaptureStatus([root], upstream_branch)
  36. issue = Backquote(['git', 'cl', 'status', '--field=id'])
  37. patchset = None
  38. self.change = presubmit_support.GitChange(name, description, absroot, files,
  39. issue, patchset)
  40. def RunHooks(hook_name, upstream_branch):
  41. commit = (hook_name == 'pre-cl-dcommit')
  42. # Create our options based on the command-line args and the current checkout.
  43. options = ChangeOptions(commit=commit, upstream_branch=upstream_branch)
  44. # Run the presubmit checks.
  45. if presubmit_support.DoPresubmitChecks(options.change,
  46. options.commit,
  47. options.verbose,
  48. sys.stdout,
  49. sys.stdin,
  50. options.default_presubmit,
  51. options.may_prompt):
  52. sys.exit(0)
  53. else:
  54. sys.exit(1)