From c903128eb70b762cec00e34f5cbf5eb694baca11 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 8 Oct 2025 01:59:23 +0000 Subject: [PATCH] upgrade-zulip-from-git: Provide better error message on a bad refname. --- scripts/lib/upgrade-zulip-from-git | 44 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/scripts/lib/upgrade-zulip-from-git b/scripts/lib/upgrade-zulip-from-git index 31d9169398..8540315e5e 100755 --- a/scripts/lib/upgrade-zulip-from-git +++ b/scripts/lib/upgrade-zulip-from-git @@ -148,28 +148,34 @@ try: ) # Generate the deployment directory via git worktree from our local repository. - try: - fullref = f"refs/tags/{refname}" - commit_hash = subprocess.check_output( - ["git", "rev-parse", "--verify", fullref], + try_refs = [ + f"refs/tags/{refname}", + f"refs/heads/{refname}" if args.local_ref else f"refs/remotes/origin/{refname}", + ] + commit_hash: str | None = None + fullref: str | None = None + for ref in try_refs: + result = subprocess.run( + ["git", "rev-parse", "--verify", ref], preexec_fn=su_to_zulip, text=True, stderr=subprocess.DEVNULL, - ).strip() - except subprocess.CalledProcessError as e: - if e.returncode == 128: - # Try in the origin namespace, or local heads if --local-ref - if args.local_ref: - fullref = f"refs/heads/{refname}" - else: - fullref = f"refs/remotes/origin/{refname}" - commit_hash = subprocess.check_output( - ["git", "rev-parse", "--verify", fullref], - preexec_fn=su_to_zulip, - text=True, - stderr=subprocess.DEVNULL, - ).strip() - refname = fullref + check=False, + ) + if result.returncode == 0: + fullref = ref + commit_hash = result.stdout.strip() + break + elif result.returncode != 128: + result.check_returncode() + if not commit_hash or not fullref: + logging.error( + "Failed to resolve %s as a tag or %s branch name!", + refname, + "local" if args.local_ref else "remote", + ) + sys.exit(1) + subprocess.check_call( ["git", "worktree", "add", "--detach", deploy_path, refname], stdout=subprocess.DEVNULL,