/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
2
#
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.
7
#
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.
12
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.200.261 by Jelmer Vernooij
More formatting fixes.
17
from dulwich.objects import (
18
    Commit,
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
19
    Tag,
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
20
    S_ISGITLINK,
0.200.261 by Jelmer Vernooij
More formatting fixes.
21
    )
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
22
from dulwich.object_store import (
23
    tree_lookup_path,
24
    )
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
25
import re
0.200.352 by Jelmer Vernooij
Simplify mode handling.
26
import stat
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
27
28
from bzrlib import (
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
29
    debug,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
30
    osutils,
0.200.261 by Jelmer Vernooij
More formatting fixes.
31
    trace,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
32
    ui,
33
    urlutils,
34
    )
35
from bzrlib.errors import (
0.239.5 by Jelmer Vernooij
Print user-understandable error message when encountering submodules.
36
    BzrError,
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
37
    NoSuchId,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
38
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
39
from bzrlib.inventory import (
40
    Inventory,
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
41
    InventoryDirectory,
42
    InventoryFile,
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
43
    InventoryLink,
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
44
    TreeReference,
0.200.261 by Jelmer Vernooij
More formatting fixes.
45
    )
0.200.301 by Jelmer Vernooij
Cache inventories created.
46
from bzrlib.lru_cache import (
47
    LRUCache,
48
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
49
from bzrlib.repository import (
50
    InterRepository,
51
    )
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
52
from bzrlib.revision import (
53
    NULL_REVISION,
54
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
55
from bzrlib.tsort import (
56
    topo_sort,
57
    )
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
58
from bzrlib.versionedfile import (
59
    FulltextContentFactory,
60
    )
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
61
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
62
from bzrlib.plugins.git.mapping import (
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
63
    DEFAULT_FILE_MODE,
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
64
    inventory_to_tree_and_blobs,
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
65
    mode_is_executable,
0.200.545 by Jelmer Vernooij
Squash revision data only if necessary.
66
    squash_revision,
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
67
    warn_unusual_mode,
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
68
    )
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
69
from bzrlib.plugins.git.object_store import (
70
    BazaarObjectStore,
71
    )
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
72
from bzrlib.plugins.git.remote import (
73
    RemoteGitRepository,
74
    )
0.200.169 by Jelmer Vernooij
Fix branch cloning.
75
from bzrlib.plugins.git.repository import (
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
76
    GitRepository,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
77
    GitRepositoryFormat,
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
78
    LocalGitRepository,
0.200.261 by Jelmer Vernooij
More formatting fixes.
79
    )
0.216.4 by Jelmer Vernooij
Add basic pack fetch infrastructure.
80
81
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
82
def import_git_blob(texts, mapping, path, hexsha, base_inv, base_ie, parent_id,
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
83
    revision_id, parent_invs, shagitmap, lookup_object, executable, symlink):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
84
    """Import a git blob object into a bzr repository.
85
0.200.261 by Jelmer Vernooij
More formatting fixes.
86
    :param texts: VersionedFiles to add to
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
87
    :param path: Path in the tree
88
    :param blob: A git blob
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
89
    :return: Inventory delta for this file
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
90
    """
91
    file_id = mapping.generate_file_id(path)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
92
    if symlink:
93
        cls = InventoryLink
94
    else:
95
        cls = InventoryFile
96
    # We just have to hope this is indeed utf-8:
0.200.354 by Jelmer Vernooij
Support symlinks in conversion to git.
97
    ie = cls(file_id, urlutils.basename(path).decode("utf-8"), parent_id)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
98
    ie.executable = executable
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
99
    # See if this has changed at all
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
100
    if base_ie is None:
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
101
        base_sha = None
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
102
    else:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
103
        try:
0.200.555 by Jelmer Vernooij
Change kind when turning symlink into dir.
104
            base_sha = shagitmap.lookup_blob(file_id, base_ie.revision)
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
105
        except KeyError:
106
            base_sha = None
107
        else:
108
            if (base_sha == hexsha and base_ie.executable == ie.executable
109
                and base_ie.kind == ie.kind):
110
                # If nothing has changed since the base revision, we're done
111
                return [], []
0.200.484 by Jelmer Vernooij
Cope with kind changes.
112
    if base_sha == hexsha and base_ie.kind == ie.kind:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
113
        ie.text_size = base_ie.text_size
114
        ie.text_sha1 = base_ie.text_sha1
115
        ie.symlink_target = base_ie.symlink_target
0.200.537 by Jelmer Vernooij
Fix handling of not-executable files becoming executable without any other changes.
116
        if ie.executable == base_ie.executable:
117
            ie.revision = base_ie.revision
118
        else:
119
            blob = lookup_object(hexsha)
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
120
    else:
121
        blob = lookup_object(hexsha)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
122
        if ie.kind == "symlink":
0.200.551 by Jelmer Vernooij
Properly set InventoryEntry revision when changing symlink targets.
123
            ie.revision = None
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
124
            ie.symlink_target = blob.data
125
            ie.text_size = None
126
            ie.text_sha1 = None
127
        else:
128
            ie.text_size = len(blob.data)
129
            ie.text_sha1 = osutils.sha_string(blob.data)
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
130
    # Check what revision we should store
0.200.283 by Jelmer Vernooij
Avoid storing repeated texts for blobs.
131
    parent_keys = []
132
    for pinv in parent_invs:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
133
        if pinv.revision_id == base_inv.revision_id:
0.200.374 by Jelmer Vernooij
Also avoid lookups in the inventory in the tree fetch functions.
134
            pie = base_ie
135
            if pie is None:
136
                continue
137
        else:
138
            try:
139
                pie = pinv[file_id]
140
            except NoSuchId:
141
                continue
0.200.551 by Jelmer Vernooij
Properly set InventoryEntry revision when changing symlink targets.
142
        if pie.text_sha1 == ie.text_sha1 and pie.executable == ie.executable and pie.symlink_target == ie.symlink_target:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
143
            # found a revision in one of the parents to use
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
144
            ie.revision = pie.revision
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
145
            break
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
146
        parent_keys.append((file_id, pie.revision))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
147
    if ie.revision is None:
148
        # Need to store a new revision
149
        ie.revision = revision_id
150
        assert file_id is not None
151
        assert ie.revision is not None
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
152
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), tuple(parent_keys), ie.text_sha1, blob.data)])
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
153
        shamap = [(hexsha, "blob", (ie.file_id, ie.revision))]
154
    else:
155
        shamap = []
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
156
    invdelta = []
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
157
    if base_ie is not None:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
158
        old_path = base_inv.id2path(file_id)
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
159
        if base_ie.kind == "directory":
160
            invdelta.extend(remove_disappeared_children(old_path, base_ie.children, []))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
161
    else:
162
        old_path = None
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
163
    invdelta.append((old_path, path, file_id, ie))
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
164
    return (invdelta, shamap)
0.200.261 by Jelmer Vernooij
More formatting fixes.
165
166
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
167
class SubmodulesRequireSubtrees(BzrError):
168
    _fmt = """The repository you are fetching from contains submodules. Please run 'bzr upgrade --development-subtree'."""
0.239.5 by Jelmer Vernooij
Print user-understandable error message when encountering submodules.
169
    internal = False
170
171
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
172
def import_git_submodule(texts, mapping, path, hexsha, base_inv, base_ie,
0.239.5 by Jelmer Vernooij
Print user-understandable error message when encountering submodules.
173
    parent_id, revision_id, parent_invs, shagitmap, lookup_object):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
174
    file_id = mapping.generate_file_id(path)
175
    ie = TreeReference(file_id, urlutils.basename(path.decode("utf-8")),
176
        parent_id)
177
    ie.revision = revision_id
178
    if base_ie is None:
179
        oldpath = None
180
    else:
181
        oldpath = path
182
        if base_ie.kind == ie.kind and base_ie.reference_revision == ie.reference_revision:
183
            ie.revision = base_ie.revision
184
    ie.reference_revision = mapping.revision_id_foreign_to_bzr(hexsha)
185
    texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), (), None, "")])
186
    invdelta = [(oldpath, path, file_id, ie)]
187
    return invdelta, {}, {}
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
188
189
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
190
def remove_disappeared_children(path, base_children, existing_children):
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
191
    ret = []
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
192
    deletable = [(osutils.pathjoin(path, k), v) for k,v in base_children.iteritems() if k not in existing_children]
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
193
    while deletable:
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
194
        (path, ie) = deletable.pop()
195
        ret.append((path, None, ie.file_id, None))
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
196
        if ie.kind == "directory":
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
197
            for name, child_ie in ie.children.iteritems():
198
                deletable.append((osutils.pathjoin(path, name), child_ie))
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
199
    return ret
200
201
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
202
def import_git_tree(texts, mapping, path, hexsha, base_inv, base_ie, parent_id,
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
203
    revision_id, parent_invs, shagitmap, lookup_object):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
204
    """Import a git tree object into a bzr repository.
205
0.200.261 by Jelmer Vernooij
More formatting fixes.
206
    :param texts: VersionedFiles object to add to
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
207
    :param path: Path in the tree
208
    :param tree: A git tree object
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
209
    :param base_inv: Base inventory against which to return inventory delta
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
210
    :return: Inventory delta for this subtree
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
211
    """
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
212
    invdelta = []
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
213
    file_id = mapping.generate_file_id(path)
0.200.297 by Jelmer Vernooij
Cope with non-ascii characters in filenames (needs a test..).
214
    # We just have to hope this is indeed utf-8:
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
215
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")),
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
216
        parent_id)
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
217
    if base_ie is None:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
218
        # Newly appeared here
219
        ie.revision = revision_id
0.200.577 by Jelmer Vernooij
Fix new directory handling.
220
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), (), None, "")])
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
221
        invdelta.append((None, path, file_id, ie))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
222
    else:
223
        # See if this has changed at all
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
224
        try:
0.200.555 by Jelmer Vernooij
Change kind when turning symlink into dir.
225
            base_sha = shagitmap.lookup_tree(file_id, base_inv.revision_id)
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
226
        except KeyError:
227
            pass
0.200.346 by Jelmer Vernooij
Track unusual file modes.
228
        else:
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
229
            if base_sha == hexsha:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
230
                # If nothing has changed since the base revision, we're done
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
231
                return [], {}, []
0.200.553 by Jelmer Vernooij
Support symlinks being turned into directories.
232
        if base_ie.kind != "directory":
233
            ie.revision = revision_id
0.200.577 by Jelmer Vernooij
Fix new directory handling.
234
            texts.insert_record_stream([FulltextContentFactory((ie.file_id, ie.revision), (), None, "")])
0.200.555 by Jelmer Vernooij
Change kind when turning symlink into dir.
235
            invdelta.append((base_inv.id2path(ie.file_id), path, ie.file_id, ie))
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
236
    if base_ie is not None and base_ie.kind == "directory":
237
        base_children = base_ie.children
238
    else:
239
        base_children = {}
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
240
    # Remember for next time
0.200.300 by Jelmer Vernooij
Fix recursive deletion of dirs.
241
    existing_children = set()
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
242
    child_modes = {}
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
243
    shamap = []
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
244
    tree = lookup_object(hexsha)
0.200.363 by Jelmer Vernooij
Fix updates of sha map during fetch.
245
    for mode, name, child_hexsha in tree.entries():
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
246
        basename = name.decode("utf-8")
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
247
        existing_children.add(basename)
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
248
        child_path = osutils.pathjoin(path, name)
0.200.352 by Jelmer Vernooij
Simplify mode handling.
249
        if stat.S_ISDIR(mode):
0.200.555 by Jelmer Vernooij
Change kind when turning symlink into dir.
250
            subinvdelta, grandchildmodes, subshamap = import_git_tree(
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
251
                    texts, mapping, child_path, child_hexsha, base_inv,
0.200.573 by Jelmer Vernooij
Use unicode names.
252
                    base_children.get(basename), file_id, revision_id, parent_invs, shagitmap,
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
253
                    lookup_object)
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
254
            invdelta.extend(subinvdelta)
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
255
            child_modes.update(grandchildmodes)
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
256
            shamap.extend(subshamap)
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
257
        elif S_ISGITLINK(mode): # submodule
258
            subinvdelta, grandchildmodes, subshamap = import_git_submodule(
0.200.592 by Jelmer Vernooij
Avoid 'No such revision' error when encountering submodules.
259
                    texts, mapping, child_path, child_hexsha, base_inv, base_children.get(basename),
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
260
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
261
            invdelta.extend(subinvdelta)
262
            child_modes.update(grandchildmodes)
263
            shamap.extend(subshamap)
0.200.352 by Jelmer Vernooij
Simplify mode handling.
264
        else:
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
265
            subinvdelta, subshamap = import_git_blob(texts, mapping,
0.200.573 by Jelmer Vernooij
Use unicode names.
266
                    child_path, child_hexsha, base_inv, base_children.get(basename), file_id,
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
267
                    revision_id, parent_invs, shagitmap, lookup_object,
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
268
                    mode_is_executable(mode), stat.S_ISLNK(mode))
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
269
            invdelta.extend(subinvdelta)
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
270
            shamap.extend(subshamap)
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
271
        if mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
272
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
0.200.352 by Jelmer Vernooij
Simplify mode handling.
273
            child_modes[child_path] = mode
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
274
    # Remove any children that have disappeared
0.200.570 by Jelmer Vernooij
Simplify remove disappeared children function.
275
    if base_ie is not None and base_ie.kind == "directory":
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
276
        invdelta.extend(remove_disappeared_children(base_inv.id2path(file_id),
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
277
            base_children, existing_children))
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
278
    shamap.append((hexsha, "tree", (file_id, revision_id)))
279
    return invdelta, child_modes, shamap
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
280
281
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
282
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever,
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
283
        heads, pb=None):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
284
    """Import a set of git objects into a bzr repository.
285
0.200.483 by Jelmer Vernooij
Add NEWS entry about sha map.
286
    :param repo: Target Bazaar repository
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
287
    :param mapping: Mapping to use
288
    :param object_iter: Iterator over Git objects.
289
    """
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
290
    def lookup_object(sha):
291
        try:
292
            return object_iter[sha]
293
        except KeyError:
294
            return target_git_object_retriever[sha]
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
295
    # TODO: a more (memory-)efficient implementation of this
0.200.158 by Jelmer Vernooij
fetch works \o/
296
    graph = []
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
297
    root_trees = {}
0.200.158 by Jelmer Vernooij
fetch works \o/
298
    revisions = {}
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
299
    checked = set()
300
    heads = list(heads)
0.200.301 by Jelmer Vernooij
Cache inventories created.
301
    parent_invs_cache = LRUCache(50)
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
302
    # Find and convert commit objects
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
303
    while heads:
304
        if pb is not None:
305
            pb.update("finding revisions to fetch", len(graph), None)
306
        head = heads.pop()
307
        assert isinstance(head, str)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
308
        try:
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
309
            o = lookup_object(head)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
310
        except KeyError:
0.200.591 by Jelmer Vernooij
Don't mask exceptions during fetch.
311
            trace.mutter('missing head %s', head)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
312
            continue
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
313
        if isinstance(o, Commit):
314
            rev = mapping.import_commit(o)
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
315
            if repo.has_revision(rev.revision_id):
316
                continue
0.200.545 by Jelmer Vernooij
Squash revision data only if necessary.
317
            squash_revision(repo, rev)
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
318
            root_trees[rev.revision_id] = o.tree
0.200.158 by Jelmer Vernooij
fetch works \o/
319
            revisions[rev.revision_id] = rev
320
            graph.append((rev.revision_id, rev.parent_ids))
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
321
            target_git_object_retriever._idmap.add_entry(o.id, "commit",
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
322
                    (rev.revision_id, o.tree))
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
323
            heads.extend([p for p in o.parents if p not in checked])
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
324
        elif isinstance(o, Tag):
325
            heads.append(o.object[1])
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
326
        else:
327
            trace.warning("Unable to import head object %r" % o)
328
        checked.add(head)
0.200.158 by Jelmer Vernooij
fetch works \o/
329
    # Order the revisions
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
330
    # Create the inventory objects
0.200.158 by Jelmer Vernooij
fetch works \o/
331
    for i, revid in enumerate(topo_sort(graph)):
332
        if pb is not None:
333
            pb.update("fetching revisions", i, len(graph))
334
        rev = revisions[revid]
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
335
        # We have to do this here, since we have to walk the tree and
336
        # we need to make sure to import the blobs / trees with the right
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
337
        # path; this may involve adding them more than once.
0.200.301 by Jelmer Vernooij
Cache inventories created.
338
        parent_invs = []
339
        for parent_id in rev.parent_ids:
340
            try:
341
                parent_invs.append(parent_invs_cache[parent_id])
342
            except KeyError:
343
                parent_inv = repo.get_inventory(parent_id)
344
                parent_invs.append(parent_inv)
345
                parent_invs_cache[parent_id] = parent_inv
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
346
        if parent_invs == []:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
347
            base_inv = Inventory(root_id=None)
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
348
            base_ie = None
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
349
        else:
350
            base_inv = parent_invs[0]
0.200.571 by Jelmer Vernooij
Keep inventory entry children around once we've fetched them.
351
            base_ie = base_inv.root
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
352
        inv_delta, unusual_modes, shamap = import_git_tree(repo.texts,
353
                mapping, "", root_trees[revid], base_inv, base_ie, None, revid,
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
354
                parent_invs, target_git_object_retriever._idmap, lookup_object)
355
        target_git_object_retriever._idmap.add_entries(shamap)
0.200.346 by Jelmer Vernooij
Track unusual file modes.
356
        if unusual_modes != {}:
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
357
            for path, mode in unusual_modes.iteritems():
358
                warn_unusual_mode(rev.foreign_revid, path, mode)
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
359
            mapping.import_unusual_file_modes(rev, unusual_modes)
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
360
        try:
361
            basis_id = rev.parent_ids[0]
362
        except IndexError:
363
            basis_id = NULL_REVISION
0.200.600 by Jelmer Vernooij
Specify already known basis inventory.
364
            base_inv = None
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
365
        rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
0.200.600 by Jelmer Vernooij
Specify already known basis inventory.
366
                  inv_delta, rev.revision_id, rev.parent_ids,
367
                  base_inv)
0.200.301 by Jelmer Vernooij
Cache inventories created.
368
        parent_invs_cache[rev.revision_id] = inv
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
369
        repo.add_revision(rev.revision_id, rev)
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
370
        if "verify" in debug.debug_flags:
0.200.549 by Jelmer Vernooij
Fix storing of unusual file modes.
371
            new_unusual_modes = mapping.export_unusual_file_modes(rev)
372
            if new_unusual_modes != unusual_modes:
373
                raise AssertionError("unusual modes don't match: %r != %r" % (unusual_modes, new_unusual_modes))
374
            objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping, unusual_modes)
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
375
            for sha1, newobj, path in objs:
376
                assert path is not None
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
377
                oldobj = tree_lookup_path(lookup_object, root_trees[revid], path)
0.200.545 by Jelmer Vernooij
Squash revision data only if necessary.
378
                if oldobj != newobj:
379
                    raise AssertionError("%r != %r in %s" % (oldobj, newobj, path))
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
380
0.200.272 by Jelmer Vernooij
Actually store idmap.
381
    target_git_object_retriever._idmap.commit()
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
382
383
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
384
class InterGitRepository(InterRepository):
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
385
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
386
    _matching_repo_format = GitRepositoryFormat()
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
387
388
    @staticmethod
389
    def _get_repo_format_to_test():
390
        return None
391
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
392
    def copy_content(self, revision_id=None, pb=None):
393
        """See InterRepository.copy_content."""
394
        self.fetch(revision_id, pb, find_ghosts=False)
395
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
396
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, mapping=None,
397
            fetch_spec=None):
0.200.247 by Jelmer Vernooij
Fix git-import.
398
        self.fetch_refs(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts,
399
                mapping=mapping, fetch_spec=fetch_spec)
400
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
401
402
class InterGitNonGitRepository(InterGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
403
    """Base InterRepository that copies revisions from a Git into a non-Git
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
404
    repository."""
405
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
406
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
0.200.247 by Jelmer Vernooij
Fix git-import.
407
              mapping=None, fetch_spec=None):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
408
        if mapping is None:
409
            mapping = self.source.get_mapping()
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
410
        if revision_id is not None:
411
            interesting_heads = [revision_id]
412
        elif fetch_spec is not None:
413
            interesting_heads = fetch_spec.heads
414
        else:
415
            interesting_heads = None
0.200.247 by Jelmer Vernooij
Fix git-import.
416
        self._refs = {}
417
        def determine_wants(refs):
418
            self._refs = refs
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
419
            if interesting_heads is None:
0.200.247 by Jelmer Vernooij
Fix git-import.
420
                ret = [sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
421
            else:
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
422
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
423
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
0.200.576 by Jelmer Vernooij
Auto-pack after fetch.
424
        pack_hint = self.fetch_objects(determine_wants, mapping, pb)
0.200.579 by Jelmer Vernooij
Only pack if it makes the target repo smaller.
425
        if pack_hint is not None and self.target._format.pack_compresses:
0.200.578 by Jelmer Vernooij
Only do optimal packing on bzr >= 1.17.
426
            self.target.pack(hint=pack_hint)
0.200.590 by Jelmer Vernooij
Add check to make sure that the requested heads were actually fetched.
427
        if interesting_heads is not None:
428
            present_interesting_heads = self.target.has_revisions(interesting_heads)
429
            missing_interesting_heads = set(interesting_heads) - present_interesting_heads
430
            if missing_interesting_heads:
431
                raise AssertionError("Missing interesting heads: %r" % missing_interesting_heads)
0.200.247 by Jelmer Vernooij
Fix git-import.
432
        return self._refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
433
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
434
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
435
_GIT_PROGRESS_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
436
def report_git_progress(pb, text):
437
    text = text.rstrip("\r\n")
438
    g = _GIT_PROGRESS_RE.match(text)
439
    if g is not None:
440
        (text, pct, current, total) = g.groups()
441
        pb.update(text, int(current), int(total))
442
    else:
443
        pb.update(text, 0, 0)
444
445
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
446
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
447
    """InterRepository that copies revisions from a remote Git into a non-Git
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
448
    repository."""
449
0.200.582 by Jelmer Vernooij
Use more efficient algorithm for finding out heads.
450
    def get_target_heads(self):
451
        # FIXME: This should be more efficient
452
        all_revs = self.target.all_revision_ids()
453
        parent_map = self.target.get_parent_map(all_revs)
454
        all_parents = set()
455
        map(all_parents.update, parent_map.itervalues())
456
        return set(all_revs) - all_parents
457
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
458
    def fetch_objects(self, determine_wants, mapping, pb=None):
459
        def progress(text):
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
460
            report_git_progress(pb, text)
0.200.466 by Jelmer Vernooij
Fix finding of heads for fetch_objects.
461
        store = BazaarObjectStore(self.target, mapping)
0.200.484 by Jelmer Vernooij
Cope with kind changes.
462
        self.target.lock_write()
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
463
        try:
0.200.582 by Jelmer Vernooij
Use more efficient algorithm for finding out heads.
464
            heads = self.get_target_heads()
0.200.484 by Jelmer Vernooij
Cope with kind changes.
465
            graph_walker = store.get_graph_walker(
466
                    [store._lookup_revision_sha1(head) for head in heads])
467
            recorded_wants = []
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
468
0.200.484 by Jelmer Vernooij
Cope with kind changes.
469
            def record_determine_wants(heads):
470
                wants = determine_wants(heads)
471
                recorded_wants.extend(wants)
472
                return wants
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
473
0.200.484 by Jelmer Vernooij
Cope with kind changes.
474
            create_pb = None
475
            if pb is None:
476
                create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
477
            try:
478
                self.target.start_write_group()
479
                try:
480
                    objects_iter = self.source.fetch_objects(
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
481
                                record_determine_wants, graph_walker,
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
482
                                store.get_raw, progress)
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
483
                    import_git_objects(self.target, mapping, objects_iter,
0.200.466 by Jelmer Vernooij
Fix finding of heads for fetch_objects.
484
                            store, recorded_wants, pb)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
485
                finally:
0.200.591 by Jelmer Vernooij
Don't mask exceptions during fetch.
486
                    pack_hint = self.target.commit_write_group()
487
                return pack_hint
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
488
            finally:
0.200.484 by Jelmer Vernooij
Cope with kind changes.
489
                if create_pb:
490
                    create_pb.finished()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
491
        finally:
0.200.484 by Jelmer Vernooij
Cope with kind changes.
492
            self.target.unlock()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
493
494
    @staticmethod
495
    def is_compatible(source, target):
496
        """Be compatible with GitRepository."""
497
        # FIXME: Also check target uses VersionedFile
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
498
        return (isinstance(source, RemoteGitRepository) and
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
499
                target.supports_rich_root() and
500
                not isinstance(target, GitRepository))
501
502
503
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
504
    """InterRepository that copies revisions from a local Git into a non-Git
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
505
    repository."""
506
507
    def fetch_objects(self, determine_wants, mapping, pb=None):
508
        wants = determine_wants(self.source._git.get_refs())
509
        create_pb = None
510
        if pb is None:
511
            create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
512
        target_git_object_retriever = BazaarObjectStore(self.target, mapping)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
513
        try:
514
            self.target.lock_write()
515
            try:
516
                self.target.start_write_group()
517
                try:
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
518
                    import_git_objects(self.target, mapping,
519
                            self.source._git.object_store,
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
520
                            target_git_object_retriever, wants, pb)
521
                finally:
0.200.591 by Jelmer Vernooij
Don't mask exceptions during fetch.
522
                    pack_hint = self.target.commit_write_group()
523
                return pack_hint
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
524
            finally:
525
                self.target.unlock()
526
        finally:
527
            if create_pb:
528
                create_pb.finished()
529
530
    @staticmethod
531
    def is_compatible(source, target):
532
        """Be compatible with GitRepository."""
533
        # FIXME: Also check target uses VersionedFile
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
534
        return (isinstance(source, LocalGitRepository) and
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
535
                target.supports_rich_root() and
536
                not isinstance(target, GitRepository))
537
538
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
539
class InterGitGitRepository(InterGitRepository):
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
540
    """InterRepository that copies between Git repositories."""
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
541
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
542
    def fetch_objects(self, determine_wants, mapping, pb=None):
543
        def progress(text):
544
            trace.note("git: %s", text)
545
        graphwalker = self.target._git.get_graph_walker()
546
        if isinstance(self.source, LocalGitRepository) and isinstance(self.target, LocalGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
547
            return self.source._git.fetch(self.target._git, determine_wants,
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
548
                progress)
549
        elif isinstance(self.source, LocalGitRepository) and isinstance(self.target, RemoteGitRepository):
550
            raise NotImplementedError
551
        elif isinstance(self.source, RemoteGitRepository) and isinstance(self.target, LocalGitRepository):
552
            f, commit = self.target._git.object_store.add_thin_pack()
553
            try:
554
                refs = self.source._git.fetch_pack(determine_wants, graphwalker,
555
                                                   f.write, progress)
556
                commit()
557
                return refs
558
            except:
559
                f.close()
560
                raise
561
        else:
562
            raise AssertionError
563
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
564
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
565
              mapping=None, fetch_spec=None, branches=None):
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
566
        if mapping is None:
567
            mapping = self.source.get_mapping()
568
        r = self.target._git
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
569
        if revision_id is not None:
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
570
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
571
        elif fetch_spec is not None:
572
            args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
573
        if branches is not None:
574
            determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
575
        elif fetch_spec is None and revision_id is None:
0.200.247 by Jelmer Vernooij
Fix git-import.
576
            determine_wants = r.object_store.determine_wants_all
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
577
        else:
0.200.247 by Jelmer Vernooij
Fix git-import.
578
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
579
        return self.fetch_objects(determine_wants, mapping)
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
580
581
582
    @staticmethod
583
    def is_compatible(source, target):
584
        """Be compatible with GitRepository."""
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
585
        return (isinstance(source, GitRepository) and
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
586
                isinstance(target, GitRepository))