Skip to content

Commit

Permalink
Add support to ignore authors and/or commits
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgoncalves authored and timvink committed May 8, 2024
1 parent 0f3f1b5 commit 96fb712
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ plugins:
fallback_to_empty: false
sort_authors_by: name
authorship_threshold_percent: 10
ignore_authors:
- [email protected]
ignore_commits: .git-blame-ignore-revs
exclude:
- index.md
enabled: true
Expand Down Expand Up @@ -59,6 +62,14 @@ If this option is set to `true` (default: `false`) the plugin will work even out

Default is empty. Specify a list of page source paths (one per line) that should not have author(s) included (excluded from processing by this plugin). This can be useful for example to remove the authors from the front page. The source path of a page is relative to your `docs/` folder. You can also use [globs](https://docs.python.org/3/library/glob.html) instead of full source paths. To exclude `docs/subfolder/page.md` specify in your `mkdocs.yml` a line under `exclude:` with `- subfolder/page.md`. Some examples:

## `ignore_authors`

Default is empty. Specifies a list of emails for authors whose contributions should be ignored by this plugin.

## `ignore_commits`

Default is empty. Specifies a file containing a list of commit hashes (one per line) that should be ignored. Changes made in these commits will be attributed to the previous commit that changed the line.

```yaml
# mkdocs.yml
plugins:
Expand Down
11 changes: 9 additions & 2 deletions mkdocs_git_authors_plugin/git/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ def _process_git_blame(self):

re_sha = re.compile(r"^\w{40}")

cmd = GitCommand("blame", ["--porcelain", str(self._path)])
args = []
if self.repo().config("ignore_commits"):
args.append("--ignore-revs-file")
args.append(self.repo().config("ignore_commits"))
args.append("--porcelain")
args.append(str(self._path))
cmd = GitCommand("blame", args)
cmd.run()

lines = cmd.stdout()
Expand All @@ -155,6 +161,7 @@ def _process_git_blame(self):
if len(lines) == 0:
raise GitCommandError

ignore_authors = self.repo().config("ignore_authors")
commit_data = {}
for line in lines:
key = line.split(" ")[0]
Expand Down Expand Up @@ -183,7 +190,7 @@ def _process_git_blame(self):
author_tz=commit_data.get("author-tz"),
summary=commit_data.get("summary"),
)
if len(line) > 1 or self.repo().config("count_empty_lines"):
if commit.author().email() not in ignore_authors and (len(line) > 1 or self.repo().config("count_empty_lines")):
author = commit.author()
if author not in self._authors:
self._authors.append(author)
Expand Down
2 changes: 2 additions & 0 deletions mkdocs_git_authors_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class GitAuthorsPlugin(BasePlugin):
("count_empty_lines", config_options.Type(bool, default=True)),
("fallback_to_empty", config_options.Type(bool, default=False)),
("exclude", config_options.Type(list, default=[])),
("ignore_commits", config_options.Type(str, default=None)),
("ignore_authors", config_options.Type(list, default=[])),
("enabled", config_options.Type(bool, default=True)),
("enabled_on_serve", config_options.Type(bool, default=True)),
("sort_authors_by", config_options.Type(str, default="name")),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="mkdocs-git-authors-plugin",
version="0.8.0",
version="0.9.0",
description="Mkdocs plugin to display git authors of a page",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
8 changes: 8 additions & 0 deletions tests/basic_setup/mkdocs_ignore_authors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
site_name: test gitauthors_plugin
use_directory_urls: true

plugins:
- search
- git-authors:
ignore_authors:
- '[email protected]'
17 changes: 17 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ def test_exclude_working(tmp_path):



def test_ignore_authors_working(tmp_path):

result = build_docs_setup("tests/basic_setup/mkdocs_ignore_authors.yml", tmp_path)
assert result.exit_code == 0, (
"'mkdocs build' command failed. Error: %s" % result.stdout
)

page_file = tmp_path / "page_with_tag/index.html"
assert page_file.exists(), "%s does not exist" % page_file

contents = page_file.read_text()
assert re.search("<span class='git-page-authors", contents)
assert re.search("<a href='mailto:[email protected]'>Tim Vink</a>", contents)
assert not re.search("Julien", contents)



def test_exclude_working_with_genfiles(tmp_path):
"""
A warning for uncommited files should not show up
Expand Down
143 changes: 143 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"sort_reverse": False,
"sort_authors_by": "name",
"authorship_threshold_percent": 0,
"ignore_authors": [],
}

#### Helpers ####
Expand Down Expand Up @@ -275,6 +276,148 @@ def test_retrieve_authors(tmp_path):
os.chdir(cwd)


def test_retrieve_authors_ignoring_commits(tmp_path):
"""
Builds a fake git project with some commits.
Args:
tmp_path (PosixPath): Directory of a tempdir
"""
cwd = os.getcwd()
os.chdir(str(tmp_path))

# Create file
file_name = str(tmp_path / "new-file")
with open(file_name, "w") as the_file:
the_file.write("line 1\n")
the_file.write("line 2\n")

# Create git repo and commit file
r = gitpython.Repo.init(tmp_path)
r.index.add([file_name])
author = gitpython.Actor("Tim", "[email protected]")
r.index.commit("initial commit", author=author)

# Update the file
with open(file_name, "w") as the_file:
the_file.write("line 1.1\n")
the_file.write("line 2.1\n")
r.index.add([file_name])
author = gitpython.Actor("John", "[email protected]")
commit = r.index.commit("second commit", author=author)

repo_instance = repo.Repo()
repo_instance.set_config(DEFAULT_CONFIG)
repo_instance.page(file_name)
authors = repo_instance.get_authors()
authors = util.page_authors(authors, file_name)
authors[0]["last_datetime"] = None

assert authors == [
{
"name": "John",
"email": "[email protected]",
"last_datetime": None,
"lines": 2,
"lines_all_pages": 2,
"contribution": "100.0%",
"contribution_all_pages": "100.0%",
}
]

# Get the authors while ignoring the last commit
ignored_commits_files = str(tmp_path / "ignored_commits.txt")
with open(ignored_commits_files, "w") as the_file:
the_file.write(commit.hexsha + "\n")
repo_instance = repo.Repo()
config = DEFAULT_CONFIG.copy()
config['ignore_commits'] = ignored_commits_files
repo_instance.set_config(config)
repo_instance.page(file_name)
authors = repo_instance.get_authors()
authors = util.page_authors(authors, file_name)
authors[0]["last_datetime"] = None

assert authors == [
{
"name": "Tim",
"email": "[email protected]",
"last_datetime": None,
"lines": 2,
"lines_all_pages": 2,
"contribution": "100.0%",
"contribution_all_pages": "100.0%",
},
]

os.chdir(cwd)


def test_retrieve_authors_ignoring_emails(tmp_path):
"""
Builds a fake git project with some commits.
Args:
tmp_path (PosixPath): Directory of a tempdir
"""
cwd = os.getcwd()
os.chdir(str(tmp_path))

# Create file
file_name = str(tmp_path / "new-file")
with open(file_name, "w") as the_file:
the_file.write("line 1\n")
the_file.write("line 2\n")

# Create git repo and commit file
r = gitpython.Repo.init(tmp_path)
r.index.add([file_name])
author = gitpython.Actor("Tim", "[email protected]")
r.index.commit("initial commit", author=author)

# Add more content
with open(file_name, "a+") as the_file:
the_file.write("line 3\n")
the_file.write("line 4\n")
r.index.add([file_name])
author = gitpython.Actor("John", "[email protected]")
r.index.commit("second commit", author=author)

# Get the authors while ignoring [email protected] user
repo_instance = repo.Repo()
config = DEFAULT_CONFIG.copy()
config['ignore_authors'] = ['[email protected]']
repo_instance.set_config(config)
repo_instance.page(file_name)
authors = repo_instance.get_authors()
authors = util.page_authors(authors, file_name)
authors[0]["last_datetime"] = None
authors[1]["last_datetime"] = None

assert authors == [
{
"contribution": "0.0%",
"contribution_all_pages": "0.0%",
"email": "[email protected]",
"last_datetime": None,
"lines": 0,
"lines_all_pages": 0,
"name": "John"
},
{
"name": "Tim",
"email": "[email protected]",
"last_datetime": None,
"lines": 2,
"lines_all_pages": 2,
"contribution": "100.0%",
"contribution_all_pages": "100.0%",
},
]

os.chdir(cwd)


def test_mkdocs_in_git_subdir(tmp_path):
"""
Sometimes `mkdocs.yml` is not in the root of the repo.
Expand Down

0 comments on commit 96fb712

Please sign in to comment.