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

Function test_checkout_callbacks

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

Source from the content-addressed store, hash-verified

109
110
111def test_checkout_callbacks(testrepo: Repository) -> None:
112 ref_i18n = testrepo.lookup_reference('refs/heads/i18n')
113
114 class MyCheckoutCallbacks(pygit2.CheckoutCallbacks):
115 def __init__(self) -> None:
116 super().__init__()
117 self.conflicting_paths: set[str] = set()
118 self.updated_paths: set[str] = set()
119 self.completed_steps = -1
120 self.total_steps = -1
121
122 def checkout_notify_flags(self) -> CheckoutNotify:
123 return CheckoutNotify.CONFLICT | CheckoutNotify.UPDATED
124
125 def checkout_notify(
126 self,
127 why: CheckoutNotify,
128 path: str,
129 baseline: Optional[DiffFile],
130 target: Optional[DiffFile],
131 workdir: Optional[DiffFile],
132 ) -> None:
133 if why == CheckoutNotify.CONFLICT:
134 self.conflicting_paths.add(path)
135 elif why == CheckoutNotify.UPDATED:
136 self.updated_paths.add(path)
137
138 def checkout_progress(
139 self, path: str, completed_steps: int, total_steps: int
140 ) -> None:
141 self.completed_steps = completed_steps
142 self.total_steps = total_steps
143
144 # checkout i18n with conflicts and default strategy should not be possible
145 callbacks = MyCheckoutCallbacks()
146 with pytest.raises(pygit2.GitError):
147 testrepo.checkout(ref_i18n, callbacks=callbacks)
148 # make sure the callbacks caught that
149 assert {'bye.txt'} == callbacks.conflicting_paths
150 assert -1 == callbacks.completed_steps # shouldn't have done anything
151
152 # checkout i18n with GIT_CHECKOUT_FORCE
153 head_object = testrepo.head
154 head = testrepo[head_object.target]
155 assert 'new' not in head.tree
156 callbacks = MyCheckoutCallbacks()
157 testrepo.checkout(ref_i18n, strategy=CheckoutStrategy.FORCE, callbacks=callbacks)
158 # make sure the callbacks caught the files affected by the checkout
159 assert set() == callbacks.conflicting_paths
160 assert {'bye.txt', 'new'} == callbacks.updated_paths
161 assert callbacks.completed_steps > 0
162 assert callbacks.completed_steps == callbacks.total_steps
163
164
165def test_checkout_aborted_from_callbacks(testrepo: Repository) -> None:

Callers

nothing calls this directly

Calls 2

MyCheckoutCallbacksClass · 0.85
checkoutMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…