Skip to content

Commit bd13837

Browse files
committed
Check if the target branch exists.
And handle a special case when the target is 'master' while our default is 'main' - give a more user-friendly error message in that case. stack-info: PR: #100, branch: ZolotukhinM/stack/1
1 parent 8c368d8 commit bd13837

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

src/stack_pr/cli.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@
184184
stack-pr config repo.target=main
185185
stack-pr config repo.reviewer=user1,user2
186186
"""
187+
ERROR_TARGET_BRANCH_MASTER_INSTEAD_OF_MAIN = """Could not find target branch '{remote}/{target}'.
188+
189+
It looks like your repository uses '{remote}/master' instead of '{remote}/main'.
190+
191+
You can fix this by specifying the target branch:
192+
stack-pr view --target=master
193+
194+
Or set it permanently in your config file:
195+
stack-pr config repo.target=master
196+
"""
197+
ERROR_TARGET_BRANCH_MISSING = """Could not find target branch '{remote}/{target}'.
198+
199+
Make sure the branch exists or specify a different target with --target option.
200+
"""
187201
UPDATE_STACK_TIP = """
188202
If you'd like to push your local changes first, you can use the following command to update the stack:
189203
$ stack-pr export -B {top_commit}~{stack_size} -H {top_commit}"""
@@ -881,6 +895,51 @@ def from_args(cls, args: argparse.Namespace) -> CommonArgs:
881895
)
882896

883897

898+
def check_target_branch_exists(args: CommonArgs) -> None:
899+
"""Check that the target branch exists on the remote.
900+
901+
Args:
902+
args: CommonArgs containing remote and target branch information
903+
904+
Raises:
905+
SystemExit: If the target branch doesn't exist
906+
"""
907+
# Check if target branch exists using git rev-parse --verify
908+
# This is fast and doesn't require listing all branches
909+
result = run_shell_command(
910+
["git", "rev-parse", "--verify", f"{args.remote}/{args.target}"],
911+
quiet=True,
912+
check=False,
913+
)
914+
915+
if result.returncode == 0:
916+
# Target branch exists, all good
917+
return
918+
919+
# Target branch doesn't exist
920+
# Check if this is the common case where repo uses 'master' instead of 'main'
921+
if args.target == "main":
922+
master_result = run_shell_command(
923+
["git", "rev-parse", "--verify", f"{args.remote}/master"],
924+
quiet=True,
925+
check=False,
926+
)
927+
if master_result.returncode == 0:
928+
# Master exists, show helpful error
929+
error(ERROR_TARGET_BRANCH_MASTER_INSTEAD_OF_MAIN.format(
930+
remote=args.remote,
931+
target=args.target,
932+
))
933+
sys.exit(1)
934+
935+
# Generic error for other cases
936+
error(ERROR_TARGET_BRANCH_MISSING.format(
937+
remote=args.remote,
938+
target=args.target,
939+
))
940+
sys.exit(1)
941+
942+
884943
def deduce_base(args: CommonArgs) -> CommonArgs:
885944
"""Deduce the base branch from the head and target branches.
886945
@@ -1567,6 +1626,7 @@ def main() -> None: # noqa: PLR0912
15671626
if args.command != "view" and not is_repo_clean():
15681627
error(ERROR_REPO_DIRTY)
15691628
return
1629+
check_target_branch_exists(common_args)
15701630
common_args = deduce_base(common_args)
15711631

15721632
if args.command in ["submit", "export"]:

0 commit comments

Comments
 (0)