1818DEFAULT_REMOTE = "origin"
1919
2020
21- async def branch_exists_on_remote (
21+ @markten_action
22+ async def branch_exists (
2223 action : ActionSession ,
2324 dir : Path ,
2425 branch : str ,
25- remote : str = DEFAULT_REMOTE ,
26+ remote : str | bool = False ,
2627) -> bool :
2728 """
2829 Return whether the given branch exists on the remote
2930
3031 Requires `git fetch` to have been run beforehand
32+
33+ Parameters
34+ ----------
35+ dir : Path
36+ Path to git repo
37+ branch : str
38+ Name of the branch to check for
39+ remote : str | bool
40+ If `remote` is specified, branches on that remote will be searched.
41+ Otherwise, only local branches will be checked
3142 """
3243 remote_branches = await process .stdout_of (
33- action , "git" , "-C" , str (dir ), "branch" , "--remote"
44+ action ,
45+ "git" ,
46+ "-C" ,
47+ str (dir ),
48+ "branch" ,
49+ * ("--remote" if remote else ()),
3450 )
35- regex = re .compile (rf"^\s*{ remote } /{ branch } $" )
51+ if remote is False :
52+ regex = re .compile (rf"^\*?\s+{ branch } $" )
53+ else :
54+ remote_name = DEFAULT_REMOTE if remote is True else remote
55+ regex = re .compile (rf"^\*?\s+{ remote_name } /{ branch } $" )
3656
3757 for remote_branch in remote_branches .splitlines ():
3858 if regex .search (remote_branch .strip ()) is not None :
@@ -64,7 +84,7 @@ async def clone(
6484 Branch to checkout after cloning is complete, by default None
6585 fallback_to_main : bool, optional
6686 Whether to fall back to the main branch if the given `branch` does
67- not exist, by default False, meaning that the action will fail if the
87+ not exist, by default False, meaning that the action will fail if the
6888 branch does not given.
6989 dir : Path | None, optional
7090 Directory to clone to, by default None for a temporary directory
@@ -82,7 +102,7 @@ async def clone(
82102 _ = await process .run (action , * program )
83103
84104 if branch :
85- if await action .child (branch_exists_on_remote , clone_path , branch ):
105+ if await action .child (branch_exists , clone_path , branch , remote = True ):
86106 checkout_action = action .make_child (checkout )
87107 try :
88108 await checkout (
@@ -230,7 +250,7 @@ async def checkout(
230250 if link_upstream is not False :
231251 remote = DEFAULT_REMOTE if link_upstream is True else link_upstream
232252 already_exists = await action .child (
233- branch_exists_on_remote ,
253+ branch_exists ,
234254 dir ,
235255 branch_name ,
236256 remote ,
@@ -248,7 +268,6 @@ async def checkout(
248268 await action .child (pull , dir )
249269 else :
250270 _ = await action .child (push , dir , set_upstream = True )
251-
252271
253272 action .succeed (
254273 f"Switched to{ ' new' if create else '' } "
0 commit comments