|
184 | 184 | stack-pr config repo.target=main |
185 | 185 | stack-pr config repo.reviewer=user1,user2 |
186 | 186 | """ |
| 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 | +""" |
187 | 201 | UPDATE_STACK_TIP = """ |
188 | 202 | If you'd like to push your local changes first, you can use the following command to update the stack: |
189 | 203 | $ stack-pr export -B {top_commit}~{stack_size} -H {top_commit}""" |
@@ -881,6 +895,51 @@ def from_args(cls, args: argparse.Namespace) -> CommonArgs: |
881 | 895 | ) |
882 | 896 |
|
883 | 897 |
|
| 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 | + |
884 | 943 | def deduce_base(args: CommonArgs) -> CommonArgs: |
885 | 944 | """Deduce the base branch from the head and target branches. |
886 | 945 |
|
@@ -1567,6 +1626,7 @@ def main() -> None: # noqa: PLR0912 |
1567 | 1626 | if args.command != "view" and not is_repo_clean(): |
1568 | 1627 | error(ERROR_REPO_DIRTY) |
1569 | 1628 | return |
| 1629 | + check_target_branch_exists(common_args) |
1570 | 1630 | common_args = deduce_base(common_args) |
1571 | 1631 |
|
1572 | 1632 | if args.command in ["submit", "export"]: |
|
0 commit comments