/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to gitlib/model.py

More refactoring. Add some direct tests for GitModel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""The model for interacting with the git process, etc."""
 
18
 
 
19
import subprocess
 
20
 
 
21
from bzrlib.plugins.git.gitlib import errors
 
22
 
 
23
 
 
24
class GitModel(object):
 
25
    """API that follows GIT model closely"""
 
26
 
 
27
    def __init__(self, git_dir):
 
28
        self.git_dir = git_dir
 
29
 
 
30
    def git_command(self, command, args):
 
31
        return ['git', '--git-dir', self.git_dir, command] + args
 
32
 
 
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()
 
39
        if p.wait() != 0:
 
40
            raise errors.GitCommandError(cmd, p.returncode,
 
41
                                         p.stderr.read().strip())
 
42
        return lines
 
43
 
 
44
    def git_line(self, command, args):
 
45
        lines = self.git_lines(command, args)
 
46
        return lines[0]
 
47
 
 
48
    def cat_file(self, type, object_id, pretty=False):
 
49
        args = []
 
50
        if pretty:
 
51
            args.append('-p')
 
52
        else:
 
53
            args.append(type)
 
54
        args.append(object_id)
 
55
        return self.git_lines('cat-file', args)
 
56
 
 
57
    def rev_list(self, heads, max_count=None, header=False, parents=False):
 
58
        args = []
 
59
        if max_count is not None:
 
60
            args.append('--max-count=%d' % max_count)
 
61
        if header:
 
62
            args.append('--header')
 
63
        if parents:
 
64
            args.append('--parents')
 
65
        if heads is None:
 
66
            args.append('--all')
 
67
        else:
 
68
            args.extend(heads)
 
69
        return self.git_lines('rev-list', args)
 
70
 
 
71
    def rev_parse(self, git_id):
 
72
        args = ['--verify', git_id]
 
73
        return self.git_line('rev-parse', args)
 
74
 
 
75
    def get_head(self):
 
76
        try:
 
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':
 
81
                return None
 
82
            raise
 
83
 
 
84
    def ancestry(self, revisions):
 
85
        ancestors = {}
 
86
        for line in self.rev_list(revisions, parents=True):
 
87
            entries = line.split()
 
88
            ancestors[entries[0]] = entries[1:]
 
89
        return ancestors
 
90
 
 
91
    def ancestor_lines(self, revisions):
 
92
        revision_lines = []
 
93
        for line in self.rev_list(revisions, header=True):
 
94
            if line.startswith('\x00'):
 
95
                yield revision_lines
 
96
                revision_lines = [line[1:].decode('latin-1')]
 
97
            else:
 
98
                revision_lines.append(line.decode('latin-1'))
 
99
        assert revision_lines == ['']
 
100
 
 
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)