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
4 # found in the LICENSE file.
6 """Upload a cherry pick CL to rietveld."""
13 from git_cl import Changelist
14 from git_common import config, run
15 from third_party.upload import EncodeMultipartFormData, GitVCS
16 from rietveld import Rietveld
19 def cherry_pick(target_branch, commit):
20 """Attempt to upload a cherry pick CL to rietveld.
23 target_branch: The branch to cherry pick onto.
24 commit: The git hash of the commit to cherry pick.
26 author = config('user.email')
28 description = '%s\n\n(cherry picked from commit %s)\n' % (
29 run('show', '--pretty=%B', '--quiet', commit), commit)
31 parent = run('show', '--pretty=%P', '--quiet', commit)
32 print 'Found parent revision:', parent
34 class Options(object):
36 self.emulate_svn_auto_props = False
38 content_type, payload = EncodeMultipartFormData([
39 ('base', '%s@%s' % (Changelist().GetRemoteUrl(), target_branch)),
40 ('cc', config('rietveld.cc')),
41 ('content_upload', '1'),
42 ('description', description),
43 ('project', '%s@%s' % (config('rietveld.project'), target_branch)),
44 ('subject', description.splitlines()[0]),
47 ('data', 'data.diff', GitVCS(Options()).PostProcessDiff(
48 run('diff', parent, commit))),
51 rietveld = Rietveld(config('rietveld.server'), author, None)
52 # pylint: disable=W0212
53 output = rietveld._send(
56 content_type=content_type,
59 # If successful, output will look like:
60 # Issue created. URL: https://codereview.chromium.org/1234567890
62 # 10001 some/path/first.file
63 # 10002 some/path/second.file
64 # 10003 some/path/third.file
66 if output[0].startswith('Issue created. URL: '):
68 issue = output[0].rsplit('/', 1)[-1]
73 file_id, filename = f.split()
77 content = run('show', '%s:%s' % (parent, filename))
78 except subprocess2.CalledProcessError:
79 # File didn't exist in the parent revision.
83 content_type, payload = EncodeMultipartFormData([
84 ('checksum', md5.md5(content).hexdigest()),
85 ('filename', filename),
86 ('is_current', 'False'),
89 ('data', filename, content),
92 # pylint: disable=W0212
93 print ' Uploading base file for %s:' % filename, rietveld._send(
94 '/%s/upload_content/%s/%s' % (issue, patchset, file_id),
96 content_type=content_type,
100 content = run('show', '%s:%s' % (commit, filename))
101 except subprocess2.CalledProcessError:
102 # File no longer exists in the new commit.
106 content_type, payload = EncodeMultipartFormData([
107 ('checksum', md5.md5(content).hexdigest()),
108 ('filename', filename),
109 ('is_current', 'True'),
112 ('data', filename, content),
115 # pylint: disable=W0212
116 print ' Uploading %s:' % filename, rietveld._send(
117 '/%s/upload_content/%s/%s' % (issue, patchset, file_id),
119 content_type=content_type,
122 # pylint: disable=W0212
123 print 'Finalizing upload:', rietveld._send('/%s/upload_complete/1' % issue)
127 parser = argparse.ArgumentParser()
131 help='The upstream branch to cherry pick to.',
137 help='SHA to cherry pick.',
140 args = parser.parse_args()
141 cherry_pick(args.branch, args.commit)
143 if __name__ == '__main__':