summaryrefslogtreecommitdiffstats
path: root/contrib/git-push-review
blob: b995fc25a6db3ff2993998be5b046258a0fbe08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# Copyright (C) 2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import argparse
import collections
import os
import subprocess
import sys


def get_config(name):
  args = ['git', 'config', '--get', name]
  p = subprocess.Popen(args, stdout=subprocess.PIPE)
  out, _ = p.communicate()
  ret = p.poll()
  if ret not in (0, 1):
    raise subprocess.CalledProcessError(ret, ' '.join(args), output=out)
  return out.strip()


def deref(name):
  p = subprocess.Popen(
      ['git', 'rev-parse', '--symbolic-full-name', name],
      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, _ = p.communicate()
  return out.strip()


def main(argv):
  p = argparse.ArgumentParser(description='Push changes to Gerrit for review')
  p.add_argument('-r', '--remote', default='', metavar='REMOTE',
                 help='remote name or URL to push to')
  p.add_argument('-b', '--branch', default='', metavar='BRANCH',
                 help='remote branch name, refs/for/BRANCH')
  p.add_argument('args', nargs='*', metavar='REVIEWER_OR_HASHTAG',
                 help='reviewer names or aliases, or #hashtags')
  p.add_argument('-t', '--topic', default='', metavar='TOPIC',
                 help='topic for new changes')
  p.add_argument('-e', '--edit', action='store_true',
                 help='upload as change edit')
  p.add_argument('-w', '--wip', action='store_true', help='upload as WIP')
  p.add_argument('-y', '--ready', action='store_true', help='set ready')
  p.add_argument('--dry-run', action='store_true',
                 help='dry run, print git command and exit')
  args = p.parse_args()

  if not args.remote or not args.branch:
    hp = 'refs/heads/'
    upstream = deref('HEAD')
    while upstream.startswith(hp):
      upstream = deref(upstream[len(hp):] + '@{u}')

    rp = 'refs/remotes/'
    if upstream.startswith(rp):
      def_remote, def_branch = upstream[len(rp):].split('/', 1)
    else:
      def_remote, def_branch = 'origin', 'master'
    args.remote = args.remote or def_remote
    args.branch = args.branch or def_branch


  opts = collections.defaultdict(list)
  is_hashtag = lambda x: x.startswith('#')
  opts['r'].extend(
      (get_config('reviewer.' + r) or r)
      for r in args.args if not is_hashtag(r))
  opts['t'].extend(t[1:] for t in args.args if is_hashtag(t))
  if args.topic:
    opts['topic'].append(args.topic)
  if args.edit:
    opts['edit'].append(True)
  if args.wip:
    opts['wip'].append(True)
  if args.ready:
    opts['ready'].append(True)

  opts_strs = []
  for k in opts:
    for v in opts[k]:
      if v == True:
        opts_strs.append(k)
      elif v != False:
        opts_strs.append('%s=%s' % (k, v))

  opts_str = ','.join(opts_strs)
  if opts_str:
    opts_str = '%' + opts_str

  git_args = ['git', 'push', args.remote,
              'HEAD:refs/for/%s%s' % (args.branch, opts_str)]
  if args.dry_run:
    print(' '.join(git_args))
    return 0
  os.execvp('git', git_args)


if __name__ == '__main__':
  sys.exit(main(sys.argv))