diff --git a/revup/revup.py b/revup/revup.py index da91dea..098e210 100755 --- a/revup/revup.py +++ b/revup/revup.py @@ -272,6 +272,9 @@ async def main() -> int: "--user-aliases", ) upload_parser.add_argument("--uploader") + upload_parser.add_argument( + "--branch_format", choices=["user+branch", "user", "branch", "none"], default="user+branch" + ) upload_parser.add_argument("--pre-upload", "-p") upload_parser.add_argument("--relative-chain", "-c", action="store_true") upload_parser.add_argument("--auto-topic", "-a", action="store_true") diff --git a/revup/topic_stack.py b/revup/topic_stack.py index 889c722..c97d4b9 100644 --- a/revup/topic_stack.py +++ b/revup/topic_stack.py @@ -28,14 +28,23 @@ # https://docs.github.com/en/get-started/using-git/dealing-with-special-characters-in-branch-and-tag-names RE_BRANCH_ALLOWED = re.compile(r"^[\w\d_\.\/-]+$") +BRANCH_FORMAT_STRATEGIES = { + "user+branch": "{uploader}/revup/{base_branch}/{topic}", + "user": "{uploader}/revup/{topic}", + "branch": "revup/{base_branch}/{topic}", + "none": "revup/{topic}", +} + -def format_remote_branch(uploader: str, base_branch: str, topic: str) -> str: +def format_remote_branch(uploader: str, base_branch: str, topic: str, branch_format: str) -> str: """ Branches are named so that it is clear that they are made by revup and can be force pushed at any time, and to minimize collision with manually created branches. """ - return f"{uploader}/revup/{base_branch}/{topic}" + return BRANCH_FORMAT_STRATEGIES[branch_format].format( + uploader=uploader, base_branch=base_branch, topic=topic + ) RE_TAGS = re.compile(r"^(?P[a-zA-Z\-]+):(?P.*)$", re.MULTILINE) @@ -49,6 +58,7 @@ def format_remote_branch(uploader: str, base_branch: str, topic: str) -> str: TAG_RELATIVE_BRANCH = "relative-branch" TAG_UPLOADER = "uploader" TAG_UPDATE_PR_BODY = "update-pr-body" +TAG_BRANCH_FORMAT = "branch-format" VALID_TAGS = { TAG_BRANCH, TAG_LABEL, @@ -59,6 +69,7 @@ def format_remote_branch(uploader: str, base_branch: str, topic: str) -> str: TAG_TOPIC, TAG_UPLOADER, TAG_UPDATE_PR_BODY, + TAG_BRANCH_FORMAT, } RE_COMMIT_LABEL = re.compile(r"^(?P[a-zA-Z\-_0-9]+):.*|^\[(?P[a-zA-Z\-_0-9]+)\].*") @@ -463,6 +474,7 @@ async def populate_reviews( auto_add_users: str = "", self_authored_only: bool = False, limit_topics: Optional[List[str]] = None, + branch_format: str = "", ) -> None: """ Populate reviews for already-parsed topics. Verify base branch and relative topic info to @@ -505,6 +517,15 @@ async def populate_reviews( f"Invalid tags for update-pr-body: {topic.tags[TAG_UPDATE_PR_BODY]}" ) + if TAG_BRANCH_FORMAT in topic.tags: + if ( + len(topic.tags[TAG_BRANCH_FORMAT]) > 1 + or min(topic.tags[TAG_BRANCH_FORMAT]).lower() not in BRANCH_FORMAT_STRATEGIES + ): + raise RevupUsageException( + f"Invalid tags for branch-format: {topic.tags[TAG_BRANCH_FORMAT]}" + ) + relative_topic = "" if force_relative_chain and last_topic is not None: relative_topic = last_topic @@ -574,9 +595,9 @@ async def populate_reviews( if name not in self.topics: logging.warning(f"Couldn't find any topic named {name}") - await self.populate_relative_reviews(uploader) + await self.populate_relative_reviews(uploader, branch_format) - async def populate_relative_reviews(self, uploader: str) -> None: + async def populate_relative_reviews(self, uploader: str, branch_format: str) -> None: for name, topic in self.topological_topics(): if topic.relative_topic: if len(topic.tags[TAG_BRANCH]) == 0: @@ -666,7 +687,14 @@ async def populate_relative_reviews(self, uploader: str) -> None: review.base_ref = await self.git_ctx.to_commit_hash(relative_branch) review.remote_base = self.git_ctx.remove_branch_prefix(relative_branch) - review.remote_head = format_remote_branch(topic_uploader, base_branch, name) + topic_branch_format = ( + min(topic.tags[TAG_BRANCH_FORMAT]).lower() + if TAG_BRANCH_FORMAT in topic.tags + else branch_format + ) + review.remote_head = format_remote_branch( + topic_uploader, base_branch, name, topic_branch_format + ) topic.reviews[branch] = review diff --git a/revup/upload.py b/revup/upload.py index a93a7ef..04a7ebe 100644 --- a/revup/upload.py +++ b/revup/upload.py @@ -41,6 +41,7 @@ async def main( auto_add_users=args.auto_add_users, self_authored_only=args.self_authored_only, limit_topics=args.topics, + branch_format=args.branch_format, ) if not args.dry_run: