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(['git'] + list(args))
29 return stdout.rstrip('\n')
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
41 self._author_format = '--pretty=format:%aN <%aE>'
42 self._year_format = ['--pretty=format:%ad', # Author date
43 '--date=short'] # YYYY-MM-DD
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())
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())
63 def is_versioned(self, filename):
64 output = self._git_cmd('log', '--follow', filename)