/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 breezy/git/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007,2012 Canonical Ltd
 
2
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
12
13
#
13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
 
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
18
19
 
19
 
from bzrlib import (
 
20
from __future__ import absolute_import
 
21
 
 
22
import contextlib
 
23
from io import BytesIO
 
24
from collections import defaultdict
 
25
 
 
26
from dulwich.config import (
 
27
    ConfigFile as GitConfigFile,
 
28
    parse_submodules,
 
29
    )
 
30
 
 
31
from dulwich.objects import (
 
32
    NotCommitError,
 
33
    ZERO_SHA,
 
34
    )
 
35
from dulwich.repo import check_ref_format
 
36
 
 
37
from .. import (
20
38
    branch,
21
39
    config,
22
 
    repository,
 
40
    controldir,
 
41
    errors,
 
42
    lock,
 
43
    repository as _mod_repository,
23
44
    revision,
24
45
    tag,
25
 
    )
26
 
from bzrlib.decorators import needs_read_lock
27
 
from bzrlib.trace import mutter
28
 
 
29
 
from bzrlib.plugins.git.foreign import ForeignBranch
30
 
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
31
 
 
32
 
from dulwich.objects import (
33
 
        Commit,
34
 
        Tag,
35
 
        )
36
 
 
37
 
class GitTagDict(tag.BasicTags):
 
46
    trace,
 
47
    transport,
 
48
    urlutils,
 
49
    )
 
50
from ..foreign import ForeignBranch
 
51
from ..revision import (
 
52
    NULL_REVISION,
 
53
    )
 
54
from ..trace import (
 
55
    is_quiet,
 
56
    mutter,
 
57
    warning,
 
58
    )
 
59
 
 
60
from .config import (
 
61
    GitBranchConfig,
 
62
    GitBranchStack,
 
63
    )
 
64
from .errors import (
 
65
    NoPushSupport,
 
66
    )
 
67
from .push import (
 
68
    remote_divergence,
 
69
    )
 
70
from .refs import (
 
71
    branch_name_to_ref,
 
72
    is_tag,
 
73
    ref_to_branch_name,
 
74
    ref_to_tag_name,
 
75
    remote_refs_dict_to_tag_refs,
 
76
    tag_name_to_ref,
 
77
    )
 
78
from .unpeel_map import (
 
79
    UnpeelMap,
 
80
    )
 
81
from .urls import (
 
82
    git_url_to_bzr_url,
 
83
    bzr_url_to_git_url,
 
84
    )
 
85
 
 
86
 
 
87
def _calculate_revnos(branch):
 
88
    if branch._format.stores_revno():
 
89
        return True
 
90
    config = branch.get_config_stack()
 
91
    return config.get('calculate_revnos')
 
92
 
 
93
 
 
94
class GitPullResult(branch.PullResult):
 
95
    """Result of a pull from a Git branch."""
 
96
 
 
97
    def _lookup_revno(self, revid):
 
98
        if not isinstance(revid, bytes):
 
99
            raise TypeError(revid)
 
100
        if not _calculate_revnos(self.target_branch):
 
101
            return None
 
102
        # Try in source branch first, it'll be faster
 
103
        with self.target_branch.lock_read():
 
104
            return self.target_branch.revision_id_to_revno(revid)
 
105
 
 
106
    @property
 
107
    def old_revno(self):
 
108
        return self._lookup_revno(self.old_revid)
 
109
 
 
110
    @property
 
111
    def new_revno(self):
 
112
        return self._lookup_revno(self.new_revid)
 
113
 
 
114
 
 
115
class GitTags(tag.BasicTags):
 
116
    """Ref-based tag dictionary."""
38
117
 
39
118
    def __init__(self, branch):
40
119
        self.branch = branch
41
120
        self.repository = branch.repository
42
121
 
 
122
    def _merge_to_remote_git(self, target_repo, source_tag_refs,
 
123
                             overwrite=False):
 
124
        updates = {}
 
125
        conflicts = []
 
126
 
 
127
        def get_changed_refs(old_refs):
 
128
            ret = dict(old_refs)
 
129
            for ref_name, tag_name, peeled, unpeeled in (
 
130
                    source_tag_refs.iteritems()):
 
131
                if old_refs.get(ref_name) == unpeeled:
 
132
                    pass
 
133
                elif overwrite or ref_name not in old_refs:
 
134
                    ret[ref_name] = unpeeled
 
135
                    updates[tag_name] = target_repo.lookup_foreign_revision_id(
 
136
                        peeled)
 
137
                else:
 
138
                    conflicts.append(
 
139
                        (tag_name,
 
140
                         self.repository.lookup_foreign_revision_id(peeled),
 
141
                         target_repo.lookup_foreign_revision_id(
 
142
                             old_refs[ref_name])))
 
143
            return ret
 
144
        target_repo.controldir.send_pack(
 
145
            get_changed_refs, lambda have, want: [])
 
146
        return updates, conflicts
 
147
 
 
148
    def _merge_to_local_git(self, target_repo, source_tag_refs,
 
149
                            overwrite=False):
 
150
        conflicts = []
 
151
        updates = {}
 
152
        for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
 
153
            if target_repo._git.refs.get(ref_name) == unpeeled:
 
154
                pass
 
155
            elif overwrite or ref_name not in target_repo._git.refs:
 
156
                try:
 
157
                    updates[tag_name] = (
 
158
                        target_repo.lookup_foreign_revision_id(peeled))
 
159
                except KeyError:
 
160
                    trace.warning('%s does not point to a valid object',
 
161
                                  tag_name)
 
162
                    continue
 
163
                except NotCommitError:
 
164
                    trace.warning('%s points to a non-commit object',
 
165
                                  tag_name)
 
166
                    continue
 
167
                target_repo._git.refs[ref_name] = unpeeled or peeled
 
168
            else:
 
169
                try:
 
170
                    source_revid = self.repository.lookup_foreign_revision_id(
 
171
                        peeled)
 
172
                    target_revid = target_repo.lookup_foreign_revision_id(
 
173
                        target_repo._git.refs[ref_name])
 
174
                except KeyError:
 
175
                    trace.warning('%s does not point to a valid object',
 
176
                                  ref_name)
 
177
                    continue
 
178
                except NotCommitError:
 
179
                    trace.warning('%s points to a non-commit object',
 
180
                                  tag_name)
 
181
                    continue
 
182
                conflicts.append((tag_name, source_revid, target_revid))
 
183
        return updates, conflicts
 
184
 
 
185
    def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
 
186
        target_repo = to_tags.repository
 
187
        if self.repository.has_same_location(target_repo):
 
188
            return {}, []
 
189
        try:
 
190
            if getattr(target_repo, "_git", None):
 
191
                return self._merge_to_local_git(
 
192
                    target_repo, source_tag_refs, overwrite)
 
193
            else:
 
194
                return self._merge_to_remote_git(
 
195
                    target_repo, source_tag_refs, overwrite)
 
196
        finally:
 
197
            to_tags.branch._tag_refs = None
 
198
 
 
199
    def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
 
200
        unpeeled_map = defaultdict(set)
 
201
        conflicts = []
 
202
        updates = {}
 
203
        result = dict(to_tags.get_tag_dict())
 
204
        for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
 
205
            if unpeeled is not None:
 
206
                unpeeled_map[peeled].add(unpeeled)
 
207
            try:
 
208
                bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
 
209
            except NotCommitError:
 
210
                continue
 
211
            if result.get(tag_name) == bzr_revid:
 
212
                pass
 
213
            elif tag_name not in result or overwrite:
 
214
                result[tag_name] = bzr_revid
 
215
                updates[tag_name] = bzr_revid
 
216
            else:
 
217
                conflicts.append((tag_name, bzr_revid, result[tag_name]))
 
218
        to_tags._set_tag_dict(result)
 
219
        if len(unpeeled_map) > 0:
 
220
            map_file = UnpeelMap.from_repository(to_tags.branch.repository)
 
221
            map_file.update(unpeeled_map)
 
222
            map_file.save_in_repository(to_tags.branch.repository)
 
223
        return updates, conflicts
 
224
 
 
225
    def merge_to(self, to_tags, overwrite=False, ignore_master=False,
 
226
                 source_tag_refs=None):
 
227
        """See Tags.merge_to."""
 
228
        if source_tag_refs is None:
 
229
            source_tag_refs = self.branch.get_tag_refs()
 
230
        if self == to_tags:
 
231
            return {}, []
 
232
        if isinstance(to_tags, GitTags):
 
233
            return self._merge_to_git(to_tags, source_tag_refs,
 
234
                                      overwrite=overwrite)
 
235
        else:
 
236
            if ignore_master:
 
237
                master = None
 
238
            else:
 
239
                master = to_tags.branch.get_master_branch()
 
240
            with contextlib.ExitStack() as es:
 
241
                if master is not None:
 
242
                    es.enter_context(master.lock_write())
 
243
                updates, conflicts = self._merge_to_non_git(
 
244
                    to_tags, source_tag_refs, overwrite=overwrite)
 
245
                if master is not None:
 
246
                    extra_updates, extra_conflicts = self.merge_to(
 
247
                        master.tags, overwrite=overwrite,
 
248
                        source_tag_refs=source_tag_refs,
 
249
                        ignore_master=ignore_master)
 
250
                    updates.update(extra_updates)
 
251
                    conflicts += extra_conflicts
 
252
                return updates, conflicts
 
253
 
43
254
    def get_tag_dict(self):
44
255
        ret = {}
45
 
        for k,v in self.repository._git.tags.iteritems():
46
 
            obj = self.repository._git.get_object(v)
47
 
            while isinstance(obj, Tag):
48
 
                v = obj.object[1]
49
 
                obj = self.repository._git.get_object(v)
50
 
            if not isinstance(obj, Commit):
51
 
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
 
256
        for (ref_name, tag_name, peeled, unpeeled) in (
 
257
                self.branch.get_tag_refs()):
 
258
            try:
 
259
                bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
 
260
            except NotCommitError:
52
261
                continue
53
 
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
262
            else:
 
263
                ret[tag_name] = bzr_revid
54
264
        return ret
55
265
 
 
266
 
 
267
class LocalGitTagDict(GitTags):
 
268
    """Dictionary with tags in a local repository."""
 
269
 
 
270
    def __init__(self, branch):
 
271
        super(LocalGitTagDict, self).__init__(branch)
 
272
        self.refs = self.repository.controldir._git.refs
 
273
 
 
274
    def _set_tag_dict(self, to_dict):
 
275
        extra = set(self.refs.allkeys())
 
276
        for k, revid in to_dict.items():
 
277
            name = tag_name_to_ref(k)
 
278
            if name in extra:
 
279
                extra.remove(name)
 
280
            try:
 
281
                self.set_tag(k, revid)
 
282
            except errors.GhostTagsNotSupported:
 
283
                pass
 
284
        for name in extra:
 
285
            if is_tag(name):
 
286
                del self.repository._git[name]
 
287
 
56
288
    def set_tag(self, name, revid):
57
 
        self.repository._git.tags[name] = revid
58
 
 
59
 
 
60
 
class GitBranchConfig(config.BranchConfig):
61
 
    """BranchConfig that uses locations.conf in place of branch.conf"""
62
 
 
63
 
    def __init__(self, branch):
64
 
        config.BranchConfig.__init__(self, branch)
65
 
        # do not provide a BranchDataConfig
66
 
        self.option_sources = self.option_sources[0], self.option_sources[2]
67
 
 
68
 
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
69
 
        """Force local to True"""
70
 
        config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
 
289
        try:
 
290
            git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
 
291
        except errors.NoSuchRevision:
 
292
            raise errors.GhostTagsNotSupported(self)
 
293
        self.refs[tag_name_to_ref(name)] = git_sha
 
294
        self.branch._tag_refs = None
 
295
 
 
296
    def delete_tag(self, name):
 
297
        ref = tag_name_to_ref(name)
 
298
        if ref not in self.refs:
 
299
            raise errors.NoSuchTag(name)
 
300
        del self.refs[ref]
 
301
        self.branch._tag_refs = None
71
302
 
72
303
 
73
304
class GitBranchFormat(branch.BranchFormat):
74
305
 
 
306
    def network_name(self):
 
307
        return b"git"
 
308
 
 
309
    def supports_tags(self):
 
310
        return True
 
311
 
 
312
    def supports_leaving_lock(self):
 
313
        return False
 
314
 
 
315
    def supports_tags_referencing_ghosts(self):
 
316
        return False
 
317
 
 
318
    def tags_are_versioned(self):
 
319
        return False
 
320
 
 
321
    def get_foreign_tests_branch_factory(self):
 
322
        from .tests.test_branch import ForeignTestsBranchFactory
 
323
        return ForeignTestsBranchFactory()
 
324
 
 
325
    def make_tags(self, branch):
 
326
        try:
 
327
            return branch.tags
 
328
        except AttributeError:
 
329
            pass
 
330
        if getattr(branch.repository, "_git", None) is None:
 
331
            from .remote import RemoteGitTagDict
 
332
            return RemoteGitTagDict(branch)
 
333
        else:
 
334
            return LocalGitTagDict(branch)
 
335
 
 
336
    def initialize(self, a_controldir, name=None, repository=None,
 
337
                   append_revisions_only=None):
 
338
        raise NotImplementedError(self.initialize)
 
339
 
 
340
    def get_reference(self, controldir, name=None):
 
341
        return controldir.get_branch_reference(name=name)
 
342
 
 
343
    def set_reference(self, controldir, name, target):
 
344
        return controldir.set_branch_reference(target, name)
 
345
 
 
346
    def stores_revno(self):
 
347
        """True if this branch format store revision numbers."""
 
348
        return False
 
349
 
 
350
    supports_reference_locations = False
 
351
 
 
352
 
 
353
class LocalGitBranchFormat(GitBranchFormat):
 
354
 
75
355
    def get_format_description(self):
76
 
        return 'Git Branch'
77
 
 
78
 
    def supports_tags(self):
79
 
        return True
 
356
        return 'Local Git Branch'
 
357
 
 
358
    @property
 
359
    def _matchingcontroldir(self):
 
360
        from .dir import LocalGitControlDirFormat
 
361
        return LocalGitControlDirFormat()
 
362
 
 
363
    def initialize(self, a_controldir, name=None, repository=None,
 
364
                   append_revisions_only=None):
 
365
        from .dir import LocalGitDir
 
366
        if not isinstance(a_controldir, LocalGitDir):
 
367
            raise errors.IncompatibleFormat(self, a_controldir._format)
 
368
        return a_controldir.create_branch(
 
369
            repository=repository, name=name,
 
370
            append_revisions_only=append_revisions_only)
80
371
 
81
372
 
82
373
class GitBranch(ForeignBranch):
83
374
    """An adapter to git repositories for bzr Branch objects."""
84
375
 
85
 
    def __init__(self, bzrdir, repository, name, head, lockfiles):
 
376
    @property
 
377
    def control_transport(self):
 
378
        return self._control_transport
 
379
 
 
380
    @property
 
381
    def user_transport(self):
 
382
        return self._user_transport
 
383
 
 
384
    def __init__(self, controldir, repository, ref, format):
86
385
        self.repository = repository
 
386
        self._format = format
 
387
        self.controldir = controldir
 
388
        self._lock_mode = None
 
389
        self._lock_count = 0
87
390
        super(GitBranch, self).__init__(repository.get_mapping())
88
 
        self.control_files = lockfiles
89
 
        self.bzrdir = bzrdir
90
 
        self.name = name
91
 
        self.head = head
92
 
        self.base = bzrdir.transport.base
93
 
        self._format = GitBranchFormat()
94
 
 
95
 
    def dpull(self, source, stop_revision=None):
96
 
        if stop_revision is None:
97
 
            stop_revision = source.last_revision()
98
 
        # FIXME: Check for diverged branches
99
 
        revidmap = self.repository.dfetch(source.repository, stop_revision)
100
 
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
101
 
        return revidmap
102
 
 
103
 
    def lock_write(self):
104
 
        self.control_files.lock_write()
 
391
        self.ref = ref
 
392
        self._head = None
 
393
        self._user_transport = controldir.user_transport.clone('.')
 
394
        self._control_transport = controldir.control_transport.clone('.')
 
395
        self._tag_refs = None
 
396
        params = {}
 
397
        try:
 
398
            self.name = ref_to_branch_name(ref)
 
399
        except ValueError:
 
400
            self.name = None
 
401
            if self.ref is not None:
 
402
                params = {"ref": urlutils.escape(self.ref)}
 
403
        else:
 
404
            if self.name != "":
 
405
                params = {"branch": urlutils.escape(self.name)}
 
406
        for k, v in params.items():
 
407
            self._user_transport.set_segment_parameter(k, v)
 
408
            self._control_transport.set_segment_parameter(k, v)
 
409
        self.base = controldir.user_transport.base
 
410
 
 
411
    def _get_checkout_format(self, lightweight=False):
 
412
        """Return the most suitable metadir for a checkout of this branch.
 
413
        Weaves are used if this branch's repository uses weaves.
 
414
        """
 
415
        if lightweight:
 
416
            return controldir.format_registry.make_controldir("git")
 
417
        else:
 
418
            return controldir.format_registry.make_controldir("default")
 
419
 
 
420
    def get_child_submit_format(self):
 
421
        """Return the preferred format of submissions to this branch."""
 
422
        ret = self.get_config_stack().get("child_submit_format")
 
423
        if ret is not None:
 
424
            return ret
 
425
        return "git"
 
426
 
 
427
    def get_config(self):
 
428
        return GitBranchConfig(self)
 
429
 
 
430
    def get_config_stack(self):
 
431
        return GitBranchStack(self)
 
432
 
 
433
    def _get_nick(self, local=False, possible_master_transports=None):
 
434
        """Find the nick name for this branch.
 
435
 
 
436
        :return: Branch nick
 
437
        """
 
438
        if getattr(self.repository, '_git', None):
 
439
            cs = self.repository._git.get_config_stack()
 
440
            try:
 
441
                return cs.get((b"branch", self.name.encode('utf-8')),
 
442
                              b"nick").decode("utf-8")
 
443
            except KeyError:
 
444
                pass
 
445
        return self.name or u"HEAD"
 
446
 
 
447
    def _set_nick(self, nick):
 
448
        cf = self.repository._git.get_config()
 
449
        cf.set((b"branch", self.name.encode('utf-8')),
 
450
               b"nick", nick.encode("utf-8"))
 
451
        f = BytesIO()
 
452
        cf.write_to_file(f)
 
453
        self.repository._git._put_named_file('config', f.getvalue())
 
454
 
 
455
    nick = property(_get_nick, _set_nick)
 
456
 
 
457
    def __repr__(self):
 
458
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
 
459
                                 self.name)
 
460
 
 
461
    def generate_revision_history(self, revid, last_rev=None,
 
462
                                  other_branch=None):
 
463
        if last_rev is not None:
 
464
            graph = self.repository.get_graph()
 
465
            if not graph.is_ancestor(last_rev, revid):
 
466
                # our previous tip is not merged into stop_revision
 
467
                raise errors.DivergedBranches(self, other_branch)
 
468
 
 
469
        self.set_last_revision(revid)
 
470
 
 
471
    def lock_write(self, token=None):
 
472
        if token is not None:
 
473
            raise errors.TokenLockingNotSupported(self)
 
474
        if self._lock_mode:
 
475
            if self._lock_mode == 'r':
 
476
                raise errors.ReadOnlyError(self)
 
477
            self._lock_count += 1
 
478
        else:
 
479
            self._lock_ref()
 
480
            self._lock_mode = 'w'
 
481
            self._lock_count = 1
 
482
        self.repository.lock_write()
 
483
        return lock.LogicalLockResult(self.unlock)
 
484
 
 
485
    def leave_lock_in_place(self):
 
486
        raise NotImplementedError(self.leave_lock_in_place)
 
487
 
 
488
    def dont_leave_lock_in_place(self):
 
489
        raise NotImplementedError(self.dont_leave_lock_in_place)
105
490
 
106
491
    def get_stacked_on_url(self):
107
492
        # Git doesn't do stacking (yet...)
108
 
        return None
109
 
 
110
 
    def get_parent(self):
 
493
        raise branch.UnstackableBranchFormat(self._format, self.base)
 
494
 
 
495
    def _get_push_origin(self, cs):
 
496
        """Get the name for the push origin.
 
497
 
 
498
        The exact behaviour is documented in the git-config(1) manpage.
 
499
        """
 
500
        try:
 
501
            return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
 
502
        except KeyError:
 
503
            try:
 
504
                return cs.get((b'branch', ), b'remote')
 
505
            except KeyError:
 
506
                try:
 
507
                    return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
 
508
                except KeyError:
 
509
                    return b'origin'
 
510
 
 
511
    def _get_origin(self, cs):
 
512
        try:
 
513
            return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
 
514
        except KeyError:
 
515
            return b'origin'
 
516
 
 
517
    def _get_related_push_branch(self, cs):
 
518
        remote = self._get_push_origin(cs)
 
519
        try:
 
520
            location = cs.get((b"remote", remote), b"url")
 
521
        except KeyError:
 
522
            return None
 
523
 
 
524
        return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
 
525
 
 
526
    def _get_related_merge_branch(self, cs):
 
527
        remote = self._get_origin(cs)
 
528
        try:
 
529
            location = cs.get((b"remote", remote), b"url")
 
530
        except KeyError:
 
531
            return None
 
532
 
 
533
        try:
 
534
            ref = cs.get((b"branch", remote), b"merge")
 
535
        except KeyError:
 
536
            ref = self.ref
 
537
 
 
538
        return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
 
539
 
 
540
    def _get_parent_location(self):
111
541
        """See Branch.get_parent()."""
112
 
        return None
113
 
 
114
 
    def set_parent(self, url):
115
 
        pass
 
542
        cs = self.repository._git.get_config_stack()
 
543
        return self._get_related_merge_branch(cs)
 
544
 
 
545
    def _write_git_config(self, cs):
 
546
        f = BytesIO()
 
547
        cs.write_to_file(f)
 
548
        self.repository._git._put_named_file('config', f.getvalue())
 
549
 
 
550
    def set_parent(self, location):
 
551
        cs = self.repository._git.get_config()
 
552
        remote = self._get_origin(cs)
 
553
        this_url = urlutils.strip_segment_parameters(self.user_url)
 
554
        target_url, branch, ref = bzr_url_to_git_url(location)
 
555
        location = urlutils.relative_url(this_url, target_url)
 
556
        cs.set((b"remote", remote), b"url", location)
 
557
        if branch:
 
558
            cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
 
559
        elif ref:
 
560
            cs.set((b"branch", remote), b"merge", ref)
 
561
        else:
 
562
            # TODO(jelmer): Maybe unset rather than setting to HEAD?
 
563
            cs.set((b"branch", remote), b"merge", b'HEAD')
 
564
        self._write_git_config(cs)
 
565
 
 
566
    def break_lock(self):
 
567
        raise NotImplementedError(self.break_lock)
116
568
 
117
569
    def lock_read(self):
118
 
        self.control_files.lock_read()
 
570
        if self._lock_mode:
 
571
            if self._lock_mode not in ('r', 'w'):
 
572
                raise ValueError(self._lock_mode)
 
573
            self._lock_count += 1
 
574
        else:
 
575
            self._lock_mode = 'r'
 
576
            self._lock_count = 1
 
577
        self.repository.lock_read()
 
578
        return lock.LogicalLockResult(self.unlock)
 
579
 
 
580
    def peek_lock_mode(self):
 
581
        return self._lock_mode
 
582
 
 
583
    def is_locked(self):
 
584
        return (self._lock_mode is not None)
 
585
 
 
586
    def _lock_ref(self):
 
587
        pass
 
588
 
 
589
    def _unlock_ref(self):
 
590
        pass
119
591
 
120
592
    def unlock(self):
121
 
        self.control_files.unlock()
 
593
        """See Branch.unlock()."""
 
594
        if self._lock_count == 0:
 
595
            raise errors.LockNotHeld(self)
 
596
        try:
 
597
            self._lock_count -= 1
 
598
            if self._lock_count == 0:
 
599
                if self._lock_mode == 'w':
 
600
                    self._unlock_ref()
 
601
                self._lock_mode = None
 
602
                self._clear_cached_state()
 
603
        finally:
 
604
            self.repository.unlock()
122
605
 
123
606
    def get_physical_lock_status(self):
124
607
        return False
125
608
 
126
 
 
 
609
    def last_revision(self):
 
610
        with self.lock_read():
 
611
            # perhaps should escape this ?
 
612
            if self.head is None:
 
613
                return revision.NULL_REVISION
 
614
            return self.lookup_foreign_revision_id(self.head)
 
615
 
 
616
    def _basic_push(self, target, overwrite=False, stop_revision=None):
 
617
        return branch.InterBranch.get(self, target)._basic_push(
 
618
            overwrite, stop_revision)
 
619
 
 
620
    def lookup_foreign_revision_id(self, foreign_revid):
 
621
        try:
 
622
            return self.repository.lookup_foreign_revision_id(foreign_revid,
 
623
                                                              self.mapping)
 
624
        except KeyError:
 
625
            # Let's try..
 
626
            return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
 
627
 
 
628
    def lookup_bzr_revision_id(self, revid):
 
629
        return self.repository.lookup_bzr_revision_id(
 
630
            revid, mapping=self.mapping)
 
631
 
 
632
    def get_unshelver(self, tree):
 
633
        raise errors.StoringUncommittedNotSupported(self)
 
634
 
 
635
    def _clear_cached_state(self):
 
636
        super(GitBranch, self)._clear_cached_state()
 
637
        self._tag_refs = None
 
638
 
 
639
    def _iter_tag_refs(self, refs):
 
640
        """Iterate over the tag refs.
 
641
 
 
642
        :param refs: Refs dictionary (name -> git sha1)
 
643
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
 
644
        """
 
645
        raise NotImplementedError(self._iter_tag_refs)
 
646
 
 
647
    def get_tag_refs(self):
 
648
        with self.lock_read():
 
649
            if self._tag_refs is None:
 
650
                self._tag_refs = list(self._iter_tag_refs())
 
651
            return self._tag_refs
 
652
 
 
653
    def import_last_revision_info_and_tags(self, source, revno, revid,
 
654
                                           lossy=False):
 
655
        """Set the last revision info, importing from another repo if necessary.
 
656
 
 
657
        This is used by the bound branch code to upload a revision to
 
658
        the master branch first before updating the tip of the local branch.
 
659
        Revisions referenced by source's tags are also transferred.
 
660
 
 
661
        :param source: Source branch to optionally fetch from
 
662
        :param revno: Revision number of the new tip
 
663
        :param revid: Revision id of the new tip
 
664
        :param lossy: Whether to discard metadata that can not be
 
665
            natively represented
 
666
        :return: Tuple with the new revision number and revision id
 
667
            (should only be different from the arguments when lossy=True)
 
668
        """
 
669
        push_result = source.push(
 
670
            self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
 
671
        return (push_result.new_revno, push_result.new_revid)
 
672
 
 
673
    def reconcile(self, thorough=True):
 
674
        """Make sure the data stored in this branch is consistent."""
 
675
        from ..reconcile import ReconcileResult
 
676
        # Nothing to do here
 
677
        return ReconcileResult()
 
678
 
 
679
 
127
680
class LocalGitBranch(GitBranch):
128
 
 
129
 
    @needs_read_lock
130
 
    def last_revision(self):
131
 
        # perhaps should escape this ?
132
 
        if self.head is None:
133
 
            return revision.NULL_REVISION
134
 
        return self.mapping.revision_id_foreign_to_bzr(self.head)
135
 
 
136
 
    def create_checkout(self, to_location, revision_id=None, 
137
 
                        lightweight=False, accelerator_tree=None, hardlink=False):
 
681
    """A local Git branch."""
 
682
 
 
683
    def __init__(self, controldir, repository, ref):
 
684
        super(LocalGitBranch, self).__init__(controldir, repository, ref,
 
685
                                             LocalGitBranchFormat())
 
686
 
 
687
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
 
688
                        accelerator_tree=None, hardlink=False):
 
689
        t = transport.get_transport(to_location)
 
690
        t.ensure_base()
 
691
        format = self._get_checkout_format(lightweight=lightweight)
 
692
        checkout = format.initialize_on_transport(t)
138
693
        if lightweight:
139
 
            raise LightWeightCheckoutsNotSupported()
140
 
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
141
 
 
142
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
143
 
                                     hardlink=False):
144
 
        """Create a new heavyweight checkout of this branch.
145
 
 
146
 
        :param to_location: URL of location to create the new checkout in.
147
 
        :param revision_id: Revision that should be the tip of the checkout.
148
 
        :param hardlink: Whether to hardlink
149
 
        :return: WorkingTree object of checkout.
150
 
        """
151
 
        checkout_branch = BzrDir.create_branch_convenience(
152
 
            to_location, force_new_tree=False, format=get_rich_root_format())
153
 
        checkout = checkout_branch.bzrdir
154
 
        checkout_branch.bind(self)
155
 
        # pull up to the specified revision_id to set the initial 
156
 
        # branch tip correctly, and seed it with history.
157
 
        checkout_branch.pull(self, stop_revision=revision_id)
158
 
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
159
 
 
160
 
    def _make_tags(self):
161
 
        return GitTagDict(self)
 
694
            from_branch = checkout.set_branch_reference(target_branch=self)
 
695
        else:
 
696
            policy = checkout.determine_repository_policy()
 
697
            policy.acquire_repository()
 
698
            checkout_branch = checkout.create_branch()
 
699
            checkout_branch.bind(self)
 
700
            checkout_branch.pull(self, stop_revision=revision_id)
 
701
            from_branch = None
 
702
        return checkout.create_workingtree(
 
703
            revision_id, from_branch=from_branch, hardlink=hardlink)
 
704
 
 
705
    def _lock_ref(self):
 
706
        self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
 
707
 
 
708
    def _unlock_ref(self):
 
709
        self._ref_lock.unlock()
 
710
 
 
711
    def break_lock(self):
 
712
        self.repository._git.refs.unlock_ref(self.ref)
162
713
 
163
714
    def _gen_revision_history(self):
164
715
        if self.head is None:
165
716
            return []
166
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
717
        last_revid = self.last_revision()
 
718
        graph = self.repository.get_graph()
 
719
        try:
 
720
            ret = list(graph.iter_lefthand_ancestry(
 
721
                last_revid, (revision.NULL_REVISION, )))
 
722
        except errors.RevisionNotPresent as e:
 
723
            raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
167
724
        ret.reverse()
168
725
        return ret
169
726
 
170
 
    def get_config(self):
171
 
        return GitBranchConfig(self)
 
727
    def _get_head(self):
 
728
        try:
 
729
            return self.repository._git.refs[self.ref]
 
730
        except KeyError:
 
731
            return None
 
732
 
 
733
    def _read_last_revision_info(self):
 
734
        last_revid = self.last_revision()
 
735
        graph = self.repository.get_graph()
 
736
        try:
 
737
            revno = graph.find_distance_to_null(
 
738
                last_revid, [(revision.NULL_REVISION, 0)])
 
739
        except errors.GhostRevisionsHaveNoRevno:
 
740
            revno = None
 
741
        return revno, last_revid
 
742
 
 
743
    def set_last_revision_info(self, revno, revision_id):
 
744
        self.set_last_revision(revision_id)
 
745
        self._last_revision_info_cache = revno, revision_id
 
746
 
 
747
    def set_last_revision(self, revid):
 
748
        if not revid or not isinstance(revid, bytes):
 
749
            raise errors.InvalidRevisionId(revision_id=revid, branch=self)
 
750
        if revid == NULL_REVISION:
 
751
            newhead = None
 
752
        else:
 
753
            (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
 
754
                revid)
 
755
            if self.mapping is None:
 
756
                raise AssertionError
 
757
        self._set_head(newhead)
 
758
 
 
759
    def _set_head(self, value):
 
760
        if value == ZERO_SHA:
 
761
            raise ValueError(value)
 
762
        self._head = value
 
763
        if value is None:
 
764
            del self.repository._git.refs[self.ref]
 
765
        else:
 
766
            self.repository._git.refs[self.ref] = self._head
 
767
        self._clear_cached_state()
 
768
 
 
769
    head = property(_get_head, _set_head)
172
770
 
173
771
    def get_push_location(self):
174
772
        """See Branch.get_push_location."""
175
 
        push_loc = self.get_config().get_user_option('push_location')
176
 
        return push_loc
 
773
        push_loc = self.get_config_stack().get('push_location')
 
774
        if push_loc is not None:
 
775
            return push_loc
 
776
        cs = self.repository._git.get_config_stack()
 
777
        return self._get_related_push_branch(cs)
177
778
 
178
779
    def set_push_location(self, location):
179
780
        """See Branch.set_push_location."""
183
784
    def supports_tags(self):
184
785
        return True
185
786
 
186
 
    def sprout(self, to_bzrdir, revision_id=None):
187
 
        """See Branch.sprout()."""
188
 
        result = to_bzrdir.create_branch()
189
 
        self.copy_content_into(result, revision_id=revision_id)
190
 
        result.set_parent(self.bzrdir.root_transport.base)
191
 
        return result
192
 
 
193
 
 
194
 
class InterGitGenericBranch(branch.InterBranch):
195
 
 
196
 
    @classmethod
197
 
    def is_compatible(self, source, target):
198
 
        return isinstance(source, GitBranch)
199
 
 
200
 
    def update_revisions(self, stop_revision=None, overwrite=False,
201
 
        graph=None):
202
 
        """See InterBranch.update_revisions()."""
203
 
        # TODO: stop_revision, overwrite
204
 
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
 
        self._last_revid = None
 
787
    def store_uncommitted(self, creator):
 
788
        raise errors.StoringUncommittedNotSupported(self)
 
789
 
 
790
    def _iter_tag_refs(self):
 
791
        """Iterate over the tag refs.
 
792
 
 
793
        :param refs: Refs dictionary (name -> git sha1)
 
794
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
 
795
        """
 
796
        refs = self.repository.controldir.get_refs_container()
 
797
        for ref_name, unpeeled in refs.as_dict().items():
 
798
            try:
 
799
                tag_name = ref_to_tag_name(ref_name)
 
800
            except (ValueError, UnicodeDecodeError):
 
801
                continue
 
802
            peeled = refs.get_peeled(ref_name)
 
803
            if peeled is None:
 
804
                peeled = unpeeled
 
805
            if not isinstance(tag_name, str):
 
806
                raise TypeError(tag_name)
 
807
            yield (ref_name, tag_name, peeled, unpeeled)
 
808
 
 
809
    def create_memorytree(self):
 
810
        from .memorytree import GitMemoryTree
 
811
        return GitMemoryTree(self, self.repository._git.object_store,
 
812
                             self.head)
 
813
 
 
814
 
 
815
def _quick_lookup_revno(local_branch, remote_branch, revid):
 
816
    if not isinstance(revid, bytes):
 
817
        raise TypeError(revid)
 
818
    # Try in source branch first, it'll be faster
 
819
    with local_branch.lock_read():
 
820
        if not _calculate_revnos(local_branch):
 
821
            return None
 
822
        try:
 
823
            return local_branch.revision_id_to_revno(revid)
 
824
        except errors.NoSuchRevision:
 
825
            graph = local_branch.repository.get_graph()
 
826
            try:
 
827
                return graph.find_distance_to_null(
 
828
                    revid, [(revision.NULL_REVISION, 0)])
 
829
            except errors.GhostRevisionsHaveNoRevno:
 
830
                if not _calculate_revnos(remote_branch):
 
831
                    return None
 
832
                # FIXME: Check using graph.find_distance_to_null() ?
 
833
                with remote_branch.lock_read():
 
834
                    return remote_branch.revision_id_to_revno(revid)
 
835
 
 
836
 
 
837
class GitBranchPullResult(branch.PullResult):
 
838
 
 
839
    def __init__(self):
 
840
        super(GitBranchPullResult, self).__init__()
 
841
        self.new_git_head = None
 
842
        self._old_revno = None
 
843
        self._new_revno = None
 
844
 
 
845
    def report(self, to_file):
 
846
        if not is_quiet():
 
847
            if self.old_revid == self.new_revid:
 
848
                to_file.write('No revisions to pull.\n')
 
849
            elif self.new_git_head is not None:
 
850
                to_file.write('Now on revision %d (git sha: %s).\n' %
 
851
                              (self.new_revno, self.new_git_head))
 
852
            else:
 
853
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
 
854
        self._show_tag_conficts(to_file)
 
855
 
 
856
    def _lookup_revno(self, revid):
 
857
        return _quick_lookup_revno(self.target_branch, self.source_branch,
 
858
                                   revid)
 
859
 
 
860
    def _get_old_revno(self):
 
861
        if self._old_revno is not None:
 
862
            return self._old_revno
 
863
        return self._lookup_revno(self.old_revid)
 
864
 
 
865
    def _set_old_revno(self, revno):
 
866
        self._old_revno = revno
 
867
 
 
868
    old_revno = property(_get_old_revno, _set_old_revno)
 
869
 
 
870
    def _get_new_revno(self):
 
871
        if self._new_revno is not None:
 
872
            return self._new_revno
 
873
        return self._lookup_revno(self.new_revid)
 
874
 
 
875
    def _set_new_revno(self, revno):
 
876
        self._new_revno = revno
 
877
 
 
878
    new_revno = property(_get_new_revno, _set_new_revno)
 
879
 
 
880
 
 
881
class GitBranchPushResult(branch.BranchPushResult):
 
882
 
 
883
    def _lookup_revno(self, revid):
 
884
        return _quick_lookup_revno(self.source_branch, self.target_branch,
 
885
                                   revid)
 
886
 
 
887
    @property
 
888
    def old_revno(self):
 
889
        return self._lookup_revno(self.old_revid)
 
890
 
 
891
    @property
 
892
    def new_revno(self):
 
893
        new_original_revno = getattr(self, "new_original_revno", None)
 
894
        if new_original_revno:
 
895
            return new_original_revno
 
896
        if getattr(self, "new_original_revid", None) is not None:
 
897
            return self._lookup_revno(self.new_original_revid)
 
898
        return self._lookup_revno(self.new_revid)
 
899
 
 
900
 
 
901
class InterFromGitBranch(branch.GenericInterBranch):
 
902
    """InterBranch implementation that pulls from Git into bzr."""
 
903
 
 
904
    @staticmethod
 
905
    def _get_branch_formats_to_test():
 
906
        try:
 
907
            default_format = branch.format_registry.get_default()
 
908
        except AttributeError:
 
909
            default_format = branch.BranchFormat._default_format
 
910
        from .remote import RemoteGitBranchFormat
 
911
        return [
 
912
            (RemoteGitBranchFormat(), default_format),
 
913
            (LocalGitBranchFormat(), default_format)]
 
914
 
 
915
    @classmethod
 
916
    def _get_interrepo(self, source, target):
 
917
        return _mod_repository.InterRepository.get(
 
918
            source.repository, target.repository)
 
919
 
 
920
    @classmethod
 
921
    def is_compatible(cls, source, target):
 
922
        if not isinstance(source, GitBranch):
 
923
            return False
 
924
        if isinstance(target, GitBranch):
 
925
            # InterLocalGitRemoteGitBranch or InterToGitBranch should be used
 
926
            return False
 
927
        if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
 
928
                is None):
 
929
            # fetch_objects is necessary for this to work
 
930
            return False
 
931
        return True
 
932
 
 
933
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
 
934
        self.fetch_objects(
 
935
            stop_revision, fetch_tags=fetch_tags, limit=limit, lossy=lossy)
 
936
        return _mod_repository.FetchResult()
 
937
 
 
938
    def fetch_objects(self, stop_revision, fetch_tags, limit=None, lossy=False):
 
939
        interrepo = self._get_interrepo(self.source, self.target)
 
940
        if fetch_tags is None:
 
941
            c = self.source.get_config_stack()
 
942
            fetch_tags = c.get('branch.fetch_tags')
 
943
 
206
944
        def determine_wants(heads):
207
 
            if not self.source.name in heads:
208
 
                raise BzrError("No such remote branch '%s', found: %r" % (
209
 
                    self.source.name, heads.keys()))
210
 
            head = heads[self.source.name]
211
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
212
 
            if self.target.repository.has_revision(self._last_revid):
213
 
                return []
214
 
            return [head]
215
 
        interrepo.fetch_objects(determine_wants, self.source.mapping)
216
 
        self.target.generate_revision_history(self._last_revid)
217
 
 
218
 
 
219
 
branch.InterBranch.register_optimiser(InterGitGenericBranch)
 
945
            if stop_revision is None:
 
946
                try:
 
947
                    head = heads[self.source.ref]
 
948
                except KeyError:
 
949
                    self._last_revid = revision.NULL_REVISION
 
950
                else:
 
951
                    self._last_revid = self.source.lookup_foreign_revision_id(
 
952
                        head)
 
953
            else:
 
954
                self._last_revid = stop_revision
 
955
            real = interrepo.get_determine_wants_revids(
 
956
                [self._last_revid], include_tags=fetch_tags)
 
957
            return real(heads)
 
958
        pack_hint, head, refs = interrepo.fetch_objects(
 
959
            determine_wants, self.source.mapping, limit=limit,
 
960
            lossy=lossy)
 
961
        if (pack_hint is not None and
 
962
                self.target.repository._format.pack_compresses):
 
963
            self.target.repository.pack(hint=pack_hint)
 
964
        return head, refs
 
965
 
 
966
    def _update_revisions(self, stop_revision=None, overwrite=False):
 
967
        head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
 
968
        if overwrite:
 
969
            prev_last_revid = None
 
970
        else:
 
971
            prev_last_revid = self.target.last_revision()
 
972
        self.target.generate_revision_history(
 
973
            self._last_revid, last_rev=prev_last_revid,
 
974
            other_branch=self.source)
 
975
        return head, refs
 
976
 
 
977
    def update_references(self, revid=None):
 
978
        if revid is None:
 
979
            revid = self.target.last_revision()
 
980
        tree = self.target.repository.revision_tree(revid)
 
981
        try:
 
982
            with tree.get_file('.gitmodules') as f:
 
983
                for path, url, section in parse_submodules(
 
984
                        GitConfigFile.from_file(f)):
 
985
                    self.target.set_reference_info(
 
986
                        tree.path2id(path.decode('utf-8')), url.decode('utf-8'),
 
987
                        path.decode('utf-8'))
 
988
        except errors.NoSuchFile:
 
989
            pass
 
990
 
 
991
    def _basic_pull(self, stop_revision, overwrite, run_hooks,
 
992
                    _override_hook_target, _hook_master):
 
993
        if overwrite is True:
 
994
            overwrite = set(["history", "tags"])
 
995
        elif not overwrite:
 
996
            overwrite = set()
 
997
        result = GitBranchPullResult()
 
998
        result.source_branch = self.source
 
999
        if _override_hook_target is None:
 
1000
            result.target_branch = self.target
 
1001
        else:
 
1002
            result.target_branch = _override_hook_target
 
1003
        with self.target.lock_write(), self.source.lock_read():
 
1004
            # We assume that during 'pull' the target repository is closer than
 
1005
            # the source one.
 
1006
            (result.old_revno, result.old_revid) = \
 
1007
                self.target.last_revision_info()
 
1008
            result.new_git_head, remote_refs = self._update_revisions(
 
1009
                stop_revision, overwrite=("history" in overwrite))
 
1010
            tags_ret = self.source.tags.merge_to(
 
1011
                self.target.tags, ("tags" in overwrite), ignore_master=True)
 
1012
            if isinstance(tags_ret, tuple):
 
1013
                result.tag_updates, result.tag_conflicts = tags_ret
 
1014
            else:
 
1015
                result.tag_conflicts = tags_ret
 
1016
            (result.new_revno, result.new_revid) = \
 
1017
                self.target.last_revision_info()
 
1018
            self.update_references(revid=result.new_revid)
 
1019
            if _hook_master:
 
1020
                result.master_branch = _hook_master
 
1021
                result.local_branch = result.target_branch
 
1022
            else:
 
1023
                result.master_branch = result.target_branch
 
1024
                result.local_branch = None
 
1025
            if run_hooks:
 
1026
                for hook in branch.Branch.hooks['post_pull']:
 
1027
                    hook(result)
 
1028
            return result
 
1029
 
 
1030
    def pull(self, overwrite=False, stop_revision=None,
 
1031
             possible_transports=None, _hook_master=None, run_hooks=True,
 
1032
             _override_hook_target=None, local=False):
 
1033
        """See Branch.pull.
 
1034
 
 
1035
        :param _hook_master: Private parameter - set the branch to
 
1036
            be supplied as the master to pull hooks.
 
1037
        :param run_hooks: Private parameter - if false, this branch
 
1038
            is being called because it's the master of the primary branch,
 
1039
            so it should not run its hooks.
 
1040
        :param _override_hook_target: Private parameter - set the branch to be
 
1041
            supplied as the target_branch to pull hooks.
 
1042
        """
 
1043
        # This type of branch can't be bound.
 
1044
        bound_location = self.target.get_bound_location()
 
1045
        if local and not bound_location:
 
1046
            raise errors.LocalRequiresBoundBranch()
 
1047
        source_is_master = False
 
1048
        with contextlib.ExitStack() as es:
 
1049
            es.enter_context(self.source.lock_read())
 
1050
            if bound_location:
 
1051
                # bound_location comes from a config file, some care has to be
 
1052
                # taken to relate it to source.user_url
 
1053
                normalized = urlutils.normalize_url(bound_location)
 
1054
                try:
 
1055
                    relpath = self.source.user_transport.relpath(normalized)
 
1056
                    source_is_master = (relpath == '')
 
1057
                except (errors.PathNotChild, urlutils.InvalidURL):
 
1058
                    source_is_master = False
 
1059
            if not local and bound_location and not source_is_master:
 
1060
                # not pulling from master, so we need to update master.
 
1061
                master_branch = self.target.get_master_branch(possible_transports)
 
1062
                es.enter_context(master_branch.lock_write())
 
1063
                # pull from source into master.
 
1064
                master_branch.pull(self.source, overwrite, stop_revision,
 
1065
                                   run_hooks=False)
 
1066
            else:
 
1067
                master_branch = None
 
1068
            return self._basic_pull(stop_revision, overwrite, run_hooks,
 
1069
                                    _override_hook_target,
 
1070
                                    _hook_master=master_branch)
 
1071
 
 
1072
    def _basic_push(self, overwrite, stop_revision):
 
1073
        if overwrite is True:
 
1074
            overwrite = set(["history", "tags"])
 
1075
        elif not overwrite:
 
1076
            overwrite = set()
 
1077
        result = branch.BranchPushResult()
 
1078
        result.source_branch = self.source
 
1079
        result.target_branch = self.target
 
1080
        result.old_revno, result.old_revid = self.target.last_revision_info()
 
1081
        result.new_git_head, remote_refs = self._update_revisions(
 
1082
            stop_revision, overwrite=("history" in overwrite))
 
1083
        tags_ret = self.source.tags.merge_to(
 
1084
            self.target.tags, "tags" in overwrite, ignore_master=True)
 
1085
        (result.tag_updates, result.tag_conflicts) = tags_ret
 
1086
        result.new_revno, result.new_revid = self.target.last_revision_info()
 
1087
        self.update_references(revid=result.new_revid)
 
1088
        return result
 
1089
 
 
1090
 
 
1091
class InterGitBranch(branch.GenericInterBranch):
 
1092
    """InterBranch implementation that pulls between Git branches."""
 
1093
 
 
1094
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
 
1095
        raise NotImplementedError(self.fetch)
 
1096
 
 
1097
 
 
1098
class InterLocalGitRemoteGitBranch(InterGitBranch):
 
1099
    """InterBranch that copies from a local to a remote git branch."""
 
1100
 
 
1101
    @staticmethod
 
1102
    def _get_branch_formats_to_test():
 
1103
        from .remote import RemoteGitBranchFormat
 
1104
        return [
 
1105
            (LocalGitBranchFormat(), RemoteGitBranchFormat())]
 
1106
 
 
1107
    @classmethod
 
1108
    def is_compatible(self, source, target):
 
1109
        from .remote import RemoteGitBranch
 
1110
        return (isinstance(source, LocalGitBranch) and
 
1111
                isinstance(target, RemoteGitBranch))
 
1112
 
 
1113
    def _basic_push(self, overwrite, stop_revision):
 
1114
        result = GitBranchPushResult()
 
1115
        result.source_branch = self.source
 
1116
        result.target_branch = self.target
 
1117
        if stop_revision is None:
 
1118
            stop_revision = self.source.last_revision()
 
1119
 
 
1120
        def get_changed_refs(old_refs):
 
1121
            old_ref = old_refs.get(self.target.ref, None)
 
1122
            if old_ref is None:
 
1123
                result.old_revid = revision.NULL_REVISION
 
1124
            else:
 
1125
                result.old_revid = self.target.lookup_foreign_revision_id(
 
1126
                    old_ref)
 
1127
            new_ref = self.source.repository.lookup_bzr_revision_id(
 
1128
                stop_revision)[0]
 
1129
            if not overwrite:
 
1130
                if remote_divergence(
 
1131
                        old_ref, new_ref,
 
1132
                        self.source.repository._git.object_store):
 
1133
                    raise errors.DivergedBranches(self.source, self.target)
 
1134
            refs = {self.target.ref: new_ref}
 
1135
            result.new_revid = stop_revision
 
1136
            for name, sha in (
 
1137
                    self.source.repository._git.refs.as_dict(b"refs/tags").items()):
 
1138
                if sha not in self.source.repository._git:
 
1139
                    trace.mutter('Ignoring missing SHA: %s', sha)
 
1140
                    continue
 
1141
                refs[tag_name_to_ref(name)] = sha
 
1142
            return refs
 
1143
        self.target.repository.send_pack(
 
1144
            get_changed_refs,
 
1145
            self.source.repository._git.object_store.generate_pack_data)
 
1146
        return result
 
1147
 
 
1148
 
 
1149
class InterGitLocalGitBranch(InterGitBranch):
 
1150
    """InterBranch that copies from a remote to a local git branch."""
 
1151
 
 
1152
    @staticmethod
 
1153
    def _get_branch_formats_to_test():
 
1154
        from .remote import RemoteGitBranchFormat
 
1155
        return [
 
1156
            (RemoteGitBranchFormat(), LocalGitBranchFormat()),
 
1157
            (LocalGitBranchFormat(), LocalGitBranchFormat())]
 
1158
 
 
1159
    @classmethod
 
1160
    def is_compatible(self, source, target):
 
1161
        return (isinstance(source, GitBranch) and
 
1162
                isinstance(target, LocalGitBranch))
 
1163
 
 
1164
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
 
1165
        interrepo = _mod_repository.InterRepository.get(
 
1166
            self.source.repository, self.target.repository)
 
1167
        if stop_revision is None:
 
1168
            stop_revision = self.source.last_revision()
 
1169
        if fetch_tags is None:
 
1170
            c = self.source.get_config_stack()
 
1171
            fetch_tags = c.get('branch.fetch_tags')
 
1172
        determine_wants = interrepo.get_determine_wants_revids(
 
1173
            [stop_revision], include_tags=fetch_tags)
 
1174
        interrepo.fetch_objects(determine_wants, limit=limit, lossy=lossy)
 
1175
        return _mod_repository.FetchResult()
 
1176
 
 
1177
    def _basic_push(self, overwrite=False, stop_revision=None):
 
1178
        if overwrite is True:
 
1179
            overwrite = set(["history", "tags"])
 
1180
        elif not overwrite:
 
1181
            overwrite = set()
 
1182
        result = GitBranchPushResult()
 
1183
        result.source_branch = self.source
 
1184
        result.target_branch = self.target
 
1185
        result.old_revid = self.target.last_revision()
 
1186
        refs, stop_revision = self.update_refs(stop_revision)
 
1187
        self.target.generate_revision_history(
 
1188
            stop_revision,
 
1189
            (result.old_revid if ("history" not in overwrite) else None),
 
1190
            other_branch=self.source)
 
1191
        tags_ret = self.source.tags.merge_to(
 
1192
            self.target.tags,
 
1193
            source_tag_refs=remote_refs_dict_to_tag_refs(refs),
 
1194
            overwrite=("tags" in overwrite))
 
1195
        if isinstance(tags_ret, tuple):
 
1196
            (result.tag_updates, result.tag_conflicts) = tags_ret
 
1197
        else:
 
1198
            result.tag_conflicts = tags_ret
 
1199
        result.new_revid = self.target.last_revision()
 
1200
        return result
 
1201
 
 
1202
    def update_refs(self, stop_revision=None):
 
1203
        interrepo = _mod_repository.InterRepository.get(
 
1204
            self.source.repository, self.target.repository)
 
1205
        c = self.source.get_config_stack()
 
1206
        fetch_tags = c.get('branch.fetch_tags')
 
1207
 
 
1208
        if stop_revision is None:
 
1209
            result = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
 
1210
            try:
 
1211
                head = result.refs[self.source.ref]
 
1212
            except KeyError:
 
1213
                stop_revision = revision.NULL_REVISION
 
1214
            else:
 
1215
                stop_revision = self.target.lookup_foreign_revision_id(head)
 
1216
        else:
 
1217
            result = interrepo.fetch(
 
1218
                revision_id=stop_revision, include_tags=fetch_tags)
 
1219
        return result.refs, stop_revision
 
1220
 
 
1221
    def pull(self, stop_revision=None, overwrite=False,
 
1222
             possible_transports=None, run_hooks=True, local=False):
 
1223
        # This type of branch can't be bound.
 
1224
        if local:
 
1225
            raise errors.LocalRequiresBoundBranch()
 
1226
        if overwrite is True:
 
1227
            overwrite = set(["history", "tags"])
 
1228
        elif not overwrite:
 
1229
            overwrite = set()
 
1230
 
 
1231
        result = GitPullResult()
 
1232
        result.source_branch = self.source
 
1233
        result.target_branch = self.target
 
1234
        with self.target.lock_write(), self.source.lock_read():
 
1235
            result.old_revid = self.target.last_revision()
 
1236
            refs, stop_revision = self.update_refs(stop_revision)
 
1237
            self.target.generate_revision_history(
 
1238
                stop_revision,
 
1239
                (result.old_revid if ("history" not in overwrite) else None),
 
1240
                other_branch=self.source)
 
1241
            tags_ret = self.source.tags.merge_to(
 
1242
                self.target.tags, overwrite=("tags" in overwrite),
 
1243
                source_tag_refs=remote_refs_dict_to_tag_refs(refs))
 
1244
            if isinstance(tags_ret, tuple):
 
1245
                (result.tag_updates, result.tag_conflicts) = tags_ret
 
1246
            else:
 
1247
                result.tag_conflicts = tags_ret
 
1248
            result.new_revid = self.target.last_revision()
 
1249
            result.local_branch = None
 
1250
            result.master_branch = result.target_branch
 
1251
            if run_hooks:
 
1252
                for hook in branch.Branch.hooks['post_pull']:
 
1253
                    hook(result)
 
1254
        return result
 
1255
 
 
1256
 
 
1257
class InterToGitBranch(branch.GenericInterBranch):
 
1258
    """InterBranch implementation that pulls into a Git branch."""
 
1259
 
 
1260
    def __init__(self, source, target):
 
1261
        super(InterToGitBranch, self).__init__(source, target)
 
1262
        self.interrepo = _mod_repository.InterRepository.get(source.repository,
 
1263
                                                             target.repository)
 
1264
 
 
1265
    @staticmethod
 
1266
    def _get_branch_formats_to_test():
 
1267
        try:
 
1268
            default_format = branch.format_registry.get_default()
 
1269
        except AttributeError:
 
1270
            default_format = branch.BranchFormat._default_format
 
1271
        from .remote import RemoteGitBranchFormat
 
1272
        return [
 
1273
            (default_format, LocalGitBranchFormat()),
 
1274
            (default_format, RemoteGitBranchFormat())]
 
1275
 
 
1276
    @classmethod
 
1277
    def is_compatible(self, source, target):
 
1278
        return (not isinstance(source, GitBranch) and
 
1279
                isinstance(target, GitBranch))
 
1280
 
 
1281
    def _get_new_refs(self, stop_revision=None, fetch_tags=None,
 
1282
                      stop_revno=None):
 
1283
        if not self.source.is_locked():
 
1284
            raise errors.ObjectNotLocked(self.source)
 
1285
        if stop_revision is None:
 
1286
            (stop_revno, stop_revision) = self.source.last_revision_info()
 
1287
        elif stop_revno is None:
 
1288
            try:
 
1289
                stop_revno = self.source.revision_id_to_revno(stop_revision)
 
1290
            except errors.NoSuchRevision:
 
1291
                stop_revno = None
 
1292
        if not isinstance(stop_revision, bytes):
 
1293
            raise TypeError(stop_revision)
 
1294
        main_ref = self.target.ref
 
1295
        refs = {main_ref: (None, stop_revision)}
 
1296
        if fetch_tags is None:
 
1297
            c = self.source.get_config_stack()
 
1298
            fetch_tags = c.get('branch.fetch_tags')
 
1299
        for name, revid in self.source.tags.get_tag_dict().items():
 
1300
            if self.source.repository.has_revision(revid):
 
1301
                ref = tag_name_to_ref(name)
 
1302
                if not check_ref_format(ref):
 
1303
                    warning("skipping tag with invalid characters %s (%s)",
 
1304
                            name, ref)
 
1305
                    continue
 
1306
                if fetch_tags:
 
1307
                    # FIXME: Skip tags that are not in the ancestry
 
1308
                    refs[ref] = (None, revid)
 
1309
        return refs, main_ref, (stop_revno, stop_revision)
 
1310
 
 
1311
    def _update_refs(self, result, old_refs, new_refs, overwrite):
 
1312
        mutter("updating refs. old refs: %r, new refs: %r",
 
1313
               old_refs, new_refs)
 
1314
        result.tag_updates = {}
 
1315
        result.tag_conflicts = []
 
1316
        ret = dict(old_refs)
 
1317
 
 
1318
        def ref_equals(refs, ref, git_sha, revid):
 
1319
            try:
 
1320
                value = refs[ref]
 
1321
            except KeyError:
 
1322
                return False
 
1323
            if (value[0] is not None and
 
1324
                git_sha is not None and
 
1325
                    value[0] == git_sha):
 
1326
                return True
 
1327
            if (value[1] is not None and
 
1328
                revid is not None and
 
1329
                    value[1] == revid):
 
1330
                return True
 
1331
            # FIXME: If one side only has the git sha available and the other
 
1332
            # only has the bzr revid, then this will cause us to show a tag as
 
1333
            # updated that hasn't actually been updated.
 
1334
            return False
 
1335
        # FIXME: Check for diverged branches
 
1336
        for ref, (git_sha, revid) in new_refs.items():
 
1337
            if ref_equals(ret, ref, git_sha, revid):
 
1338
                # Already up to date
 
1339
                if git_sha is None:
 
1340
                    git_sha = old_refs[ref][0]
 
1341
                if revid is None:
 
1342
                    revid = old_refs[ref][1]
 
1343
                ret[ref] = new_refs[ref] = (git_sha, revid)
 
1344
            elif ref not in ret or overwrite:
 
1345
                try:
 
1346
                    tag_name = ref_to_tag_name(ref)
 
1347
                except ValueError:
 
1348
                    pass
 
1349
                else:
 
1350
                    result.tag_updates[tag_name] = revid
 
1351
                ret[ref] = (git_sha, revid)
 
1352
            else:
 
1353
                # FIXME: Check diverged
 
1354
                diverged = False
 
1355
                if diverged:
 
1356
                    try:
 
1357
                        name = ref_to_tag_name(ref)
 
1358
                    except ValueError:
 
1359
                        pass
 
1360
                    else:
 
1361
                        result.tag_conflicts.append(
 
1362
                            (name, revid, ret[name][1]))
 
1363
                else:
 
1364
                    ret[ref] = (git_sha, revid)
 
1365
        return ret
 
1366
 
 
1367
    def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
 
1368
              limit=None):
 
1369
        if stop_revision is None:
 
1370
            stop_revision = self.source.last_revision()
 
1371
        ret = []
 
1372
        if fetch_tags:
 
1373
            for k, v in self.source.tags.get_tag_dict().items():
 
1374
                ret.append((None, v))
 
1375
        ret.append((None, stop_revision))
 
1376
        try:
 
1377
            revidmap = self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
 
1378
        except NoPushSupport:
 
1379
            raise errors.NoRoundtrippingSupport(self.source, self.target)
 
1380
        return _mod_repository.FetchResult(revidmap={
 
1381
            old_revid: new_revid
 
1382
            for (old_revid, (new_sha, new_revid)) in revidmap.items()})
 
1383
 
 
1384
    def pull(self, overwrite=False, stop_revision=None, local=False,
 
1385
             possible_transports=None, run_hooks=True, _stop_revno=None):
 
1386
        result = GitBranchPullResult()
 
1387
        result.source_branch = self.source
 
1388
        result.target_branch = self.target
 
1389
        with self.source.lock_read(), self.target.lock_write():
 
1390
            new_refs, main_ref, stop_revinfo = self._get_new_refs(
 
1391
                stop_revision, stop_revno=_stop_revno)
 
1392
 
 
1393
            def update_refs(old_refs):
 
1394
                return self._update_refs(result, old_refs, new_refs, overwrite)
 
1395
            try:
 
1396
                result.revidmap, old_refs, new_refs = (
 
1397
                    self.interrepo.fetch_refs(update_refs, lossy=False))
 
1398
            except NoPushSupport:
 
1399
                raise errors.NoRoundtrippingSupport(self.source, self.target)
 
1400
            (old_sha1, result.old_revid) = old_refs.get(
 
1401
                main_ref, (ZERO_SHA, NULL_REVISION))
 
1402
            if result.old_revid is None:
 
1403
                result.old_revid = self.target.lookup_foreign_revision_id(
 
1404
                    old_sha1)
 
1405
            result.new_revid = new_refs[main_ref][1]
 
1406
            result.local_branch = None
 
1407
            result.master_branch = self.target
 
1408
            if run_hooks:
 
1409
                for hook in branch.Branch.hooks['post_pull']:
 
1410
                    hook(result)
 
1411
        return result
 
1412
 
 
1413
    def push(self, overwrite=False, stop_revision=None, lossy=False,
 
1414
             _override_hook_source_branch=None, _stop_revno=None):
 
1415
        result = GitBranchPushResult()
 
1416
        result.source_branch = self.source
 
1417
        result.target_branch = self.target
 
1418
        result.local_branch = None
 
1419
        result.master_branch = result.target_branch
 
1420
        with self.source.lock_read(), self.target.lock_write():
 
1421
            new_refs, main_ref, stop_revinfo = self._get_new_refs(
 
1422
                stop_revision, stop_revno=_stop_revno)
 
1423
 
 
1424
            def update_refs(old_refs):
 
1425
                return self._update_refs(result, old_refs, new_refs, overwrite)
 
1426
            try:
 
1427
                result.revidmap, old_refs, new_refs = (
 
1428
                    self.interrepo.fetch_refs(
 
1429
                        update_refs, lossy=lossy, overwrite=overwrite))
 
1430
            except NoPushSupport:
 
1431
                raise errors.NoRoundtrippingSupport(self.source, self.target)
 
1432
            (old_sha1, result.old_revid) = old_refs.get(
 
1433
                main_ref, (ZERO_SHA, NULL_REVISION))
 
1434
            if lossy or result.old_revid is None:
 
1435
                result.old_revid = self.target.lookup_foreign_revision_id(
 
1436
                    old_sha1)
 
1437
            result.new_revid = new_refs[main_ref][1]
 
1438
            (result.new_original_revno,
 
1439
                result.new_original_revid) = stop_revinfo
 
1440
            for hook in branch.Branch.hooks['post_push']:
 
1441
                hook(result)
 
1442
        return result
 
1443
 
 
1444
 
 
1445
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
 
1446
branch.InterBranch.register_optimiser(InterFromGitBranch)
 
1447
branch.InterBranch.register_optimiser(InterToGitBranch)
 
1448
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)