/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-01-19 19:20:59 UTC
  • mfrom: (7452.1.1 git-submodule-not-checked-out)
  • Revision ID: breezy.the.bot@gmail.com-20200119192059-ad3bxkjwn9k212l0
Don't show submodules that are not checked out as deltas.

Merged from https://code.launchpad.net/~jelmer/brz/git-submodule-not-checked-out/+merge/377807

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