65
64
entry = inv.add_path(path, kind=kind)
66
65
mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))
68
68
def add_action_add_and_print(inv, path, kind):
69
69
"""Add each file to the given inventory, and print a line to stdout."""
70
70
add_action_add(inv, path, kind)
83
83
tree = WorkingTree.open_containing(file_list[0])[0]
84
84
return smart_add_tree(tree, file_list, recurse, action)
86
87
def smart_add_tree(tree, file_list, recurse=True, action=add_action_add):
87
88
"""Add files to version, optionally recursing into directories.
97
98
from bzrlib.errors import BadFileKindError, ForbiddenFileError
98
99
assert isinstance(recurse, bool)
101
orig_list = file_list
100
102
file_list = _prepare_file_list(file_list)
101
user_list = file_list[:]
103
mutter("smart add of %r, originally %r", file_list, orig_list)
102
104
inv = tree.read_working_inventory()
108
af = tree.abspath(rf)
107
user_files = set(file_list)
109
for filepath in file_list:
110
# convert a random abs or cwd-relative path to tree relative.
111
rf = tree.relpath(filepath)
113
# validate user parameters. Our recursive code avoids adding new files
114
# that need such validation
115
if filepath in user_files and tree.is_control_filename(rf):
116
raise ForbiddenFileError('cannot add control file %s' % filepath)
118
# find the kind of the path being added. This is not
119
# currently determined when we list directories
120
# recursively, but in theory we can determine while
121
# doing the directory listing on *some* platformans.
122
# TODO: a safe, portable, clean interface which will
123
# be faster than os.listdir() + stat. Specifically,
124
# readdir - dirent.d_type supplies the file type when
125
# it is defined. (Apparently Mac OSX has the field but
126
# does not fill it in ?!) Robert C, Martin P.
111
kind = bzrlib.osutils.file_kind(af)
128
kind = bzrlib.osutils.file_kind(filepath)
112
129
except OSError, e:
113
130
if hasattr(e, 'errno') and e.errno == errno.ENOENT:
114
raise errors.NoSuchFile(rf)
131
raise errors.NoSuchFile(filepath)
134
# we need to call this to determine the inventory kind to create.
117
135
if not InventoryEntry.versionable_kind(kind):
119
raise BadFileKindError("cannot add %s of type %s" % (f, kind))
136
if filepath in user_files:
137
raise BadFileKindError("cannot add %s of type %s" % (filepath, kind))
121
warning("skipping %s (can't add file of kind '%s')", f, kind)
139
warning("skipping %s (can't add file of kind '%s')", filepath, kind)
124
mutter("smart add of %r, abs=%r", f, af)
126
if tree.is_control_filename(af):
127
raise ForbiddenFileError('cannot add control file %s' % f)
129
versioned = (inv.path2id(rf) != None)
142
# TODO make has_filename faster or provide a better api for accessing/determining
143
# this status. perhaps off the inventory directory object.
144
versioned = inv.has_filename(rf)
131
146
if kind == 'directory':
133
sub_branch = WorkingTree.open(af)
148
sub_branch = bzrlib.bzrdir.BzrDir.open(filepath)
135
150
except NotBranchError:
143
mutter("tree root doesn't need to be added")
158
# mutter("tree root doesn't need to be added")
146
mutter("%r is already versioned", f)
162
# mutter("%r is already versioned", filepath)
148
mutter("%r is a bzr tree", f)
164
mutter("%r is a nested bzr tree", filepath)
150
166
added.extend(__add_one(tree, inv, rf, kind, action))
152
168
if kind == 'directory' and recurse and not sub_tree:
153
for subf in os.listdir(af):
169
for subf in os.listdir(filepath):
170
# here we could use TreeDirectory rather than
171
# string concatenation.
154
172
subp = bzrlib.osutils.pathjoin(rf, subf)
173
# TODO: is_control_filename is very slow. Make it faster.
174
# TreeDirectory.is_control_filename could also make this
175
# faster - its impossible for a non root dir to have a
155
177
if tree.is_control_filename(subp):
156
178
mutter("skip control directory %r", subp)
180
# ignore while selecting files - if we globbed in the
181
# outer loop we would ignore user files.
158
182
ignore_glob = tree.is_ignored(subp)
159
183
if ignore_glob is not None:
160
mutter("skip ignored sub-file %r", subp)
184
# mutter("skip ignored sub-file %r", subp)
161
185
if ignore_glob not in ignored:
162
186
ignored[ignore_glob] = []
163
187
ignored[ignore_glob].append(subp)
165
mutter("queue to add sub-file %r", subp)
189
#mutter("queue to add sub-file %r", subp)
166
190
file_list.append(tree.abspath(subp))
169
mutter('added %d entries', len(added))
171
192
if len(added) > 0:
172
193
tree._write_inventory(inv)
174
194
return added, ignored
176
197
def __add_one(tree, inv, path, kind, action):
177
198
"""Add a file or directory, automatically add unversioned parents."""