~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from os.path import dirname
20
20
import sys
21
21
 
22
 
import bzrlib.bzrdir
23
22
import bzrlib.errors as errors
24
23
from bzrlib.inventory import InventoryEntry
25
24
from bzrlib.trace import mutter, note, warning
75
74
        :param path: The FastPath being added
76
75
        :param kind: The kind of the object being added.
77
76
        """
78
 
        if self.should_print:
79
 
            self._to_file.write('added %s\n' % _quote(path.raw_path))
80
 
        return None
81
 
 
82
 
 
83
 
class AddFromBaseAction(AddAction):
84
 
    """This class will try to extract file ids from another tree."""
85
 
 
86
 
    def __init__(self, base_tree, base_path, to_file=None, should_print=None):
87
 
        super(AddFromBaseAction, self).__init__(to_file=to_file,
88
 
                                                should_print=should_print)
89
 
        self.base_tree = base_tree
90
 
        self.base_path = base_path
91
 
 
92
 
    def __call__(self, inv, parent_ie, path, kind):
93
 
        # Place the parent call
94
 
        # Now check to see if we can extract an id for this file
95
 
        file_id, base_path = self._get_base_file_id(path, parent_ie)
96
 
        if file_id is not None:
97
 
            if self.should_print:
98
 
                self._to_file.write('added %s w/ file id from %s\n'
99
 
                                    % (path.raw_path, base_path))
100
 
        else:
101
 
            # we aren't doing anything special, so let the default
102
 
            # reporter happen
103
 
            file_id = super(AddFromBaseAction, self).__call__(
104
 
                        inv, parent_ie, path, kind)
105
 
        return file_id
106
 
 
107
 
    def _get_base_file_id(self, path, parent_ie):
108
 
        """Look for a file id in the base branch.
109
 
 
110
 
        First, if the base tree has the parent directory,
111
 
        we look for a file with the same name in that directory.
112
 
        Else, we look for an entry in the base tree with the same path.
113
 
        """
114
 
 
115
 
        if (parent_ie.file_id in self.base_tree):
116
 
            base_parent_ie = self.base_tree.inventory[parent_ie.file_id]
117
 
            base_child_ie = base_parent_ie.children.get(path.base_path)
118
 
            if base_child_ie is not None:
119
 
                return (base_child_ie.file_id,
120
 
                        self.base_tree.id2path(base_child_ie.file_id))
121
 
        full_base_path = bzrlib.osutils.pathjoin(self.base_path, path.raw_path)
122
 
        # This may return None, but it is our last attempt
123
 
        return self.base_tree.path2id(full_base_path), full_base_path
 
77
        if not self.should_print:
 
78
            return
 
79
        self._to_file.write('added %s\n' % _quote(path.raw_path))
124
80
 
125
81
 
126
82
# TODO: jam 20050105 These could be used for compatibility
145
101
    """
146
102
    file_list = _prepare_file_list(file_list)
147
103
    tree = WorkingTree.open_containing(file_list[0])[0]
148
 
    return smart_add_tree(tree, file_list, recurse, action=action, save=save)
 
104
    return smart_add_tree(tree, file_list, recurse, action=action)
149
105
 
150
106
 
151
107
class FastPath(object):
169
125
 
170
126
 
171
127
def smart_add_tree(tree, file_list, recurse=True, action=None, save=True):
172
 
    tree.lock_tree_write()
173
 
    try:
174
 
        return _smart_add_tree(tree=tree, file_list=file_list, recurse=recurse,
175
 
                               action=action, save=save)
176
 
    finally:
177
 
        tree.unlock()
178
 
 
179
 
def _smart_add_tree(tree, file_list, recurse=True, action=None, save=True):
180
128
    """Add files to version, optionally recursing into directories.
181
129
 
182
130
    This is designed more towards DWIM for humans than API simplicity.
212
160
        # validate user parameters. Our recursive code avoids adding new files
213
161
        # that need such validation 
214
162
        if tree.is_control_filename(rf.raw_path):
215
 
            raise errors.ForbiddenControlFileError(filename=rf.raw_path)
 
163
            raise errors.ForbiddenControlFileError(filename=rf)
216
164
        
217
165
        abspath = tree.abspath(rf.raw_path)
218
166
        kind = bzrlib.osutils.file_kind(abspath)
287
235
            pass
288
236
            # mutter("%r is already versioned", abspath)
289
237
        elif sub_tree:
290
 
            # XXX: This is wrong; people *might* reasonably be trying to add
291
 
            # subtrees as subtrees.  This should probably only be done in formats 
292
 
            # which can represent subtrees, and even then perhaps only when
293
 
            # the user asked to add subtrees.  At the moment you can add them
294
 
            # specially through 'join --reference', which is perhaps
295
 
            # reasonable: adding a new reference is a special operation and
296
 
            # can have a special behaviour.  mbp 20070306
297
238
            mutter("%r is a nested bzr tree", abspath)
298
239
        else:
299
240
            __add_one(tree, inv, parent_ie, directory, kind, action)
312
253
                else:
313
254
                    this_ie = inv[this_id]
314
255
 
315
 
            for subf in sorted(os.listdir(abspath)):
 
256
            for subf in os.listdir(abspath):
316
257
                # here we could use TreeDirectory rather than 
317
258
                # string concatenation.
318
259
                subp = bzrlib.osutils.pathjoin(directory.raw_path, subf)
380
321
    :param inv: Inventory which will receive the new entry.
381
322
    :param parent_ie: Parent inventory entry.
382
323
    :param kind: Kind of new entry (file, directory, etc)
383
 
    :param action: callback(inv, parent_ie, path, kind); return a file_id 
384
 
        or None to generate a new file id
 
324
    :param action: callback(inv, parent_ie, path, kind); return ignored.
385
325
    :returns: None
386
326
    """
387
 
    file_id = action(inv, parent_ie, path, kind)
388
 
    entry = bzrlib.inventory.make_entry(kind, path.base_path, parent_ie.file_id,
389
 
                                        file_id=file_id)
 
327
    action(inv, parent_ie, path, kind)
 
328
    entry = bzrlib.inventory.make_entry(kind, path.base_path, parent_ie.file_id)
390
329
    inv.add(entry)