29
29
WorkingTree.open(dir).
32
# TODO: Give the workingtree sole responsibility for the working inventory;
33
# remove the variable and references to it from the branch. This may require
34
# updating the commit code so as to update the inventory within the working
35
# copy, and making sure there's only one WorkingTree for any directory on disk.
36
# At the moment they may alias the inventory and have old copies of it in
37
# memory. (Now done? -- mbp 20060309)
39
33
from cStringIO import StringIO
101
96
from bzrlib.filters import filtered_input_file
102
97
from bzrlib.trace import mutter, note
103
98
from bzrlib.transport.local import LocalTransport
104
from bzrlib.progress import DummyProgress, ProgressPhase
105
99
from bzrlib.revision import CURRENT_REVISION
106
100
from bzrlib.rio import RioReader, rio_file, Stanza
107
101
from bzrlib.symbol_versioning import (
209
210
self._branch = self.bzrdir.open_branch()
210
211
self.basedir = realpath(basedir)
211
# if branch is at our basedir and is a format 6 or less
212
if isinstance(self._format, WorkingTreeFormat2):
213
# share control object
214
self._control_files = self.branch.control_files
216
# assume all other formats have their own control files.
217
self._control_files = _control_files
212
self._control_files = _control_files
218
213
self._transport = self._control_files._transport
219
214
# update the whole cache up front and write to disk if anything changed;
220
215
# in the future we might want to do this more selectively
342
345
path = osutils.getcwd()
343
346
control, relpath = bzrdir.BzrDir.open_containing(path)
345
347
return control.open_workingtree(), relpath
350
def open_containing_paths(file_list, default_directory=None,
351
canonicalize=True, apply_view=True):
352
"""Open the WorkingTree that contains a set of paths.
354
Fail if the paths given are not all in a single tree.
356
This is used for the many command-line interfaces that take a list of
357
any number of files and that require they all be in the same tree.
359
if default_directory is None:
360
default_directory = u'.'
361
# recommended replacement for builtins.internal_tree_files
362
if file_list is None or len(file_list) == 0:
363
tree = WorkingTree.open_containing(default_directory)[0]
364
# XXX: doesn't really belong here, and seems to have the strange
365
# side effect of making it return a bunch of files, not the whole
366
# tree -- mbp 20100716
367
if tree.supports_views() and apply_view:
368
view_files = tree.views.lookup_view()
370
file_list = view_files
371
view_str = views.view_display_str(view_files)
372
note("Ignoring files outside view. View is %s" % view_str)
373
return tree, file_list
374
if default_directory == u'.':
377
seed = default_directory
378
file_list = [osutils.pathjoin(default_directory, f)
380
tree = WorkingTree.open_containing(seed)[0]
381
return tree, tree.safe_relpath_files(file_list, canonicalize,
382
apply_view=apply_view)
384
def safe_relpath_files(self, file_list, canonicalize=True, apply_view=True):
385
"""Convert file_list into a list of relpaths in tree.
387
:param self: A tree to operate on.
388
:param file_list: A list of user provided paths or None.
389
:param apply_view: if True and a view is set, apply it or check that
390
specified files are within it
391
:return: A list of relative paths.
392
:raises errors.PathNotChild: When a provided path is in a different self
395
if file_list is None:
397
if self.supports_views() and apply_view:
398
view_files = self.views.lookup_view()
402
# self.relpath exists as a "thunk" to osutils, but canonical_relpath
403
# doesn't - fix that up here before we enter the loop.
405
fixer = lambda p: osutils.canonical_relpath(self.basedir, p)
408
for filename in file_list:
409
relpath = fixer(osutils.dereference_path(filename))
410
if view_files and not osutils.is_inside_any(view_files, relpath):
411
raise errors.FileOutsideView(filename, view_files)
412
new_list.append(relpath)
348
416
def open_downlevel(path=None):
349
417
"""Open an unsupported working tree.
363
431
return True, None
365
433
return True, tree
366
transport = get_transport(location)
367
iterator = bzrdir.BzrDir.find_bzrdirs(transport, evaluate=evaluate,
434
t = transport.get_transport(location)
435
iterator = bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate,
368
436
list_current=list_current)
369
return [t for t in iterator if t is not None]
437
return [tr for tr in iterator if tr is not None]
371
439
# should be deprecated - this is slow and in any case treating them as a
372
440
# container is (we now know) bad style -- mbp 20070302
457
525
return (file_obj, stat_value)
459
527
def get_file_text(self, file_id, path=None, filtered=True):
460
return self.get_file(file_id, path=path, filtered=filtered).read()
528
my_file = self.get_file(file_id, path=path, filtered=filtered)
530
return my_file.read()
462
534
def get_file_byname(self, filename, filtered=True):
463
535
path = self.abspath(filename)
518
590
# Now we have the parents of this content
519
591
annotator = self.branch.repository.texts.get_annotator()
520
text = self.get_file(file_id).read()
592
text = self.get_file_text(file_id)
521
593
this_key =(file_id, default_revision)
522
594
annotator.add_special_text(this_key, file_parent_keys, text)
523
595
annotations = [(key[-1], line)
909
981
branch.last_revision().
911
983
from bzrlib.merge import Merger, Merge3Merger
912
pb = ui.ui_factory.nested_progress_bar()
914
merger = Merger(self.branch, this_tree=self, pb=pb)
915
merger.pp = ProgressPhase("Merge phase", 5, pb)
916
merger.pp.next_phase()
917
# check that there are no local alterations
918
if not force and self.has_changes():
919
raise errors.UncommittedChanges(self)
920
if to_revision is None:
921
to_revision = _mod_revision.ensure_null(branch.last_revision())
922
merger.other_rev_id = to_revision
923
if _mod_revision.is_null(merger.other_rev_id):
924
raise errors.NoCommits(branch)
925
self.branch.fetch(branch, last_revision=merger.other_rev_id)
926
merger.other_basis = merger.other_rev_id
927
merger.other_tree = self.branch.repository.revision_tree(
929
merger.other_branch = branch
930
merger.pp.next_phase()
931
if from_revision is None:
934
merger.set_base_revision(from_revision, branch)
935
if merger.base_rev_id == merger.other_rev_id:
936
raise errors.PointlessMerge
937
merger.backup_files = False
938
if merge_type is None:
939
merger.merge_type = Merge3Merger
941
merger.merge_type = merge_type
942
merger.set_interesting_files(None)
943
merger.show_base = False
944
merger.reprocess = False
945
conflicts = merger.do_merge()
984
merger = Merger(self.branch, this_tree=self)
985
# check that there are no local alterations
986
if not force and self.has_changes():
987
raise errors.UncommittedChanges(self)
988
if to_revision is None:
989
to_revision = _mod_revision.ensure_null(branch.last_revision())
990
merger.other_rev_id = to_revision
991
if _mod_revision.is_null(merger.other_rev_id):
992
raise errors.NoCommits(branch)
993
self.branch.fetch(branch, last_revision=merger.other_rev_id)
994
merger.other_basis = merger.other_rev_id
995
merger.other_tree = self.branch.repository.revision_tree(
997
merger.other_branch = branch
998
if from_revision is None:
1001
merger.set_base_revision(from_revision, branch)
1002
if merger.base_rev_id == merger.other_rev_id:
1003
raise errors.PointlessMerge
1004
merger.backup_files = False
1005
if merge_type is None:
1006
merger.merge_type = Merge3Merger
1008
merger.merge_type = merge_type
1009
merger.set_interesting_files(None)
1010
merger.show_base = False
1011
merger.reprocess = False
1012
conflicts = merger.do_merge()
1013
merger.set_pending()
949
1014
return conflicts
951
1016
@needs_read_lock
1098
1163
tree_transport = self.bzrdir.root_transport.clone(sub_path)
1099
1164
if tree_transport.base != branch_transport.base:
1100
1165
tree_bzrdir = format.initialize_on_transport(tree_transport)
1101
branch.BranchReferenceFormat().initialize(tree_bzrdir, new_branch)
1166
branch.BranchReferenceFormat().initialize(tree_bzrdir,
1167
target_branch=new_branch)
1103
1169
tree_bzrdir = branch_bzrdir
1104
1170
wt = tree_bzrdir.create_workingtree(_mod_revision.NULL_REVISION)
1142
1208
This does not include files that have been deleted in this
1143
1209
tree. Skips the control directory.
1145
:param include_root: if True, do not return an entry for the root
1211
:param include_root: if True, return an entry for the root
1146
1212
:param from_dir: start from this directory or None for the root
1147
1213
:param recursive: whether to recurse into subdirectories or not
1203
1269
# absolute path
1204
1270
fap = from_dir_abspath + '/' + f
1206
f_ie = inv.get_child(from_dir_id, f)
1272
dir_ie = inv[from_dir_id]
1273
if dir_ie.kind == 'directory':
1274
f_ie = dir_ie.children.get(f)
1209
1279
elif self.is_ignored(fp[1:]):
1212
# we may not have found this file, because of a unicode issue
1282
# we may not have found this file, because of a unicode
1283
# issue, or because the directory was actually a symlink.
1213
1284
f_norm, can_access = osutils.normalized_filename(f)
1214
1285
if f == f_norm or not can_access:
1215
1286
# No change, so treat this file normally
1299
1370
# check for deprecated use of signature
1300
1371
if to_dir is None:
1301
to_dir = kwargs.get('to_name', None)
1303
raise TypeError('You must supply a target directory')
1305
symbol_versioning.warn('The parameter to_name was deprecated'
1306
' in version 0.13. Use to_dir instead',
1372
raise TypeError('You must supply a target directory')
1309
1373
# check destination directory
1310
1374
if isinstance(from_paths, basestring):
1311
1375
raise ValueError()
1602
1666
@needs_write_lock
1603
1667
def pull(self, source, overwrite=False, stop_revision=None,
1604
change_reporter=None, possible_transports=None, local=False):
1605
top_pb = ui.ui_factory.nested_progress_bar()
1668
change_reporter=None, possible_transports=None, local=False,
1606
1670
source.lock_read()
1608
pp = ProgressPhase("Pull phase", 2, top_pb)
1610
1672
old_revision_info = self.branch.last_revision_info()
1611
1673
basis_tree = self.basis_tree()
1612
1674
count = self.branch.pull(source, overwrite, stop_revision,
1625
1685
new_basis_tree,
1627
1687
this_tree=self,
1629
change_reporter=change_reporter)
1689
change_reporter=change_reporter,
1690
show_base=show_base)
1630
1691
basis_root_id = basis_tree.get_root_id()
1631
1692
new_root_id = new_basis_tree.get_root_id()
1632
1693
if basis_root_id != new_root_id:
1633
1694
self.set_root_id(new_root_id)
1636
1696
basis_tree.unlock()
1637
1697
# TODO - dedup parents list with things merged by pull ?
1638
1698
# reuse the revisiontree we merged against to set the new
1806
1865
raise errors.ObjectNotLocked(self)
1808
1867
def lock_read(self):
1809
"""See Branch.lock_read, and WorkingTree.unlock."""
1868
"""Lock the tree for reading.
1870
This also locks the branch, and can be unlocked via self.unlock().
1872
:return: A bzrlib.lock.LogicalLockResult.
1810
1874
if not self.is_locked():
1811
1875
self._reset_data()
1812
1876
self.branch.lock_read()
1814
return self._control_files.lock_read()
1878
self._control_files.lock_read()
1879
return LogicalLockResult(self.unlock)
1816
1881
self.branch.unlock()
1819
1884
def lock_tree_write(self):
1820
"""See MutableTree.lock_tree_write, and WorkingTree.unlock."""
1885
"""See MutableTree.lock_tree_write, and WorkingTree.unlock.
1887
:return: A bzrlib.lock.LogicalLockResult.
1821
1889
if not self.is_locked():
1822
1890
self._reset_data()
1823
1891
self.branch.lock_read()
1825
return self._control_files.lock_write()
1893
self._control_files.lock_write()
1894
return LogicalLockResult(self.unlock)
1827
1896
self.branch.unlock()
1830
1899
def lock_write(self):
1831
"""See MutableTree.lock_write, and WorkingTree.unlock."""
1900
"""See MutableTree.lock_write, and WorkingTree.unlock.
1902
:return: A bzrlib.lock.LogicalLockResult.
1832
1904
if not self.is_locked():
1833
1905
self._reset_data()
1834
1906
self.branch.lock_write()
1836
return self._control_files.lock_write()
1908
self._control_files.lock_write()
1909
return LogicalLockResult(self.unlock)
1838
1911
self.branch.unlock()
1904
1977
# revision_id is set. We must check for this full string, because a
1905
1978
# root node id can legitimately look like 'revision_id' but cannot
1906
1979
# contain a '"'.
1907
xml = self.branch.repository.get_inventory_xml(new_revision)
1980
xml = self.branch.repository._get_inventory_xml(new_revision)
1908
1981
firstline = xml.split('\n', 1)[0]
1909
1982
if (not 'revision_id="' in firstline or
1910
1983
'format="7"' not in firstline):
2032
all_files = set() # specified and nested files
1960
2033
unknown_nested_files=set()
1961
2034
if to_file is None:
1962
2035
to_file = sys.stdout
2037
files_to_backup = []
1964
2039
def recurse_directory_to_add_files(directory):
1965
2040
# Recurse directory and add all files
1966
2041
# so we can check if they have changed.
1967
for parent_info, file_infos in\
1968
self.walkdirs(directory):
2042
for parent_info, file_infos in self.walkdirs(directory):
1969
2043
for relpath, basename, kind, lstat, fileid, kind in file_infos:
1970
2044
# Is it versioned or ignored?
1971
if self.path2id(relpath) or self.is_ignored(relpath):
2045
if self.path2id(relpath):
1972
2046
# Add nested content for deletion.
1973
new_files.add(relpath)
2047
all_files.add(relpath)
1975
# Files which are not versioned and not ignored
2049
# Files which are not versioned
1976
2050
# should be treated as unknown.
1977
unknown_nested_files.add((relpath, None, kind))
2051
files_to_backup.append(relpath)
1979
2053
for filename in files:
1980
2054
# Get file name into canonical form.
1981
2055
abspath = self.abspath(filename)
1982
2056
filename = self.relpath(abspath)
1983
2057
if len(filename) > 0:
1984
new_files.add(filename)
2058
all_files.add(filename)
1985
2059
recurse_directory_to_add_files(filename)
1987
files = list(new_files)
2061
files = list(all_files)
1989
2063
if len(files) == 0:
1990
2064
return # nothing to do
1995
2069
# Bail out if we are going to delete files we shouldn't
1996
2070
if not keep_files and not force:
1997
has_changed_files = len(unknown_nested_files) > 0
1998
if not has_changed_files:
1999
for (file_id, path, content_change, versioned, parent_id, name,
2000
kind, executable) in self.iter_changes(self.basis_tree(),
2001
include_unchanged=True, require_versioned=False,
2002
want_unversioned=True, specific_files=files):
2003
if versioned == (False, False):
2004
# The record is unknown ...
2005
if not self.is_ignored(path[1]):
2006
# ... but not ignored
2007
has_changed_files = True
2009
elif content_change and (kind[1] is not None):
2010
# Versioned and changed, but not deleted
2011
has_changed_files = True
2071
for (file_id, path, content_change, versioned, parent_id, name,
2072
kind, executable) in self.iter_changes(self.basis_tree(),
2073
include_unchanged=True, require_versioned=False,
2074
want_unversioned=True, specific_files=files):
2075
if versioned[0] == False:
2076
# The record is unknown or newly added
2077
files_to_backup.append(path[1])
2078
elif (content_change and (kind[1] is not None) and
2079
osutils.is_inside_any(files, path[1])):
2080
# Versioned and changed, but not deleted, and still
2081
# in one of the dirs to be deleted.
2082
files_to_backup.append(path[1])
2014
if has_changed_files:
2015
# Make delta show ALL applicable changes in error message.
2016
tree_delta = self.changes_from(self.basis_tree(),
2017
require_versioned=False, want_unversioned=True,
2018
specific_files=files)
2019
for unknown_file in unknown_nested_files:
2020
if unknown_file not in tree_delta.unversioned:
2021
tree_delta.unversioned.extend((unknown_file,))
2022
raise errors.BzrRemoveChangedFilesError(tree_delta)
2084
def backup(file_to_backup):
2085
backup_name = self.bzrdir._available_backup_name(file_to_backup)
2086
osutils.rename(abs_path, self.abspath(backup_name))
2087
return "removed %s (but kept a copy: %s)" % (file_to_backup,
2024
2090
# Build inv_delta and delete files where applicable,
2025
2091
# do this before any modifications to inventory.
2049
2115
len(os.listdir(abs_path)) > 0):
2051
2117
osutils.rmtree(abs_path)
2118
message = "deleted %s" % (f,)
2053
message = "%s is not an empty directory "\
2054
"and won't be deleted." % (f,)
2056
osutils.delete_any(abs_path)
2057
message = "deleted %s" % (f,)
2122
if f in files_to_backup:
2125
osutils.delete_any(abs_path)
2126
message = "deleted %s" % (f,)
2058
2127
elif message is not None:
2059
2128
# Only care if we haven't done anything yet.
2060
2129
message = "%s does not exist." % (f,)
2197
2266
_marker = object()
2199
2268
def update(self, change_reporter=None, possible_transports=None,
2200
revision=None, old_tip=_marker):
2269
revision=None, old_tip=_marker, show_base=False):
2201
2270
"""Update a working tree along its branch.
2203
2272
This will update the branch if its bound too, which means we have
2241
2310
if old_tip is self._marker:
2243
return self._update_tree(old_tip, change_reporter, revision)
2312
return self._update_tree(old_tip, change_reporter, revision, show_base)
2247
2316
@needs_tree_write_lock
2248
def _update_tree(self, old_tip=None, change_reporter=None, revision=None):
2317
def _update_tree(self, old_tip=None, change_reporter=None, revision=None,
2249
2319
"""Update a tree to the master branch.
2251
2321
:param old_tip: if supplied, the previous tip revision the branch,
2261
2331
# We MUST save it even if an error occurs, because otherwise the users
2262
2332
# local work is unreferenced and will appear to have been lost.
2266
2336
last_rev = self.get_parent_ids()[0]
2267
2337
except IndexError:
2268
2338
last_rev = _mod_revision.NULL_REVISION
2269
2339
if revision is None:
2270
2340
revision = self.branch.last_revision()
2272
if revision not in self.branch.revision_history():
2273
raise errors.NoSuchRevision(self.branch, revision)
2342
old_tip = old_tip or _mod_revision.NULL_REVISION
2344
if not _mod_revision.is_null(old_tip) and old_tip != last_rev:
2345
# the branch we are bound to was updated
2346
# merge those changes in first
2347
base_tree = self.basis_tree()
2348
other_tree = self.branch.repository.revision_tree(old_tip)
2349
nb_conflicts = merge.merge_inner(self.branch, other_tree,
2350
base_tree, this_tree=self,
2351
change_reporter=change_reporter,
2352
show_base=show_base)
2354
self.add_parent_tree((old_tip, other_tree))
2355
trace.note('Rerun update after fixing the conflicts.')
2274
2358
if last_rev != _mod_revision.ensure_null(revision):
2275
# merge tree state up to specified revision.
2359
# the working tree is up to date with the branch
2360
# we can merge the specified revision from master
2361
to_tree = self.branch.repository.revision_tree(revision)
2362
to_root_id = to_tree.get_root_id()
2276
2364
basis = self.basis_tree()
2277
2365
basis.lock_read()
2279
to_tree = self.branch.repository.revision_tree(revision)
2280
to_root_id = to_tree.get_root_id()
2281
2367
if (basis.inventory.root is None
2282
2368
or basis.inventory.root.file_id != to_root_id):
2283
2369
self.set_root_id(to_root_id)
2285
result += merge.merge_inner(
2290
change_reporter=change_reporter)
2291
self.set_last_revision(revision)
2374
# determine the branch point
2375
graph = self.branch.repository.get_graph()
2376
base_rev_id = graph.find_unique_lca(self.branch.last_revision(),
2378
base_tree = self.branch.repository.revision_tree(base_rev_id)
2380
nb_conflicts = merge.merge_inner(self.branch, to_tree, base_tree,
2382
change_reporter=change_reporter,
2383
show_base=show_base)
2384
self.set_last_revision(revision)
2294
2385
# TODO - dedup parents list with things merged by pull ?
2295
2386
# reuse the tree we've updated to to set the basis:
2296
2387
parent_trees = [(revision, to_tree)]
2303
2394
for parent in merges:
2304
2395
parent_trees.append(
2305
2396
(parent, self.branch.repository.revision_tree(parent)))
2306
if (old_tip is not None and not _mod_revision.is_null(old_tip)):
2397
if not _mod_revision.is_null(old_tip):
2307
2398
parent_trees.append(
2308
2399
(old_tip, self.branch.repository.revision_tree(old_tip)))
2309
2400
self.set_parent_trees(parent_trees)
2310
2401
last_rev = parent_trees[0][0]
2312
# the working tree had the same last-revision as the master
2313
# branch did. We may still have pivot local work from the local
2314
# branch into old_tip:
2315
if (old_tip is not None and not _mod_revision.is_null(old_tip)):
2316
self.add_parent_tree_id(old_tip)
2317
if (old_tip is not None and not _mod_revision.is_null(old_tip)
2318
and old_tip != last_rev):
2319
# our last revision was not the prior branch last revision
2320
# and we have converted that last revision to a pending merge.
2321
# base is somewhere between the branch tip now
2322
# and the now pending merge
2324
# Since we just modified the working tree and inventory, flush out
2325
# the current state, before we modify it again.
2326
# TODO: jam 20070214 WorkingTree3 doesn't require this, dirstate
2327
# requires it only because TreeTransform directly munges the
2328
# inventory and calls tree._write_inventory(). Ultimately we
2329
# should be able to remove this extra flush.
2331
graph = self.branch.repository.get_graph()
2332
base_rev_id = graph.find_unique_lca(revision, old_tip)
2333
base_tree = self.branch.repository.revision_tree(base_rev_id)
2334
other_tree = self.branch.repository.revision_tree(old_tip)
2335
result += merge.merge_inner(
2340
change_reporter=change_reporter)
2343
2404
def _write_hashcache_if_dirty(self):
2344
2405
"""Write out the hashcache if it is dirty."""