From 96edb9b573d6c58b1990090c467ce0c2809bc0b1 Mon Sep 17 00:00:00 2001 From: Sergiy Belozorov Date: Mon, 4 Mar 2024 19:48:52 +0100 Subject: upload: Add support for setting patchset description Bug: 308467447 Change-Id: I7abcbc98131b826120fc9ab85d5b889f90db4b0a Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/355968 Tested-by: Sergiy Belozorov Reviewed-by: Mike Frysinger Commit-Queue: Sergiy Belozorov --- project.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'project.py') diff --git a/project.py b/project.py index 1821c354..40ca116d 100644 --- a/project.py +++ b/project.py @@ -21,6 +21,7 @@ import random import re import shutil import stat +import string import subprocess import sys import tarfile @@ -266,6 +267,7 @@ class ReviewableBranch: dest_branch=None, validate_certs=True, push_options=None, + patchset_description=None, ): self.project.UploadForReview( branch=self.name, @@ -281,6 +283,7 @@ class ReviewableBranch: dest_branch=dest_branch, validate_certs=validate_certs, push_options=push_options, + patchset_description=patchset_description, ) def GetPublishedRefs(self): @@ -1089,6 +1092,7 @@ class Project: dest_branch=None, validate_certs=True, push_options=None, + patchset_description=None, ): """Uploads the named branch for code review.""" if branch is None: @@ -1171,6 +1175,10 @@ class Project: opts += ["wip"] if ready: opts += ["ready"] + if patchset_description: + opts += [ + f"m={self._encode_patchset_description(patchset_description)}" + ] if opts: ref_spec = ref_spec + "%" + ",".join(opts) cmd.append(ref_spec) @@ -1183,6 +1191,30 @@ class Project: R_PUB + branch.name, R_HEADS + branch.name, message=msg ) + @staticmethod + def _encode_patchset_description(original): + """Applies percent-encoding for strings sent as patchset description. + + The encoding used is based on but stricter than URL encoding (Section + 2.1 of RFC 3986). The only non-escaped characters are alphanumerics, and + 'SPACE' (U+0020) can be represented as 'LOW LINE' (U+005F) or + 'PLUS SIGN' (U+002B). + + For more information, see the Gerrit docs here: + https://gerrit-review.googlesource.com/Documentation/user-upload.html#patch_set_description + """ + SAFE = {ord(x) for x in string.ascii_letters + string.digits} + + def _enc(b): + if b in SAFE: + return chr(b) + elif b == ord(" "): + return "_" + else: + return f"%{b:02x}" + + return "".join(_enc(x) for x in original.encode("utf-8")) + def _ExtractArchive(self, tarpath, path=None): """Extract the given tar on its current location -- cgit v1.2.3-54-g00ecf