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."""
22
from bzrlib.plugins.git import errors
25
class GitModel(object):
26
"""API that follows GIT model closely"""
28
def __init__(self, git_dir):
29
self.git_dir = git_dir
31
def git_command(self, command, args):
32
return ['git', command] + args
34
def git_lines(self, command, args):
35
cmd = self.git_command(command, args)
36
env = os.environ.copy()
37
env['GIT_DIR'] = self.git_dir
38
p = subprocess.Popen(cmd,
39
stdout=subprocess.PIPE,
40
stderr=subprocess.PIPE,
42
lines = p.stdout.readlines()
44
raise errors.GitCommandError(cmd, p.returncode,
45
p.stderr.read().strip())
48
def git_line(self, command, args):
49
lines = self.git_lines(command, args)
52
def cat_file(self, type, object_id, pretty=False):
58
args.append(object_id)
59
return self.git_lines('cat-file', args)
61
def rev_list(self, heads, max_count=None, header=False, parents=False,
62
topo_order=False, paths=None):
64
if max_count is not None:
65
args.append('--max-count=%d' % max_count)
67
args.append('--header')
69
args.append('--parents')
71
args.append('--topo-order')
79
return self.git_lines('rev-list', args)
81
def rev_parse(self, git_id):
82
args = ['--verify', git_id]
83
return self.git_line('rev-parse', args).strip()
87
return self.rev_parse('HEAD')
88
except errors.GitCommandError, e:
89
# Most likely, this is a null branch, so treat it as such
90
if e.stderr == 'fatal: Needed a single revision':
94
def get_revision_graph(self, revisions):
96
for line in self.rev_list(revisions, parents=True):
97
entries = line.split()
98
ancestors[entries[0]] = entries[1:]
101
def get_ancestry(self, revisions):
102
args = ['--topo-order', '--reverse'] + revisions
103
return [line[:-1] for line in self.git_lines('rev-list', args)]
105
def ancestor_lines(self, revisions):
107
for line in self.rev_list(revisions, header=True):
108
if line.startswith('\x00'):
110
revision_lines = [line[1:].decode('latin-1')]
112
revision_lines.append(line.decode('latin-1'))
113
assert revision_lines == ['']
115
def get_inventory(self, tree_id):
116
for line in self.git_lines('ls-tree', ['-r', '-t', tree_id]):
117
# Ideally, we would use -z so we would not have to handle escaped
118
# file names. But then we could not use readlines() to split the
119
# data as it is read.
120
permissions, type, hash_and_path = line.split(' ', 2)
121
hash, name = hash_and_path.split('\t', 1)
122
name = name[:-1] # strip trailing newline
123
if name.startswith('"'):
124
name = name[1:-1].decode('string_escape')
125
name = name.decode('utf-8')
126
yield permissions, type, hash, name