Skip to content

Commit 931af6d

Browse files
Add branch_exists as an action
1 parent f5278fb commit 931af6d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

markten/actions/__git.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,41 @@
1818
DEFAULT_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 ''} "

markten/actions/git.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
44
Actions associated with `git` and Git repos.
55
"""
6+
67
from . import __gitlab as gitlab
7-
from .__git import add, checkout, clone, commit, current_branch, pull, push
8+
from .__git import (
9+
add,
10+
branch_exists,
11+
checkout,
12+
clone,
13+
commit,
14+
current_branch,
15+
pull,
16+
push,
17+
)
818

919
__all__ = [
1020
"add",
21+
"branch_exists",
1122
"checkout",
1223
"clone",
1324
"commit",

0 commit comments

Comments
 (0)