26
26
from trace import mutter, note
27
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
74
75
"""Branch holding a history of revisions.
77
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.
81
def __init__(self, base, init=False, find_root=True, lock_mode='w'):
90
def __init__(self, base, init=False, find_root=True):
82
91
"""Create new branch object at a particular location.
84
base -- Base directory for the branch.
93
:param base: Base directory for the branch.
86
init -- If True, create new control files in a previously
95
:param init: If True, create new control files in a previously
87
96
unversioned directory. If False, the branch must already
90
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
91
100
existing branch containing base.
93
102
In the test suite, creation of new trees is tested using the
119
127
__repr__ = __str__
123
def lock(self, mode='w'):
124
"""Lock the on-disk branch, excluding other processes."""
130
om = os.O_WRONLY | os.O_CREAT
135
raise BzrError("invalid locking mode %r" % mode)
138
lockfile = os.open(self.controlfilename('branch-lock'), om)
140
if e.errno == errno.ENOENT:
141
# might not exist on branches from <0.0.4
142
self.controlfile('branch-lock', 'w').close()
143
lockfile = os.open(self.controlfilename('branch-lock'), om)
147
fcntl.lockf(lockfile, lm)
149
fcntl.lockf(lockfile, fcntl.LOCK_UN)
151
self._lockmode = None
153
self._lockmode = mode
155
warning("please write a locking method for platform %r" % sys.platform)
157
self._lockmode = None
159
self._lockmode = mode
162
def _need_readlock(self):
163
if self._lockmode not in ['r', 'w']:
164
raise BzrError('need read lock on branch, only have %r' % self._lockmode)
166
def _need_writelock(self):
167
if self._lockmode not in ['w']:
168
raise BzrError('need write lock on branch, only have %r' % self._lockmode)
171
130
def abspath(self, name):
172
131
"""Return absolute filename for something in the branch"""
173
132
return os.path.join(self.base, name)
196
155
def controlfile(self, file_or_path, mode='r'):
197
"""Open a control file for this branch.
199
There are two classes of file in the control directory: text
200
and binary. binary files are untranslated byte streams. Text
201
control files are stored with Unix newlines and in UTF-8, even
202
if the platform or locale defaults are different.
205
fn = self.controlfilename(file_or_path)
207
if mode == 'rb' or mode == 'wb':
208
return file(fn, mode)
209
elif mode == 'r' or mode == 'w':
210
# open in binary mode anyhow so there's no newline translation;
211
# codecs uses line buffering by default; don't want that.
213
return codecs.open(fn, mode + 'b', 'utf-8',
216
raise BzrError("invalid controlfile mode %r" % mode)
156
"""Open a control file for this branch"""
157
return file(self.controlfilename(file_or_path), mode)
220
160
def _make_control(self):
627
541
## TODO: Also calculate and store the inventory SHA1
628
542
mutter("committing patch r%d" % (self.revno() + 1))
631
self.append_revision(rev_id)
634
note("commited r%d" % self.revno())
637
def append_revision(self, revision_id):
638
mutter("add {%s} to revision-history" % revision_id)
639
rev_history = self.revision_history()
641
tmprhname = self.controlfilename('revision-history.tmp')
642
rhname = self.controlfilename('revision-history')
644
f = file(tmprhname, 'wt')
645
rev_history.append(revision_id)
646
f.write('\n'.join(rev_history))
650
if sys.platform == 'win32':
652
os.rename(tmprhname, rhname)
544
mutter("append to revision-history")
545
self.controlfile('revision-history', 'at').write(rev_id + '\n')
656
550
def get_revision(self, revision_id):
657
551
"""Return the Revision object for a named revision"""
658
self._need_readlock()
659
552
r = Revision.read_xml(self.revision_store[revision_id])
660
553
assert r.revision_id == revision_id
798
def rename_one(self, from_rel, to_rel):
801
This can change the directory or the filename or both.
803
self._need_writelock()
804
tree = self.working_tree()
806
if not tree.has_filename(from_rel):
807
bailout("can't rename: old working file %r does not exist" % from_rel)
808
if tree.has_filename(to_rel):
809
bailout("can't rename: new working file %r already exists" % to_rel)
811
file_id = inv.path2id(from_rel)
813
bailout("can't rename: old name %r is not versioned" % from_rel)
815
if inv.path2id(to_rel):
816
bailout("can't rename: new name %r is already versioned" % to_rel)
818
to_dir, to_tail = os.path.split(to_rel)
819
to_dir_id = inv.path2id(to_dir)
820
if to_dir_id == None and to_dir != '':
821
bailout("can't determine destination directory id for %r" % to_dir)
823
mutter("rename_one:")
824
mutter(" file_id {%s}" % file_id)
825
mutter(" from_rel %r" % from_rel)
826
mutter(" to_rel %r" % to_rel)
827
mutter(" to_dir %r" % to_dir)
828
mutter(" to_dir_id {%s}" % to_dir_id)
830
inv.rename(file_id, to_dir_id, to_tail)
832
print "%s => %s" % (from_rel, to_rel)
834
from_abs = self.abspath(from_rel)
835
to_abs = self.abspath(to_rel)
837
os.rename(from_abs, to_abs)
839
bailout("failed to rename %r to %r: %s"
840
% (from_abs, to_abs, e[1]),
841
["rename rolled back"])
843
self._write_inventory(inv)
847
def move(self, from_paths, to_name):
850
to_name must exist as a versioned directory.
852
If to_name exists and is a directory, the files are moved into
853
it, keeping their old names. If it is a directory,
855
Note that to_name is only the last component of the new name;
856
this doesn't change the directory.
858
self._need_writelock()
859
## TODO: Option to move IDs only
860
assert not isinstance(from_paths, basestring)
861
tree = self.working_tree()
863
to_abs = self.abspath(to_name)
864
if not isdir(to_abs):
865
bailout("destination %r is not a directory" % to_abs)
866
if not tree.has_filename(to_name):
867
bailout("destination %r not in working directory" % to_abs)
868
to_dir_id = inv.path2id(to_name)
869
if to_dir_id == None and to_name != '':
870
bailout("destination %r is not a versioned directory" % to_name)
871
to_dir_ie = inv[to_dir_id]
872
if to_dir_ie.kind not in ('directory', 'root_directory'):
873
bailout("destination %r is not a directory" % to_abs)
875
to_idpath = Set(inv.get_idpath(to_dir_id))
878
if not tree.has_filename(f):
879
bailout("%r does not exist in working tree" % f)
880
f_id = inv.path2id(f)
882
bailout("%r is not versioned" % f)
883
name_tail = splitpath(f)[-1]
884
dest_path = appendpath(to_name, name_tail)
885
if tree.has_filename(dest_path):
886
bailout("destination %r already exists" % dest_path)
887
if f_id in to_idpath:
888
bailout("can't move %r to a subdirectory of itself" % f)
890
# OK, so there's a race here, it's possible that someone will
891
# create a file in this interval and then the rename might be
892
# left half-done. But we should have caught most problems.
895
name_tail = splitpath(f)[-1]
896
dest_path = appendpath(to_name, name_tail)
897
print "%s => %s" % (f, dest_path)
898
inv.rename(inv.path2id(f), to_dir_id, name_tail)
900
os.rename(self.abspath(f), self.abspath(dest_path))
902
bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
903
["rename rolled back"])
905
self._write_inventory(inv)
909
def show_status(self, show_all=False):
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):
910
698
"""Display single-line status for non-ignored working files.
912
700
The list is show sorted in order by file name.