Use `with_emails` argument in `VCSBackend.authors()`.
[update-copyright.git] / update_copyright / vcs / git.py
1 # Copyright (C) 2012 W. Trevor King
2 #
3 # This file is part of update-copyright.
4 #
5 # update-copyright is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # update-copyright is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with update-copyright.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 from . import VCSBackend as _VCSBackend
20 from . import utils as _utils
21
22
23 class GitBackend (_VCSBackend):
24     name = 'Git'
25
26     @staticmethod
27     def _git_cmd(*args):
28         status,stdout,stderr = _utils.invoke(['git'] + list(args))
29         return stdout.rstrip('\n')
30
31     def __init__(self, **kwargs):
32         super(GitBackend, self).__init__(**kwargs)
33         self._version = self._git_cmd('--version').split(' ')[-1]
34         if self._version.startswith('1.5.'):
35             # Author name <author email>
36             self._author_format = '--pretty=format:%an <%ae>'
37             self._year_format = ['--pretty=format:%ai']  # Author date
38             # YYYY-MM-DD HH:MM:SS Z
39             # Earlier versions of Git don't seem to recognize --date=short
40         else:
41             self._author_format = '--pretty=format:%aN <%aE>'
42             self._year_format = ['--pretty=format:%ad',  # Author date
43                                  '--date=short']         # YYYY-MM-DD
44
45     def _years(self, filename=None):
46         args = ['log'] + self._year_format
47         if filename is not None:
48             args.extend(['--follow'] + [filename])
49         output = self._git_cmd(*args)
50         if self._version.startswith('1.5.'):
51             output = '\n'.join([x.split()[0] for x in output.splitlines()])
52         years = set(int(line.split('-', 1)[0]) for line in output.splitlines())
53         return years
54
55     def _authors(self, filename=None):
56         args = ['log', self._author_format]
57         if filename is not None:
58             args.extend(['--follow', filename])
59         output = self._git_cmd(*args)
60         authors = set(output.splitlines())
61         return authors
62
63     def is_versioned(self, filename):
64         output = self._git_cmd('log', '--follow', filename)
65         if len(output) == 0:
66             return False
67         return True