/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
70 by mbp at sourcefrog
Prepare for smart recursive add.
1
# Copyright (C) 2005 Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
import types, os, sys, stat
18
import bzrlib
19
86 by mbp at sourcefrog
first cut at recursive add
20
from osutils import quotefn, appendpath
70 by mbp at sourcefrog
Prepare for smart recursive add.
21
from errors import bailout
86 by mbp at sourcefrog
first cut at recursive add
22
from trace import mutter
70 by mbp at sourcefrog
Prepare for smart recursive add.
23
86 by mbp at sourcefrog
first cut at recursive add
24
def smart_add(file_list, verbose=False, recurse=True):
70 by mbp at sourcefrog
Prepare for smart recursive add.
25
    """Add files to version, optionall recursing into directories.
26
27
    This is designed more towards DWIM for humans than API simplicity.
28
    For the specific behaviour see the help for cmd_add().
29
    """
30
    assert file_list
31
    assert not isinstance(file_list, types.StringTypes)
32
    b = bzrlib.branch.Branch(file_list[0], find_root=True)
33
    inv = b.read_working_inventory()
86 by mbp at sourcefrog
first cut at recursive add
34
    tree = b.working_tree()
70 by mbp at sourcefrog
Prepare for smart recursive add.
35
    dirty = False
36
86 by mbp at sourcefrog
first cut at recursive add
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
70 by mbp at sourcefrog
Prepare for smart recursive add.
46
    for f in file_list:
47
        rf = b.relpath(f)
48
        af = b.abspath(rf)
49
50
        bzrlib.mutter("smart add of %r" % f)
51
        
52
        if bzrlib.branch.is_control_file(af):
53
            bailout("cannot add control file %r" % af)
54
55
        kind = bzrlib.osutils.file_kind(f)
86 by mbp at sourcefrog
first cut at recursive add
56
        versioned = (inv.path2id(rf) != None)
57
58
        ## TODO: It's OK to add '.' but only in recursive mode
59
70 by mbp at sourcefrog
Prepare for smart recursive add.
60
        if kind == 'file':
86 by mbp at sourcefrog
first cut at recursive add
61
            if versioned:
70 by mbp at sourcefrog
Prepare for smart recursive add.
62
                bzrlib.warning("%r is already versioned" % f)
63
                continue
86 by mbp at sourcefrog
first cut at recursive add
64
            else:
65
                add_one(rf, kind)
70 by mbp at sourcefrog
Prepare for smart recursive add.
66
        elif kind == 'directory':
86 by mbp at sourcefrog
first cut at recursive add
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)
70 by mbp at sourcefrog
Prepare for smart recursive add.
82
        else:
83
            bailout("can't smart_add file kind %r" % kind)
84
85
    if dirty:
86
        b._write_inventory(inv)