# Path: crowd_anki/history/dulwich_repo.py
# class DulwichAnkiRepo(AnkiRepo):
#     repo_path: Path
#     git: Any = porcelain
#     dulwich_repo: Repo = field(init=False)
# 
#     def __post_init__(self):
#         path_string = str(self.repo_path.resolve())
#         try:
#             self.dulwich_repo = self.git.init(path_string)
#         except FileExistsError:
#             logger.info(f"Using existing repository at the following path: {self.repo_path}")
#             self.dulwich_repo = Repo(path_string)
# 
#     def stage_all(self):
#         status = self.status()
#         self.dulwich_repo.stage(status.untracked + status.unstaged)
# 
#     def commit(self, message: str = None):
#         if self.there_are_staged_changes():
#             self.git.commit(self.dulwich_repo, message=message or str(self.status()))
# 
#     def there_are_staged_changes(self):
#         return bool(list(chain(*self.status().staged.values())))
# 
#     def status(self) -> GitStatus:
#         return self.git.status(self.dulwich_repo)
# 
#     def close(self):
#         self.dulwich_repo.close()


from tempfile import TemporaryDirectory
from dulwich import porcelain
from dulwich.porcelain import GitStatus
from expects import expect, equal, contain
from expects.matchers.built_in import not_, be_none
from mamba import description, it, context
from pathlib import Path
from unittest.mock import MagicMock
from crowd_anki.history.dulwich_repo import DulwichAnkiRepo
    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                expect(staged_files(repo)).to(contain(str(file.name).encode()))

        with it("should add modified files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                repo.commit()
                file.write_text("data")
                repo.stage_all()

                expect(staged_files(repo)).to(contain(str(file.name).encode()))

    with context('commit'):
        with it('performs no commit if there are no changes'):
            with TemporaryDirectory() as dir_name:
                git_mock = mock_git_interface()

                DulwichAnkiRepo(Path(dir_name), git_mock).commit()

                git_mock.commit.assert_not_called()

        with it('performs commit when new files are added'):
            with TemporaryDirectory() as dir_name:
                git_mock = mock_git_interface(('new_file',))


# Path: crowd_anki/history/dulwich_repo.py
# class DulwichAnkiRepo(AnkiRepo):
#     repo_path: Path
#     git: Any = porcelain
#     dulwich_repo: Repo = field(init=False)
# 
#     def __post_init__(self):
#         path_string = str(self.repo_path.resolve())
#         try:
#             self.dulwich_repo = self.git.init(path_string)
#         except FileExistsError:
#             logger.info(f"Using existing repository at the following path: {self.repo_path}")
#             self.dulwich_repo = Repo(path_string)
# 
#     def stage_all(self):
#         status = self.status()
#         self.dulwich_repo.stage(status.untracked + status.unstaged)
# 
#     def commit(self, message: str = None):
#         if self.there_are_staged_changes():
#             self.git.commit(self.dulwich_repo, message=message or str(self.status()))
# 
#     def there_are_staged_changes(self):
#         return bool(list(chain(*self.status().staged.values())))
# 
#     def status(self) -> GitStatus:
#         return self.git.status(self.dulwich_repo)
# 
#     def close(self):
#         self.dulwich_repo.close()


# Path: test/history/dulwich_repo_spec.py
from tempfile import TemporaryDirectory
from dulwich import porcelain
from dulwich.porcelain import GitStatus
from expects import expect, equal, contain
from expects.matchers.built_in import not_, be_none
from mamba import description, it, context
from pathlib import Path
from unittest.mock import MagicMock
from crowd_anki.history.dulwich_repo import DulwichAnkiRepo

    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                expect(staged_files(repo)).to(contain(str(file.name).encode()))

        with it("should add modified files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                repo.commit()
                file.write_text("data")
                repo.stage_all()

                expect(staged_files(repo)).to(contain(str(file.name).encode()))

    with context('commit'):
        with it('performs no commit if there are no changes'):
            with TemporaryDirectory() as dir_name:
                git_mock = mock_git_interface()

                DulwichAnkiRepo(Path(dir_name), git_mock).commit()

                git_mock.commit.assert_not_called()

        with it('performs commit when new files are added'):
            with TemporaryDirectory() as dir_name:
                git_mock = mock_git_interface(('new_file',))


                DulwichAnkiRepo(Path(dir_name), git_mock).commit()