1
 
# Copyright (C) 2006-2009 Canonical Ltd
 
3
 
# Authors: Robert Collins <robert.collins@canonical.com>
 
4
 
#          Jelmer Vernooij <jelmer@samba.org>
 
5
 
#          John Carr <john.carr@unrouted.co.uk>
 
7
 
# This program is free software; you can redistribute it and/or modify
 
8
 
# it under the terms of the GNU General Public License as published by
 
9
 
# the Free Software Foundation; either version 2 of the License, or
 
10
 
# (at your option) any later version.
 
12
 
# This program is distributed in the hope that it will be useful,
 
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 
# GNU General Public License for more details.
 
17
 
# You should have received a copy of the GNU General Public License
 
18
 
# along with this program; if not, write to the Free Software
 
19
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 
"""Git-specific subcommands for Bazaar."""
 
23
 
from bzrlib.commands import (
 
27
 
from bzrlib.option import (
 
32
 
class cmd_git_import(Command):
 
33
 
    """Import all branches from a git repository.
 
37
 
    takes_args = ["src_location", "dest_location?"]
 
39
 
    def run(self, src_location, dest_location=None):
 
45
 
        from bzrlib.bzrdir import (
 
48
 
        from bzrlib.errors import (
 
53
 
        from bzrlib.repository import (
 
57
 
        from bzrlib.plugins.git.branch import (
 
61
 
        from bzrlib.plugins.git.repository import GitRepository
 
63
 
        if dest_location is None:
 
64
 
            dest_location = os.path.basename(src_location.rstrip("/\\"))
 
66
 
        source_repo = Repository.open(src_location)
 
67
 
        if not isinstance(source_repo, GitRepository):
 
68
 
            raise BzrCommandError("%r is not a git repository" % src_location)
 
70
 
            target_bzrdir = BzrDir.open(dest_location)
 
71
 
        except NotBranchError:
 
72
 
            target_bzrdir = BzrDir.create(dest_location)
 
74
 
            target_repo = target_bzrdir.find_repository()
 
75
 
        except NoRepositoryPresent:
 
76
 
            target_repo = target_bzrdir.create_repository(shared=True)
 
78
 
        if not target_repo.supports_rich_root():
 
79
 
            raise BzrCommandError("Target repository doesn't support rich roots")
 
81
 
        interrepo = InterRepository.get(source_repo, target_repo)
 
82
 
        mapping = source_repo.get_mapping()
 
83
 
        refs = interrepo.fetch()
 
85
 
        for k, v in extract_tags(refs).iteritems():
 
86
 
            tags[k] = mapping.revision_id_foreign_to_bzr(v)
 
87
 
        pb = ui.ui_factory.nested_progress_bar()
 
89
 
            for i, (name, ref) in enumerate(refs.iteritems()):
 
90
 
                if name.startswith("refs/tags/"):
 
92
 
                pb.update("creating branches", i, len(refs))
 
93
 
                head_loc = os.path.join(dest_location, name)
 
95
 
                    head_bzrdir = BzrDir.open(head_loc)
 
96
 
                except NotBranchError:
 
97
 
                    parent_path = urlutils.dirname(head_loc)
 
98
 
                    if not os.path.isdir(parent_path):
 
99
 
                        os.makedirs(parent_path)
 
100
 
                    head_bzrdir = BzrDir.create(head_loc)
 
102
 
                    head_branch = head_bzrdir.open_branch()
 
103
 
                except NotBranchError:
 
104
 
                    head_branch = head_bzrdir.create_branch()
 
105
 
                revid = mapping.revision_id_foreign_to_bzr(ref)
 
106
 
                source_branch = GitBranch(source_repo.bzrdir, source_repo,
 
108
 
                source_branch.head = ref
 
109
 
                if head_branch.last_revision() != revid:
 
110
 
                    head_branch.generate_revision_history(revid)
 
111
 
                source_branch.tags.merge_to(head_branch.tags)
 
116
 
class cmd_git_object(Command):
 
117
 
    """List or display Git objects by SHA.
 
119
 
    Cat a particular object's Git representation if a SHA is specified.
 
120
 
    List all available SHAs otherwise.
 
125
 
    aliases = ["git-objects", "git-cat"]
 
126
 
    takes_args = ["sha1?"]
 
127
 
    takes_options = [Option('directory',
 
129
 
        help='Location of repository.', type=unicode),
 
130
 
        Option('pretty', help='Pretty-print objects.')]
 
131
 
    encoding_type = 'exact'
 
134
 
    def run(self, sha1=None, directory=".", pretty=False):
 
135
 
        from bzrlib.errors import (
 
138
 
        from bzrlib.bzrdir import (
 
141
 
        bzrdir, _ = BzrDir.open_containing(directory)
 
142
 
        repo = bzrdir.find_repository()
 
143
 
        from bzrlib.plugins.git.object_store import (
 
146
 
        object_store = get_object_store(repo)
 
151
 
                    obj = object_store[str(sha1)]
 
153
 
                    raise BzrCommandError("Object not found: %s" % sha1)
 
155
 
                    text = obj.as_pretty_string()
 
157
 
                    text = obj.as_raw_string()
 
158
 
                self.outf.write(text)
 
160
 
                for sha1 in object_store:
 
161
 
                    self.outf.write("%s\n" % sha1)
 
166
 
class cmd_git_refs(Command):
 
167
 
    """Output all of the virtual refs for a repository.
 
173
 
    takes_options = [Option('directory',
 
175
 
        help='Location of repository.', type=unicode)]
 
178
 
    def run(self, directory="."):
 
179
 
        from bzrlib.bzrdir import (
 
182
 
        from bzrlib.plugins.git.refs import (
 
185
 
        from bzrlib.plugins.git.object_store import (
 
188
 
        bzrdir, _ = BzrDir.open_containing(directory)
 
189
 
        repo = bzrdir.find_repository()
 
192
 
            object_store = get_object_store(repo)
 
193
 
            refs = BazaarRefsContainer(bzrdir, object_store)
 
194
 
            for k, v in refs.as_dict().iteritems():
 
195
 
                self.outf.write("%s -> %s\n" % (k, v))
 
200
 
class cmd_git_apply(Command):
 
201
 
    """Apply a series of git-am style patches.
 
203
 
    This command will in the future probably be integrated into 
 
207
 
    takes_args = ["patches*"]
 
209
 
    def _apply_patch(self, wt, f):
 
210
 
        from dulwich.patch import git_am_patch_split
 
211
 
        (c, diff, version) = git_am_patch_split(f)
 
212
 
        # FIXME: Process diff
 
213
 
        wt.commit(committer=c.committer,
 
216
 
    def run(self, patches_list=None):
 
217
 
        from bzrlib.workingtree import WorkingTree
 
218
 
        if patches_list is None:
 
221
 
        tree, _ = WorkingTree.open_containing(".")
 
224
 
            for patch in patches_list:
 
227
 
                    self._apply_patch(tree, f)