Skip to content

Commit ed6bfff

Browse files
authored
Merge pull request #6 from uliska/show-contribution-new
Show contribution new
2 parents 98873fa + 894d0c4 commit ed6bfff

File tree

4 files changed

+82
-31
lines changed

4 files changed

+82
-31
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ An example of how to use in your templates:
8686

8787
Alternatively, you could use the simple preformatted ``{{ git_authors_summary }}`` to insert a summary of the authors.
8888

89+
## Options
90+
91+
### `show_contribution`
92+
93+
If this option is set to `true` (default: `false`) The contribution to a page is
94+
printed as a percentage of (source file) lines per author. The output is
95+
suppressed if there is only *one* author for a page.
96+
97+
Example output:
98+
99+
* Authors: [John Doe](#) (33.33%), [Jane Doe](#) (66.67%) *(more than one author)*
100+
* Authors: [John Doe](#) *(one author)*
101+
89102
### Aggregating Authors
90103

91104
In some repositories authors may have committed with differing name/email combinations.

mkdocs_git_authors_plugin/plugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import re
2+
from mkdocs.config import config_options
23
from mkdocs.plugins import BasePlugin
34
from .util import Util
45

56
class GitAuthorsPlugin(BasePlugin):
7+
config_scheme = (
8+
('show_contribution', config_options.Type(bool, default=False)),
9+
)
10+
611
def __init__(self):
712
self.util = Util()
813

@@ -35,7 +40,7 @@ def on_page_markdown(self, markdown, page, config, files):
3540
authors = self.util.get_authors(
3641
path = page.file.abs_src_path
3742
)
38-
authors_summary = self.util.summarize(authors)
43+
authors_summary = self.util.summarize(authors, self.config)
3944

4045
return re.sub(pattern,
4146
authors_summary,
@@ -65,7 +70,7 @@ def on_page_context(self, context, page, **kwargs):
6570
authors = self.util.get_authors(
6671
path = page.file.abs_src_path
6772
)
68-
authors_summary = self.util.summarize(authors)
73+
authors_summary = self.util.summarize(authors, self.config)
6974

7075
context['git_authors'] = authors
7176
context['git_authors_summary'] = authors_summary

mkdocs_git_authors_plugin/util.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_authors(self, path):
2727

2828
if authors:
2929
return authors
30-
30+
3131
try:
3232
blame = self.repo.blame('HEAD',path)
3333
except:
@@ -82,7 +82,7 @@ def _format_perc(n, decimals = 2):
8282
return str(round(n * 100, decimals)) + '%'
8383

8484
@staticmethod
85-
def summarize(authors):
85+
def summarize(authors, config):
8686
"""
8787
Summarized list of authors to a HTML string
8888
@@ -93,6 +93,21 @@ def summarize(authors):
9393
str: HTML text with authors
9494
"""
9595

96-
authors_summary = ["<a href='mailto:%s'>%s</a>" % (x['email'] ,x['name']) for x in authors]
96+
def format_author(author):
97+
contrib = (
98+
' (%s)' % author['contribution']
99+
if (
100+
config['show_contribution']
101+
and len(authors) > 1
102+
)
103+
else ''
104+
)
105+
return "<a href='mailto:%s'>%s</a>%s" % (
106+
author['email'],
107+
author['name'],
108+
contrib
109+
)
110+
111+
authors_summary = [format_author(author) for author in authors]
97112
authors_summary = ', '.join(authors_summary)
98113
return "<span class='git-authors'>" + authors_summary + "</span>"

tests/test_util.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from git import Actor, Repo
44

55
def test_empty_file(tmp_path):
6-
6+
77
# Create empty file
88
file_name = os.path.join(tmp_path, 'new-file')
99
open(file_name, 'a').close()
@@ -13,24 +13,24 @@ def test_empty_file(tmp_path):
1313
instance = util.Util(tmp_path)
1414
authors = instance.get_authors(path = file_name)
1515
assert authors == []
16-
16+
1717
# Get authors of empty, committed file
1818
r.index.add([file_name])
1919
author = Actor('Tim', '[email protected]')
2020
r.index.commit("initial commit", author = author)
2121
authors = instance.get_authors(path = file_name)
2222
assert authors == []
23-
23+
2424
def test_retrieve_authors(tmp_path):
2525
"""
2626
Builds a fake git project with some commits.
27-
27+
2828
pytest offers a `tmp_path`. You can reproduce locally with
2929
>>> import tempfile
3030
>>> from pathlib import Path
3131
>>> tmp_path = Path(tempfile.gettempdir()) / 'pytest-retrieve-authors'
3232
>>> os.mkdir(tmp_path)
33-
33+
3434
Args:
3535
tmp_path (PosixPath): Directory of a tempdir
3636
"""
@@ -45,52 +45,52 @@ def test_retrieve_authors(tmp_path):
4545
r.index.add([file_name])
4646
author = Actor('Tim', '[email protected]')
4747
r.index.commit("initial commit", author = author)
48-
48+
4949
instance = util.Util(tmp_path)
5050
authors = instance.get_authors(path = file_name)
5151
# We don't want to test datetime
5252
authors[0]['last_datetime'] = None
53-
53+
5454
assert authors == [{
5555
'name' : "Tim",
5656
'email' : "[email protected]",
5757
'last_datetime' : None,
5858
'lines' : 1,
5959
'contribution' : '100.0%'
6060
}]
61-
62-
# Now add a line to the file
61+
62+
# Now add a line to the file
6363
# From a second author with same email
6464
with open(file_name, 'a+') as the_file:
6565
the_file.write('World\n')
6666
r.index.add([file_name])
6767
author = Actor('Tim2', '[email protected]')
68-
r.index.commit("another commit", author = author)
69-
68+
r.index.commit("another commit", author = author)
69+
7070
instance = util.Util(tmp_path)
7171
authors = instance.get_authors(path = file_name)
72-
authors[0]['last_datetime'] = None
73-
72+
authors[0]['last_datetime'] = None
73+
7474
assert authors == [{
7575
'name' : "Tim",
7676
'email' : "[email protected]",
7777
'last_datetime' : None,
7878
'lines' : 2,
7979
'contribution' : '100.0%'
8080
}]
81-
81+
8282
# Then a third commit from a new author
8383
with open(file_name, 'a+') as the_file:
8484
the_file.write('A new line\n')
8585
r.index.add([file_name])
8686
author = Actor('John', '[email protected]')
87-
r.index.commit("third commit", author = author)
88-
89-
instance = util.Util(tmp_path)
87+
r.index.commit("third commit", author = author)
88+
89+
instance = util.Util(tmp_path)
9090
authors = instance.get_authors(path = file_name)
91-
authors[0]['last_datetime'] = None
92-
authors[1]['last_datetime'] = None
93-
91+
authors[0]['last_datetime'] = None
92+
authors[1]['last_datetime'] = None
93+
9494
assert authors == [{
9595
'name' : "John",
9696
'email' : "[email protected]",
@@ -103,15 +103,33 @@ def test_retrieve_authors(tmp_path):
103103
'last_datetime' : None,
104104
'lines' : 2,
105105
'contribution' : '66.67%'
106-
}]
106+
}]
107107

108108
def test_summarize_authors():
109-
109+
110110
authors = [
111111
{'name' : 'Tim',
112-
'email' : '[email protected]'
112+
'email' : '[email protected]',
113+
'contribution' : '64.23%'
113114
}
114115
]
115-
116-
summary = util.Util().summarize(authors)
117-
assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a></span>"
116+
117+
# Default case: don't show contribution
118+
config = { 'show_contribution' : False }
119+
summary = util.Util().summarize(authors, config)
120+
assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a></span>"
121+
122+
# Do show contribution, but hide it because there's only one author
123+
config = { 'show_contribution' : True }
124+
summary = util.Util().summarize(authors, config)
125+
assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a></span>"
126+
127+
# Add another author
128+
authors.append({
129+
'name' : 'Tom',
130+
'email' : '[email protected]',
131+
'contribution' : '35.77%'
132+
})
133+
# Now contribution is displayed
134+
summary = util.Util().summarize(authors, config)
135+
assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a> (64.23%), <a href='mailto:[email protected]'>Tom</a> (35.77%)</span>"

0 commit comments

Comments
 (0)