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):
55
class AddAction(object):
56
"""A class which defines what action to take when adding a file."""
61
def __init__(self, to_file=None, should_add=None, should_print=None):
62
self._to_file = to_file
64
self._to_file = sys.stdout
65
if should_add is not None:
66
self.should_add = should_add
67
if should_print is not None:
68
self.should_print = should_print
70
def __call__(self, inv, path, kind):
71
"""Add path to inventory.
73
The default action does nothing.
75
:param inv: The inventory we are working with.
76
:param path: The path being added
77
:param kind: The kind of the object being added.
80
self._add_to_inv(inv, path, kind)
82
self._print(inv, path, kind)
84
def _print(self, inv, path, kind):
85
self._to_file.write('added ')
86
self._to_file.write(bzrlib.osutils.quotefn(path))
87
self._to_file.write('\n')
89
def _add_to_inv(self, inv, path, kind):
90
entry = inv.add_path(path, kind=kind)
91
mutter("added %r kind %r file_id={%s}", path, kind, entry.file_id)
94
# TODO: jam 20050105 These could be used for compatibility
95
# however, they bind against the current stdout, not the
96
# one which exists at the time they are called, so they
97
# don't work for the test suite.
99
add_action_null = AddAction()
100
add_action_add = AddAction(should_add=True)
101
add_action_print = AddAction(should_print=True)
102
add_action_add_and_print = AddAction(should_add=True, should_print=True)
105
def smart_add(file_list, recurse=True, action=None):
75
106
"""Add files to version, optionally recursing into directories.
77
108
This is designed more towards DWIM for humans than API simplicity.
78
109
For the specific behaviour see the help for cmd_add().
80
111
Returns the number of files added.
82
114
file_list = _prepare_file_list(file_list)
83
115
tree = WorkingTree.open_containing(file_list[0])[0]
84
return smart_add_tree(tree, file_list, recurse, action)
86
def smart_add_tree(tree, file_list, recurse=True, action=add_action_add):
116
return smart_add_tree(tree, file_list, recurse, action=action)
119
def smart_add_tree(tree, file_list, recurse=True, action=None):
87
120
"""Add files to version, optionally recursing into directories.
89
122
This is designed more towards DWIM for humans than API simplicity.