~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Robert Collins
  • Date: 2006-06-09 19:41:06 UTC
  • mto: (1767.2.2 integration)
  • mto: This revision was merged to the branch mainline in revision 1769.
  • Revision ID: robertc@robertcollins.net-20060609194106-4423d88b66f47b87
Merge basename call eliminate for smart_add.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
        The default action does nothing.
73
73
 
74
74
        :param inv: The inventory we are working with.
75
 
        :param path: The path being added
 
75
        :param path: The FastPath being added
76
76
        :param kind: The kind of the object being added.
77
77
        """
78
78
        if self.should_add:
83
83
    def _print(self, inv, parent_ie, path, kind):
84
84
        """Print a line to self._to_file for each file that would be added."""
85
85
        self._to_file.write('added ')
86
 
        self._to_file.write(bzrlib.osutils.quotefn(path))
 
86
        self._to_file.write(bzrlib.osutils.quotefn(path.raw_path))
87
87
        self._to_file.write('\n')
88
88
 
89
89
    def _add_to_inv(self, inv, parent_ie, path, kind):
90
90
        """Add each file to the given inventory. Produce no output."""
91
91
        if parent_ie is not None:
92
92
            entry = bzrlib.inventory.make_entry(
93
 
                kind, bzrlib.osutils.basename(path),  parent_ie.file_id)
 
93
                kind, bzrlib.osutils.basename(path.raw_path),  parent_ie.file_id)
94
94
            inv.add(entry)
95
95
        else:
96
 
            entry = inv.add_path(path, kind=kind)
 
96
            entry = inv.add_path(path.raw_path, kind=kind)
97
97
        # mutter("added %r kind %r file_id={%s}", path, kind, entry.file_id)
98
98
 
99
99
 
121
121
    return smart_add_tree(tree, file_list, recurse, action=action)
122
122
 
123
123
 
 
124
class FastPath(object):
 
125
    """A path object with fast accessors for things like basename."""
 
126
 
 
127
    __slots__ = ['raw_path', 'base_path']
 
128
 
 
129
    def __init__(self, path, base_path=None):
 
130
        """Construct a FastPath from path."""
 
131
        if base_path is None:
 
132
            self.base_path = bzrlib.osutils.basename(path)
 
133
        else:
 
134
            self.base_path = base_path
 
135
        self.raw_path = path
 
136
 
 
137
 
124
138
def smart_add_tree(tree, file_list, recurse=True, action=None):
125
139
    """Add files to version, optionally recursing into directories.
126
140
 
149
163
    # relative : its cheaper to make a tree relative path an abspath
150
164
    # than to convert an abspath to tree relative.
151
165
    for filepath in prepared_list:
152
 
        rf = tree.relpath(filepath)
 
166
        rf = FastPath(tree.relpath(filepath))
153
167
        user_files.add(rf)
154
168
        files_to_add.append((rf, None))
155
169
        # validate user parameters. Our recursive code avoids adding new files
156
170
        # that need such validation 
157
 
        if tree.is_control_filename(rf):
 
171
        if tree.is_control_filename(rf.raw_path):
158
172
            raise ForbiddenFileError('cannot add control file %s' % filepath)
159
173
 
160
174
    for filepath, parent_ie in files_to_add:
161
175
        # filepath is tree-relative
162
 
        abspath = tree.abspath(filepath)
 
176
        abspath = tree.abspath(filepath.raw_path)
163
177
 
164
178
        # find the kind of the path being added. This is not
165
179
        # currently determined when we list directories 
186
200
                continue
187
201
 
188
202
        if parent_ie is not None:
189
 
            versioned = bzrlib.osutils.basename(filepath) in parent_ie.children
 
203
            versioned = filepath.base_path in parent_ie.children
190
204
        else:
191
205
            # without the parent ie, use the relatively slower inventory 
192
206
            # probing method
193
 
            versioned = inv.has_filename(filepath)
 
207
            versioned = inv.has_filename(filepath.raw_path)
194
208
 
195
209
        if kind == 'directory':
196
210
            try:
203
217
        else:
204
218
            sub_tree = False
205
219
 
206
 
        if filepath == '':
 
220
        if filepath.raw_path == '':
207
221
            # mutter("tree root doesn't need to be added")
208
222
            sub_tree = False
209
223
        elif versioned:
218
232
            try:
219
233
                if parent_ie is not None:
220
234
                    # must be present:
221
 
                    this_ie = parent_ie.children[bzrlib.osutils.basename(filepath)]
 
235
                    this_ie = parent_ie.children[filepath.base_path]
222
236
                else:
223
237
                    # without the parent ie, use the relatively slower inventory 
224
238
                    # probing method
225
 
                    this_id = inv.path2id(filepath)
 
239
                    this_id = inv.path2id(filepath.raw_path)
226
240
                    if this_id is None:
227
241
                        this_ie = None
228
242
                    else:
233
247
            for subf in os.listdir(abspath):
234
248
                # here we could use TreeDirectory rather than 
235
249
                # string concatenation.
236
 
                subp = bzrlib.osutils.pathjoin(filepath, subf)
 
250
                subp = bzrlib.osutils.pathjoin(filepath.raw_path, subf)
237
251
                # TODO: is_control_filename is very slow. Make it faster. 
238
252
                # TreeDirectory.is_control_filename could also make this 
239
253
                # faster - its impossible for a non root dir to have a 
251
265
                        ignored[ignore_glob].append(subp)
252
266
                    else:
253
267
                        #mutter("queue to add sub-file %r", subp)
254
 
                        files_to_add.append((subp, this_ie))
 
268
                        files_to_add.append((FastPath(subp, subf), this_ie))
255
269
 
256
270
    if len(added) > 0:
257
271
        tree._write_inventory(inv)
280
294
        added = []
281
295
    else:
282
296
        # slower but does not need parent_ie
283
 
        if inv.has_filename(path):
 
297
        if inv.has_filename(path.raw_path):
284
298
            return []
285
 
        # add parent
286
 
        added = __add_one(tree, inv, None, dirname(path), 'directory', action)
287
 
        parent_id = inv.path2id(dirname(path))
 
299
        # its really not there : add the parent
 
300
        # note that the dirname use leads to some extra str copying etc but as
 
301
        # there are a limited number of dirs we can be nested under, it should
 
302
        # generally find it very fast and not recurse after that.
 
303
        added = __add_one(tree, inv, None, dirname(path.raw_path), 'directory', action)
 
304
        parent_id = inv.path2id(dirname(path.raw_path))
288
305
        if parent_id is not None:
289
 
            parent_ie = inv[inv.path2id(dirname(path))]
 
306
            parent_ie = inv[inv.path2id(dirname(path.raw_path))]
290
307
        else:
291
308
            parent_ie = None
292
309
    action(inv, parent_ie, path, kind)