~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Martin Pool
  • Date: 2005-05-10 08:15:58 UTC
  • Revision ID: mbp@sourcefrog.net-20050510081558-9a38e2c46ba4ebc4
- Patch from Fredrik Lundh to check Python version and 
  try to find a better one if it's too old.

  Patched to try to prevent infinite loops in wierd configurations,
  and to log to stderr.

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