11#!/usr/bin/env python3
22
3+ import asyncio
34import logging
45import re
6+ from typing import Iterable
57
68import ghstack .github
79import ghstack .github_utils
810import ghstack .shell
911
1012
13+ async def _fetch_refs (
14+ sh : ghstack .shell .Shell , * , remote_name : str , refs : Iterable [str ]
15+ ) -> None :
16+ refspecs = [
17+ f"+refs/heads/{ ref } :refs/remotes/{ remote_name } /{ ref } "
18+ for ref in sorted (set (refs ))
19+ ]
20+ await sh .agit ("fetch" , "--prune" , remote_name , * refspecs )
21+
22+
1123async def main (
1224 pull_request : str ,
1325 github : ghstack .github .GitHubEndpoint ,
@@ -19,7 +31,24 @@ async def main(
1931 params = await ghstack .github_utils .parse_pull_request (
2032 pull_request , sh = sh , remote_name = remote_name
2133 )
22- head_ref = await github .get_head_ref (** params )
34+ head_ref_task = asyncio .ensure_future (github .get_head_ref (** params ))
35+
36+ if same_base :
37+ repo_info_task = asyncio .ensure_future (
38+ ghstack .github_utils .get_github_repo_info (
39+ github = github ,
40+ sh = sh ,
41+ repo_owner = params ["owner" ],
42+ repo_name = params ["name" ],
43+ github_url = params ["github_url" ],
44+ remote_name = remote_name ,
45+ )
46+ )
47+ head_ref , repo_info = await asyncio .gather (head_ref_task , repo_info_task )
48+ else :
49+ head_ref = await head_ref_task
50+ repo_info = None
51+
2352 orig_ref = re .sub (r"/head$" , "/orig" , head_ref )
2453 if orig_ref == head_ref :
2554 logging .warning (
@@ -30,15 +59,7 @@ async def main(
3059
3160 # If --same-base is specified, check if checkout would change the merge-base
3261 if same_base :
33- # Get the default branch name from the repo
34- repo_info = await 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- )
62+ assert repo_info is not None
4263 default_branch = repo_info ["default_branch" ]
4364 default_branch_ref = f"{ remote_name } /{ default_branch } "
4465
@@ -48,7 +69,11 @@ async def main(
4869 current_base = None
4970 default_branch_ref = None
5071
51- await sh .agit ("fetch" , "--prune" , remote_name )
72+ refs_to_fetch = [orig_ref ]
73+ if same_base :
74+ assert repo_info is not None
75+ refs_to_fetch .append (repo_info ["default_branch" ])
76+ await _fetch_refs (sh , remote_name = remote_name , refs = refs_to_fetch )
5277
5378 # If --same-base is specified, check what the new merge-base would be
5479 if same_base :
0 commit comments