41
41
return expanded_file_list
44
def add_reporter_null(path, kind, entry):
45
"""Absorb add reports and do nothing."""
49
def add_reporter_print(path, kind, entry):
50
"""Print a line to stdout for each file that's added."""
51
print "added", bzrlib.osutils.quotefn(path)
54
44
def _prepare_file_list(file_list):
55
45
"""Prepare a file list for use by smart_add_*."""
57
47
if sys.platform == 'win32':
58
48
file_list = glob_expand_for_win32(file_list)
61
51
file_list = list(file_list)
65
def smart_add(file_list, recurse=True, reporter=add_reporter_null):
55
def add_action_null(inv, path, kind):
56
"""Absorb add actions and do nothing."""
59
def add_action_print(inv, path, kind):
60
"""Print a line to stdout for each file that would be added."""
61
print "added", bzrlib.osutils.quotefn(path)
63
def add_action_add(inv, path, kind):
64
"""Add each file to the given inventory. Produce no output."""
65
entry = inv.add_path(path, kind=kind)
66
mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))
68
def add_action_add_and_print(inv, path, kind):
69
"""Add each file to the given inventory, and print a line to stdout."""
70
add_action_add(inv, path, kind)
71
add_action_print(inv, path, kind)
74
def smart_add(file_list, recurse=True, action=add_action_add):
66
75
"""Add files to version, optionally recursing into directories.
68
77
This is designed more towards DWIM for humans than API simplicity.
73
82
file_list = _prepare_file_list(file_list)
74
83
tree = WorkingTree.open_containing(file_list[0])[0]
75
return smart_add_tree(tree, file_list, recurse, reporter)
84
return smart_add_tree(tree, file_list, recurse, action)
78
def smart_add_tree(tree, file_list, recurse=True, reporter=add_reporter_null):
86
def smart_add_tree(tree, file_list, recurse=True, action=add_action_add):
79
87
"""Add files to version, optionally recursing into directories.
81
89
This is designed more towards DWIM for humans than API simplicity.
86
94
Returns the number of files added.
89
97
from bzrlib.errors import BadFileKindError, ForbiddenFileError
90
98
assert isinstance(recurse, bool)
92
100
file_list = _prepare_file_list(file_list)
93
101
user_list = file_list[:]
94
102
inv = tree.read_working_inventory()
97
106
for f in file_list:
98
107
rf = tree.relpath(f)
99
108
af = tree.abspath(rf)
101
kind = bzrlib.osutils.file_kind(af)
111
kind = bzrlib.osutils.file_kind(af)
113
if hasattr(e, 'errno') and e.errno == errno.ENOENT:
114
raise errors.NoSuchFile(rf)
103
117
if not InventoryEntry.versionable_kind(kind):
104
118
if f in user_list:
134
148
mutter("%r is a bzr tree", f)
136
count += __add_one(tree, inv, rf, kind, reporter)
150
added.extend(__add_one(tree, inv, rf, kind, action))
138
152
if kind == 'directory' and recurse and not sub_tree:
139
153
for subf in os.listdir(af):
140
154
subp = os.path.join(rf, subf)
141
155
if subf == bzrlib.BZRDIR:
142
156
mutter("skip control directory %r", subp)
143
elif tree.is_ignored(subp):
144
mutter("skip ignored sub-file %r", subp)
146
mutter("queue to add sub-file %r", subp)
147
file_list.append(tree.abspath(subp))
150
mutter('added %d entries', count)
158
ignore_glob = tree.is_ignored(subp)
159
if ignore_glob is not None:
160
mutter("skip ignored sub-file %r", subp)
161
if ignore_glob not in ignored:
162
ignored[ignore_glob] = []
163
ignored[ignore_glob].append(subp)
165
mutter("queue to add sub-file %r", subp)
166
file_list.append(tree.abspath(subp))
169
mutter('added %d entries', len(added))
153
172
tree._write_inventory(inv)
174
return added, ignored
157
def __add_one(tree, inv, path, kind, reporter):
176
def __add_one(tree, inv, path, kind, action):
158
177
"""Add a file or directory, automatically add unversioned parents."""
160
179
# Nothing to do if path is already versioned.
161
180
# This is safe from infinite recursion because the tree root is
162
181
# always versioned.
163
182
if inv.path2id(path) != None:
167
count = __add_one(tree, inv, dirname(path), 'directory', reporter)
169
entry = inv.add_path(path, kind=kind)
170
mutter("added %r kind %r file_id={%s}", path, kind, entry.file_id)
171
reporter(path, kind, entry)
186
added = __add_one(tree, inv, dirname(path), 'directory', action)
187
action(inv, path, kind)
189
return added + [path]