# 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

def staged_files(repository):
    staged = porcelain.status(repository.dulwich_repo).staged
    return staged['modify'] + staged['add']


def mock_git_interface(add=tuple()):
    git = MagicMock()
    git.status.return_value = GitStatus(dict(add=add, modify=[], delete=[]), [], [])
    return git


with description(DulwichAnkiRepo) as self:
    with context('init'):
        with it("should use existing repo if it's present"):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)
                dulwich_repo = porcelain.init(repo_path)

                assert_repo_exists_at_given_path_after_init(repo_path)

        with it('should create a new repo if it does not exist'):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)

                assert_repo_exists_at_given_path_after_init(repo_path)

    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:

# 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


def staged_files(repository):
    staged = porcelain.status(repository.dulwich_repo).staged
    return staged['modify'] + staged['add']


def mock_git_interface(add=tuple()):
    git = MagicMock()
    git.status.return_value = GitStatus(dict(add=add, modify=[], delete=[]), [], [])
    return git


with description(DulwichAnkiRepo) as self:
    with context('init'):
        with it("should use existing repo if it's present"):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)
                dulwich_repo = porcelain.init(repo_path)

                assert_repo_exists_at_given_path_after_init(repo_path)

        with it('should create a new repo if it does not exist'):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)

                assert_repo_exists_at_given_path_after_init(repo_path)

    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:

                repo, file = repo_with_new_file(dir_name)