/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: Martin
  • Date: 2018-11-16 16:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7172.
  • Revision ID: gzlist@googlemail.com-20181116163822-yg1h1cdng6w7w9kn
Make --profile-imports work on Python 3

Also tweak heading to line up correctly.

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