~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-25 02:58:34 UTC
  • Revision ID: mbp@sourcefrog.net-20050325025833-1ba1757344c31376
first cut at recursive add

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
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.
30
31
    assert not isinstance(file_list, types.StringTypes)
31
32
    b = bzrlib.branch.Branch(file_list[0], find_root=True)
32
33
    inv = b.read_working_inventory()
 
34
    tree = b.working_tree()
33
35
    dirty = False
34
36
 
 
37
    def add_one(rf, kind):
 
38
        file_id = bzrlib.branch.gen_file_id(rf)
 
39
        inv.add_path(rf, kind=kind, file_id=file_id)
 
40
        bzrlib.mutter("added %r kind %r file_id={%s}" % (rf, kind, file_id))
 
41
        dirty = True
 
42
        if verbose:
 
43
            bzrlib.textui.show_status('A', kind, quotefn(f))
 
44
        
 
45
 
35
46
    for f in file_list:
36
47
        rf = b.relpath(f)
37
48
        af = b.abspath(rf)
42
53
            bailout("cannot add control file %r" % af)
43
54
 
44
55
        kind = bzrlib.osutils.file_kind(f)
 
56
        versioned = (inv.path2id(rf) != None)
 
57
 
 
58
        ## TODO: It's OK to add '.' but only in recursive mode
 
59
 
45
60
        if kind == 'file':
46
 
            if inv.path2id(rf):
 
61
            if versioned:
47
62
                bzrlib.warning("%r is already versioned" % f)
48
63
                continue
 
64
            else:
 
65
                add_one(rf, kind)
49
66
        elif kind == 'directory':
50
 
            if inv.path2id(rf):
51
 
                if not recurse:
52
 
                    bzrlib.warning("%r is already versioned" % f)
53
 
                    continue
54
 
                else:
55
 
                    # TODO: don't add, but recurse down
56
 
                    continue
 
67
            if versioned and not recurse:
 
68
                bzrlib.warning("%r is already versioned" % f)
 
69
                continue
 
70
            
 
71
            if not versioned:
 
72
                add_one(rf, kind)
 
73
 
 
74
            if recurse:
 
75
                for subf in os.listdir(af):
 
76
                    subp = appendpath(rf, subf)
 
77
                    if tree.is_ignored(subp):
 
78
                        mutter("skip ignored sub-file %r" % subp)
 
79
                    else:
 
80
                        mutter("queue to add sub-file %r" % (subp))
 
81
                        file_list.append(subp)
57
82
        else:
58
83
            bailout("can't smart_add file kind %r" % kind)
59
84
 
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
64
 
        if verbose:
65
 
            bzrlib.textui.show_status('A', kind, quotefn(f))
66
 
 
67
85
    if dirty:
68
86
        b._write_inventory(inv)