1 # Copyright (C) 2012-2013 W. Trevor King <wking@tremily.us>
3 # This file is part of update-copyright.
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
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
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/>.
18 from . import VCSBackend as _VCSBackend
19 from . import utils as _utils
22 class GitBackend (_VCSBackend):
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
35 self._author_format = '--pretty=format:%aN <%aE>'
36 self._year_format = ['--pretty=format:%ad', # Author date
37 '--date=short'] # YYYY-MM-DD
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')
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()
53 def _years(self, filename=None):
54 dates = self._dates(filename=filename)
55 years = set(int(date.split('-', 1)[0]) for date in dates)
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())
66 def is_versioned(self, filename):
67 output = self._git_cmd('log', '--follow', filename)