~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-23 06:25:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050323062555-5489339018d0c043
- import a subset of elementtree for easier installation

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