Skip to content

Commit

Permalink
Support shallow detached repository (#122)
Browse files Browse the repository at this point in the history
* use remote refs instead of branches

* lint and constraint type

* fix name if else
  • Loading branch information
CTY-git authored May 27, 2024
1 parent 1ee5544 commit 5743b88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
36 changes: 25 additions & 11 deletions patchwork/steps/CommitChanges/CommitChanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ def get_slug_from_remote_url(remote_url: str) -> str:
return potential_slug.removesuffix(".git")


@contextlib.contextmanager
def transitioning_branches(
repo: Repo, branch_prefix: str, branch_suffix: str = "", force: bool = True
) -> Generator[tuple[Head, Head], None, None]:
def get_current_branch(repo: Repo) -> Head:
remote = repo.remote("origin")
if repo.head.is_detached:
from_branch = next((branch for branch in repo.branches if branch.commit == repo.head.commit), None)
from_branch = next(
(branch for branch in remote.refs if branch.commit == repo.head.commit and branch.remote_head != "HEAD"),
None,
)
else:
from_branch = repo.active_branch

Expand All @@ -35,16 +36,27 @@ def transitioning_branches(
"Make sure repository is not in a detached HEAD state with additional commits."
)

next_branch_name = f"{branch_prefix}{from_branch.name}{branch_suffix}"
return from_branch


@contextlib.contextmanager
def transitioning_branches(
repo: Repo, branch_prefix: str, branch_suffix: str = "", force: bool = True
) -> Generator[tuple[str, str], None, None]:
from_branch = get_current_branch(repo)
from_branch_name = from_branch.name if not from_branch.is_remote() else from_branch.remote_head
next_branch_name = f"{branch_prefix}{from_branch_name}{branch_suffix}"
if next_branch_name in repo.heads and not force:
raise ValueError(f'Branch "{next_branch_name}" already exists.')
raise ValueError(f'Local Branch "{next_branch_name}" already exists.')
if next_branch_name in repo.remote("origin").refs and not force:
raise ValueError(f'Remote Branch "{next_branch_name}" already exists.')

logger.info(f'Creating new branch "{next_branch_name}".')
to_branch = repo.create_head(next_branch_name, force=force)

try:
to_branch.checkout()
yield from_branch, to_branch
yield from_branch_name, next_branch_name
finally:
from_branch.checkout()

Expand Down Expand Up @@ -137,7 +149,9 @@ def run(self) -> dict:
repo = git.Repo(Path.cwd())
if not self.enabled:
logger.debug("Branch creation is disabled.")
return dict(target_branch=repo.active_branch.name)
from_branch = get_current_branch(repo)
from_branch_name = from_branch.name if not from_branch.is_remote() else from_branch.remote_head
return dict(target_branch=from_branch_name)

modified_files = {modified_code_file["path"] for modified_code_file in self.modified_code_files}

Expand All @@ -153,6 +167,6 @@ def run(self) -> dict:

logger.info(f"Run completed {self.__class__.__name__}")
return dict(
base_branch=from_branch.name,
target_branch=to_branch.name,
base_branch=from_branch,
target_branch=to_branch,
)
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "patchwork-cli"
version = "0.0.10"
version = "0.0.11"
description = ""
authors = ["patched.codes"]
license = "AGPL"
Expand Down

0 comments on commit 5743b88

Please sign in to comment.