def test_commit(self):
        with TemporaryDirectory() as path:
            repo = Repository(path)
            repo.init()
            repo.commit_all('commit1')
            self.assertNoCommits(repo)

            with open(os.path.join(path, 'test_commit'), 'w') as f:
                f.write('foo')

            repo.commit_all('commit2')
            self.assertHead(repo, message='commit2')
    def test_remove_history(self):
        with TemporaryDirectory() as path:
            repo = Repository(path)
            repo.init()

            remove = self._random_commits(5, repo, 'to be removed')  # we're going to remove these 'old' commits
            keep = self._random_commits(1, repo, 'anchor to be kept')  # as sync anchor, the last 'old' commit is kept
            cutoff = datetime.now()
            time.sleep(1)
            keep += self._random_commits(5, repo, 'to be kept')  # we're going to keep these 'new' commits

            self.assertHasCommits(repo, remove + keep)

            repo.remove_history(before=cutoff)

            self.assertHasCommits(repo, keep)
            self.assertHasNotCommits(repo, remove)
 def _random_commit(self, repo: Repository, message=''):
     with open(os.path.join(repo.path, self._random_string(16)), 'w') as f:
         f.write(self._random_string(500))
     repo.commit_all(message)
     return repo.get_head()[0]
 def assertNoCommits(self, repo: Repository):
     head = repo.get_head()
     self.assertEqual(head, (None, None), f'Expected that repository has no commits, but HEAD was {head}.')
 def assertHasNotCommit(self, repo: Repository, commit_id):
     self.assertIsNone(
         repo.get_commit(commit_id)[0], f'Expected repository to not have commit {commit_id}, but it had.'
     )
 def test_init(self):
     with TemporaryDirectory() as path:
         repo = Repository(path)
         repo.init()
         self.assertGit(path)