git_try.py 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Wrapper for trychange.py for git checkout."""

import logging
import sys

from scm import GIT
11
import subprocess2
12 13
import third_party.upload
import trychange
14
import git_cl
15 16 17 18 19


def GetRietveldIssueNumber():
  try:
    return GIT.Capture(
20 21
        ['config', 'branch.%s.rietveldissue' % GIT.GetBranch('.')],
        '.').strip()
22
  except subprocess2.CalledProcessError:
23 24 25 26 27 28
    return None


def GetRietveldPatchsetNumber():
  try:
    return GIT.Capture(
29 30
        ['config', 'branch.%s.rietveldpatchset' % GIT.GetBranch('.')],
        '.').strip()
31
  except subprocess2.CalledProcessError:
32 33 34 35 36
    return None


def GetRietveldServerUrl():
  try:
37
    return GIT.Capture(['config', 'rietveld.server'], '.').strip()
38
  except subprocess2.CalledProcessError:
39 40 41
    return None


42
def main(args):
43 44 45 46 47 48 49 50 51 52 53
  patchset = GetRietveldPatchsetNumber()
  if patchset:
    args.extend([
        '--issue', GetRietveldIssueNumber(),
        '--patchset', patchset,
    ])
  else:
    rietveld_url = GetRietveldServerUrl()
    if rietveld_url:
      args.extend(['--rietveld_url', GetRietveldServerUrl()])
  try:
54 55
    cl = git_cl.Changelist()
    change = cl.GetChange(cl.GetUpstreamBranch(), None)
56 57
    # Hack around a limitation in logging.
    logging.getLogger().handlers = []
58
    sys.exit(trychange.TryChange(
59
        args, change, swallow_exception=False,
60
        prog='git try',
61 62 63 64 65 66 67
        extra_epilog='\n'
                     'git try will diff against your tracked branch and will '
                     'detect your rietveld\n'
                     'code review if you are using git-cl\n'))
  except third_party.upload.ClientLoginError, e:
    print('Got an exception while trying to log in to Rietveld.')
    print(str(e))
68 69 70 71 72 73 74 75 76
  return 0


if __name__ == '__main__':
  try:
    sys.exit(main(sys.argv[1:]))
  except KeyboardInterrupt:
    sys.stderr.write('interrupted\n')
    sys.exit(1)