~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Martin Pool
  • Date: 2005-06-06 13:24:18 UTC
  • Revision ID: mbp@sourcefrog.net-20050606132418-1082c87e2473e266
- manpage generator by Hans Ulrich Niedermann

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import types, os, sys, stat
 
17
import os, sys
18
18
import bzrlib
19
19
 
20
 
from osutils import quotefn
21
 
from errors import bailout
 
20
from trace import mutter, note
22
21
 
23
 
def smart_add(file_list, verbose=False, recurse=False):
24
 
    """Add files to version, optionall recursing into directories.
 
22
def smart_add(file_list, verbose=True, recurse=True):
 
23
    """Add files to version, optionally recursing into directories.
25
24
 
26
25
    This is designed more towards DWIM for humans than API simplicity.
27
26
    For the specific behaviour see the help for cmd_add().
28
27
    """
 
28
    from bzrlib.osutils import quotefn, kind_marker
 
29
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
 
30
 
29
31
    assert file_list
30
 
    assert not isinstance(file_list, types.StringTypes)
 
32
    user_list = file_list[:]
 
33
    assert not isinstance(file_list, basestring)
31
34
    b = bzrlib.branch.Branch(file_list[0], find_root=True)
32
35
    inv = b.read_working_inventory()
33
 
    dirty = False
 
36
    tree = b.working_tree()
 
37
    count = 0
34
38
 
35
39
    for f in file_list:
36
40
        rf = b.relpath(f)
37
41
        af = b.abspath(rf)
38
42
 
39
 
        bzrlib.mutter("smart add of %r" % f)
 
43
        kind = bzrlib.osutils.file_kind(af)
 
44
 
 
45
        if kind != 'file' and kind != 'directory':
 
46
            if f in user_list:
 
47
                raise BadFileKindError("cannot add %s of type %s" % (f, kind))
 
48
            else:
 
49
                print "skipping %s (can't add file of kind '%s')" % (f, kind)
 
50
                continue
 
51
 
 
52
        bzrlib.mutter("smart add of %r, abs=%r" % (f, af))
40
53
        
41
54
        if bzrlib.branch.is_control_file(af):
42
 
            bailout("cannot add control file %r" % af)
43
 
 
44
 
        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
 
55
            raise ForbiddenFileError('cannot add control file %s' % f)
 
56
            
 
57
        versioned = (inv.path2id(rf) != None)
 
58
 
 
59
        if rf == '':
 
60
            mutter("branch root doesn't need to be added")
 
61
        elif versioned:
 
62
            mutter("%r is already versioned" % f)
 
63
        else:
 
64
            file_id = bzrlib.branch.gen_file_id(rf)
 
65
            inv.add_path(rf, kind=kind, file_id=file_id)
 
66
            bzrlib.mutter("added %r kind %r file_id={%s}" % (rf, kind, file_id))
 
67
            count += 1 
 
68
 
 
69
            print 'added', quotefn(f)
 
70
 
 
71
        if kind == 'directory' and recurse:
 
72
            for subf in os.listdir(af):
 
73
                subp = os.path.join(rf, subf)
 
74
                if subf == bzrlib.BZRDIR:
 
75
                    mutter("skip control directory %r" % subp)
 
76
                elif tree.is_ignored(subp):
 
77
                    mutter("skip ignored sub-file %r" % subp)
54
78
                else:
55
 
                    # TODO: don't add, but recurse down
56
 
                    continue
57
 
        else:
58
 
            bailout("can't smart_add file kind %r" % kind)
 
79
                    mutter("queue to add sub-file %r" % subp)
 
80
                    file_list.append(b.abspath(subp))
59
81
 
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
 
82
    if count > 0:
64
83
        if verbose:
65
 
            bzrlib.textui.show_status('A', kind, quotefn(f))
66
 
 
67
 
    if dirty:
 
84
            note('added %d' % count)
68
85
        b._write_inventory(inv)