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

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

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