Skip to content

Commit 7cd1ec8

Browse files
authored
Add squash-merging flag to transactions, default to False (#313)
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.
1 parent 08b4d3a commit 7cd1ec8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/lakefs_spec/transaction.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self, fs: "LakeFSFileSystem"):
5252
self.base_branch: Branch | None = None
5353
self.automerge: bool = False
5454
self.delete: Literal["onsuccess", "always", "never"] = "onsuccess"
55+
self.squash: bool = False
5556
self._ephemeral_branch: Branch | None = None
5657

5758
def __call__(
@@ -61,6 +62,7 @@ def __call__(
6162
branch_name: str | None = None,
6263
automerge: bool = True,
6364
delete: Literal["onsuccess", "always", "never"] = "onsuccess",
65+
squash: bool = False,
6466
) -> "LakeFSTransaction":
6567
"""
6668
Creates an ephemeral branch, conducts all uploads and operations on that branch,
@@ -83,6 +85,8 @@ def __call__(
8385
or failure.
8486
8587
If ``"never"``, the transaction branch is always left in the repository.
88+
squash: bool
89+
Optionally squash-merges the transaction branch into the base branch.
8690
"""
8791

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

103107
self.automerge = automerge
104108
self.delete = delete
109+
self.squash = squash
105110

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

137142
if success and self.automerge:
138143
if any(self.base_branch.diff(self._ephemeral_branch)):
139-
self._ephemeral_branch.merge_into(self.base_branch)
144+
self._ephemeral_branch.merge_into(self.base_branch, squash_merge=self.squash)
140145
if self.delete == "always" or (success and self.delete == "onsuccess"):
141146
self._ephemeral_branch.delete()
142147

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

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

173-
def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
178+
def merge(self, source_ref: str | Branch, into: str | Branch, squash: bool = False) -> Commit:
174179
"""
175180
Merge a branch into another branch in a repository.
176181
@@ -184,6 +189,8 @@ def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
184189
Can be a branch name or partial commit SHA.
185190
into: str | Branch
186191
Target branch into which the changes will be merged.
192+
squash: bool
193+
Optionally squash-merges the source reference into the target branch.
187194
188195
Returns
189196
-------
@@ -194,7 +201,7 @@ def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
194201
dest = _ensurebranch(into, self.repository, self.fs.client)
195202

196203
if any(dest.diff(source)):
197-
source.merge_into(dest)
204+
source.merge_into(dest, squash_merge=squash)
198205
return dest.head.get_commit()
199206

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

0 commit comments

Comments
 (0)