/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
70 by mbp at sourcefrog
Prepare for smart recursive add.
1
# Copyright (C) 2005 Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
17
import sys
1185.1.53 by Robert Collins
raise a specific error on unsupported branches so that they can be distinguished from generic errors
18
from os.path import dirname
19
20
import bzrlib.errors as errors
1399.1.1 by Robert Collins
move checks for versionability of file kinds into InventoryEntry
21
from bzrlib.inventory import InventoryEntry
1128 by Martin Pool
- smart_add routine now doesn't print filenames directly, but rather
22
from bzrlib.trace import mutter, note, warning
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
23
from bzrlib.errors import NotBranchError
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
24
from bzrlib.workingtree import is_control_file
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
25
import bzrlib.osutils
26
from bzrlib.workingtree import WorkingTree
70 by mbp at sourcefrog
Prepare for smart recursive add.
27
686 by Martin Pool
- glob expand add arguments on win32
28
def glob_expand_for_win32(file_list):
1185.23.2 by Aaron Bentley
win32 glob expansion fix from Belchenko
29
    if not file_list:
30
        return
686 by Martin Pool
- glob expand add arguments on win32
31
    import glob
32
    expanded_file_list = []
33
    for possible_glob in file_list:
34
        glob_files = glob.glob(possible_glob)
35
       
36
        if glob_files == []:
37
            # special case to let the normal code path handle
38
            # files that do not exists
39
            expanded_file_list.append(possible_glob)
40
        else:
41
            expanded_file_list += glob_files
42
    return expanded_file_list
43
1128 by Martin Pool
- smart_add routine now doesn't print filenames directly, but rather
44
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
45
def _prepare_file_list(file_list):
46
    """Prepare a file list for use by smart_add_*."""
47
    if sys.platform == 'win32':
48
        file_list = glob_expand_for_win32(file_list)
49
    if not file_list:
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
50
        file_list = [u'.']
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
51
    file_list = list(file_list)
52
    return file_list
53
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
54
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
55
class AddAction(object):
56
    """A class which defines what action to take when adding a file."""
57
58
    should_add = False
59
    should_print = False
60
61
    def __init__(self, to_file=None, should_add=None, should_print=None):
62
        self._to_file = to_file
63
        if to_file is None:
64
            self._to_file = sys.stdout
65
        if should_add is not None:
66
            self.should_add = should_add
67
        if should_print is not None:
68
            self.should_print = should_print
69
70
    def __call__(self, inv, path, kind):
71
        """Add path to inventory.
72
73
        The default action does nothing.
74
75
        :param inv: The inventory we are working with.
76
        :param path: The path being added
77
        :param kind: The kind of the object being added.
78
        """
79
        if self.should_add:
80
            self._add_to_inv(inv, path, kind)
81
        if self.should_print:
82
            self._print(inv, path, kind)
83
84
    def _print(self, inv, path, kind):
85
        self._to_file.write('added ')
86
        self._to_file.write(bzrlib.osutils.quotefn(path))
87
        self._to_file.write('\n')
88
89
    def _add_to_inv(self, inv, path, kind):
90
        entry = inv.add_path(path, kind=kind)
91
        mutter("added %r kind %r file_id={%s}", path, kind, entry.file_id)
92
93
94
# TODO: jam 20050105 These could be used for compatibility
95
#       however, they bind against the current stdout, not the
96
#       one which exists at the time they are called, so they
97
#       don't work for the test suite.
98
# deprecated
99
add_action_null = AddAction()
100
add_action_add = AddAction(should_add=True)
101
add_action_print = AddAction(should_print=True)
102
add_action_add_and_print = AddAction(should_add=True, should_print=True)
103
104
105
def smart_add(file_list, recurse=True, action=None):
598 by Martin Pool
doc
106
    """Add files to version, optionally recursing into directories.
70 by mbp at sourcefrog
Prepare for smart recursive add.
107
108
    This is designed more towards DWIM for humans than API simplicity.
109
    For the specific behaviour see the help for cmd_add().
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
110
111
    Returns the number of files added.
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
112
70 by mbp at sourcefrog
Prepare for smart recursive add.
113
    """
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
114
    file_list = _prepare_file_list(file_list)
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
115
    tree = WorkingTree.open_containing(file_list[0])[0]
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
116
    return smart_add_tree(tree, file_list, recurse, action=action)
117
118
119
def smart_add_tree(tree, file_list, recurse=True, action=None):
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
120
    """Add files to version, optionally recursing into directories.
121
122
    This is designed more towards DWIM for humans than API simplicity.
123
    For the specific behaviour see the help for cmd_add().
1128 by Martin Pool
- smart_add routine now doesn't print filenames directly, but rather
124
1508.1.11 by Robert Collins
Documentation.
125
    This calls reporter with each (path, kind, file_id) of added files.
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
126
127
    Returns the number of files added.
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
128
    """
1185.56.2 by Michael Ellerman
Raise NoSuchFile when someone tries to add a non-existant file.
129
    import os, errno
599 by Martin Pool
- better error reporting from smart_add
130
    from bzrlib.errors import BadFileKindError, ForbiddenFileError
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
131
    assert isinstance(recurse, bool)
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
132
    if action is None:
133
        action = AddAction(should_add=True)
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
134
1092.1.29 by Robert Collins
break smart_add into smart_add and smart_add_branch which will accept a branch parameter
135
    file_list = _prepare_file_list(file_list)
426 by Martin Pool
- Skip symlinks during recursive add (path from aaron)
136
    user_list = file_list[:]
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
137
    inv = tree.read_working_inventory()
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
138
    added = []
139
    ignored = {}
86 by mbp at sourcefrog
first cut at recursive add
140
70 by mbp at sourcefrog
Prepare for smart recursive add.
141
    for f in file_list:
1457.1.4 by Robert Collins
Branch.relpath has been moved to WorkingTree.relpath.
142
        rf = tree.relpath(f)
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
143
        af = tree.abspath(rf)
510 by Martin Pool
- fix add of files when standing in subdirectory
144
1185.56.2 by Michael Ellerman
Raise NoSuchFile when someone tries to add a non-existant file.
145
        try:
146
            kind = bzrlib.osutils.file_kind(af)
147
        except OSError, e:
148
            if hasattr(e, 'errno') and e.errno == errno.ENOENT:
149
                raise errors.NoSuchFile(rf)
150
            raise
426 by Martin Pool
- Skip symlinks during recursive add (path from aaron)
151
1399.1.1 by Robert Collins
move checks for versionability of file kinds into InventoryEntry
152
        if not InventoryEntry.versionable_kind(kind):
599 by Martin Pool
- better error reporting from smart_add
153
            if f in user_list:
154
                raise BadFileKindError("cannot add %s of type %s" % (f, kind))
155
            else:
1128 by Martin Pool
- smart_add routine now doesn't print filenames directly, but rather
156
                warning("skipping %s (can't add file of kind '%s')", f, kind)
426 by Martin Pool
- Skip symlinks during recursive add (path from aaron)
157
                continue
158
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
159
        mutter("smart add of %r, abs=%r", f, af)
70 by mbp at sourcefrog
Prepare for smart recursive add.
160
        
1534.5.5 by Robert Collins
Move is_control_file into WorkingTree.is_control_filename and test.
161
        if tree.is_control_filename(af):
599 by Martin Pool
- better error reporting from smart_add
162
            raise ForbiddenFileError('cannot add control file %s' % f)
87 by mbp at sourcefrog
- clean up smart_add code, and make it commit the inventory
163
            
86 by mbp at sourcefrog
first cut at recursive add
164
        versioned = (inv.path2id(rf) != None)
165
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
166
        if kind == 'directory':
167
            try:
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
168
                sub_branch = WorkingTree.open(af)
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
169
                sub_tree = True
170
            except NotBranchError:
171
                sub_tree = False
1185.1.53 by Robert Collins
raise a specific error on unsupported branches so that they can be distinguished from generic errors
172
            except errors.UnsupportedFormatError:
173
                sub_tree = True
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
174
        else:
175
            sub_tree = False
176
91 by mbp at sourcefrog
special handling of root directory for recursive add
177
        if rf == '':
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
178
            mutter("tree root doesn't need to be added")
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
179
            sub_tree = False
91 by mbp at sourcefrog
special handling of root directory for recursive add
180
        elif versioned:
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
181
            mutter("%r is already versioned", f)
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
182
        elif sub_tree:
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
183
            mutter("%r is a bzr tree", f)
70 by mbp at sourcefrog
Prepare for smart recursive add.
184
        else:
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
185
            added.extend(__add_one(tree, inv, rf, kind, action))
87 by mbp at sourcefrog
- clean up smart_add code, and make it commit the inventory
186
1092.1.27 by Robert Collins
two bugfixes to smart_add - do not add paths from nested trees to the parent tree, and do not mutate the user supplied file list
187
        if kind == 'directory' and recurse and not sub_tree:
87 by mbp at sourcefrog
- clean up smart_add code, and make it commit the inventory
188
            for subf in os.listdir(af):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
189
                subp = bzrlib.osutils.pathjoin(rf, subf)
1534.5.5 by Robert Collins
Move is_control_file into WorkingTree.is_control_filename and test.
190
                if tree.is_control_filename(subp):
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
191
                    mutter("skip control directory %r", subp)
87 by mbp at sourcefrog
- clean up smart_add code, and make it commit the inventory
192
                else:
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
193
                    ignore_glob = tree.is_ignored(subp)
194
                    if ignore_glob is not None:
195
                        mutter("skip ignored sub-file %r", subp)
196
                        if ignore_glob not in ignored:
197
                            ignored[ignore_glob] = []
198
                        ignored[ignore_glob].append(subp)
199
                    else:
200
                        mutter("queue to add sub-file %r", subp)
201
                        file_list.append(tree.abspath(subp))
202
203
204
    mutter('added %d entries', len(added))
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
205
    
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
206
    if len(added) > 0:
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
207
        tree._write_inventory(inv)
1159 by Martin Pool
- clean up parameters to smart_add and smart_add_branch
208
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
209
    return added, ignored
1185.3.3 by Martin Pool
- patch from mpe to automatically add parent directories
210
1185.85.12 by John Arbash Meinel
Refactoring AddAction to allow redirecting to an encoding file.
211
1185.53.1 by Michael Ellerman
Add support for bzr add --dry-run
212
def __add_one(tree, inv, path, kind, action):
1185.3.3 by Martin Pool
- patch from mpe to automatically add parent directories
213
    """Add a file or directory, automatically add unversioned parents."""
214
215
    # Nothing to do if path is already versioned.
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
216
    # This is safe from infinite recursion because the tree root is
1185.3.3 by Martin Pool
- patch from mpe to automatically add parent directories
217
    # always versioned.
218
    if inv.path2id(path) != None:
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
219
        return []
1185.3.3 by Martin Pool
- patch from mpe to automatically add parent directories
220
221
    # add parent
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
222
    added = __add_one(tree, inv, dirname(path), 'directory', action)
1185.53.1 by Michael Ellerman
Add support for bzr add --dry-run
223
    action(inv, path, kind)
1185.3.3 by Martin Pool
- patch from mpe to automatically add parent directories
224
1185.46.8 by Aaron Bentley
bzr add reports ignored patterns.
225
    return added + [path]