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

ImplementĀ GitBranch.__repr__.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# along with this program; if not, write to the Free Software
19
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
20
 
21
 
 
22
21
"""Git-specific subcommands for Bazaar."""
23
22
 
24
 
from bzrlib.commands import Command
25
 
from bzrlib.option import Option
 
23
from bzrlib.commands import (
 
24
    Command,
 
25
    )
 
26
from bzrlib.option import (
 
27
    Option,
 
28
    )
 
29
 
 
30
from bzrlib.plugins.git import (
 
31
    get_rich_root_format,
 
32
    )
26
33
 
27
34
class cmd_git_serve(Command):
28
35
    """Provide access to a Bazaar branch using the git protocol.
52
59
        server = TCPGitServer(backend, 'localhost')
53
60
        server.serve_forever()
54
61
 
 
62
 
55
63
class cmd_git_import(Command):
56
64
    """Import all branches from a git repository.
57
65
 
58
66
    """
59
67
 
60
 
    takes_args = ["src_location", "dest_location"]
61
 
 
62
 
    def run(self, src_location, dest_location):
63
 
        from bzrlib.bzrdir import BzrDir, format_registry
64
 
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
65
 
        from bzrlib.repository import Repository
 
68
    takes_args = ["src_location", "dest_location?"]
 
69
 
 
70
    def run(self, src_location, dest_location=None):
 
71
        import os
 
72
        from bzrlib import (
 
73
            ui,
 
74
            urlutils,
 
75
            )
 
76
        from bzrlib.bzrdir import (
 
77
            BzrDir,
 
78
            format_registry,
 
79
            )
 
80
        from bzrlib.errors import (
 
81
            BzrCommandError,
 
82
            NoRepositoryPresent,
 
83
            NotBranchError,
 
84
            )
 
85
        from bzrlib.repository import (
 
86
            InterRepository,
 
87
            Repository,
 
88
            )
 
89
        from bzrlib.plugins.git.branch import GitBranch
 
90
        from bzrlib.plugins.git.repository import GitRepository
 
91
 
 
92
        if dest_location is None:
 
93
            dest_location = os.path.basename(src_location.rstrip("/\\"))
 
94
 
66
95
        source_repo = Repository.open(src_location)
67
 
        format = format_registry.make_bzrdir('rich-root-pack')
 
96
        if not isinstance(source_repo, GitRepository):
 
97
            raise BzrCommandError("%r is not a git repository" % src_location)
 
98
        format = get_rich_root_format()
68
99
        try:
69
100
            target_bzrdir = BzrDir.open(dest_location)
70
101
        except NotBranchError:
74
105
        except NoRepositoryPresent:
75
106
            target_repo = target_bzrdir.create_repository(shared=True)
76
107
 
77
 
        target_repo.fetch(source_repo)
78
 
        for name, ref in source_repo._git.heads().iteritems():
79
 
            head_loc = os.path.join(dest_location, name)
80
 
            try:
81
 
                head_bzrdir = BzrDir.open(head_loc)
82
 
            except NotBranchError:
83
 
                head_bzrdir = BzrDir.create(head_loc, format=format)
84
 
            try:
85
 
                head_branch = head_bzrdir.open_branch()
86
 
            except NotBranchError:
87
 
                head_branch = head_bzrdir.create_branch()
88
 
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
 
108
        interrepo = InterRepository.get(source_repo, target_repo)
 
109
        mapping = source_repo.get_mapping()
 
110
        refs = interrepo.fetch_refs()
 
111
        pb = ui.ui_factory.nested_progress_bar()
 
112
        try:
 
113
            for i, (name, ref) in enumerate(refs.iteritems()):
 
114
                if name.startswith("refs/tags/"):
 
115
                    continue
 
116
                pb.update("creating branches", i, len(refs))
 
117
                head_loc = os.path.join(dest_location, name)
 
118
                try:
 
119
                    head_bzrdir = BzrDir.open(head_loc)
 
120
                except NotBranchError:
 
121
                    parent_path = urlutils.dirname(head_loc)
 
122
                    if not os.path.isdir(parent_path):
 
123
                        os.makedirs(parent_path)
 
124
                    head_bzrdir = BzrDir.create(head_loc, format=format)
 
125
                try:
 
126
                    head_branch = head_bzrdir.open_branch()
 
127
                except NotBranchError:
 
128
                    head_branch = head_bzrdir.create_branch()
 
129
                revid = mapping.revision_id_foreign_to_bzr(ref)
 
130
                source_branch = GitBranch(source_repo.bzrdir, source_repo, 
 
131
                    name, ref, None)
 
132
                head_branch.generate_revision_history(revid)
 
133
                source_branch.tags.merge_to(head_branch.tags)
 
134
        finally:
 
135
            pb.finished()
89
136