/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 __init__.py

More refactoring. Add some direct tests for GitModel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""A GIT branch and repository format implementation for bzr."""
20
20
 
21
 
 
22
 
from StringIO import StringIO
23
 
 
24
 
import stgit
25
 
import stgit.git as git
26
 
 
27
21
from bzrlib import (
28
 
    commands,
29
 
    config,
30
 
    deprecated_graph,
31
 
    iterablefile,
32
 
    osutils,
33
 
    urlutils,
34
 
    )
35
 
from bzrlib.decorators import *
36
 
import bzrlib.branch
37
 
import bzrlib.bzrdir
38
 
import bzrlib.errors as errors
39
 
import bzrlib.repository
40
 
from bzrlib.revision import Revision
41
 
 
42
 
 
43
 
class GitInventory(object):
44
 
 
45
 
    def __init__(self, revision_id):
46
 
        self.entries = {}
47
 
        self.root = GitEntry('', 'directory', revision_id)
48
 
        self.entries[''] = self.root
49
 
 
50
 
    def __getitem__(self, key):
51
 
        return self.entries[key]
52
 
 
53
 
    def iter_entries(self):
54
 
        return iter(sorted(self.entries.items()))
55
 
 
56
 
    def iter_entries_by_dir(self):
57
 
        return self.iter_entries()
58
 
 
59
 
    def __len__(self):
60
 
        return len(self.entries)
61
 
 
62
 
 
63
 
class GitEntry(object):
64
 
 
65
 
    def __init__(self, path, kind, revision, text_sha1=None, executable=False,
66
 
                 text_size=None):
67
 
        self.path = path
68
 
        self.file_id = path
69
 
        self.kind = kind
70
 
        self.executable = executable
71
 
        self.name = osutils.basename(path)
72
 
        if path == '':
73
 
            self.parent_id = None
74
 
        else:
75
 
            self.parent_id = osutils.dirname(path)
76
 
        self.revision = revision
77
 
        self.symlink_target = None
78
 
        self.text_sha1 = text_sha1
79
 
        self.text_size = None
80
 
 
81
 
    def __repr__(self):
82
 
        return "GitEntry(%r, %r, %r, %r)" % (self.path, self.kind, 
83
 
                                             self.revision, self.parent_id)
84
 
 
85
 
 
86
 
class GitModel(object):
87
 
    """API that follows GIT model closely"""
88
 
 
89
 
    def __init__(self, git_dir):
90
 
        self.git_dir = git_dir
91
 
 
92
 
    def git_command(self, command, args):
93
 
        args = ' '.join("'%s'" % arg for arg in args)
94
 
        return 'git --git-dir=%s %s %s' % (self.git_dir, command, args) 
95
 
 
96
 
    def git_lines(self, command, args):
97
 
        return stgit.git._output_lines(self.git_command(command, args))
98
 
 
99
 
    def git_line(self, command, args):
100
 
        return stgit.git._output_one_line(self.git_command(command, args))
101
 
 
102
 
    def cat_file(self, type, object_id, pretty=False):
103
 
        args = []
104
 
        if pretty:
105
 
            args.append('-p')
106
 
        else:
107
 
            args.append(type)
108
 
        args.append(object_id)
109
 
        return self.git_lines('cat-file', args)
110
 
 
111
 
    def rev_list(self, heads, max_count=None, header=False):
112
 
        args = []
113
 
        if max_count is not None:
114
 
            args.append('--max-count=%d' % max_count)
115
 
        if header is not False:
116
 
            args.append('--header')
117
 
        if heads is None:
118
 
            args.append('--all')
119
 
        else:
120
 
            args.extend(heads)
121
 
        return self.git_lines('rev-list', args)
122
 
 
123
 
    def rev_parse(self, git_id):
124
 
        args = ['--verify', git_id]
125
 
        return self.git_line('rev-parse', args)
126
 
 
127
 
    def get_head(self):
128
 
        return self.rev_parse('HEAD')
129
 
 
130
 
    def ancestor_lines(self, revisions):
131
 
        revision_lines = []
132
 
        for line in self.rev_list(revisions, header=True):
133
 
            if line.startswith('\x00'):
134
 
                yield revision_lines
135
 
                revision_lines = [line[1:].decode('latin-1')]
136
 
            else:
137
 
                revision_lines.append(line.decode('latin-1'))
138
 
        assert revision_lines == ['']
139
 
 
140
 
    def get_inventory(self, tree_id):
141
 
        for line in self.cat_file('tree', tree_id, True):
142
 
            sections = line.split(' ', 2)
143
 
            obj_id, name = sections[2].split('\t', 1)
144
 
            name = name.rstrip('\n')
145
 
            if name.startswith('"'):
146
 
                name = name[1:-1].decode('string_escape').decode('utf-8')
147
 
            yield (sections[0], sections[1], obj_id, name)
148
 
 
149
 
 
150
 
class cmd_test_git(commands.Command):
151
 
 
152
 
    def run(self):
153
 
        from bzrlib.tests import selftest
154
 
        selftest
 
22
#    branch,
 
23
    bzrdir,
 
24
#    repository,
 
25
    )
 
26
 
 
27
bzrdir.format_registry.register_lazy('git',
 
28
    'bzrlib.plugins.git.gitlib.git_dir', 'GitDirFormat',
 
29
    help='GIT - the stupid content tracker.',
 
30
    native=False, hidden=True, experimental=True,
 
31
    )
 
32
 
 
33
# formats which have no format string are not discoverable
 
34
# and not independently creatable, so are not registered.
 
35
 
 
36
 
155
37
def test_suite():
156
38
    from bzrlib.plugins.git import tests
157
39
    return tests.test_suite()