~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-09-04 02:59:56 UTC
  • mfrom: (1172)
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050904025956-776ba4f07de97700
Merged mpool's latest changes (~0.0.7)

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
18
 
import bzrlib
19
 
 
20
 
from trace import mutter, note
 
17
from bzrlib.trace import mutter, note, warning
 
18
from bzrlib.errors import NotBranchError
 
19
from bzrlib.branch import Branch
 
20
from bzrlib.osutils import quotefn
21
21
 
22
22
def glob_expand_for_win32(file_list):
23
23
    import glob
34
34
            expanded_file_list += glob_files
35
35
    return expanded_file_list
36
36
 
37
 
def smart_add(file_list, verbose=True, recurse=True):
38
 
    """Add files to version, optionally recursing into directories.
39
 
 
40
 
    This is designed more towards DWIM for humans than API simplicity.
41
 
    For the specific behaviour see the help for cmd_add().
42
 
    """
43
 
    from bzrlib.osutils import quotefn, kind_marker
44
 
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
45
 
 
46
 
    assert file_list
 
37
 
 
38
def add_reporter_null(path, kind, entry):
 
39
    """Absorb add reports and do nothing."""
 
40
    pass
 
41
 
 
42
def add_reporter_print(path, kind, entry):
 
43
    """Print a line to stdout for each file that's added."""
 
44
    print "added", quotefn(path)
47
45
    
 
46
def _prepare_file_list(file_list):
 
47
    """Prepare a file list for use by smart_add_*."""
 
48
    import sys
48
49
    if sys.platform == 'win32':
49
50
        file_list = glob_expand_for_win32(file_list)
50
 
    
 
51
    if not file_list:
 
52
        file_list = ['.']
 
53
    file_list = list(file_list)
 
54
    return file_list
 
55
 
 
56
 
 
57
def smart_add(file_list, recurse=True, reporter=add_reporter_null):
 
58
    """Add files to version, optionally recursing into directories.
 
59
 
 
60
    This is designed more towards DWIM for humans than API simplicity.
 
61
    For the specific behaviour see the help for cmd_add().
 
62
 
 
63
    Returns the number of files added.
 
64
    """
 
65
    file_list = _prepare_file_list(file_list)
 
66
    b = Branch(file_list[0], find_root=True)
 
67
    return smart_add_branch(b, file_list, recurse, reporter)
 
68
 
 
69
        
 
70
def smart_add_branch(branch, file_list, recurse=True, reporter=add_reporter_null):
 
71
    """Add files to version, optionally recursing into directories.
 
72
 
 
73
    This is designed more towards DWIM for humans than API simplicity.
 
74
    For the specific behaviour see the help for cmd_add().
 
75
 
 
76
    This yields a sequence of (path, kind, file_id) for added files.
 
77
 
 
78
    Returns the number of files added.
 
79
    """
 
80
    import os
 
81
    import sys
 
82
    from bzrlib.osutils import quotefn
 
83
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
 
84
    import bzrlib.branch
 
85
    import bzrlib.osutils
 
86
 
 
87
    assert isinstance(recurse, bool)
 
88
 
 
89
    file_list = _prepare_file_list(file_list)
51
90
    user_list = file_list[:]
52
 
    assert not isinstance(file_list, basestring)
53
 
    b = bzrlib.branch.Branch(file_list[0], find_root=True)
54
 
    inv = b.read_working_inventory()
55
 
    tree = b.working_tree()
 
91
    inv = branch.read_working_inventory()
 
92
    tree = branch.working_tree()
56
93
    count = 0
57
94
 
58
95
    for f in file_list:
59
 
        rf = b.relpath(f)
60
 
        af = b.abspath(rf)
 
96
        rf = branch.relpath(f)
 
97
        af = branch.abspath(rf)
61
98
 
62
99
        kind = bzrlib.osutils.file_kind(af)
63
100
 
65
102
            if f in user_list:
66
103
                raise BadFileKindError("cannot add %s of type %s" % (f, kind))
67
104
            else:
68
 
                print "skipping %s (can't add file of kind '%s')" % (f, kind)
 
105
                warning("skipping %s (can't add file of kind '%s')", f, kind)
69
106
                continue
70
107
 
71
 
        bzrlib.mutter("smart add of %r, abs=%r" % (f, af))
 
108
        mutter("smart add of %r, abs=%r" % (f, af))
72
109
        
73
110
        if bzrlib.branch.is_control_file(af):
74
111
            raise ForbiddenFileError('cannot add control file %s' % f)
75
112
            
76
113
        versioned = (inv.path2id(rf) != None)
77
114
 
 
115
        if kind == 'directory':
 
116
            try:
 
117
                sub_branch = Branch(af, find_root=False)
 
118
                sub_tree = True
 
119
            except NotBranchError:
 
120
                sub_tree = False
 
121
        else:
 
122
            sub_tree = False
 
123
 
78
124
        if rf == '':
79
125
            mutter("branch root doesn't need to be added")
 
126
            sub_tree = False
80
127
        elif versioned:
81
128
            mutter("%r is already versioned" % f)
 
129
        elif sub_tree:
 
130
            mutter("%r is a bzr tree" %f)
82
131
        else:
83
 
            file_id = bzrlib.branch.gen_file_id(rf)
84
 
            inv.add_path(rf, kind=kind, file_id=file_id)
85
 
            bzrlib.mutter("added %r kind %r file_id={%s}" % (rf, kind, file_id))
 
132
            entry = inv.add_path(rf, kind=kind)
 
133
            mutter("added %r kind %r file_id={%s}" % (rf, kind, entry.file_id))
86
134
            count += 1 
87
 
 
88
 
            print 'added', quotefn(f)
89
 
 
90
 
        if kind == 'directory' and recurse:
 
135
            reporter(rf, kind, entry)
 
136
 
 
137
        if kind == 'directory' and recurse and not sub_tree:
91
138
            for subf in os.listdir(af):
92
139
                subp = os.path.join(rf, subf)
93
140
                if subf == bzrlib.BZRDIR:
96
143
                    mutter("skip ignored sub-file %r" % subp)
97
144
                else:
98
145
                    mutter("queue to add sub-file %r" % subp)
99
 
                    file_list.append(b.abspath(subp))
100
 
 
 
146
                    file_list.append(branch.abspath(subp))
 
147
 
 
148
 
 
149
    mutter('added %d entries', count)
 
150
    
101
151
    if count > 0:
102
 
        if verbose:
103
 
            note('added %d' % count)
104
 
        b._write_inventory(inv)
 
152
        branch._write_inventory(inv)
 
153
 
 
154
    return count