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

Function test_stash_apply_checkout_options

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

Source from the content-addressed store, hash-verified

544
545
546def test_stash_apply_checkout_options(testrepo: Repository) -> None:
547 sig = pygit2.Signature(
548 name='Stasher', email='stasher@example.com', time=1641000000, offset=0
549 )
550
551 hello_txt = Path(testrepo.workdir) / 'hello.txt'
552
553 # some changes to working dir
554 with hello_txt.open('w') as f:
555 f.write('stashed content')
556
557 # create the stash
558 testrepo.stash(sig, include_untracked=True, message='custom stash message')
559
560 # define callbacks that raise an InterruptedError when checkout detects a conflict
561 class MyStashApplyCallbacks(pygit2.StashApplyCallbacks):
562 def checkout_notify(
563 self,
564 why: CheckoutNotify,
565 path: str,
566 baseline: Optional[DiffFile],
567 target: Optional[DiffFile],
568 workdir: Optional[DiffFile],
569 ) -> None:
570 if why == CheckoutNotify.CONFLICT:
571 raise InterruptedError('Applying the stash would create a conflict')
572
573 # overwrite hello.txt so that applying the stash would create a conflict
574 with hello_txt.open('w') as f:
575 f.write('conflicting content')
576
577 # apply the stash with the default (safe) strategy;
578 # the callbacks should detect a conflict on checkout
579 with pytest.raises(InterruptedError):
580 testrepo.stash_apply(
581 strategy=CheckoutStrategy.SAFE, callbacks=MyStashApplyCallbacks()
582 )
583
584 # hello.txt should be intact
585 with hello_txt.open('r') as f:
586 assert f.read() == 'conflicting content'
587
588 # force apply the stash; this should work
589 testrepo.stash_apply(
590 strategy=CheckoutStrategy.FORCE, callbacks=MyStashApplyCallbacks()
591 )
592 with hello_txt.open('r') as f:
593 assert f.read() == 'stashed content'
594
595
596def test_revert_commit(testrepo: Repository) -> None:

Callers

nothing calls this directly

Calls 6

openMethod · 0.80
stashMethod · 0.80
stash_applyMethod · 0.80
writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…