~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

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
"""Helper functions for adding files to working trees."""
 
18
 
17
19
import errno
18
20
import os
19
21
from os.path import dirname
20
22
import sys
21
23
 
 
24
import bzrlib.bzrdir
22
25
import bzrlib.errors as errors
23
26
from bzrlib.inventory import InventoryEntry
24
27
from bzrlib.trace import mutter, note, warning
28
31
 
29
32
 
30
33
def glob_expand_for_win32(file_list):
 
34
    """Replacement for glob expansion by the shell.
 
35
 
 
36
    Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
 
37
    here.
 
38
 
 
39
    :param file_list: A list of filenames which may include shell globs.
 
40
    :return: An expanded list of filenames.
 
41
    """
31
42
    if not file_list:
32
43
        return
33
44
    import glob
34
45
    expanded_file_list = []
35
46
    for possible_glob in file_list:
36
47
        glob_files = glob.glob(possible_glob)
37
 
       
 
48
 
38
49
        if glob_files == []:
39
50
            # special case to let the normal code path handle
40
51
            # files that do not exists
58
69
    """A class which defines what action to take when adding a file."""
59
70
 
60
71
    def __init__(self, to_file=None, should_print=None):
 
72
        """Initialize an action which prints added files to an output stream.
 
73
 
 
74
        :param to_file: The stream to write into. This is expected to take
 
75
            Unicode paths. If not supplied, it will default to ``sys.stdout``.
 
76
        :param should_print: If False, printing will be supressed.
 
77
        """
61
78
        self._to_file = to_file
62
79
        if to_file is None:
63
80
            self._to_file = sys.stdout
176
193
    This calls reporter with each (path, kind, file_id) of added files.
177
194
 
178
195
    Returns the number of files added.
179
 
    
 
196
 
180
197
    :param save: Save the inventory after completing the adds. If False this
181
 
    provides dry-run functionality by doing the add and not saving the
182
 
    inventory.  Note that the modified inventory is left in place, allowing 
183
 
    further dry-run tasks to take place. To restore the original inventory
184
 
    call tree.read_working_inventory().
 
198
        provides dry-run functionality by doing the add and not saving the
 
199
        inventory.  Note that the modified inventory is left in place, allowing
 
200
        further dry-run tasks to take place. To restore the original inventory
 
201
        call tree.read_working_inventory().
 
202
    """
 
203
    tree.lock_tree_write()
 
204
    try:
 
205
        return _smart_add_tree(tree=tree, file_list=file_list, recurse=recurse,
 
206
                               action=action, save=save)
 
207
    finally:
 
208
        tree.unlock()
 
209
 
 
210
 
 
211
def _smart_add_tree(tree, file_list, recurse=True, action=None, save=True):
 
212
    """Helper for smart_add_tree.
 
213
 
 
214
    The tree should be locked before entering this function. See smart_add_tree
 
215
    for parameter definitions.
185
216
    """
186
217
    assert isinstance(recurse, bool)
187
218
    if action is None:
203
234
        # validate user parameters. Our recursive code avoids adding new files
204
235
        # that need such validation 
205
236
        if tree.is_control_filename(rf.raw_path):
206
 
            raise errors.ForbiddenControlFileError(filename=rf)
 
237
            raise errors.ForbiddenControlFileError(filename=rf.raw_path)
207
238
        
208
239
        abspath = tree.abspath(rf.raw_path)
209
240
        kind = bzrlib.osutils.file_kind(abspath)
278
309
            pass
279
310
            # mutter("%r is already versioned", abspath)
280
311
        elif sub_tree:
 
312
            # XXX: This is wrong; people *might* reasonably be trying to add
 
313
            # subtrees as subtrees.  This should probably only be done in formats 
 
314
            # which can represent subtrees, and even then perhaps only when
 
315
            # the user asked to add subtrees.  At the moment you can add them
 
316
            # specially through 'join --reference', which is perhaps
 
317
            # reasonable: adding a new reference is a special operation and
 
318
            # can have a special behaviour.  mbp 20070306
281
319
            mutter("%r is a nested bzr tree", abspath)
282
320
        else:
283
321
            __add_one(tree, inv, parent_ie, directory, kind, action)
331
369
 
332
370
    :param inv: Inventory which will receive the new entry.
333
371
    :param parent_ie: Parent inventory entry if known, or None.  If
334
 
    None, the parent is looked up by name and used if present, otherwise
335
 
    it is recursively added.
 
372
        None, the parent is looked up by name and used if present, otherwise it
 
373
        is recursively added.
336
374
    :param kind: Kind of new entry (file, directory, etc)
337
375
    :param action: callback(inv, parent_ie, path, kind); return ignored.
338
 
    :returns: A list of paths which have been added.
 
376
    :return: A list of paths which have been added.
339
377
    """
340
378
    # Nothing to do if path is already versioned.
341
379
    # This is safe from infinite recursion because the tree root is