Skip to content

Commit b19118e

Browse files
committed
Return created commits on merge and revert in transaction
This seems like the more natural thing to do. Adjusts the tests to expect the new behavior. Also, since the merge logic only optionally creates a new commit on the destination branch, we can unconditionally return the destination HEAD in `LakeFSTransaction.merge()`.
1 parent 2b8b645 commit b19118e

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/lakefs_spec/transaction.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def commit(self, message: str, metadata: dict[str, str] | None = None) -> Refere
149149

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

152-
def merge(self, source_ref: str | Branch, into: str | Branch) -> str:
152+
def merge(self, source_ref: str | Branch, into: str | Branch) -> Commit:
153153
"""
154154
Merge a branch into another branch in a repository.
155155
@@ -166,17 +166,17 @@ def merge(self, source_ref: str | Branch, into: str | Branch) -> str:
166166
167167
Returns
168168
-------
169-
str
170-
The ID of either the created merge commit, or the tip of the target branch.
169+
Commit
170+
Either the created merge commit, or the head commit of the target branch.
171171
"""
172172
source = ensurebranch(source_ref, self.repository, self.fs.client)
173173
dest = ensurebranch(into, self.repository, self.fs.client)
174174

175175
if any(dest.diff(source)):
176-
return source.merge_into(dest)
177-
return dest.head.get_commit().id
176+
source.merge_into(dest)
177+
return dest.head.get_commit()
178178

179-
def revert(self, branch: str | Branch, ref: ReferenceType, parent_number: int = 1) -> None:
179+
def revert(self, branch: str | Branch, ref: ReferenceType, parent_number: int = 1) -> Commit:
180180
"""
181181
Revert a previous commit on a branch.
182182
@@ -190,13 +190,18 @@ def revert(self, branch: str | Branch, ref: ReferenceType, parent_number: int =
190190
If there are multiple parents to a commit, specify to which parent
191191
the commit should be reverted. ``parent_number = 1`` (the default)
192192
refers to the first parent commit of the current ``branch`` tip.
193+
194+
Returns
195+
-------
196+
Commit
197+
The created revert commit.
193198
"""
194199

195200
b = ensurebranch(branch, self.repository, self.fs.client)
196201

197202
ref_id = ref if isinstance(ref, str) else ref.id
198203
b.revert(ref_id, parent_number=parent_number)
199-
return None
204+
return b.head.get_commit()
200205

201206
def rev_parse(self, ref: ReferenceType) -> Commit:
202207
"""

tests/test_transactions.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,16 @@ def test_transaction_revert(
8585
random_file = random_file_factory.make()
8686

8787
lpath = str(random_file)
88-
rpath = f"{repository.id}/{temp_branch.id}/{random_file.name}"
89-
9088
message = f"Add file {random_file.name}"
9189

92-
with fs.transaction(repository, temp_branch) as tx:
90+
with fs.transaction(repository, temp_branch, automerge=True) as tx:
9391
fs.put_file(lpath, f"{repository.id}/{tx.branch.id}/{random_file.name}")
9492
tx.commit(message=message)
95-
tx.revert(temp_branch, temp_branch.head)
93+
revert_commit = tx.revert(temp_branch, temp_branch.head)
9694

97-
head, head_tilde = list(temp_branch.log(max_amount=2))
98-
assert head.message.startswith("Merge")
99-
assert head_tilde.message.startswith("Revert")
95+
# first commit should be the merge commit
96+
assert temp_branch.head.get_commit().message.startswith("Merge")
97+
assert revert_commit.message.startswith("Revert")
10098

10199

102100
def test_transaction_failure(
@@ -109,7 +107,6 @@ def test_transaction_failure(
109107

110108
lpath = str(random_file)
111109
rpath = f"{repository.id}/{temp_branch.id}/{random_file.name}"
112-
113110
message = f"Add file {random_file.name}"
114111

115112
try:

0 commit comments

Comments
 (0)