MCPcopy Create free account
hub / github.com/libgit2/pygit2 / test_checkout_aborted_from_callbacks

Function test_checkout_aborted_from_callbacks

test/test_repository.py:165–209  ·  view source on GitHub ↗
(testrepo: Repository)

Source from the content-addressed store, hash-verified

163
164
165def test_checkout_aborted_from_callbacks(testrepo: Repository) -> None:
166 ref_i18n = testrepo.lookup_reference('refs/heads/i18n')
167
168 def read_bye_txt() -> bytes:
169 blob = testrepo[testrepo.create_blob_fromworkdir('bye.txt')]
170 assert isinstance(blob, Blob)
171 return blob.data
172
173 s = testrepo.status()
174 assert s == {'bye.txt': FileStatus.WT_NEW}
175
176 class MyCheckoutCallbacks(pygit2.CheckoutCallbacks):
177 def __init__(self) -> None:
178 super().__init__()
179 self.invoked_times = 0
180
181 def checkout_notify(
182 self,
183 why: CheckoutNotify,
184 path: str,
185 baseline: Optional[DiffFile],
186 target: Optional[DiffFile],
187 workdir: Optional[DiffFile],
188 ) -> None:
189 self.invoked_times += 1
190 # skip one file so we're certain that NO files are affected,
191 # even if aborting the checkout from the second file
192 if self.invoked_times == 2:
193 raise InterruptedError('Stop the checkout!')
194
195 head_object = testrepo.head
196 head = testrepo[head_object.target]
197 assert 'new' not in head.tree
198 assert b'bye world\n' == read_bye_txt()
199 callbacks = MyCheckoutCallbacks()
200
201 # checkout i18n with GIT_CHECKOUT_FORCE - callbacks should prevent checkout from completing
202 with pytest.raises(InterruptedError):
203 testrepo.checkout(
204 ref_i18n, strategy=CheckoutStrategy.FORCE, callbacks=callbacks
205 )
206
207 assert callbacks.invoked_times == 2
208 assert 'new' not in head.tree
209 assert b'bye world\n' == read_bye_txt()
210
211
212def test_checkout_branch(testrepo: Repository) -> None:

Callers

nothing calls this directly

Calls 4

read_bye_txtFunction · 0.85
MyCheckoutCallbacksClass · 0.85
statusMethod · 0.80
checkoutMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…