OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Upload a cherry pick CL to rietveld.""" | 6 """Upload a cherry pick CL to rietveld.""" |
7 | 7 |
8 import argparse | |
9 import md5 | 8 import md5 |
| 9 import optparse |
10 import subprocess2 | 10 import subprocess2 |
11 import sys | 11 import sys |
12 | 12 |
| 13 import auth |
| 14 |
13 from git_cl import Changelist | 15 from git_cl import Changelist |
14 from git_common import config, run | 16 from git_common import config, run |
15 from third_party.upload import EncodeMultipartFormData, GitVCS | 17 from third_party.upload import EncodeMultipartFormData, GitVCS |
16 from rietveld import Rietveld | 18 from rietveld import Rietveld |
17 | 19 |
18 | 20 |
19 def cherry_pick(target_branch, commit): | 21 def cherry_pick(target_branch, commit, auth_config): |
20 """Attempt to upload a cherry pick CL to rietveld. | 22 """Attempt to upload a cherry pick CL to rietveld. |
21 | 23 |
22 Args: | 24 Args: |
23 target_branch: The branch to cherry pick onto. | 25 target_branch: The branch to cherry pick onto. |
24 commit: The git hash of the commit to cherry pick. | 26 commit: The git hash of the commit to cherry pick. |
| 27 auth_config: auth.AuthConfig object with authentication configuration. |
25 """ | 28 """ |
26 author = config('user.email') | 29 author = config('user.email') |
27 | 30 |
28 description = '%s\n\n(cherry picked from commit %s)\n' % ( | 31 description = '%s\n\n(cherry picked from commit %s)\n' % ( |
29 run('show', '--pretty=%B', '--quiet', commit), commit) | 32 run('show', '--pretty=%B', '--quiet', commit), commit) |
30 | 33 |
31 parent = run('show', '--pretty=%P', '--quiet', commit) | 34 parent = run('show', '--pretty=%P', '--quiet', commit) |
32 print 'Found parent revision:', parent | 35 print 'Found parent revision:', parent |
33 | 36 |
34 class Options(object): | 37 class Options(object): |
35 def __init__(self): | 38 def __init__(self): |
36 self.emulate_svn_auto_props = False | 39 self.emulate_svn_auto_props = False |
37 | 40 |
38 content_type, payload = EncodeMultipartFormData([ | 41 content_type, payload = EncodeMultipartFormData([ |
39 ('base', '%s@%s' % (Changelist().GetRemoteUrl(), target_branch)), | 42 ('base', '%s@%s' % (Changelist().GetRemoteUrl(), target_branch)), |
40 ('cc', config('rietveld.cc')), | 43 ('cc', config('rietveld.cc')), |
41 ('content_upload', '1'), | 44 ('content_upload', '1'), |
42 ('description', description), | 45 ('description', description), |
43 ('project', '%s@%s' % (config('rietveld.project'), target_branch)), | 46 ('project', '%s@%s' % (config('rietveld.project'), target_branch)), |
44 ('subject', description.splitlines()[0]), | 47 ('subject', description.splitlines()[0]), |
45 ('user', author), | 48 ('user', author), |
46 ], [ | 49 ], [ |
47 ('data', 'data.diff', GitVCS(Options()).PostProcessDiff( | 50 ('data', 'data.diff', GitVCS(Options()).PostProcessDiff( |
48 run('diff', parent, commit))), | 51 run('diff', parent, commit))), |
49 ]) | 52 ]) |
50 | 53 |
51 rietveld = Rietveld(config('rietveld.server'), author, None) | 54 rietveld = Rietveld(config('rietveld.server'), auth_config, author) |
52 # pylint: disable=W0212 | 55 # pylint: disable=W0212 |
53 output = rietveld._send( | 56 output = rietveld._send( |
54 '/upload', | 57 '/upload', |
55 payload=payload, | 58 payload=payload, |
56 content_type=content_type, | 59 content_type=content_type, |
57 ).splitlines() | 60 ).splitlines() |
58 | 61 |
59 # If successful, output will look like: | 62 # If successful, output will look like: |
60 # Issue created. URL: https://codereview.chromium.org/1234567890 | 63 # Issue created. URL: https://codereview.chromium.org/1234567890 |
61 # 1 | 64 # 1 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 '/%s/upload_content/%s/%s' % (issue, patchset, file_id), | 120 '/%s/upload_content/%s/%s' % (issue, patchset, file_id), |
118 payload=payload, | 121 payload=payload, |
119 content_type=content_type, | 122 content_type=content_type, |
120 ) | 123 ) |
121 | 124 |
122 # pylint: disable=W0212 | 125 # pylint: disable=W0212 |
123 print 'Finalizing upload:', rietveld._send('/%s/upload_complete/1' % issue) | 126 print 'Finalizing upload:', rietveld._send('/%s/upload_complete/1' % issue) |
124 | 127 |
125 | 128 |
126 def main(): | 129 def main(): |
127 parser = argparse.ArgumentParser() | 130 parser = optparse.OptionParser( |
128 parser.add_argument( | 131 usage='usage: %prog --branch <branch> <commit>') |
| 132 parser.add_option( |
129 '--branch', | 133 '--branch', |
130 '-b', | 134 '-b', |
131 help='The upstream branch to cherry pick to.', | 135 help='The upstream branch to cherry pick to.', |
132 metavar='<branch>', | 136 metavar='<branch>') |
133 required=True, | 137 auth.add_auth_options(parser) |
134 ) | 138 options, args = parser.parse_args() |
135 parser.add_argument( | 139 auth_config = auth.extract_auth_config_from_options |
136 'commit', | 140 |
137 help='SHA to cherry pick.', | 141 if not options.branch: |
138 metavar='<commit>', | 142 parser.error('--branch is required') |
139 ) | 143 if len(args) != 1: |
140 args = parser.parse_args() | 144 parser.error('Expecting single argument <commit>') |
141 cherry_pick(args.branch, args.commit) | 145 |
| 146 cherry_pick(options.branch, args[0], auth_config) |
142 return 0 | 147 return 0 |
143 | 148 |
144 | 149 |
145 if __name__ == '__main__': | 150 if __name__ == '__main__': |
146 try: | 151 try: |
147 sys.exit(main()) | 152 sys.exit(main()) |
148 except KeyboardInterrupt: | 153 except KeyboardInterrupt: |
149 sys.stderr.write('interrupted\n') | 154 sys.stderr.write('interrupted\n') |
150 sys.exit(1) | 155 sys.exit(1) |
OLD | NEW |