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

Add squash-merging flag to transactions #313

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/lakefs_spec/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, fs: "LakeFSFileSystem"):
self.base_branch: Branch | None = None
self.automerge: bool = False
self.delete: Literal["onsuccess", "always", "never"] = "onsuccess"
self.squash: bool = False
self._ephemeral_branch: Branch | None = None

def __call__(
Expand All @@ -61,6 +62,7 @@ def __call__(
branch_name: str | None = None,
automerge: bool = True,
delete: Literal["onsuccess", "always", "never"] = "onsuccess",
squash: bool = False,
) -> "LakeFSTransaction":
"""
Creates an ephemeral branch, conducts all uploads and operations on that branch,
Expand All @@ -83,6 +85,8 @@ def __call__(
or failure.

If ``"never"``, the transaction branch is always left in the repository.
squash: bool
Optionally squash-merges the transaction branch into the base branch.
"""

if isinstance(repository, str):
Expand All @@ -102,6 +106,7 @@ def __call__(

self.automerge = automerge
self.delete = delete
self.squash = squash

ephem_name = branch_name or "transaction-" + "".join(random.choices(string.digits, k=6)) # nosec: B311
self._ephemeral_branch = Branch(self.repository, ephem_name, client=self.fs.client)
Expand Down Expand Up @@ -136,7 +141,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):

if success and self.automerge:
if any(self.base_branch.diff(self._ephemeral_branch)):
self._ephemeral_branch.merge_into(self.base_branch)
self._ephemeral_branch.merge_into(self.base_branch, squash_merge=self.squash)
if self.delete == "always" or (success and self.delete == "onsuccess"):
self._ephemeral_branch.delete()

Expand Down Expand Up @@ -170,7 +175,7 @@ def commit(self, message: str, metadata: dict[str, str] | None = None) -> Refere

return self.branch.commit(message, metadata=metadata)

def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
def merge(self, source_ref: str | Branch, into: str | Branch, squash: bool = False) -> Commit:
"""
Merge a branch into another branch in a repository.

Expand All @@ -184,6 +189,8 @@ def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
Can be a branch name or partial commit SHA.
into: str | Branch
Target branch into which the changes will be merged.
squash: bool
Optionally squash-merges the source reference into the target branch.

Returns
-------
Expand All @@ -194,7 +201,7 @@ def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
dest = _ensurebranch(into, self.repository, self.fs.client)

if any(dest.diff(source)):
source.merge_into(dest)
source.merge_into(dest, squash_merge=squash)
return dest.head.get_commit()

def revert(self, branch: str | Branch, ref: ReferenceType, parent_number: int = 1) -> Commit:
Expand Down
Loading