From cd1e3d409227886fb461debf7e605b60bc34b6fa Mon Sep 17 00:00:00 2001 From: "smut@google.com" Date: Tue, 29 Jul 2014 19:01:13 +0000 Subject: [PATCH] Add git cherry picking extension This extension uploads a fake cherry pick-style diff to rietveld with a modified project parameter. The modified project is intended to be used by the commit queue to attempt to land the change on a branch. This works by grabbing the parent of the targeted revision and generating the diff. It is intended to be used to CQ trivial cherry picks which apply cleanly on top of other branches without conflicts. BUG=387111 Review URL: https://codereview.chromium.org/397593004 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@286273 0039d316-1c4b-4281-b951-d872f2087c98 --- git-cherry-pick-upload | 8 + git_cherry_pick_upload.py | 144 +++ man/html/git-cherry-pick-upload.html | 824 ++++++++++++++++++ man/man1/git-cherry-pick-upload.1 | 89 ++ .../_git-cherry-pick-upload_desc.helper.txt | 1 + man/src/git-cherry-pick-upload.demo.1.sh | 17 + man/src/git-cherry-pick-upload.txt | 35 + 7 files changed, 1118 insertions(+) create mode 100755 git-cherry-pick-upload create mode 100755 git_cherry_pick_upload.py create mode 100644 man/html/git-cherry-pick-upload.html create mode 100644 man/man1/git-cherry-pick-upload.1 create mode 100644 man/src/_git-cherry-pick-upload_desc.helper.txt create mode 100755 man/src/git-cherry-pick-upload.demo.1.sh create mode 100644 man/src/git-cherry-pick-upload.txt diff --git a/git-cherry-pick-upload b/git-cherry-pick-upload new file mode 100755 index 000000000..4ab9b6317 --- /dev/null +++ b/git-cherry-pick-upload @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Copyright 2014 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. + +# git_cherry_pick_upload.py -- Upload a cherry pick CL to rietveld. + +. $(type -P python_git_runner.sh) diff --git a/git_cherry_pick_upload.py b/git_cherry_pick_upload.py new file mode 100755 index 000000000..2a048faae --- /dev/null +++ b/git_cherry_pick_upload.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python +# Copyright 2014 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. + +"""Upload a cherry pick CL to rietveld.""" + +import argparse +import md5 +import subprocess2 +import sys + +from git_cl import Changelist +from git_common import config, run +from third_party.upload import EncodeMultipartFormData, GitVCS +from rietveld import Rietveld + + +def cherry_pick(target_branch, commit): + """Attempt to upload a cherry pick CL to rietveld. + + Args: + target_branch: The branch to cherry pick onto. + commit: The git hash of the commit to cherry pick. + """ + author = config('user.email') + + description = '%s\n\n(cherry picked from commit %s)\n' % ( + run('show', '--pretty=%B', '--quiet', commit), commit) + + parent = run('show', '--pretty=%P', '--quiet', commit) + print 'Found parent revision:', parent + + class Options(object): + def __init__(self): + self.emulate_svn_auto_props = False + + content_type, payload = EncodeMultipartFormData([ + ('base', '%s@%s' % (Changelist().GetRemoteUrl(), target_branch)), + ('cc', config('rietveld.cc')), + ('content_upload', '1'), + ('description', description), + ('project', '%s@%s' % (config('rietveld.project'), target_branch)), + ('subject', description.splitlines()[0]), + ('user', author), + ], [ + ('data', 'data.diff', GitVCS(Options()).PostProcessDiff( + run('diff', parent, commit))), + ]) + + rietveld = Rietveld(config('rietveld.server'), author, None) + # pylint: disable=W0212 + output = rietveld._send( + '/upload', + payload=payload, + content_type=content_type, + ).splitlines() + + # If successful, output will look like: + # Issue created. URL: https://codereview.chromium.org/1234567890 + # 1 + # 10001 some/path/first.file + # 10002 some/path/second.file + # 10003 some/path/third.file + + if output[0].startswith('Issue created. URL: '): + print output[0] + issue = output[0].rsplit('/', 1)[-1] + patchset = output[1] + files = output[2:] + + for f in files: + file_id, filename = f.split() + mode = 'M' + + try: + content = run('show', '%s:%s' % (parent, filename)) + except subprocess2.CalledProcessError: + # File didn't exist in the parent revision. + content = '' + mode = 'A' + + content_type, payload = EncodeMultipartFormData([ + ('checksum', md5.md5(content).hexdigest()), + ('filename', filename), + ('is_current', 'False'), + ('status', mode), + ], [ + ('data', filename, content), + ]) + + # pylint: disable=W0212 + print ' Uploading base file for %s:' % filename, rietveld._send( + '/%s/upload_content/%s/%s' % (issue, patchset, file_id), + payload=payload, + content_type=content_type, + ) + + try: + content = run('show', '%s:%s' % (commit, filename)) + except subprocess2.CalledProcessError: + # File no longer exists in the new commit. + content = '' + mode = 'D' + + content_type, payload = EncodeMultipartFormData([ + ('checksum', md5.md5(content).hexdigest()), + ('filename', filename), + ('is_current', 'True'), + ('status', mode), + ], [ + ('data', filename, content), + ]) + + # pylint: disable=W0212 + print ' Uploading %s:' % filename, rietveld._send( + '/%s/upload_content/%s/%s' % (issue, patchset, file_id), + payload=payload, + content_type=content_type, + ) + + # pylint: disable=W0212 + print 'Finalizing upload:', rietveld._send('/%s/upload_complete/1' % issue) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + '--branch', + '-b', + help='The upstream branch to cherry pick to.', + metavar='', + required=True, + ) + parser.add_argument( + 'commit', + help='SHA to cherry pick.', + metavar='', + ) + args = parser.parse_args() + cherry_pick(args.branch, args.commit) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/man/html/git-cherry-pick-upload.html b/man/html/git-cherry-pick-upload.html new file mode 100644 index 000000000..b212bd410 --- /dev/null +++ b/man/html/git-cherry-pick-upload.html @@ -0,0 +1,824 @@ + + + + + +git-cherry-pick-upload(1) + + + + + +
+
+

SYNOPSIS

+
+
+
git cherry-pick-upload --branch <remote_branch_name> <commit_hash>
+
+
+
+
+
+

DESCRIPTION

+
+

git cherry-pick-upload is used to upload a cherry pick to rietveld. It uses +your view of the remote to generate the diff between a revision and its parent, +and then uploads that diff to rietveld.

+

The commit message is annotated with "(cherry picked from commit [parent sha1])" +as if it were generated by git cherry-pick -x.

+
+
+
+

EXAMPLE

+
+

$ git cherry-pick-upload -h
+usage: git_cherry_pick_upload.py [-h] --branch <branch> <commit>
+
+positional arguments:
+  <commit>              SHA to cherry pick.
+
+optional arguments:
+  -h, --help            show this help message and exit
+  --branch <branch>, -b <branch>
+                        The upstream branch to cherry pick to.
+
+$ git cherry-pick-upload -b my_branch c02b7d24a066adb747fdeb12deb21bfa
+Found parent revision: b96d69fda53845a205151613a9c4cc93
+Loaded authentication cookies from .codereview_upload_cookies
+Issue created. URL: https://codereview.chromium.org/1234567890
+  Uploading base_file for some/path/first.file: OK
+  Uploading some/path/first.file: OK
+  Uploading base_file for some/path/second.file: OK
+  Uploading some/path/second.file: OK
+  Uploading base_file for some/path/third.file: OK
+  Uploading some/path/third.file: OK
+Finalizing upload: OK
+

+
+
+ +
+

CHROMIUM DEPOT_TOOLS

+
+

Part of the chromium depot_tools(7) suite. These tools are meant to +assist with the development of chromium and related projects. Download the tools +from here.

+
+
+
+

+ + + diff --git a/man/man1/git-cherry-pick-upload.1 b/man/man1/git-cherry-pick-upload.1 new file mode 100644 index 000000000..aaf249c91 --- /dev/null +++ b/man/man1/git-cherry-pick-upload.1 @@ -0,0 +1,89 @@ +'\" t +.\" Title: git-cherry-pick-upload +.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author] +.\" Generator: DocBook XSL Stylesheets v1.76.1 +.\" Date: 07/23/2014 +.\" Manual: Chromium depot_tools Manual +.\" Source: depot_tools dff725e +.\" Language: English +.\" +.TH "GIT\-CHERRY-PICK-UPLOAD" "1" "07/23/2014" "depot_tools dff725e" "Chromium depot_tools Manual" +.\" ----------------------------------------------------------------- +.\" * Define some portability stuff +.\" ----------------------------------------------------------------- +.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.\" http://bugs.debian.org/507673 +.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html +.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" ----------------------------------------------------------------- +.\" * set default formatting +.\" ----------------------------------------------------------------- +.\" disable hyphenation +.nh +.\" disable justification (adjust text to left margin only) +.ad l +.\" ----------------------------------------------------------------- +.\" * MAIN CONTENT STARTS HERE * +.\" ----------------------------------------------------------------- +.SH "NAME" +git-cherry-pick-upload \- Upload the diff between a revision and its parent to rietveld\&. +.SH "SYNOPSIS" +.sp +.nf +\fIgit cherry-pick-upload\fR \-\-branch +.fi +.sp +.SH "DESCRIPTION" +.sp +git cherry-pick-upload is used to upload a cherry pick to rietveld\&. It uses your view of the remote to generate the diff between a revision and its parent, and then uploads that diff to rietveld\&. +.sp +The commit message is annotated with "(cherry picked from commit [parent sha1])" as if it were generated by git cherry\-pick \-x\&. +.SH "EXAMPLE" +.sp + +.sp +.if n \{\ +.RS 4 +.\} +.nf +\fB$ git cherry-pick-upload \-h\fR +usage: git_cherry_pick_upload\&.py [\-h] \-\-branch + +positional arguments: + SHA to cherry pick\&. + +optional arguments: + \-h, \-\-help show this help message and exit + \-\-branch , \-b + The upstream branch to cherry pick to\&. + +\fB$ git cherry-pick-upload \-b my_branch c02b7d24a066adb747fdeb12deb21bfa\fR +Found parent revision: b96d69fda53845a205151613a9c4cc93 +Loaded authentication cookies from \&.codereview_upload_cookies +Issue created\&. URL: https://codereview\&.chromium\&.org/1234567890 + Uploading base_file for some/path/first\&.file: OK + Uploading some/path/first\&.file: OK + Uploading base_file for some/path/second\&.file: OK + Uploading some/path/second\&.file: OK + Uploading base_file for some/path/third\&.file: OK + Uploading some/path/third\&.file: OK +Finalizing upload: OK +.fi +.if n \{\ +.RE +.\} +.sp +.SH "SEE ALSO" +.sp +\fBgit-cherry-pick\fR(1) \fBgit-cl-upload\fR(1) +.SH "CHROMIUM DEPOT_TOOLS" +.sp +Part of the chromium \fBdepot_tools\fR(7) suite\&. These tools are meant to assist with the development of chromium and related projects\&. Download the tools from \m[blue]\fBhere\fR\m[]\&\s-2\u[1]\d\s+2\&. +.SH "NOTES" +.IP " 1." 4 +here +.RS 4 +\%https://chromium.googlesource.com/chromium/tools/depot_tools.git +.RE diff --git a/man/src/_git-cherry-pick-upload_desc.helper.txt b/man/src/_git-cherry-pick-upload_desc.helper.txt new file mode 100644 index 000000000..8298fa099 --- /dev/null +++ b/man/src/_git-cherry-pick-upload_desc.helper.txt @@ -0,0 +1 @@ +Upload the diff between a revision and its parent to rietveld. diff --git a/man/src/git-cherry-pick-upload.demo.1.sh b/man/src/git-cherry-pick-upload.demo.1.sh new file mode 100755 index 000000000..d4b395511 --- /dev/null +++ b/man/src/git-cherry-pick-upload.demo.1.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +. demo_repo.sh + +run git cherry-pick-upload -h +echo + +pcommand git cherry-pick-upload -b my_branch c02b7d24a066adb747fdeb12deb21bfa +echo 'Found parent revision: b96d69fda53845a205151613a9c4cc93' +echo 'Loaded authentication cookies from .codereview_upload_cookies' +echo 'Issue created. URL: https://codereview.chromium.org/1234567890' +echo ' Uploading base_file for some/path/first.file: OK' +echo ' Uploading some/path/first.file: OK' +echo ' Uploading base_file for some/path/second.file: OK' +echo ' Uploading some/path/second.file: OK' +echo ' Uploading base_file for some/path/third.file: OK' +echo ' Uploading some/path/third.file: OK' +echo 'Finalizing upload: OK' diff --git a/man/src/git-cherry-pick-upload.txt b/man/src/git-cherry-pick-upload.txt new file mode 100644 index 000000000..88f9b6f7c --- /dev/null +++ b/man/src/git-cherry-pick-upload.txt @@ -0,0 +1,35 @@ +git-cherry-pick-upload(1) +============= + +NAME +---- +git-cherry-pick-upload - +include::_git-cherry-pick-upload_desc.helper.txt[] + +SYNOPSIS +-------- +[verse] +'git cherry-pick-upload' --branch + +DESCRIPTION +----------- + +`git cherry-pick-upload` is used to upload a cherry pick to rietveld. It uses +your view of the remote to generate the diff between a revision and its parent, +and then uploads that diff to rietveld. + +The commit message is annotated with "(cherry picked from commit [parent sha1])" +as if it were generated by `git cherry-pick -x`. + +EXAMPLE +------- +demo:1[] + +SEE ALSO +-------- +linkgit:git-cherry-pick[1] +linkgit:git-cl-upload[1] + +include::_footer.txt[] + +// vim: ft=asciidoc: