17
17
import types, os, sys, stat
20
from osutils import quotefn
20
from osutils import quotefn, appendpath
21
21
from errors import bailout
22
from trace import mutter, note
23
def smart_add(file_list, verbose=False, recurse=False):
24
def smart_add(file_list, verbose=False, recurse=True):
24
25
"""Add files to version, optionall recursing into directories.
26
27
This is designed more towards DWIM for humans than API simplicity.
27
28
For the specific behaviour see the help for cmd_add().
30
assert not isinstance(file_list, types.StringTypes)
31
assert not isinstance(file_list, basestring)
31
32
b = bzrlib.branch.Branch(file_list[0], find_root=True)
32
33
inv = b.read_working_inventory()
34
tree = b.working_tree()
35
37
for f in file_list:
41
## TODO: It's OK to add root but only in recursive mode
39
43
bzrlib.mutter("smart add of %r" % f)
41
45
if bzrlib.branch.is_control_file(af):
42
46
bailout("cannot add control file %r" % af)
44
48
kind = bzrlib.osutils.file_kind(f)
47
bzrlib.warning("%r is already versioned" % f)
49
elif kind == 'directory':
52
bzrlib.warning("%r is already versioned" % f)
50
if kind != 'file' and kind != 'directory':
51
bailout("can't add file of kind %r" % kind)
53
versioned = (inv.path2id(rf) != None)
56
mutter("branch root doesn't need to be added")
58
mutter("%r is already versioned" % f)
60
file_id = bzrlib.branch.gen_file_id(rf)
61
inv.add_path(rf, kind=kind, file_id=file_id)
62
bzrlib.mutter("added %r kind %r file_id={%s}" % (rf, kind, file_id))
65
bzrlib.textui.show_status('A', kind, quotefn(f))
67
if kind == 'directory' and recurse:
68
for subf in os.listdir(af):
69
subp = appendpath(rf, subf)
70
if tree.is_ignored(subp):
71
mutter("skip ignored sub-file %r" % subp)
55
# TODO: don't add, but recurse down
58
bailout("can't smart_add file kind %r" % kind)
73
mutter("queue to add sub-file %r" % (subp))
74
file_list.append(subp)
60
file_id = bzrlib.branch.gen_file_id(rf)
61
inv.add_path(rf, kind=kind, file_id=file_id)
62
bzrlib.mutter("added %r kind %r file_id={%s}" % (rf, kind, file_id))
65
bzrlib.textui.show_status('A', kind, quotefn(f))
78
note('added %d' % count)
68
79
b._write_inventory(inv)