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