/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

Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
2
2
# Authors: Robert Collins <robert.collins@canonical.com>
3
 
#          Jelmer Vernooij <jelmer@samba.org>
4
 
#          John Carr <john.carr@unrouted.co.uk>
5
3
#
6
4
# This program is free software; you can redistribute it and/or modify
7
5
# it under the terms of the GNU General Public License as published by
20
18
 
21
19
"""A GIT branch and repository format implementation for bzr."""
22
20
 
23
 
try:
24
 
    import dulwich as git
25
 
except ImportError:
26
 
    import os, sys
27
 
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dulwich"))
28
 
    import dulwich as git
29
 
from bzrlib import bzrdir
30
 
from bzrlib.foreign import ForeignVcs, VcsMappingRegistry, foreign_vcs_registry
31
 
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
32
 
from bzrlib.transport import register_lazy_transport
33
 
from bzrlib.commands import Command, register_command
34
 
from bzrlib.option import Option
35
 
 
36
 
bzrdir.format_registry.register(
37
 
    'git', LocalGitBzrDirFormat,
38
 
    help='GIT repository.', 
39
 
    native=False, experimental=True,
 
21
 
 
22
from StringIO import StringIO
 
23
 
 
24
import stgit
 
25
import stgit.git as git
 
26
 
 
27
from bzrlib import (
 
28
    commands,
 
29
    config,
 
30
    deprecated_graph,
 
31
    iterablefile,
 
32
    osutils,
 
33
    urlutils,
40
34
    )
41
 
 
42
 
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
43
 
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
44
 
 
45
 
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
46
 
                        'GitSmartTransport')
47
 
 
48
 
 
49
 
class ForeignGit(ForeignVcs):
50
 
    """Foreign Git."""
51
 
 
52
 
 
53
 
git_mapping_registry = VcsMappingRegistry()
54
 
git_mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
55
 
                                   "BzrGitMappingExperimental")
56
 
foreign_vcs_registry.register("git", ForeignGit(git_mapping_registry), 
57
 
                                      "Stupid content tracker")
58
 
 
59
 
 
60
 
class cmd_git_serve(Command):
61
 
    """Provide access to a Bazaar branch using the git protocol.
62
 
 
63
 
    This command is experimental and doesn't do much yet.
64
 
    """
65
 
    takes_options = [
66
 
        Option('directory',
67
 
               help='serve contents of directory',
68
 
               type=unicode)
69
 
    ]
70
 
 
71
 
    def run(self, directory=None):
72
 
        from dulwich.server import TCPGitServer
73
 
        from bzrlib.plugins.git.server import BzrBackend
74
 
        from bzrlib.trace import warning
75
 
        import os
76
 
 
77
 
        warning("server support in bzr-git is experimental.")
78
 
 
79
 
        if directory is None:
80
 
            directory = os.getcwd()
81
 
 
82
 
        backend = BzrBackend(directory)
83
 
 
84
 
        server = TCPGitServer(backend, 'localhost')
85
 
        server.serve_forever()
86
 
 
87
 
register_command(cmd_git_serve)
88
 
 
89
 
 
90
 
class cmd_git_import(Command):
91
 
    """Import all branches from a git repository.
92
 
 
93
 
    """
94
 
 
95
 
    takes_args = ["src_location", "dest_location"]
96
 
 
97
 
    def run(self, src_location, dest_location):
98
 
        from bzrlib.bzrdir import BzrDir, format_registry
99
 
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
100
 
        from bzrlib.repository import Repository
101
 
        source_repo = Repository.open(src_location)
102
 
        format = format_registry.make_bzrdir('rich-root-pack')
103
 
        try:
104
 
            target_bzrdir = BzrDir.open(dest_location)
105
 
        except NotBranchError:
106
 
            target_bzrdir = BzrDir.create(dest_location, format=format)
107
 
        try:
108
 
            target_repo = target_bzrdir.open_repository()
109
 
        except NoRepositoryPresent:
110
 
            target_repo = target_bzrdir.create_repository(shared=True)
111
 
 
112
 
        target_repo.fetch(source_repo)
113
 
        for name, ref in source_repo._git.heads().iteritems():
114
 
            head_loc = os.path.join(dest_location, name)
115
 
            try:
116
 
                head_bzrdir = BzrDir.open(head_loc)
117
 
            except NotBranchError:
118
 
                head_bzrdir = BzrDir.create(head_loc, format=format)
119
 
            try:
120
 
                head_branch = head_bzrdir.open_branch()
121
 
            except NotBranchError:
122
 
                head_branch = head_bzrdir.create_branch()
123
 
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
124
 
 
125
 
 
126
 
register_command(cmd_git_import)
127
 
 
128
 
 
 
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
129
155
def test_suite():
130
156
    from bzrlib.plugins.git import tests
131
157
    return tests.test_suite()