18
18
from bzrlib.errors import NotBranchError
19
19
from bzrlib.branch import Branch
20
20
from bzrlib.osutils import quotefn
21
from os.path import dirname
23
22
def glob_expand_for_win32(file_list):
25
25
expanded_file_list = []
26
26
for possible_glob in file_list:
27
27
glob_files = glob.glob(possible_glob)
35
35
return expanded_file_list
38
def add_reporter_null(path, kind, entry):
39
"""Absorb add reports and do nothing."""
38
def _NullAddCallback(path, kind, entry):
42
def add_reporter_print(path, kind, entry):
43
"""Print a line to stdout for each file that's added."""
41
def _PrintAddCallback(path, kind, entry):
44
42
print "added", quotefn(path)
46
44
def _prepare_file_list(file_list):
53
51
file_list = list(file_list)
52
assert not isinstance(file_list, basestring)
57
def smart_add(file_list, recurse=True, reporter=add_reporter_null):
55
def smart_add(file_list, verbose=True, recurse=True, callback=_NullAddCallback):
58
56
"""Add files to version, optionally recursing into directories.
60
58
This is designed more towards DWIM for humans than API simplicity.
61
59
For the specific behaviour see the help for cmd_add().
63
Returns the number of files added.
65
61
file_list = _prepare_file_list(file_list)
66
b = Branch.open_containing(file_list[0])
67
return smart_add_branch(b, file_list, recurse, reporter)
62
b = Branch(file_list[0], find_root=True)
63
return smart_add_branch(b, file_list, verbose, recurse)
70
def smart_add_branch(branch, file_list, recurse=True, reporter=add_reporter_null):
65
def smart_add_branch(branch, file_list, verbose=True, recurse=True,
66
callback=_NullAddCallback):
71
67
"""Add files to version, optionally recursing into directories.
73
69
This is designed more towards DWIM for humans than API simplicity.
74
70
For the specific behaviour see the help for cmd_add().
76
72
This yields a sequence of (path, kind, file_id) for added files.
78
Returns the number of files added.
76
from bzrlib.osutils import quotefn
81
77
from bzrlib.errors import BadFileKindError, ForbiddenFileError
82
78
import bzrlib.branch
84
assert isinstance(recurse, bool)
86
81
file_list = _prepare_file_list(file_list)
87
82
user_list = file_list[:]
127
122
mutter("%r is a bzr tree" %f)
129
count += __add_one(branch, inv, rf, kind, reporter)
124
entry = inv.add_path(rf, kind=kind)
125
mutter("added %r kind %r file_id={%s}" % (rf, kind, entry.file_id))
127
callback(rf, kind, entry)
131
129
if kind == 'directory' and recurse and not sub_tree:
132
130
for subf in os.listdir(af):
140
138
file_list.append(branch.abspath(subp))
143
mutter('added %d entries', count)
143
note('added %d' % count)
146
144
branch._write_inventory(inv)
150
def __add_one(branch, inv, path, kind, reporter):
151
"""Add a file or directory, automatically add unversioned parents."""
153
# Nothing to do if path is already versioned.
154
# This is safe from infinite recursion because the branch root is
156
if inv.path2id(path) != None:
160
count = __add_one(branch, inv, dirname(path), 'directory', reporter)
162
entry = inv.add_path(path, kind=kind)
163
mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))
164
reporter(path, kind, entry)
146
note("nothing new to add")
147
# should this return 1 to the shell?