~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Martin Pool
  • Date: 2005-08-30 06:10:39 UTC
  • Revision ID: mbp@sourcefrog.net-20050830061039-1d0347fb236c39ad
- clean up some code in revision.py

- move all exceptions to bzrlib.errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from bzrlib.errors import NotBranchError
19
19
from bzrlib.branch import Branch
20
20
from bzrlib.osutils import quotefn
21
 
from os.path import dirname
22
21
 
23
22
def glob_expand_for_win32(file_list):
24
23
    import glob
 
24
    
25
25
    expanded_file_list = []
26
26
    for possible_glob in file_list:
27
27
        glob_files = glob.glob(possible_glob)
35
35
    return expanded_file_list
36
36
 
37
37
 
38
 
def add_reporter_null(path, kind, entry):
39
 
    """Absorb add reports and do nothing."""
 
38
def _NullAddCallback(path, kind, entry):
40
39
    pass
41
40
 
42
 
def add_reporter_print(path, kind, entry):
43
 
    """Print a line to stdout for each file that's added."""
 
41
def _PrintAddCallback(path, kind, entry):
44
42
    print "added", quotefn(path)
45
43
    
46
44
def _prepare_file_list(file_list):
51
49
    if not file_list:
52
50
        file_list = ['.']
53
51
    file_list = list(file_list)
 
52
    assert not isinstance(file_list, basestring)
54
53
    return file_list
55
54
 
56
 
 
57
 
def smart_add(file_list, recurse=True, reporter=add_reporter_null):
 
55
def smart_add(file_list, verbose=True, recurse=True, callback=_NullAddCallback):
58
56
    """Add files to version, optionally recursing into directories.
59
57
 
60
58
    This is designed more towards DWIM for humans than API simplicity.
61
59
    For the specific behaviour see the help for cmd_add().
62
 
 
63
 
    Returns the number of files added.
64
60
    """
65
61
    file_list = _prepare_file_list(file_list)
66
 
    b = Branch.open_containing(file_list[0])
67
 
    return smart_add_branch(b, file_list, recurse, reporter)
68
 
 
 
62
    b = Branch(file_list[0], find_root=True)
 
63
    return smart_add_branch(b, file_list, verbose, recurse)
69
64
        
70
 
def smart_add_branch(branch, file_list, recurse=True, reporter=add_reporter_null):
 
65
def smart_add_branch(branch, file_list, verbose=True, recurse=True,
 
66
                     callback=_NullAddCallback):
71
67
    """Add files to version, optionally recursing into directories.
72
68
 
73
69
    This is designed more towards DWIM for humans than API simplicity.
74
70
    For the specific behaviour see the help for cmd_add().
75
71
 
76
72
    This yields a sequence of (path, kind, file_id) for added files.
77
 
 
78
 
    Returns the number of files added.
79
73
    """
80
74
    import os
 
75
    import sys
 
76
    from bzrlib.osutils import quotefn
81
77
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
82
78
    import bzrlib.branch
83
 
 
84
 
    assert isinstance(recurse, bool)
 
79
    import bzrlib.osutils
85
80
 
86
81
    file_list = _prepare_file_list(file_list)
87
82
    user_list = file_list[:]
111
106
 
112
107
        if kind == 'directory':
113
108
            try:
114
 
                sub_branch = Branch.open(af)
 
109
                sub_branch = Branch(af, find_root=False)
115
110
                sub_tree = True
116
111
            except NotBranchError:
117
112
                sub_tree = False
126
121
        elif sub_tree:
127
122
            mutter("%r is a bzr tree" %f)
128
123
        else:
129
 
            count += __add_one(branch, inv, rf, kind, reporter)
 
124
            entry = inv.add_path(rf, kind=kind)
 
125
            mutter("added %r kind %r file_id={%s}" % (rf, kind, entry.file_id))
 
126
            count += 1 
 
127
            callback(rf, kind, entry)
130
128
 
131
129
        if kind == 'directory' and recurse and not sub_tree:
132
130
            for subf in os.listdir(af):
140
138
                    file_list.append(branch.abspath(subp))
141
139
 
142
140
 
143
 
    mutter('added %d entries', count)
144
 
    
145
141
    if count > 0:
 
142
        if verbose:
 
143
            note('added %d' % count)
146
144
        branch._write_inventory(inv)
147
 
 
148
 
    return count
149
 
 
150
 
def __add_one(branch, inv, path, kind, reporter):
151
 
    """Add a file or directory, automatically add unversioned parents."""
152
 
 
153
 
    # Nothing to do if path is already versioned.
154
 
    # This is safe from infinite recursion because the branch root is
155
 
    # always versioned.
156
 
    if inv.path2id(path) != None:
157
 
        return 0
158
 
 
159
 
    # add parent
160
 
    count = __add_one(branch, inv, dirname(path), 'directory', reporter)
161
 
 
162
 
    entry = inv.add_path(path, kind=kind)
163
 
    mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))
164
 
    reporter(path, kind, entry)
165
 
 
166
 
    return count + 1
 
145
    else:
 
146
        note("nothing new to add")
 
147
        # should this return 1 to the shell?