Skip to content

Commit 98d150e

Browse files
committed
feat(git): rebase fork on default branch
1 parent fe89275 commit 98d150e

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/conjuring/spells/fork.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from invoke import Context, Exit, task
44

5-
from conjuring.spells.git import Git
5+
from conjuring.spells import git
66

77
SHOULD_PREFIX = True
88

@@ -30,6 +30,6 @@ def remote(c: Context, username: str, remote_: str = "upstream") -> None:
3030
def sync(c: Context, remote_: str = "upstream") -> None:
3131
"""[Sync a fork](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork)."""
3232
c.run(f"git fetch {remote_}")
33-
existing_branch = Git(c).checkout("master", "main")
34-
c.run(f"git merge {remote_}/{existing_branch}")
33+
default_branch = git.set_default_branch(c)
34+
c.run(f"git rebase {remote_}/{default_branch}")
3535
c.run("git push")

src/conjuring/spells/git.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class Git:
5656
# Use "tail +2" to remove the blank line at the top
5757
SHOW_ALL_FILE_HISTORY = 'git log --pretty="format:" --name-only | sort -u | tail +2'
5858

59+
READ_DEFAULT_BRANCH = "git config git-extras.default-branch"
60+
5961
def __init__(self, context: Context) -> None:
6062
self.context = context
6163

@@ -64,11 +66,8 @@ def current_branch(self) -> str:
6466
return run_stdout(self.context, "git branch --show-current")
6567

6668
def default_branch(self) -> str:
67-
"""Return the default branch name (master/main/develop/development)."""
68-
return run_stdout(
69-
self.context,
70-
"git branch -a | rg -o -e /master -e /develop.+ -e /main | sort -u | cut -b 2- | head -1",
71-
)
69+
"""Return the default branch nam as configured in git-extras.default-branch, if available."""
70+
return run_stdout(self.context, Git.READ_DEFAULT_BRANCH, warn=True, dry=False)
7271

7372
def checkout(self, *branches: str) -> str:
7473
"""Try checking out the specified branches in order."""
@@ -322,19 +321,18 @@ def merge_default(
322321

323322
def set_default_branch(c: Context, remote: bool = False) -> str:
324323
"""Set the default branch config on the repo, if not configured yet."""
325-
cmd_read_default_branch = "git config git-extras.default-branch"
326-
default_branch = run_stdout(c, cmd_read_default_branch, warn=True, dry=False)
327-
if not default_branch:
328-
default_branch = run_with_fzf(
324+
branch = Git(c).default_branch()
325+
if not branch:
326+
branch = run_with_fzf(
329327
c,
330328
"git branch --list",
331329
"--all" if remote else "",
332330
"| cut -b 3- | grep -v HEAD | sed -E 's#remotes/[^/]+/##g' | sort -u",
333331
)
334-
run_command(c, cmd_read_default_branch, default_branch)
335-
run_command(c, "git config init.defaultBranch", default_branch)
332+
run_command(c, Git.READ_DEFAULT_BRANCH, branch)
333+
run_command(c, "git config init.defaultBranch", branch)
336334
run_command(c, "git config --list | rg default.*branch")
337-
return default_branch
335+
return branch
338336

339337

340338
@task(

0 commit comments

Comments
 (0)