Skip to content

Commit

Permalink
Fix merge and remote settings being incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
Grub4K committed Oct 9, 2024
1 parent 4f657f0 commit 744a062
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
4 changes: 3 additions & 1 deletion git_pr_helper/actions/action_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def run(console: rich.console.Console, args: argparse.Namespace):
remote = f"git@github.com:{remote}.git"

name = f"pr/{pr_number}"
git("switch", "-c", name, "--track", f"{pr_remote}/{name}")
git("switch", "-c", name, head_hash)
git("config", "--null", f"branch.{name}.remote", pr_remote)
git("config", "--null", f"branch.{name}.merge", f"refs/pull/{pr_number}/head")
write_pr_branch_info(name, PrBranchInfo(remote, branch, []))

if args.prune:
Expand Down
10 changes: 5 additions & 5 deletions git_pr_helper/actions/action_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def run(console: rich.console.Console, args: argparse.Namespace):
)
for item in items:
head, commit_full, commit, local, remote = item.split(" ")
pr_branch_info = pr_branch_infos.get(local)
if not pr_branch_info:
continue
ahead, _, behind = git(
"rev-list",
"--left-right",
"--count",
f"{commit_full}...{remote}",
)[0].partition("\t")
pr_branch_info = pr_branch_infos.get(local)
if not pr_branch_info:
continue
branches[local] = BranchInfo(
head == "1",
commit,
Expand Down Expand Up @@ -100,8 +100,8 @@ def run(console: rich.console.Console, args: argparse.Namespace):
table.add_column("branch", style=styles.ACCENT)
table.add_column("note", style="bright_white")

# TODO(Grub4K): take this from the config actually
default_remote = "upstream"
# TODO(Grub4K): allow specifying this manually
default_remote = "origin"

for branch in branches.values():
local = rich.text.Text(branch.local)
Expand Down
31 changes: 20 additions & 11 deletions git_pr_helper/actions/action_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,34 @@ def configure_parser(parser: argparse.ArgumentParser):
parser.add_argument(dest="remaining", nargs=argparse.REMAINDER)


def parse_ref_line(line: str):
head_ref, _, current_remote_ref = line.partition(" ")
current_branch = head_ref.removeprefix("refs/heads/")
current_remote, _, remote_ref = current_remote_ref.removeprefix(
"refs/remotes/"
).partition("/")
remote_ref = remote_ref.partition("/")[2]

return current_branch, current_remote, remote_ref


def run(console: rich.console.Console, args: argparse.Namespace):
config = dict(read_pr_branch_infos())
current_hash = git("rev-list", "-n", "1", "HEAD")[0]
head_ref, _, current_remote_ref = git(
lines = git(
"for-each-ref",
"--format=%(refname) %(upstream)",
"--points-at",
current_hash,
"refs/heads/**",
)[0].partition(" ")
current_branch = head_ref.removeprefix("refs/heads/")
current_remote, _, remote_ref = (
current_remote_ref.partition("/")[2].partition("/")[2].partition("/")
)
remote_ref = remote_ref.partition("/")[2]

config = dict(read_pr_branch_infos())
pr_branch_info = config.get(current_branch)
if not pr_branch_info:
for line in lines:
current_branch, current_remote, remote_ref = parse_ref_line(line)
pr_branch_info = config.get(current_branch)
if pr_branch_info:
break
else:
console.print(
rich.text.Text.assemble(
("error", styles.ERROR),
Expand All @@ -51,7 +61,6 @@ def run(console: rich.console.Console, args: argparse.Namespace):

# TODO(Grub4K): lazy if already up to date
git("push", *args.remaining, pr_branch_info.remote, f"HEAD:{pr_branch_info.branch}")

ref_spec = f"refs/pull/{remote_ref}/head:pr/{remote_ref}"
git("fetch", "--all", "--update-head-ok", current_remote, ref_spec)
git("fetch", "--update-head-ok", current_remote, ref_spec)
return 0

0 comments on commit 744a062

Please sign in to comment.