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, NotVersionedError)
27
def add(file_list, recursive=False):
28
""" Add listed files to the branch.
30
:param file_list - list of files to be added (using full paths)
32
:param recursive - if True, all unknown files will be added
34
:return: count of ignored files
39
added, ignored = bzrlib.add.smart_add(file_list, recursive)
40
except errors.NotBranchError:
46
for glob, paths in ignored.items():
47
match_len += len(paths)
52
""" Create new versioned directory.
54
:param directory: the full path to the directory to be created
56
from bzrlib.workingtree import WorkingTree
62
raise DirectoryAlreadyExists(directory)
64
wt, dd = WorkingTree.open_containing(directory)
68
""" Move or rename given files.
70
: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
72
from bzrlib.builtins import tree_files
74
if len(names_list) < 2:
75
raise MissingArgumentError
76
tree, rel_names = tree_files(names_list)
78
if os.path.isdir(names_list[-1]):
79
# move into existing directory
80
for pair in tree.move(rel_names[:-1], rel_names[-1]):
83
if len(names_list) != 2:
84
raise MultipleMoveError
85
tree.rename_one(rel_names[0], rel_names[1])
87
def remove(file_list, new=False):
88
""" Make selected files unversioned.
90
:param file_list: list of files/directories to be removed
92
:param new: if True, the 'added' files will be removed
94
from bzrlib.builtins import tree_files
97
tree, file_list = tree_files(file_list)
98
except errors.NotBranchError:
104
if file_list is None:
105
raise NoFilesSpecified
107
from bzrlib.delta import compare_trees
108
added = [compare_trees(tree.basis_tree(), tree,
109
specific_files=file_list).added]
110
file_list = sorted([f[0] for f in added[0]], reverse=True)
111
if len(file_list) == 0:
112
raise NoMatchingFiles
115
tree.remove(file_list)
116
except errors.NotVersionedError:
117
raise NotVersionedError
119
def rename(source, target):
120
""" Rename a versioned file
122
:param source: full path to the original file
124
:param target: full path to the new file
126
if os.access(source, os.F_OK) is not True:
127
raise NonExistingSource(source)
129
move([source, target])
131
def status(filename):
132
""" Get the status of a file.
134
:param filename: the full path to the file
136
:return: renamed | added | removed | modified | unchanged | unknown
138
from bzrlib.delta import compare_trees
139
from bzrlib.workingtree import WorkingTree
142
tree1 = WorkingTree.open_containing(filename)[0]
143
except errors.NotBranchError:
146
branch = tree1.branch
147
tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
149
# find the relative path to the given file (needed for proper delta)
150
wtpath = tree1.basedir
151
#print "DEBUG: wtpath =", wtpath
153
#print "DEBUG: fullpath =", fullpath
155
wtsplit = wtpath.split('/')
156
fpsplit = fullpath.split('/')
157
fpcopy = fullpath.split('/')
159
if i is not len(wtsplit):
160
if item == wtsplit[i]:
163
rel = '/'.join(fpcopy)
164
#print "DEBUG: rel =", rel
166
delta = compare_trees(old_tree=tree2,
169
specific_files=[rel])
171
""" Debug information (could be usable in the future, so didn't cut out)
172
print "DEBUG: delta.renamed:"
173
for path, id, kind, text_modified, meta_modified in delta.renamed:
176
print "DEBUG: delta.added:"
177
for path, id, kind in delta.added:
180
print "DEBUG: delta.removed:"
181
for path, id, kind, text_modified, meta_modified in delta.removed:
184
print "DEBUG: delta.modified:"
185
for path, id, kind, text_modified, meta_modified in delta.modified:
188
print "DEBUG: delta.unchanged:"
189
for path, id, kind in delta.unchanged:
193
if len(delta.renamed):
195
elif len(delta.added):
197
elif len(delta.removed):
199
elif len(delta.modified):
201
elif len(delta.unchanged):