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
from bzrlib.errors import (BzrError, NotBranchError, NotVersionedError,
25
class DirectoryAlreadyExists(BzrError):
26
""" The specified directory already exists
33
class MultipleMoveError(BzrError):
34
""" Occurs when moving/renaming more than 2 files, but the last argument is not a directory
41
class NoMatchingFiles(BzrError):
42
""" No files found which could match the criteria
49
def add(file_list, recursive=False):
50
""" Add listed files to the branch.
52
:param file_list - list of files to be added (using full paths)
54
:param recursive - if True, all unknown files will be added
56
:return: count of ignored files
60
added, ignored = bzrlib.add.smart_add(file_list, recursive)
63
for glob, paths in ignored.items():
64
match_len += len(paths)
69
""" Create new versioned directory.
71
:param directory: the full path to the directory to be created
73
from bzrlib.workingtree import WorkingTree
79
raise DirectoryAlreadyExists(directory)
81
wt, dd = WorkingTree.open_containing(directory)
85
""" Move or rename given files.
87
: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
89
from bzrlib.builtins import tree_files
91
tree, rel_names = tree_files(names_list)
93
if os.path.isdir(names_list[-1]):
94
# move into existing directory
95
for pair in tree.move(rel_names[:-1], rel_names[-1]):
98
if len(names_list) != 2:
99
raise MultipleMoveError
100
tree.rename_one(rel_names[0], rel_names[1])
103
def remove(file_list, new=False):
104
""" Make selected files unversioned.
106
:param file_list: list of files/directories to be removed
108
:param new: if True, the 'added' files will be removed
111
from bzrlib.builtins import tree_files
113
tree, file_list = tree_files(file_list)
116
from bzrlib.delta import compare_trees
117
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
118
added = [compare_trees(tree.basis_tree(), tree,
119
specific_files=file_list).added]
121
added = [tree.changes_from(tree.basis_tree(),
122
specific_files=file_list).added]
123
file_list = sorted([f[0] for f in added[0]], reverse=True)
124
if len(file_list) == 0:
125
raise NoMatchingFiles
127
tree.remove(file_list)
130
def rename(source, target):
131
""" Rename a versioned file
133
:param source: full path to the original file
135
:param target: full path to the new file
137
move([source, target])
139
def status(filename):
140
""" Get the status of a file.
142
:param filename: the full path to the file
144
:return: renamed | added | removed | modified | unchanged | unknown
147
from bzrlib.delta import compare_trees
148
from bzrlib.workingtree import WorkingTree
151
tree1 = WorkingTree.open_containing(filename)[0]
152
except NotBranchError:
155
branch = tree1.branch
156
tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
158
# find the relative path to the given file (needed for proper delta)
159
wtpath = tree1.basedir
160
#print "DEBUG: wtpath =", wtpath
162
#print "DEBUG: fullpath =", fullpath
164
wtsplit = wtpath.split('/')
165
fpsplit = fullpath.split('/')
166
fpcopy = fullpath.split('/')
168
if i is not len(wtsplit):
169
if item == wtsplit[i]:
172
rel = '/'.join(fpcopy)
173
#print "DEBUG: rel =", rel
175
if bzrlib.version_info[1] < 9:
176
delta = compare_trees(old_tree=tree2,
179
specific_files=[rel])
181
delta = tree1.changes_from(tree2,
183
specific_files=[rel])
185
""" Debug information (could be usable in the future, so didn't cut out)
186
print "DEBUG: delta.renamed:"
187
for path, id, kind, text_modified, meta_modified in delta.renamed:
190
print "DEBUG: delta.added:"
191
for path, id, kind in delta.added:
194
print "DEBUG: delta.removed:"
195
for path, id, kind, text_modified, meta_modified in delta.removed:
198
print "DEBUG: delta.modified:"
199
for path, id, kind, text_modified, meta_modified in delta.modified:
202
print "DEBUG: delta.unchanged:"
203
for path, id, kind in delta.unchanged:
207
if len(delta.renamed):
209
elif len(delta.added):
211
elif len(delta.removed):
213
elif len(delta.modified):
215
elif len(delta.unchanged):