75
74
:param path: The FastPath being added
76
75
:param kind: The kind of the object being added.
79
self._to_file.write('added %s\n' % _quote(path.raw_path))
83
class AddFromBaseAction(AddAction):
84
"""This class will try to extract file ids from another tree."""
86
def __init__(self, base_tree, base_path, to_file=None, should_print=None):
87
super(AddFromBaseAction, self).__init__(to_file=to_file,
88
should_print=should_print)
89
self.base_tree = base_tree
90
self.base_path = base_path
92
def __call__(self, inv, parent_ie, path, kind):
93
# Place the parent call
94
# Now check to see if we can extract an id for this file
95
file_id, base_path = self._get_base_file_id(path, parent_ie)
96
if file_id is not None:
98
self._to_file.write('added %s w/ file id from %s\n'
99
% (path.raw_path, base_path))
101
# we aren't doing anything special, so let the default
103
file_id = super(AddFromBaseAction, self).__call__(
104
inv, parent_ie, path, kind)
107
def _get_base_file_id(self, path, parent_ie):
108
"""Look for a file id in the base branch.
110
First, if the base tree has the parent directory,
111
we look for a file with the same name in that directory.
112
Else, we look for an entry in the base tree with the same path.
115
if (parent_ie.file_id in self.base_tree):
116
base_parent_ie = self.base_tree.inventory[parent_ie.file_id]
117
base_child_ie = base_parent_ie.children.get(path.base_path)
118
if base_child_ie is not None:
119
return (base_child_ie.file_id,
120
self.base_tree.id2path(base_child_ie.file_id))
121
full_base_path = bzrlib.osutils.pathjoin(self.base_path, path.raw_path)
122
# This may return None, but it is our last attempt
123
return self.base_tree.path2id(full_base_path), full_base_path
77
if not self.should_print:
79
self._to_file.write('added %s\n' % _quote(path.raw_path))
126
82
# TODO: jam 20050105 These could be used for compatibility
146
102
file_list = _prepare_file_list(file_list)
147
103
tree = WorkingTree.open_containing(file_list[0])[0]
148
return smart_add_tree(tree, file_list, recurse, action=action, save=save)
104
return smart_add_tree(tree, file_list, recurse, action=action)
151
107
class FastPath(object):
171
127
def smart_add_tree(tree, file_list, recurse=True, action=None, save=True):
172
tree.lock_tree_write()
174
return _smart_add_tree(tree=tree, file_list=file_list, recurse=recurse,
175
action=action, save=save)
179
def _smart_add_tree(tree, file_list, recurse=True, action=None, save=True):
180
128
"""Add files to version, optionally recursing into directories.
182
130
This is designed more towards DWIM for humans than API simplicity.
212
160
# validate user parameters. Our recursive code avoids adding new files
213
161
# that need such validation
214
162
if tree.is_control_filename(rf.raw_path):
215
raise errors.ForbiddenControlFileError(filename=rf.raw_path)
163
raise errors.ForbiddenControlFileError(filename=rf)
217
165
abspath = tree.abspath(rf.raw_path)
218
166
kind = bzrlib.osutils.file_kind(abspath)
288
236
# mutter("%r is already versioned", abspath)
290
# XXX: This is wrong; people *might* reasonably be trying to add
291
# subtrees as subtrees. This should probably only be done in formats
292
# which can represent subtrees, and even then perhaps only when
293
# the user asked to add subtrees. At the moment you can add them
294
# specially through 'join --reference', which is perhaps
295
# reasonable: adding a new reference is a special operation and
296
# can have a special behaviour. mbp 20070306
297
238
mutter("%r is a nested bzr tree", abspath)
299
240
__add_one(tree, inv, parent_ie, directory, kind, action)
380
321
:param inv: Inventory which will receive the new entry.
381
322
:param parent_ie: Parent inventory entry.
382
323
:param kind: Kind of new entry (file, directory, etc)
383
:param action: callback(inv, parent_ie, path, kind); return a file_id
384
or None to generate a new file id
324
:param action: callback(inv, parent_ie, path, kind); return ignored.
387
file_id = action(inv, parent_ie, path, kind)
388
entry = bzrlib.inventory.make_entry(kind, path.base_path, parent_ie.file_id,
327
action(inv, parent_ie, path, kind)
328
entry = bzrlib.inventory.make_entry(kind, path.base_path, parent_ie.file_id)