Skip to content

Commit 5388018

Browse files
authored
misc: fallback to base branch in ci.py set-branch (#81)
1 parent 8508bb8 commit 5388018

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

scripts/ci.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def has_remote_branch(repo_dir, branch_name):
6060

6161
def update_repo(repo_dir, branch_name):
6262
"""
63-
Attempt to update the repository to the given branch name
63+
Attempt to update the repository to the given branch name, if it exists.
64+
Otherwise tries the branch name set by `GITHUB_BASE_REF`, if it exists.
6465
"""
6566
branch_name = branch_name.removeprefix(GIT_ORIGIN_REFIX)
6667
curr_branch = get_current_branch(repo_dir)
@@ -69,14 +70,18 @@ def update_repo(repo_dir, branch_name):
6970
vprint("branches match already, nothing to do")
7071
return
7172

72-
has_branch = has_remote_branch(repo_dir, branch_name)
73-
if has_branch:
74-
vprint(f"repo has target branch `{branch_name}`: {has_branch}...updating")
73+
base_branch = os.environ.get("GITHUB_BASE_REF")
74+
75+
if has_remote_branch(repo_dir, branch_name):
76+
vprint(f"repo has target branch {branch_name}...updating")
7577
pout = shell(f"git switch {branch_name}", cwd=repo_dir)
7678
vprint(pout.stdout.decode("utf-8"))
79+
elif (base_branch != curr_branch) and has_remote_branch(repo_dir, base_branch):
80+
vprint(f"repo does not have target branch {branch_name}, but has base branch {base_branch}...updating")
81+
pout = shell(f"git switch {base_branch}", cwd=repo_dir)
82+
vprint(pout.stdout.decode("utf-8"))
7783
else:
78-
vprint(f"repo does not have target branch `{branch_name}`, leaving at {curr_branch}")
79-
84+
vprint(f"repo does not have target branch {branch_name} nor base_branch {base_branch}, leaving at {curr_branch}")
8085

8186
def _get_branch_cmd(opts):
8287
branch = "main"

0 commit comments

Comments
 (0)