Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow diff to take a branch name as origin using worktree #655

Merged
merged 2 commits into from
May 10, 2024

Conversation

Alexsaphir
Copy link
Contributor

Following the discussion on discord, I've extended the behaviour of create_worktree to use a branch and not be detached. This allows us to compare two branch, the current and main for example easily.

@codecov-commenter
Copy link

codecov-commenter commented Apr 28, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 93.50%. Comparing base (171e3bc) to head (893fc9a).
Report is 2 commits behind head on main.

Files Patch % Lines
flux_local/git_repo.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #655      +/-   ##
==========================================
- Coverage   93.54%   93.50%   -0.04%     
==========================================
  Files          20       20              
  Lines        2122     2125       +3     
==========================================
+ Hits         1985     1987       +2     
- Misses        137      138       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@allenporter allenporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, i have a few small change requests and this looks good to get in. I don't have any easy testing ideas, since they would all involve creating branches which seems not worth it. Manual testing is fine.

@@ -213,8 +222,8 @@ def add_diff_flags(args: ArgumentParser) -> None:

@contextmanager
def create_diff_path(
selector: git_repo.PathSelector,
**kwargs: Any,
selector: git_repo.PathSelector,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove whitespace changes? This is not the same format used by ruff so consider running the pre-commit also.

Also revert the other vertical whitespace changes if you don't mind.

Copy link
Contributor Author

@Alexsaphir Alexsaphir May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running pre-commit seems to apply changes to things not modified by myself. I've removed all the space and spacing that I added.

@@ -225,9 +234,10 @@ def create_diff_path(
yield git_repo.PathSelector(path_orig, sources=kwargs.get("sources"))
return

with git_repo.create_worktree(selector.repo) as worktree:
yield git_repo.PathSelector(pathlib.Path(worktree) / selector.relative_path)
branch_orig = kwargs.get('branch_orig', None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The None is not needed here as it is already the default value so prefer to write without it. While it makes it more explicit, this is a common enough paradigm that it its OK to be implicit.

Suggested change
branch_orig = kwargs.get('branch_orig', None)
branch_orig = kwargs.get('branch_orig')

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider inlining the whole thing in the create_worktree branch if it can fit since its a trivial expression and the variable isn't used again.

"""Create a ContextManager for a new git worktree in the current repo.

This is used to get a fork of the current repo without any local changes
in order to produce a diff.
"""
orig = os.getcwd()

with tempfile.TemporaryDirectory() as tmp_dir:
_LOGGER.debug("Creating worktree in %s", tmp_dir)
# Add --detach to avoid creating a branch since we will not make modifications
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Push this comment inside the "branch is None" case

@@ -772,20 +772,26 @@ async def update_kustomization(cluster: Cluster) -> None:


@contextlib.contextmanager
def create_worktree(repo: git.repo.Repo) -> Generator[Path, None, None]:
def create_worktree(repo: git.repo.Repo, branch: str | None = None) -> Generator[Path, None, None]:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment in the pydoc that branch is an existing branch? (or variable could be named existing_branch). I just want to clarify here that it is not creating a new branch since much of the code below is about avoiding creating a new branch, implying that it may be creating a branch when passed in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the argument to existing_branch like you suggested and added a comment.

@Alexsaphir Alexsaphir reopened this May 8, 2024
@allenporter allenporter merged commit 6db2bfc into allenporter:main May 10, 2024
26 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants