Skip to content

Commit

Permalink
Add squash-merging flag to transactions, default to False (#313)
Browse files Browse the repository at this point in the history
lakeFS v1.48.0 added squash-merging, which, as the name suggests, merges
a source branch into a target branch by squashing the additional history
into a single commit (which is also actually the merge commit in lakeFS).

The interesting thing is that it enables squashing _by default_, which changes
the user-facing behavior of our (merge-based) transactions, and also all
other merges done via `tx.merge()`.

This broke some of our transaction tests, which try to assert correct behavior
by counting commits since branch creation, and verify by commit message.

This commit fixes the tests by introducing a new `squash` flag to the transaction
class, which controls squash-merging of the transaction branch and any other
branches/refs merged via `tx.merge()`.

Interestingly, no special logic is needed to backport the extra keyword argument
to `Reference.merge_into()` - I suspect that this is some pydantic compatibility
detail.
  • Loading branch information
nicholasjng authored Jan 9, 2025
1 parent 08b4d3a commit 7cd1ec8
Showing 1 changed file with 10 additions and 3 deletions.
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

0 comments on commit 7cd1ec8

Please sign in to comment.