1 # Copyright (C) 2012 W. Trevor King
3 # This file is part of update-copyright.
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.
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.
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/>.
19 from . import VCSBackend as _VCSBackend
20 from . import utils as _utils
23 class GitBackend (_VCSBackend):
28 status,stdout,stderr = _utils.invoke(
29 ['git'] + list(args), unicode_output=True)
30 return stdout.rstrip('\n')
32 def __init__(self, **kwargs):
33 super(GitBackend, self).__init__(**kwargs)
34 self._version = self._git_cmd('--version').split(' ')[-1]
35 if self._version.startswith('1.5.'):
36 # Author name <author email>
37 self._author_format = '--pretty=format:%an <%ae>'
38 self._year_format = ['--pretty=format:%ai'] # Author date
39 # YYYY-MM-DD HH:MM:SS Z
40 # Earlier versions of Git don't seem to recognize --date=short
42 self._author_format = '--pretty=format:%aN <%aE>'
43 self._year_format = ['--pretty=format:%ad', # Author date
44 '--date=short'] # YYYY-MM-DD
46 def _years(self, filename=None):
47 args = ['log'] + self._year_format
48 if filename is not None:
49 args.extend(['--follow'] + [filename])
50 output = self._git_cmd(*args)
51 if self._version.startswith('1.5.'):
52 output = '\n'.join([x.split()[0] for x in output.splitlines()])
53 years = set(int(line.split('-', 1)[0]) for line in output.splitlines())
56 def _authors(self, filename=None):
57 args = ['log', self._author_format]
58 if filename is not None:
59 args.extend(['--follow', filename])
60 output = self._git_cmd(*args)
61 authors = set(output.splitlines())
64 def is_versioned(self, filename):
65 output = self._git_cmd('log', '--follow', filename)