~gagern/bzr-svn/bug242321

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""Generation of file-ids."""

from bzrlib import ui
from bzrlib.errors import NotBranchError, RevisionNotPresent
from bzrlib.knit import KnitVersionedFile
from bzrlib.revision import NULL_REVISION
from bzrlib.trace import mutter

import urllib

from mapping import escape_svn_path, default_mapping

def generate_file_id(repos, revid, inv_path):
    """Generate a file id for a path created in a specific revision.

    :note: This should not necessarily be the revnum in which the 
        node first appeared in Subversion, but the revnum in which 
        bzr-svn required a new file identity to be created.

    :param repos: Repository object.
    :param revid: bzr-svn revision id for the revision in which the path first
                  appeared.
    :param inv_path: Inventory path in the specified revision.
    :return: A file id
    """
    assert isinstance(revid, str)
    assert isinstance(inv_path, unicode)
    (branch, revnum, _) = repos.lookup_revision_id(revid)
    return default_mapping.generate_file_id(repos.uuid, revnum, branch, inv_path)


def get_local_changes(paths, scheme, generate_revid, get_children=None):
    new_paths = {}
    for p in sorted(paths.keys()):
        data = paths[p]
        new_p = scheme.unprefix(p)[1]
        if data[1] is not None:
            try:
                (cbp, crp) = scheme.unprefix(data[1])

                # Branch copy
                if (crp == "" and new_p == ""):
                    data = ('M', None, None)
                else:
                    data = (data[0], crp, generate_revid(
                                  data[2], cbp, str(scheme)))
            except NotBranchError:
                # Copied from outside of a known branch
                # Make it look like the files were added in this revision
                if get_children is not None:
                    for c in get_children(data[1], data[2]):
                        mutter('oops: %r child %r' % (data[1], c))
                        new_paths[(new_p+"/"+c[len(data[1]):].strip("/")).strip("/")] = (data[0], None, -1)
                data = (data[0], None, -1)

        new_paths[new_p] = data
    return new_paths


FILEIDMAP_VERSION = 1

class FileIdMap(object):
    """ File id store. 

    Keeps a map

    revnum -> branch -> path -> fileid
    """
    def __init__(self, repos, cache_transport):
        self.repos = repos
        self.idmap_knit = KnitVersionedFile("fileidmap-v%d" % FILEIDMAP_VERSION, cache_transport, create=True)

    def save(self, revid, parent_revids, _map):
        mutter('saving file id map for %r' % revid)
                
        self.idmap_knit.add_lines_with_ghosts(revid, parent_revids, 
                ["%s\t%s\t%s\n" % (urllib.quote(filename), urllib.quote(_map[filename][0]), 
                                        urllib.quote(_map[filename][1])) for filename in sorted(_map.keys())])

    def load(self, revid):
        map = {}
        for line in self.idmap_knit.get_lines(revid):
            (filename, id, create_revid) = line.rstrip("\n").split("\t", 3)
            map[urllib.unquote(filename)] = (urllib.unquote(id), urllib.unquote(create_revid))
            assert isinstance(map[urllib.unquote(filename)][0], str)

        return map

    def apply_changes(self, uuid, revnum, branch, global_changes, 
                      renames, scheme, find_children=None):
        """Change file id map to incorporate specified changes.

        :param uuid: UUID of repository changes happen in
        :param revnum: Revno for revision in which changes happened
        :param branch: Branch path where changes happened
        :param global_changes: Dict with global changes that happened
        :param renames: List of renames (known file ids for particular paths)
        :param scheme: Branching scheme
        """
        changes = get_local_changes(global_changes, scheme,
                    self.repos.generate_revision_id, find_children)
        if find_children is not None:
            def get_children(path, revid):
                (bp, revnum, scheme) = self.repos.lookup_revision_id(revid)
                for p in find_children(bp+"/"+path, revnum):
                    yield scheme.unprefix(p)[1]
        else:
            get_children = None

        revid = self.repos.generate_revision_id(revnum, branch, str(scheme))

        def new_file_id(x):
            return generate_file_id(self.repos, revid, x)
         
        idmap = self._apply_changes(new_file_id, changes, get_children)
        idmap.update(renames)
        return idmap

    def get_map(self, uuid, revnum, branch, renames_cb, scheme):
        """Make sure the map is up to date until revnum."""
        # First, find the last cached map
        todo = []
        next_parent_revs = []
        if revnum == 0:
            assert branch == ""
            return {"": (default_mapping.generate_file_id(uuid, revnum, branch, u""), 
              self.repos.generate_revision_id(revnum, branch, str(scheme)))}

        # No history -> empty map
        for (bp, paths, rev) in self.repos.follow_branch_history(branch, 
                                             revnum, scheme):
            revid = self.repos.generate_revision_id(rev, bp, 
                                                    str(scheme))
            try:
                map = self.load(revid)
                # found the nearest cached map
                next_parent_revs = [revid]
                break
            except RevisionNotPresent:
                todo.append((revid, paths))
   
        # target revision was present
        if len(todo) == 0:
            return map

        if len(next_parent_revs) == 0:
            if scheme.is_branch(""):
                map = {u"": (default_mapping.generate_file_id(uuid, 0, "", u""), NULL_REVISION)}
            else:
                map = {}

        pb = ui.ui_factory.nested_progress_bar()

        try:
            i = 1
            for (revid, global_changes) in reversed(todo):
                expensive = False
                def log_find_children(path, revnum):
                    expensive = True
                    return self.repos._log.find_children(path, revnum)
                changes = get_local_changes(global_changes, scheme,
                                            self.repos.generate_revision_id, 
                                            log_find_children)
                pb.update('generating file id map', i, len(todo))

                def find_children(path, revid):
                    (bp, revnum, scheme) = self.repos.lookup_revision_id(revid)
                    for p in log_find_children(bp+"/"+path, revnum):
                        yield scheme.unprefix(p)[1]

                parent_revs = next_parent_revs

                def new_file_id(x):
                    return generate_file_id(self.repos, revid, x)
                
                revmap = self._apply_changes(new_file_id, changes, find_children)
                revmap.update(renames_cb(revid))

                for p in changes:
                    if changes[p][0] == 'M' and not revmap.has_key(p):
                        revmap[p] = map[p][0]

                map.update(dict([(x, (str(revmap[x]), revid)) for x in revmap]))

                # Mark all parent paths as changed
                for p in revmap:
                    parts = p.split("/")
                    for j in range(1, len(parts)+1):
                        parent = "/".join(parts[0:len(parts)-j])
                        assert map.has_key(parent), "Parent item %s of %s doesn't exist in map" % (parent, p)
                        if map[parent][1] == revid:
                            break
                        map[parent] = map[parent][0], revid
                        
                saved = False
                if i % 500 == 0 or expensive:
                    self.save(revid, parent_revs, map)
                    saved = True
                next_parent_revs = [revid]
                i += 1
        finally:
            pb.finished()
        if not saved:
            self.save(revid, parent_revs, map)
        return map


class SimpleFileIdMap(FileIdMap):
    @staticmethod
    def _apply_changes(new_file_id, changes, find_children=None):
        map = {}
        for p in sorted(changes.keys()):
            data = changes[p]

            if data[0] in ('A', 'R'):
                inv_p = p.decode("utf-8")
                map[inv_p] = new_file_id(inv_p)

                if data[1] is not None:
                    mutter('%r copied from %r:%s' % (inv_p, data[1], data[2]))
                    if find_children is not None:
                        for c in find_children(data[1], data[2]):
                            inv_c = c.decode("utf-8")
                            path = c.replace(data[1].decode("utf-8"), inv_p+"/", 1).replace(u"//", u"/")
                            map[path] = new_file_id(path)
                            mutter('added mapping %r -> %r' % (path, map[path]))

        return map