~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Copyright (C) 2005 Canonical Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from os.path import dirname

import bzrlib.errors as errors
from bzrlib.inventory import InventoryEntry
from bzrlib.trace import mutter, note, warning
from bzrlib.errors import NotBranchError
import bzrlib.osutils
from bzrlib.workingtree import WorkingTree

def glob_expand_for_win32(file_list):
    if not file_list:
        return
    import glob
    expanded_file_list = []
    for possible_glob in file_list:
        glob_files = glob.glob(possible_glob)
       
        if glob_files == []:
            # special case to let the normal code path handle
            # files that do not exists
            expanded_file_list.append(possible_glob)
        else:
            expanded_file_list += glob_files
    return expanded_file_list


def _prepare_file_list(file_list):
    """Prepare a file list for use by smart_add_*."""
    import sys
    if sys.platform == 'win32':
        file_list = glob_expand_for_win32(file_list)
    if not file_list:
        file_list = [u'.']
    file_list = list(file_list)
    return file_list


def add_action_null(inv, parent_ie, path, kind):
    """Absorb add actions and do nothing."""
    pass


def add_action_print(inv, parent_ie, path, kind):
    """Print a line to stdout for each file that would be added."""
    print "added", bzrlib.osutils.quotefn(path)


def add_action_add(inv, parent_ie, path, kind):
    """Add each file to the given inventory. Produce no output."""
    if parent_ie is not None:
        entry = bzrlib.inventory.make_entry(
            kind, bzrlib.osutils.basename(path),  parent_ie.file_id)
        inv.add(entry)
    else:
        entry = inv.add_path(path, kind=kind)
    # mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))


def add_action_add_and_print(inv, parent_ie, path, kind):
    """Add each file to the given inventory, and print a line to stdout."""
    add_action_add(inv, parent_ie, path, kind)
    add_action_print(inv, parent_ie, path, kind)


def smart_add(file_list, recurse=True, action=add_action_add):
    """Add files to version, optionally recursing into directories.

    This is designed more towards DWIM for humans than API simplicity.
    For the specific behaviour see the help for cmd_add().

    Returns the number of files added.
    """
    file_list = _prepare_file_list(file_list)
    tree = WorkingTree.open_containing(file_list[0])[0]
    return smart_add_tree(tree, file_list, recurse, action)


def smart_add_tree(tree, file_list, recurse=True, action=add_action_add):
    """Add files to version, optionally recursing into directories.

    This is designed more towards DWIM for humans than API simplicity.
    For the specific behaviour see the help for cmd_add().

    This calls reporter with each (path, kind, file_id) of added files.

    Returns the number of files added.
    """
    import os, errno
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
    assert isinstance(recurse, bool)
    
    prepared_list = _prepare_file_list(file_list)
    mutter("smart add of %r, originally %r", prepared_list, file_list)
    inv = tree.read_working_inventory()
    added = []
    ignored = {}
    user_files = set()
    files_to_add = []

    # validate user file paths and convert all paths to tree 
    # relative : its cheaper to make a tree relative path an abspath
    # than to convert an abspath to tree relative.
    for filepath in prepared_list:
        rf = tree.relpath(filepath)
        user_files.add(rf)
        files_to_add.append((rf, None))
        # validate user parameters. Our recursive code avoids adding new files
        # that need such validation 
        if tree.is_control_filename(rf):
            raise ForbiddenFileError('cannot add control file %s' % filepath)

    for filepath, parent_ie in files_to_add:
        # filepath is tree-relative
        abspath = tree.abspath(filepath)

        # find the kind of the path being added. This is not
        # currently determined when we list directories 
        # recursively, but in theory we can determine while 
        # doing the directory listing on *some* platformans.
        # TODO: a safe, portable, clean interface which will 
        # be faster than os.listdir() + stat. Specifically,
        # readdir - dirent.d_type supplies the file type when
        # it is defined. (Apparently Mac OSX has the field but
        # does not fill it in ?!) Robert C, Martin P.
        try:
            kind = bzrlib.osutils.file_kind(abspath)
        except OSError, e:
            if hasattr(e, 'errno') and e.errno == errno.ENOENT:
                raise errors.NoSuchFile(abspath)
            raise

        # we need to call this to determine the inventory kind to create.
        if not InventoryEntry.versionable_kind(kind):
            if filepath in user_files:
                raise BadFileKindError("cannot add %s of type %s" % (abspath, kind))
            else:
                warning("skipping %s (can't add file of kind '%s')", abspath, kind)
                continue

        if parent_ie is not None:
            versioned = bzrlib.osutils.basename(filepath) in parent_ie.children
        else:
            # without the parent ie, use the relatively slower inventory 
            # probing method
            versioned = inv.has_filename(filepath)

        if kind == 'directory':
            try:
                sub_branch = bzrlib.bzrdir.BzrDir.open(abspath)
                sub_tree = True
            except NotBranchError:
                sub_tree = False
            except errors.UnsupportedFormatError:
                sub_tree = True
        else:
            sub_tree = False

        if filepath == '':
            # mutter("tree root doesn't need to be added")
            sub_tree = False
        elif versioned:
            pass
            # mutter("%r is already versioned", abspath)
        elif sub_tree:
            mutter("%r is a nested bzr tree", abspath)
        else:
            added.extend(__add_one(tree, inv, parent_ie, filepath, kind, action))

        if kind == 'directory' and recurse and not sub_tree:
            try:
                if parent_ie is not None:
                    # must be present:
                    this_ie = parent_ie.children[bzrlib.osutils.basename(filepath)]
                else:
                    # without the parent ie, use the relatively slower inventory 
                    # probing method
                    this_id = inv.path2id(filepath)
                    if this_id is None:
                        this_ie = None
                    else:
                        this_ie = inv[this_id]
            except KeyError:
                this_ie = None

            for subf in os.listdir(abspath):
                # here we could use TreeDirectory rather than 
                # string concatenation.
                subp = bzrlib.osutils.pathjoin(filepath, subf)
                # TODO: is_control_filename is very slow. Make it faster. 
                # TreeDirectory.is_control_filename could also make this 
                # faster - its impossible for a non root dir to have a 
                # control file.
                if tree.is_control_filename(subp):
                    mutter("skip control directory %r", subp)
                else:
                    # ignore while selecting files - if we globbed in the
                    # outer loop we would ignore user files.
                    ignore_glob = tree.is_ignored(subp)
                    if ignore_glob is not None:
                        # mutter("skip ignored sub-file %r", subp)
                        if ignore_glob not in ignored:
                            ignored[ignore_glob] = []
                        ignored[ignore_glob].append(subp)
                    else:
                        #mutter("queue to add sub-file %r", subp)
                        files_to_add.append((subp, this_ie))

    if len(added) > 0:
        tree._write_inventory(inv)
    return added, ignored


def __add_one(tree, inv, parent_ie, path, kind, action):
    """Add a new entry to the inventory and automatically add unversioned parents.

    Actual adding of the entry is delegated to the action callback.

    :param inv: Inventory which will receive the new entry.
    :param parent_ie: Parent inventory entry if known, or None.  If
    None, the parent is looked up by name and used if present, otherwise
    it is recursively added.
    :param kind: Kind of new entry (file, directory, etc)
    :param action: callback(inv, parent_ie, path, kind); return ignored.
    :returns: A list of paths which have been added.
    """

    # Nothing to do if path is already versioned.
    # This is safe from infinite recursion because the tree root is
    # always versioned.
    if parent_ie is not None:
        # we have a parent ie already
        added = []
    else:
        # slower but does not need parent_ie
        if inv.has_filename(path):
            return []
        # add parent
        added = __add_one(tree, inv, None, dirname(path), 'directory', action)
        parent_id = inv.path2id(dirname(path))
        if parent_id is not None:
            parent_ie = inv[inv.path2id(dirname(path))]
        else:
            parent_ie = None
    action(inv, parent_ie, path, kind)

    return added + [path]