1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""The model for interacting with the git process, etc."""
21
from bzrlib.plugins.git.gitlib import errors
24
class GitModel(object):
25
"""API that follows GIT model closely"""
27
def __init__(self, git_dir):
28
self.git_dir = git_dir
30
def git_command(self, command, args):
31
return ['git', '--git-dir', self.git_dir, command] + args
33
def git_lines(self, command, args):
34
cmd = self.git_command(command, args)
35
p = subprocess.Popen(cmd,
36
stdout=subprocess.PIPE,
37
stderr=subprocess.PIPE)
38
lines = p.stdout.readlines()
40
raise errors.GitCommandError(cmd, p.returncode,
41
p.stderr.read().strip())
44
def git_line(self, command, args):
45
lines = self.git_lines(command, args)
48
def cat_file(self, type, object_id, pretty=False):
54
args.append(object_id)
55
return self.git_lines('cat-file', args)
57
def rev_list(self, heads, max_count=None, header=False, parents=False):
59
if max_count is not None:
60
args.append('--max-count=%d' % max_count)
62
args.append('--header')
64
args.append('--parents')
69
return self.git_lines('rev-list', args)
71
def rev_parse(self, git_id):
72
args = ['--verify', git_id]
73
return self.git_line('rev-parse', args)
77
return self.rev_parse('HEAD')
78
except errors.GitCommandError, e:
79
# Most likely, this is a null branch, so treat it as such
80
if e.stderr == 'fatal: Needed a single revision':
84
def ancestry(self, revisions):
86
for line in self.rev_list(revisions, parents=True):
87
entries = line.split()
88
ancestors[entries[0]] = entries[1:]
91
def ancestor_lines(self, revisions):
93
for line in self.rev_list(revisions, header=True):
94
if line.startswith('\x00'):
96
revision_lines = [line[1:].decode('latin-1')]
98
revision_lines.append(line.decode('latin-1'))
99
assert revision_lines == ['']
101
def get_inventory(self, tree_id):
102
for line in self.cat_file('tree', tree_id, True):
103
sections = line.split(' ', 2)
104
obj_id, name = sections[2].split('\t', 1)
105
name = name.rstrip('\n')
106
if name.startswith('"'):
107
name = name[1:-1].decode('string_escape').decode('utf-8')
108
yield (sections[0], sections[1], obj_id, name)