OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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. |
| 5 |
| 6 """Upload a cherry pick CL to rietveld.""" |
| 7 |
| 8 import argparse |
| 9 import md5 |
| 10 import subprocess2 |
| 11 import sys |
| 12 |
| 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 |
| 17 |
| 18 |
| 19 def cherry_pick(target_branch, commit): |
| 20 """Attempt to upload a cherry pick CL to rietveld. |
| 21 |
| 22 Args: |
| 23 target_branch: The branch to cherry pick onto. |
| 24 commit: The git hash of the commit to cherry pick. |
| 25 """ |
| 26 author = config('user.email') |
| 27 |
| 28 description = '%s\n\n(cherry picked from commit %s)\n' % ( |
| 29 run('show', '--pretty=%B', '--quiet', commit), commit) |
| 30 |
| 31 parent = run('show', '--pretty=%P', '--quiet', commit) |
| 32 print 'Found parent revision:', parent |
| 33 |
| 34 class Options(object): |
| 35 def __init__(self): |
| 36 self.emulate_svn_auto_props = False |
| 37 |
| 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]), |
| 45 ('user', author), |
| 46 ], [ |
| 47 ('data', 'data.diff', GitVCS(Options()).PostProcessDiff( |
| 48 run('diff', parent, commit))), |
| 49 ]) |
| 50 |
| 51 rietveld = Rietveld(config('rietveld.server'), author, None) |
| 52 # pylint: disable=W0212 |
| 53 output = rietveld._send( |
| 54 '/upload', |
| 55 payload=payload, |
| 56 content_type=content_type, |
| 57 ).splitlines() |
| 58 |
| 59 # If successful, output will look like: |
| 60 # Issue created. URL: https://codereview.chromium.org/1234567890 |
| 61 # 1 |
| 62 # 10001 some/path/first.file |
| 63 # 10002 some/path/second.file |
| 64 # 10003 some/path/third.file |
| 65 |
| 66 if output[0].startswith('Issue created. URL: '): |
| 67 print output[0] |
| 68 issue = output[0].rsplit('/', 1)[-1] |
| 69 patchset = output[1] |
| 70 files = output[2:] |
| 71 |
| 72 for f in files: |
| 73 file_id, filename = f.split() |
| 74 mode = 'M' |
| 75 |
| 76 try: |
| 77 content = run('show', '%s:%s' % (parent, filename)) |
| 78 except subprocess2.CalledProcessError: |
| 79 # File didn't exist in the parent revision. |
| 80 content = '' |
| 81 mode = 'A' |
| 82 |
| 83 content_type, payload = EncodeMultipartFormData([ |
| 84 ('checksum', md5.md5(content).hexdigest()), |
| 85 ('filename', filename), |
| 86 ('is_current', 'False'), |
| 87 ('status', mode), |
| 88 ], [ |
| 89 ('data', filename, content), |
| 90 ]) |
| 91 |
| 92 # pylint: disable=W0212 |
| 93 print ' Uploading base file for %s:' % filename, rietveld._send( |
| 94 '/%s/upload_content/%s/%s' % (issue, patchset, file_id), |
| 95 payload=payload, |
| 96 content_type=content_type, |
| 97 ) |
| 98 |
| 99 try: |
| 100 content = run('show', '%s:%s' % (commit, filename)) |
| 101 except subprocess2.CalledProcessError: |
| 102 # File no longer exists in the new commit. |
| 103 content = '' |
| 104 mode = 'D' |
| 105 |
| 106 content_type, payload = EncodeMultipartFormData([ |
| 107 ('checksum', md5.md5(content).hexdigest()), |
| 108 ('filename', filename), |
| 109 ('is_current', 'True'), |
| 110 ('status', mode), |
| 111 ], [ |
| 112 ('data', filename, content), |
| 113 ]) |
| 114 |
| 115 # pylint: disable=W0212 |
| 116 print ' Uploading %s:' % filename, rietveld._send( |
| 117 '/%s/upload_content/%s/%s' % (issue, patchset, file_id), |
| 118 payload=payload, |
| 119 content_type=content_type, |
| 120 ) |
| 121 |
| 122 # pylint: disable=W0212 |
| 123 print 'Finalizing upload:', rietveld._send('/%s/upload_complete/1' % issue) |
| 124 |
| 125 |
| 126 def main(): |
| 127 parser = argparse.ArgumentParser() |
| 128 parser.add_argument( |
| 129 '--branch', |
| 130 '-b', |
| 131 help='The upstream branch to cherry pick to.', |
| 132 metavar='<branch>', |
| 133 required=True, |
| 134 ) |
| 135 parser.add_argument( |
| 136 'commit', |
| 137 help='SHA to cherry pick.', |
| 138 metavar='<commit>', |
| 139 ) |
| 140 args = parser.parse_args() |
| 141 cherry_pick(args.branch, args.commit) |
| 142 |
| 143 if __name__ == '__main__': |
| 144 sys.exit(main()) |
OLD | NEW |