Repository: timvink/mkdocs-git-authors-plugin

Issue:
ZeroDivisionError
I'm starting a new mkdocs project and build is failing because of this line:

https://github.com/timvink/mkdocs-git-authors-plugin/blob/bee54c4ea89fc447ae2f4973cd28327617e35c6c/mkdocs_git_authors_plugin/git/author.py#L64

I still do not know why it's failing, so I put the full trace below and here is my mkdocs.yml:

```yml
# Project information
site_name: New project
site_description: Documentation
site_author: Guts

# advanced options
docs_dir: '.'
site_dir: '../build/mkdocs/site'

# Plugins
plugins:
  - awesome-pages
  - git-authors
  - git-revision-date-localized
  - minify:
      minify_html: true
  - search:
      lang: fr
      prebuild_index: python

# Theme
theme:
  name: 'material'
  feature:
    tabs: true
  font: false
  language: 'fr'
  palette:
    primary: 'blue-grey'
    accent: 'deep-orange'

# Customization
extra:
  manifest: 'manifest.webmanifest'
  version: 1.0

# Extensions to enhance markdown - see: https://squidfunk.github.io/mkdocs-material/getting-started/#extensions
markdown_extensions:
  - admonition  # https://squidfunk.github.io/mkdocs-material/extensions/admonition/
  - codehilite: # https://squidfunk.github.io/mkdocs-material/extensions/codehilite/
      linenums: true
  - meta        # https://squidfunk.github.io/mkdocs-material/extensions/metadata/
  - toc:
      permalink: true # https://squidfunk.github.io/mkdocs-material/extensions/permalinks/

# Navigation - Menu organization
nav:
  - Accueil: index.md
  - Installation:
    - installation/requirements.md
    - installation/install.md
    - installation/configuration.md
  - Usage:
    - usage/main.md
    - usage/subcommands.md
    - usage/version.md
    - usage/check.md
    - usage/listing.md
    - usage/sign.md
    - usage/lookup.md
    - usage/sync.md
    - usage/clean.md
  - Automatiser:
    - schedule/scheduling_windows.md
```

## Trace

```python
ERROR   -  Error building page 'index.md': division by zero 
Traceback (most recent call last):
  File "C:\Users\uzeur\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\uzeur\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\uzeur\Documents\GitHub\Isogeo\scan-offline\.venv\Scripts\mkdocs.exe\__main__.py", line 7, in <module>
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\click\core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\__main__.py", line 143, in serve_command     
    **kwargs
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\commands\serve.py", line 141, in serve       
    config = builder()
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\commands\serve.py", line 136, in builder     
    build(config, live_server=live_server, dirty=dirty)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\commands\build.py", line 295, in build       
    _build_page(file.page, config, files, nav, env, dirty)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\commands\build.py", line 209, in _build_page 
    'page_context', context, page=page, config=config, nav=nav
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs\plugins.py", line 94, in run_event
    result = method(item, **kwargs)
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs_git_authors_plugin\plugin.py", line 183, in on_page_context
    "site_authors": util.page_authors(self.repo().get_authors(), path),
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs_git_authors_plugin\util.py", line 134, in page_authors
    for author in authors
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs_git_authors_plugin\util.py", line 134, in <listcomp>
    for author in authors
  File "d:\uzeur\documents\github\isogeo\scan-offline\.venv\lib\site-packages\mkdocs_git_authors_plugin\git\author.py", line 64, in contribution
    result = lines / total_lines
ZeroDivisionError: division by zero
```

Discussion:

Patch:
diff --git a/mkdocs_git_authors_plugin/git/author.py b/mkdocs_git_authors_plugin/git/author.py
index 0168e1b..e1e74da 100644
--- a/mkdocs_git_authors_plugin/git/author.py
+++ b/mkdocs_git_authors_plugin/git/author.py
@@ -61,7 +61,13 @@ class Author(AbstractRepoObject):
         total_lines = (
             self.page(path)["page"].total_lines() if path else self.repo().total_lines()
         )
-        result = lines / total_lines
+
+        # Some pages are empty, that case contribution is 0 by default
+        if total_lines == 0:
+            result = 0
+        else:
+            result = lines / total_lines
+
         if _type == float:
             return result
         else:
diff --git a/mkdocs_git_authors_plugin/git/page.py b/mkdocs_git_authors_plugin/git/page.py
index c69a0f0..11a298a 100644
--- a/mkdocs_git_authors_plugin/git/page.py
+++ b/mkdocs_git_authors_plugin/git/page.py
@@ -16,7 +16,7 @@ class Page(AbstractRepoObject):
 
     def __init__(self, repo: Repo, path: Path):
         """
-        Instantiante a Page object
+        Instantiate a Page object
 
         Args:
             repo: Reference to the global Repo instance
@@ -125,8 +125,14 @@ class Page(AbstractRepoObject):
         cmd = GitCommand("blame", ["--porcelain", str(self._path)])
         cmd.run()
 
+        lines = cmd.stdout()
+
+        # in case of empty, non-committed files, raise error
+        if len(lines) == 0:
+            raise GitCommandError
+
         commit_data = {}
-        for line in cmd.stdout():
+        for line in lines:
             key = line.split(" ")[0]
             m = re_sha.match(key)
             if m:


Test patch:
diff --git a/tests/basic_setup/docs/empty_page.md b/tests/basic_setup/docs/empty_page.md
new file mode 100644
index 0000000..e69de29
diff --git a/tests/basic_setup/mkdocs_w_contribution.yml b/tests/basic_setup/mkdocs_w_contribution.yml
new file mode 100644
index 0000000..284ae58
--- /dev/null
+++ b/tests/basic_setup/mkdocs_w_contribution.yml
@@ -0,0 +1,9 @@
+site_name: test gitauthors_plugin
+use_directory_urls: true
+
+plugins:
+    - search
+    - git-authors:
+        count_empty_lines: false
+        show_contribution: true
+        show_line_count: true
\ No newline at end of file
diff --git a/tests/test_basic.py b/tests/test_basic.py
index b1b6613..f53bdb6 100644
--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -1,6 +1,27 @@
+"""
+Note that pytest offers a `tmp_path`. 
+You can reproduce locally with
+
+```python
+%load_ext autoreload
+%autoreload 2
+import os
+import tempfile
+import shutil
+from pathlib import Path
+tmp_path = Path(tempfile.gettempdir()) / 'pytest-retrieve-authors'
+if os.path.exists(tmp_path):
+    shutil.rmtree(tmp_path)
+os.mkdir(tmp_path)
+```
+"""
+
 import re
+import shutil
+import os
 from click.testing import CliRunner
 from mkdocs.__main__ import build_command
+import git as gitpython
 
 
 def build_docs_setup(mkdocs_path, output_path):
@@ -13,10 +34,46 @@ def build_docs_setup(mkdocs_path, output_path):
 def test_basic_working(tmp_path):
 
     result = build_docs_setup("tests/basic_setup/mkdocs.yml", tmp_path)
-    assert result.exit_code == 0, "'mkdocs build' command failed"
+    assert result.exit_code == 0, (
+        "'mkdocs build' command failed. Error: %s" % result.stdout
+    )
 
     index_file = tmp_path / "index.html"
     assert index_file.exists(), "%s does not exist" % index_file
 
     contents = index_file.read_text()
     assert re.search("<span class='git-page-authors", contents)
+
+
+def test_project_with_no_commits(tmp_path):
+    """
+    Structure:
+    
+    tmp_path/testproject
+    website/
+        ├── docs/
+        └── mkdocs.yml"""
+    testproject_path = tmp_path / "testproject"
+
+    shutil.copytree(
+        "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
+    )
+    shutil.copyfile(
+        "tests/basic_setup/mkdocs_w_contribution.yml",
+        str(testproject_path / "website" / "mkdocs.yml"),
+    )
+
+    cwd = os.getcwd()
+    os.chdir(str(testproject_path))
+
+    # run 'git init'
+    gitpython.Repo.init(testproject_path, bare=False)
+
+    result = build_docs_setup(
+        str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
+    )
+    assert result.exit_code == 0, (
+        "'mkdocs build' command failed. Error: %s" % result.stdout
+    )
+
+    os.chdir(cwd)