25
25
from inventory import Inventory
26
26
from trace import mutter, note
27
from tree import Tree, EmptyTree, RevisionTree
27
from tree import Tree, EmptyTree, RevisionTree, WorkingTree
28
28
from inventory import InventoryEntry, Inventory
29
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, \
29
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, chomp, \
30
30
format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
31
joinpath, sha_string, file_kind, local_time_offset, appendpath
31
joinpath, sha_string, file_kind, local_time_offset
32
32
from store import ImmutableStore
33
33
from revision import Revision
34
from errors import bailout, BzrError
34
from errors import bailout
35
35
from textui import show_status
36
36
from diff import diff_trees
87
75
"""Branch holding a history of revisions.
90
Base directory of the branch.
77
:todo: Perhaps use different stores for different classes of object,
78
so that we can keep track of how much space each one uses,
79
or garbage-collect them.
81
:todo: Add a RemoteBranch subclass. For the basic case of read-only
82
HTTP access this should be very easy by,
83
just redirecting controlfile access into HTTP requests.
84
We would need a RemoteStore working similarly.
86
:todo: Keep the on-disk branch locked while the object exists.
88
:todo: mkdir() method.
94
def __init__(self, base, init=False, find_root=True, lock_mode='w'):
90
def __init__(self, base, init=False, find_root=True):
95
91
"""Create new branch object at a particular location.
97
base -- Base directory for the branch.
93
:param base: Base directory for the branch.
99
init -- If True, create new control files in a previously
95
:param init: If True, create new control files in a previously
100
96
unversioned directory. If False, the branch must already
103
find_root -- If true and init is false, find the root of the
99
:param find_root: If true and init is false, find the root of the
104
100
existing branch containing base.
106
102
In the test suite, creation of new trees is tested using the
132
127
__repr__ = __str__
136
def lock(self, mode='w'):
137
"""Lock the on-disk branch, excluding other processes."""
143
om = os.O_WRONLY | os.O_CREAT
148
raise BzrError("invalid locking mode %r" % mode)
151
lockfile = os.open(self.controlfilename('branch-lock'), om)
153
if e.errno == errno.ENOENT:
154
# might not exist on branches from <0.0.4
155
self.controlfile('branch-lock', 'w').close()
156
lockfile = os.open(self.controlfilename('branch-lock'), om)
160
fcntl.lockf(lockfile, lm)
162
fcntl.lockf(lockfile, fcntl.LOCK_UN)
164
self._lockmode = None
166
self._lockmode = mode
168
warning("please write a locking method for platform %r" % sys.platform)
170
self._lockmode = None
172
self._lockmode = mode
175
def _need_readlock(self):
176
if self._lockmode not in ['r', 'w']:
177
raise BzrError('need read lock on branch, only have %r' % self._lockmode)
179
def _need_writelock(self):
180
if self._lockmode not in ['w']:
181
raise BzrError('need write lock on branch, only have %r' % self._lockmode)
184
130
def abspath(self, name):
185
131
"""Return absolute filename for something in the branch"""
186
132
return os.path.join(self.base, name)
815
def rename_one(self, from_rel, to_rel):
818
This can change the directory or the filename or both.
820
self._need_writelock()
821
tree = self.working_tree()
823
if not tree.has_filename(from_rel):
824
bailout("can't rename: old working file %r does not exist" % from_rel)
825
if tree.has_filename(to_rel):
826
bailout("can't rename: new working file %r already exists" % to_rel)
828
file_id = inv.path2id(from_rel)
830
bailout("can't rename: old name %r is not versioned" % from_rel)
832
if inv.path2id(to_rel):
833
bailout("can't rename: new name %r is already versioned" % to_rel)
835
to_dir, to_tail = os.path.split(to_rel)
836
to_dir_id = inv.path2id(to_dir)
837
if to_dir_id == None and to_dir != '':
838
bailout("can't determine destination directory id for %r" % to_dir)
840
mutter("rename_one:")
841
mutter(" file_id {%s}" % file_id)
842
mutter(" from_rel %r" % from_rel)
843
mutter(" to_rel %r" % to_rel)
844
mutter(" to_dir %r" % to_dir)
845
mutter(" to_dir_id {%s}" % to_dir_id)
847
inv.rename(file_id, to_dir_id, to_tail)
849
print "%s => %s" % (from_rel, to_rel)
664
def write_log(self, show_timezone='original'):
665
"""Write out human-readable log of commits to this branch
667
:param utc: If true, show dates in universal time, not local time."""
668
## TODO: Option to choose either original, utc or local timezone
671
for p in self.revision_history():
673
print 'revno:', revno
674
## TODO: Show hash if --id is given.
675
##print 'revision-hash:', p
676
rev = self.get_revision(p)
677
print 'committer:', rev.committer
678
print 'timestamp: %s' % (format_date(rev.timestamp, rev.timezone or 0,
681
## opportunistic consistency check, same as check_patch_chaining
682
if rev.precursor != precursor:
683
bailout("mismatched precursor!")
687
print ' (no message)'
689
for l in rev.message.split('\n'):
697
def show_status(branch, show_all=False):
698
"""Display single-line status for non-ignored working files.
700
The list is show sorted in order by file name.
702
>>> b = ScratchBranch(files=['foo', 'foo~'])
708
>>> b.commit("add foo")
710
>>> os.unlink(b.abspath('foo'))
851
from_abs = self.abspath(from_rel)
852
to_abs = self.abspath(to_rel)
854
os.rename(from_abs, to_abs)
856
bailout("failed to rename %r to %r: %s"
857
% (from_abs, to_abs, e[1]),
858
["rename rolled back"])
860
self._write_inventory(inv)
864
def move(self, from_paths, to_name):
867
to_name must exist as a versioned directory.
869
If to_name exists and is a directory, the files are moved into
870
it, keeping their old names. If it is a directory,
872
Note that to_name is only the last component of the new name;
873
this doesn't change the directory.
715
:todo: Get state for single files.
717
:todo: Perhaps show a slash at the end of directory names.
875
self._need_writelock()
876
## TODO: Option to move IDs only
877
assert not isinstance(from_paths, basestring)
878
tree = self.working_tree()
880
to_abs = self.abspath(to_name)
881
if not isdir(to_abs):
882
bailout("destination %r is not a directory" % to_abs)
883
if not tree.has_filename(to_name):
884
bailout("destination %r not in working directory" % to_abs)
885
to_dir_id = inv.path2id(to_name)
886
if to_dir_id == None and to_name != '':
887
bailout("destination %r is not a versioned directory" % to_name)
888
to_dir_ie = inv[to_dir_id]
889
if to_dir_ie.kind not in ('directory', 'root_directory'):
890
bailout("destination %r is not a directory" % to_abs)
892
to_idpath = Set(inv.get_idpath(to_dir_id))
895
if not tree.has_filename(f):
896
bailout("%r does not exist in working tree" % f)
897
f_id = inv.path2id(f)
899
bailout("%r is not versioned" % f)
900
name_tail = splitpath(f)[-1]
901
dest_path = appendpath(to_name, name_tail)
902
if tree.has_filename(dest_path):
903
bailout("destination %r already exists" % dest_path)
904
if f_id in to_idpath:
905
bailout("can't move %r to a subdirectory of itself" % f)
907
# OK, so there's a race here, it's possible that someone will
908
# create a file in this interval and then the rename might be
909
# left half-done. But we should have caught most problems.
912
name_tail = splitpath(f)[-1]
913
dest_path = appendpath(to_name, name_tail)
914
print "%s => %s" % (f, dest_path)
915
inv.rename(inv.path2id(f), to_dir_id, name_tail)
917
os.rename(self.abspath(f), self.abspath(dest_path))
919
bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
920
["rename rolled back"])
922
self._write_inventory(inv)
721
# We have to build everything into a list first so that it can
722
# sorted by name, incorporating all the different sources.
724
# FIXME: Rather than getting things in random order and then sorting,
725
# just step through in order.
727
# Interesting case: the old ID for a file has been removed,
728
# but a new file has been created under that name.
730
old = branch.basis_tree()
731
old_inv = old.inventory
732
new = branch.working_tree()
733
new_inv = new.inventory
735
for fs, fid, oldname, newname, kind in diff_trees(old, new):
737
show_status(fs, kind,
738
oldname + ' => ' + newname)
739
elif fs == 'A' or fs == 'M':
740
show_status(fs, kind, newname)
742
show_status(fs, kind, oldname)
745
show_status(fs, kind, newname)
748
show_status(fs, kind, newname)
750
show_status(fs, kind, newname)
752
bailout("wierd file state %r" % ((fs, fid),))
927
756
class ScratchBranch(Branch):