~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-29 07:29:29 UTC
  • Revision ID: mbp@sourcefrog.net-20050329072929-1d7cbfa072bc4b17
better "unknowns" based on just listing the relevant files

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import types, os, sys, stat
18
18
import bzrlib
19
19
 
20
 
from osutils import quotefn
 
20
from osutils import quotefn, appendpath
21
21
from errors import bailout
 
22
from trace import mutter, note
22
23
 
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.
25
26
 
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().
28
29
    """
29
30
    assert file_list
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()
33
 
    dirty = False
 
34
    tree = b.working_tree()
 
35
    count = 0
34
36
 
35
37
    for f in file_list:
36
38
        rf = b.relpath(f)
37
39
        af = b.abspath(rf)
38
40
 
 
41
        ## TODO: It's OK to add root but only in recursive mode
 
42
 
39
43
        bzrlib.mutter("smart add of %r" % f)
40
44
        
41
45
        if bzrlib.branch.is_control_file(af):
42
46
            bailout("cannot add control file %r" % af)
43
47
 
44
48
        kind = bzrlib.osutils.file_kind(f)
45
 
        if kind == 'file':
46
 
            if inv.path2id(rf):
47
 
                bzrlib.warning("%r is already versioned" % f)
48
 
                continue
49
 
        elif kind == 'directory':
50
 
            if inv.path2id(rf):
51
 
                if not recurse:
52
 
                    bzrlib.warning("%r is already versioned" % f)
53
 
                    continue
 
49
 
 
50
        if kind != 'file' and kind != 'directory':
 
51
            bailout("can't add file of kind %r" % kind)
 
52
            
 
53
        versioned = (inv.path2id(rf) != None)
 
54
 
 
55
        if rf == '':
 
56
            mutter("branch root doesn't need to be added")
 
57
        elif versioned:
 
58
            mutter("%r is already versioned" % f)
 
59
        else:
 
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))
 
63
            count += 1 
 
64
            if verbose:
 
65
                bzrlib.textui.show_status('A', kind, quotefn(f))
 
66
 
 
67
        if kind == 'directory' and recurse:
 
68
            for subf in os.listdir(af):
 
69
                subp = appendpath(rf, subf)
 
70
                if subf == bzrlib.BZRDIR:
 
71
                    mutter("skip control directory %r" % subp)
 
72
                elif tree.is_ignored(subp):
 
73
                    mutter("skip ignored sub-file %r" % subp)
54
74
                else:
55
 
                    # TODO: don't add, but recurse down
56
 
                    continue
57
 
        else:
58
 
            bailout("can't smart_add file kind %r" % kind)
 
75
                    mutter("queue to add sub-file %r" % (subp))
 
76
                    file_list.append(subp)
59
77
 
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))
63
 
        dirty = True
 
78
    if count > 0:
64
79
        if verbose:
65
 
            bzrlib.textui.show_status('A', kind, quotefn(f))
66
 
 
67
 
    if dirty:
 
80
            note('added %d' % count)
68
81
        b._write_inventory(inv)