37
37
takes_args = ["src_location", "dest_location?"]
39
def _get_colocated_branch(self, target_bzrdir, name):
40
from bzrlib.errors import NotBranchError
42
return target_bzrdir.open_branch(name=name)
43
except NotBranchError:
44
return target_bzrdir.create_branch(name=name)
46
def _get_nested_branch(self, dest_transport, dest_format, name):
47
from bzrlib.bzrdir import BzrDir
48
from bzrlib.errors import NotBranchError
49
head_transport = dest_transport.clone(name)
51
head_bzrdir = BzrDir.open_from_transport(head_transport)
52
except NotBranchError:
53
head_bzrdir = dest_format.initialize_on_transport_ex(
54
head_transport, create_prefix=True)[1]
56
return head_bzrdir.open_branch()
57
except NotBranchError:
58
return head_bzrdir.create_branch()
39
60
def run(self, src_location, dest_location=None):
41
63
from bzrlib import (
50
74
NoRepositoryPresent,
77
from bzrlib.plugins.git import gettext
53
78
from bzrlib.repository import (
82
from bzrlib.transport import get_transport
57
83
from bzrlib.plugins.git.branch import (
86
from bzrlib.plugins.git.refs import (
61
89
from bzrlib.plugins.git.repository import GitRepository
91
dest_format = controldir.ControlDirFormat.get_default_format()
63
93
if dest_location is None:
64
94
dest_location = os.path.basename(src_location.rstrip("/\\"))
96
dest_transport = get_transport(dest_location)
66
98
source_repo = Repository.open(src_location)
67
99
if not isinstance(source_repo, GitRepository):
68
raise BzrCommandError("%r is not a git repository" % src_location)
100
raise BzrCommandError(gettext("%r is not a git repository") % src_location)
70
target_bzrdir = BzrDir.open(dest_location)
102
target_bzrdir = BzrDir.open_from_transport(dest_transport)
71
103
except NotBranchError:
72
target_bzrdir = BzrDir.create(dest_location)
104
target_bzrdir = dest_format.initialize_on_transport_ex(
105
dest_transport, shared_repo=True)[1]
74
107
target_repo = target_bzrdir.find_repository()
75
108
except NoRepositoryPresent:
76
109
target_repo = target_bzrdir.create_repository(shared=True)
78
111
if not target_repo.supports_rich_root():
79
raise BzrCommandError("Target repository doesn't support rich roots")
112
raise BzrCommandError(gettext("Target repository doesn't support rich roots"))
81
114
interrepo = InterRepository.get(source_repo, target_repo)
82
115
mapping = source_repo.get_mapping()
83
116
refs = interrepo.fetch()
85
for k, v in extract_tags(refs).iteritems():
86
tags[k] = mapping.revision_id_foreign_to_bzr(v)
117
refs_dict = refs.as_dict()
87
118
pb = ui.ui_factory.nested_progress_bar()
89
for i, (name, ref) in enumerate(refs.iteritems()):
90
if name.startswith("refs/tags/"):
120
for i, (name, sha) in enumerate(refs_dict.iteritems()):
122
branch_name = ref_to_branch_name(name)
124
# Not a branch, ignore
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)
126
pb.update(gettext("creating branches"), i, len(refs_dict))
127
if getattr(target_bzrdir._format, "colocated_branches", False):
130
head_branch = self._get_colocated_branch(target_bzrdir, branch_name)
132
head_branch = self._get_nested_branch(dest_transport, dest_format, branch_name)
133
revid = mapping.revision_id_foreign_to_bzr(sha)
106
134
source_branch = GitBranch(source_repo.bzrdir, source_repo,
108
source_branch.head = ref
136
source_branch.head = sha
109
137
if head_branch.last_revision() != revid:
110
138
head_branch.generate_revision_history(revid)
111
139
source_branch.tags.merge_to(head_branch.tags)
140
if not head_branch.get_parent():
141
url = urlutils.join_segment_parameters(
142
source_branch.base, {"ref": urllib.quote(name, '')})
143
head_branch.set_parent(url)
147
"Use 'bzr checkout' to create a working tree in "
148
"the newly created branches."))
116
151
class cmd_git_object(Command):
138
173
from bzrlib.bzrdir import (
176
from bzrlib.plugins.git.object_store import (
179
from bzrlib.plugins.git import gettext
141
180
bzrdir, _ = BzrDir.open_containing(directory)
142
181
repo = bzrdir.find_repository()
143
from bzrlib.plugins.git.object_store import (
146
182
object_store = get_object_store(repo)
183
object_store.lock_read()
149
185
if sha1 is not None:
151
187
obj = object_store[str(sha1)]
153
raise BzrCommandError("Object not found: %s" % sha1)
189
raise BzrCommandError(gettext("Object not found: %s") % sha1)
155
191
text = obj.as_pretty_string()
173
takes_options = [Option('directory',
175
help='Location of repository.', type=unicode)]
209
takes_args = ["location?"]
178
def run(self, directory="."):
212
def run(self, location="."):
179
213
from bzrlib.bzrdir import (
182
216
from bzrlib.plugins.git.refs import (
185
219
from bzrlib.plugins.git.object_store import (
186
220
get_object_store,
188
bzrdir, _ = BzrDir.open_containing(directory)
222
bzrdir, _ = BzrDir.open_containing(location)
189
223
repo = bzrdir.find_repository()
224
object_store = get_object_store(repo)
225
object_store.lock_read()
192
object_store = get_object_store(repo)
193
refs = BazaarRefsContainer(bzrdir, object_store)
227
refs = get_refs_container(bzrdir, object_store)
194
228
for k, v in refs.as_dict().iteritems():
195
229
self.outf.write("%s -> %s\n" % (k, v))
231
object_store.unlock()
200
234
class cmd_git_apply(Command):
242
Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
244
help='Apply patches even if tree has uncommitted changes.')
207
246
takes_args = ["patches*"]
209
def _apply_patch(self, wt, f):
248
def _apply_patch(self, wt, f, signoff):
251
:param wt: A Bazaar working tree object.
252
:param f: Patch file to read.
253
:param signoff: Add Signed-Off-By flag.
255
from bzrlib.errors import BzrCommandError
210
256
from dulwich.patch import git_am_patch_split
211
258
(c, diff, version) = git_am_patch_split(f)
212
# FIXME: Process diff
213
wt.commit(committer=c.committer,
259
# FIXME: Cope with git-specific bits in patch
260
# FIXME: Add new files to working tree
261
p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE,
266
raise BzrCommandError(gettext("error running patch"))
269
signed_off_by = wt.branch.get_config().username()
270
message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
271
wt.commit(authors=[c.author], message=message)
216
def run(self, patches_list=None):
273
def run(self, patches_list=None, signoff=False, force=False):
274
from bzrlib.errors import UncommittedChanges
217
275
from bzrlib.workingtree import WorkingTree
218
276
if patches_list is None:
219
277
patches_list = []
221
279
tree, _ = WorkingTree.open_containing(".")
280
if tree.basis_tree().changes_from(tree).has_changed() and not force:
281
raise UncommittedChanges(tree)
222
282
tree.lock_write()
224
284
for patch in patches_list:
225
285
f = open(patch, 'r')
227
self._apply_patch(tree, f)
287
self._apply_patch(tree, f, signoff=signoff)