/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: 2018-11-06 02:25:29 UTC
  • mto: This revision was merged to the branch mainline in revision 7150.
  • Revision ID: jelmer@jelmer.uk-20181106022529-qlctdqketvoibpvz
Simplify brz-git, drop imports.

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