@@ -13,6 +13,7 @@ def main(
1313 github : ghstack .github .GitHubEndpoint ,
1414 sh : ghstack .shell .Shell ,
1515 remote_name : str ,
16+ same_base : bool = False ,
1617) -> None :
1718
1819 params = ghstack .github_utils .parse_pull_request (
@@ -27,5 +28,37 @@ def main(
2728
2829 # TODO: Handle remotes correctly too (so this subsumes hub)
2930
31+ # If --same-base is specified, check if checkout would change the merge-base
32+ if same_base :
33+ # Get the default branch name from the repo
34+ repo_info = ghstack .github_utils .get_github_repo_info (
35+ github = github ,
36+ sh = sh ,
37+ repo_owner = params ["owner" ],
38+ repo_name = params ["name" ],
39+ github_url = params ["github_url" ],
40+ remote_name = remote_name ,
41+ )
42+ default_branch = repo_info ["default_branch" ]
43+ default_branch_ref = f"{ remote_name } /{ default_branch } "
44+
45+ # Get current merge-base with default branch
46+ current_base = sh .git ("merge-base" , default_branch_ref , "HEAD" )
47+ else :
48+ current_base = None
49+ default_branch_ref = None
50+
3051 sh .git ("fetch" , "--prune" , remote_name )
52+
53+ # If --same-base is specified, check what the new merge-base would be
54+ if same_base :
55+ target_ref = remote_name + "/" + orig_ref
56+ new_base = sh .git ("merge-base" , default_branch_ref , target_ref )
57+
58+ if current_base != new_base :
59+ raise RuntimeError (
60+ f"Checkout would change merge-base from { current_base [:8 ]} to { new_base [:8 ]} , "
61+ f"aborting due to --same-base flag"
62+ )
63+
3164 sh .git ("checkout" , remote_name + "/" + orig_ref )
0 commit comments