def test_empty_repo(self, rw_dir):
        """Assure we can handle empty repositories"""
        r = Repo.init(rw_dir, mkdir=False)
        # It's ok not to be able to iterate a commit, as there is none.
        self.assertRaises(ValueError, r.iter_commits)
        self.assertEqual(r.active_branch.name, "master")
        assert not r.active_branch.is_valid(), "Branch is yet to be born"

        # Actually, when trying to create a new branch without a commit, git itself
        # fails. We should, however, not fail ungracefully.
        self.assertRaises(BadName, r.create_head, "foo")
        self.assertRaises(BadName, r.create_head, "master")
        # It's expected to not be able to access a tree
        self.assertRaises(ValueError, r.tree)

        new_file_path = osp.join(rw_dir, "new_file.ext")
        touch(new_file_path)
        r.index.add([new_file_path])
        r.index.commit("initial commit\nBAD MESSAGE 1\n")

        # Now a branch should be creatable.
        nb = r.create_head("foo")
        assert nb.is_valid()

        with open(new_file_path, "w") as f:
            f.write("Line 1\n")

        r.index.add([new_file_path])
        r.index.commit("add line 1\nBAD MESSAGE 2\n")

        with open("%s/.git/logs/refs/heads/master" % (rw_dir,), "r") as f:
            contents = f.read()

        assert "BAD MESSAGE" not in contents, "log is corrupt"
Assure we can handle empty repositories
test/test_repo.py