1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 
17
"""Basic push implementation."""
 
 
19
from __future__ import absolute_import
 
 
25
    GitSmartRemoteNotSupported,
 
 
29
class GitPushResult(PushResult):
 
 
31
    def _lookup_revno(self, revid):
 
 
32
        from .branch import _quick_lookup_revno
 
 
34
            return _quick_lookup_revno(self.source_branch, self.target_branch,
 
 
36
        except GitSmartRemoteNotSupported:
 
 
41
        return self._lookup_revno(self.old_revid)
 
 
45
        return self._lookup_revno(self.new_revid)
 
 
48
class MissingObjectsIterator(object):
 
 
49
    """Iterate over git objects that are missing from a target repository.
 
 
53
    def __init__(self, store, source, pb=None):
 
 
54
        """Create a new missing objects iterator.
 
 
58
        self._object_store = store
 
 
62
    def import_revisions(self, revids, lossy):
 
 
63
        """Import a set of revisions into this git repository.
 
 
65
        :param revids: Revision ids of revisions to import
 
 
66
        :param lossy: Whether to not roundtrip bzr metadata
 
 
68
        for i, revid in enumerate(revids):
 
 
70
                self.pb.update("pushing revisions", i, len(revids))
 
 
71
            git_commit = self.import_revision(revid, lossy)
 
 
72
            yield (revid, git_commit)
 
 
74
    def import_revision(self, revid, lossy):
 
 
75
        """Import a revision into this Git repository.
 
 
77
        :param revid: Revision id of the revision
 
 
78
        :param roundtrip: Whether to roundtrip bzr metadata
 
 
80
        tree = self._object_store.tree_cache.revision_tree(revid)
 
 
81
        rev = self.source.get_revision(revid)
 
 
83
        for path, obj in self._object_store._revision_to_objects(
 
 
85
            if obj.type_name == b"commit":
 
 
87
            self._pending.append((obj, path))
 
 
89
            raise AssertionError("no commit object generated for revision %s" %
 
 
94
        return len(self._pending)
 
 
97
        return iter(self._pending)
 
 
100
class ObjectStoreParentsProvider(object):
 
 
102
    def __init__(self, store):
 
 
105
    def get_parent_map(self, shas):
 
 
112
                    parents = self._store[sha].parents
 
 
119
def remote_divergence(old_sha, new_sha, store):
 
 
122
    if not isinstance(old_sha, bytes):
 
 
123
        raise TypeError(old_sha)
 
 
124
    if not isinstance(new_sha, bytes):
 
 
125
        raise TypeError(new_sha)
 
 
126
    from breezy.graph import Graph
 
 
127
    graph = Graph(ObjectStoreParentsProvider(store))
 
 
128
    return not graph.is_ancestor(old_sha, new_sha)