1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
 
2
# Some parts of the code are:
 
 
3
# Copyright (C) 2005, 2006 by Canonical Ltd
 
 
5
# This program is free software; you can redistribute it and/or modify
 
 
6
# it under the terms of the GNU General Public License as published by
 
 
7
# the Free Software Foundation; either version 2 of the License, or
 
 
8
# (at your option) any later version.
 
 
10
# This program is distributed in the hope that it will be useful,
 
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
13
# GNU General Public License for more details.
 
 
15
# You should have received a copy of the GNU General Public License
 
 
16
# along with this program; if not, write to the Free Software
 
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
21
import bzrlib.errors as errors
 
 
23
from errors import (DirectoryAlreadyExists, MissingArgumentError,
 
 
24
                    MultipleMoveError, NoFilesSpecified, NoMatchingFiles,
 
 
25
                    NonExistingSource, NotBranchError, NotSameBranchError,
 
 
26
                    NotVersionedError, PermissionDenied)
 
 
28
def add(file_list, recursive=False):
 
 
29
    """ Add listed files to the branch. 
 
 
31
    :param file_list - list of files to be added (using full paths)
 
 
33
    :param recursive - if True, all unknown files will be added
 
 
35
    :return: count of ignored files
 
 
40
        added, ignored = bzrlib.add.smart_add(file_list, recursive)
 
 
41
    except errors.NotBranchError:
 
 
47
    for glob, paths in ignored.items():
 
 
48
        match_len += len(paths)
 
 
53
    """ Create new versioned directory.
 
 
55
    :param directory: the full path to the directory to be created
 
 
57
    from bzrlib.workingtree import WorkingTree
 
 
63
            raise DirectoryAlreadyExists(directory)
 
 
66
            wt, dd = WorkingTree.open_containing(directory)
 
 
67
        except errors.NotBranchError:
 
 
73
    """ Move or rename given files.
 
 
75
    :param file_list: if two elements, then rename the first to the second, if more elements then move all of them to the directory specified in the last element
 
 
77
    from bzrlib.builtins import tree_files
 
 
79
    if len(names_list) < 2:
 
 
80
        raise MissingArgumentError
 
 
83
        tree, rel_names = tree_files(names_list)
 
 
84
    except errors.NotBranchError:
 
 
86
    except errors.BzrCommandError:
 
 
87
        # not the same branch presumably
 
 
88
        raise NotSameBranchError
 
 
90
    if os.path.isdir(names_list[-1]):
 
 
91
        # move into existing directory
 
 
92
        for pair in tree.move(rel_names[:-1], rel_names[-1]):
 
 
95
        if len(names_list) != 2:
 
 
96
            raise MultipleMoveError
 
 
97
        tree.rename_one(rel_names[0], rel_names[1])
 
 
99
def remove(file_list, new=False):
 
 
100
    """ Make selected files unversioned.
 
 
102
    :param file_list: list of files/directories to be removed
 
 
104
    :param new: if True, the 'added' files will be removed
 
 
107
    from bzrlib.builtins import tree_files
 
 
110
        tree, file_list = tree_files(file_list)
 
 
111
    except errors.NotBranchError:
 
 
117
        if file_list is None:
 
 
118
            raise NoFilesSpecified
 
 
120
        from bzrlib.delta import compare_trees
 
 
121
        if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
 
122
            added = [compare_trees(tree.basis_tree(), tree,
 
 
123
                                   specific_files=file_list).added]
 
 
125
            added = [tree.changes_from(tree.basis_tree(),
 
 
126
                                       specific_files=file_list).added]
 
 
127
        file_list = sorted([f[0] for f in added[0]], reverse=True)
 
 
128
        if len(file_list) == 0:
 
 
129
            raise NoMatchingFiles
 
 
132
        tree.remove(file_list)
 
 
133
    except errors.NotVersionedError:
 
 
134
        raise NotVersionedError
 
 
136
def rename(source, target):
 
 
137
    """ Rename a versioned file
 
 
139
    :param source: full path to the original file
 
 
141
    :param target: full path to the new file
 
 
143
    if os.access(source, os.F_OK) is not True:
 
 
144
        raise NonExistingSource(source)
 
 
146
    move([source, target])
 
 
148
def status(filename):
 
 
149
    """ Get the status of a file.
 
 
151
    :param filename: the full path to the file
 
 
153
    :return: renamed | added | removed | modified | unchanged | unknown
 
 
156
    from bzrlib.delta import compare_trees
 
 
157
    from bzrlib.workingtree import WorkingTree
 
 
160
        tree1 = WorkingTree.open_containing(filename)[0]
 
 
161
    except errors.NotBranchError:
 
 
163
    except errors.PermissionDenied:
 
 
164
        raise PermissionDenied(filename)
 
 
166
    branch = tree1.branch
 
 
167
    tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
 
 
169
    # find the relative path to the given file (needed for proper delta)
 
 
170
    wtpath = tree1.basedir
 
 
171
    #print "DEBUG: wtpath =", wtpath
 
 
173
    #print "DEBUG: fullpath =", fullpath
 
 
175
    wtsplit = wtpath.split('/')
 
 
176
    fpsplit = fullpath.split('/')
 
 
177
    fpcopy = fullpath.split('/')
 
 
179
        if i is not len(wtsplit):
 
 
180
            if item == wtsplit[i]:
 
 
183
    rel = '/'.join(fpcopy)
 
 
184
    #print "DEBUG: rel =", rel
 
 
186
    if bzrlib.version_info[1] < 9:
 
 
187
        delta = compare_trees(old_tree=tree2,
 
 
190
                              specific_files=[rel])
 
 
192
        delta = tree1.changes_from(tree2,
 
 
194
                                   specific_files=[rel])
 
 
196
    """ Debug information (could be usable in the future, so didn't cut out)
 
 
197
    print "DEBUG: delta.renamed:"
 
 
198
    for path, id, kind, text_modified, meta_modified in delta.renamed:
 
 
201
    print "DEBUG: delta.added:"
 
 
202
    for path, id, kind in delta.added:
 
 
205
    print "DEBUG: delta.removed:"
 
 
206
    for path, id, kind, text_modified, meta_modified in delta.removed:
 
 
209
    print "DEBUG: delta.modified:"
 
 
210
    for path, id, kind, text_modified, meta_modified in delta.modified:
 
 
213
    print "DEBUG: delta.unchanged:"
 
 
214
    for path, id, kind in delta.unchanged:
 
 
218
    if len(delta.renamed):
 
 
220
    elif len(delta.added):
 
 
222
    elif len(delta.removed):
 
 
224
    elif len(delta.modified):
 
 
226
    elif len(delta.unchanged):