git_try.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/python
  2. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Wrapper for trychange.py for git checkout."""
  6. import logging
  7. import sys
  8. import breakpad # pylint: disable=W0611
  9. from scm import GIT
  10. import subprocess2
  11. import third_party.upload
  12. import trychange
  13. import git_cl
  14. def GetRietveldIssueNumber():
  15. try:
  16. return GIT.Capture(
  17. ['config', 'branch.%s.rietveldissue' % GIT.GetBranch('.')],
  18. '.').strip()
  19. except subprocess2.CalledProcessError:
  20. return None
  21. def GetRietveldPatchsetNumber():
  22. try:
  23. return GIT.Capture(
  24. ['config', 'branch.%s.rietveldpatchset' % GIT.GetBranch('.')],
  25. '.').strip()
  26. except subprocess2.CalledProcessError:
  27. return None
  28. def GetRietveldServerUrl():
  29. try:
  30. return GIT.Capture(['config', 'rietveld.server'], '.').strip()
  31. except subprocess2.CalledProcessError:
  32. return None
  33. def main(args):
  34. patchset = GetRietveldPatchsetNumber()
  35. if patchset:
  36. args.extend([
  37. '--issue', GetRietveldIssueNumber(),
  38. '--patchset', patchset,
  39. ])
  40. else:
  41. rietveld_url = GetRietveldServerUrl()
  42. if rietveld_url:
  43. args.extend(['--rietveld_url', GetRietveldServerUrl()])
  44. try:
  45. cl = git_cl.Changelist()
  46. change = cl.GetChange(cl.GetUpstreamBranch(), None)
  47. # Hack around a limitation in logging.
  48. logging.getLogger().handlers = []
  49. sys.exit(trychange.TryChange(
  50. args, change, swallow_exception=False,
  51. prog='git try',
  52. extra_epilog='\n'
  53. 'git try will diff against your tracked branch and will '
  54. 'detect your rietveld\n'
  55. 'code review if you are using git-cl\n'))
  56. except third_party.upload.ClientLoginError, e:
  57. print('Got an exception while trying to log in to Rietveld.')
  58. print(str(e))
  59. return 0
  60. if __name__ == '__main__':
  61. try:
  62. sys.exit(main(sys.argv[1:]))
  63. except KeyboardInterrupt:
  64. sys.stderr.write('interrupted\n')
  65. sys.exit(1)