15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
import sys, os, os.path, random, time, sha, sets, types, re, shutil, tempfile
19
import traceback, socket, fnmatch, difflib, time
20
from binascii import hexlify
23
from inventory import Inventory
24
from trace import mutter, note
25
from tree import Tree, EmptyTree, RevisionTree
26
from inventory import InventoryEntry, Inventory
27
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, \
28
format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
29
joinpath, sha_file, sha_string, file_kind, local_time_offset, appendpath
30
from store import ImmutableStore
31
from revision import Revision
32
from errors import BzrError
33
from textui import show_status
22
from bzrlib.trace import mutter, note
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
25
sha_file, appendpath, file_kind
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
29
from bzrlib.textui import show_status
30
from bzrlib.revision import Revision
31
from bzrlib.xml import unpack_xml
32
from bzrlib.delta import compare_trees
33
from bzrlib.tree import EmptyTree, RevisionTree
35
38
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
36
39
## TODO: Maybe include checks for common corruption of newlines, etc?
42
# TODO: Some operations like log might retrieve the same revisions
43
# repeatedly to calculate deltas. We could perhaps have a weakref
44
# cache in memory to make this faster.
46
# TODO: please move the revision-string syntax stuff out of the branch
47
# object; it's clutter
40
50
def find_branch(f, **args):
41
51
if f and (f.startswith('http://') or f.startswith('https://')):
315
351
['use a different bzr version',
316
352
'or remove the .bzr directory and "bzr init" again'])
354
def get_root_id(self):
355
"""Return the id of this branches root"""
356
inv = self.read_working_inventory()
357
return inv.root.file_id
359
def set_root_id(self, file_id):
360
inv = self.read_working_inventory()
361
orig_root_id = inv.root.file_id
362
del inv._byid[inv.root.file_id]
363
inv.root.file_id = file_id
364
inv._byid[inv.root.file_id] = inv.root
367
if entry.parent_id in (None, orig_root_id):
368
entry.parent_id = inv.root.file_id
369
self._write_inventory(inv)
320
371
def read_working_inventory(self):
321
372
"""Read the working inventory."""
323
# ElementTree does its own conversion from UTF-8, so open in
373
from bzrlib.inventory import Inventory
374
from bzrlib.xml import unpack_xml
375
from time import time
327
inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
379
# ElementTree does its own conversion from UTF-8, so open in
381
inv = unpack_xml(Inventory,
382
self.controlfile('inventory', 'rb'))
328
383
mutter("loaded inventory of %d items in %f"
329
% (len(inv), time.time() - before))
384
% (len(inv), time() - before))
520
580
return self.working_tree().unknowns()
523
def append_revision(self, revision_id):
524
mutter("add {%s} to revision-history" % revision_id)
583
def append_revision(self, *revision_ids):
584
from bzrlib.atomicfile import AtomicFile
586
for revision_id in revision_ids:
587
mutter("add {%s} to revision-history" % revision_id)
525
589
rev_history = self.revision_history()
527
tmprhname = self.controlfilename('revision-history.tmp')
528
rhname = self.controlfilename('revision-history')
530
f = file(tmprhname, 'wt')
531
rev_history.append(revision_id)
532
f.write('\n'.join(rev_history))
536
if sys.platform == 'win32':
538
os.rename(tmprhname, rhname)
590
rev_history.extend(revision_ids)
592
f = AtomicFile(self.controlfilename('revision-history'))
594
for rev_id in rev_history:
601
def get_revision_xml(self, revision_id):
602
"""Return XML file object for revision object."""
603
if not revision_id or not isinstance(revision_id, basestring):
604
raise InvalidRevisionId(revision_id)
609
return self.revision_store[revision_id]
611
raise bzrlib.errors.NoSuchRevision(self, revision_id)
542
616
def get_revision(self, revision_id):
543
617
"""Return the Revision object for a named revision"""
544
if not revision_id or not isinstance(revision_id, basestring):
545
raise ValueError('invalid revision-id: %r' % revision_id)
546
r = Revision.read_xml(self.revision_store[revision_id])
618
xml_file = self.get_revision_xml(revision_id)
621
r = unpack_xml(Revision, xml_file)
622
except SyntaxError, e:
623
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
547
627
assert r.revision_id == revision_id
631
def get_revision_delta(self, revno):
632
"""Return the delta for one revision.
634
The delta is relative to its mainline predecessor, or the
635
empty tree for revision 1.
637
assert isinstance(revno, int)
638
rh = self.revision_history()
639
if not (1 <= revno <= len(rh)):
640
raise InvalidRevisionNumber(revno)
642
# revno is 1-based; list is 0-based
644
new_tree = self.revision_tree(rh[revno-1])
646
old_tree = EmptyTree()
648
old_tree = self.revision_tree(rh[revno-2])
650
return compare_trees(old_tree, new_tree)
550
654
def get_revision_sha1(self, revision_id):
551
655
"""Hash the stored value of a revision, and return it."""
552
656
# In the future, revision entries will be signed. At that
719
813
if stop_revision is None:
720
814
stop_revision = other_len
721
815
elif stop_revision > other_len:
722
raise NoSuchRevision(self, stop_revision)
816
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
724
818
return other_history[self_len:stop_revision]
727
821
def update_revisions(self, other, stop_revision=None):
728
822
"""Pull in all new revisions from other branch.
730
>>> from bzrlib.commit import commit
731
>>> bzrlib.trace.silent = True
732
>>> br1 = ScratchBranch(files=['foo', 'bar'])
735
>>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
736
>>> br2 = ScratchBranch()
737
>>> br2.update_revisions(br1)
741
>>> br2.revision_history()
743
>>> br2.update_revisions(br1)
747
>>> br1.text_store.total_size() == br2.text_store.total_size()
750
from bzrlib.progress import ProgressBar
824
from bzrlib.fetch import greedy_fetch
826
pb = bzrlib.ui.ui_factory.progress_bar()
754
827
pb.update('comparing histories')
755
829
revision_ids = self.missing_revisions(other, stop_revision)
831
if len(revision_ids) > 0:
832
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
835
self.append_revision(*revision_ids)
836
## note("Added %d revisions." % count)
841
def install_revisions(self, other, revision_ids, pb):
842
if hasattr(other.revision_store, "prefetch"):
843
other.revision_store.prefetch(revision_ids)
844
if hasattr(other.inventory_store, "prefetch"):
845
inventory_ids = [other.get_revision(r).inventory_id
846
for r in revision_ids]
847
other.inventory_store.prefetch(inventory_ids)
850
pb = bzrlib.ui.ui_factory.progress_bar()
757
needed_texts = sets.Set()
759
for rev_id in revision_ids:
761
pb.update('fetching revision', i, len(revision_ids))
762
rev = other.get_revision(rev_id)
857
for i, rev_id in enumerate(revision_ids):
858
pb.update('fetching revision', i+1, len(revision_ids))
860
rev = other.get_revision(rev_id)
861
except bzrlib.errors.NoSuchRevision:
763
865
revisions.append(rev)
764
866
inv = other.get_inventory(str(rev.inventory_id))
765
867
for key, entry in inv.iter_entries():
773
count = self.text_store.copy_multi(other.text_store, needed_texts)
774
print "Added %d texts." % count
875
count, cp_fail = self.text_store.copy_multi(other.text_store,
877
#print "Added %d texts." % count
775
878
inventory_ids = [ f.inventory_id for f in revisions ]
776
count = self.inventory_store.copy_multi(other.inventory_store,
778
print "Added %d inventories." % count
879
count, cp_fail = self.inventory_store.copy_multi(other.inventory_store,
881
#print "Added %d inventories." % count
779
882
revision_ids = [ f.revision_id for f in revisions]
780
count = self.revision_store.copy_multi(other.revision_store,
782
for revision_id in revision_ids:
783
self.append_revision(revision_id)
784
print "Added %d revisions." % count
884
count, cp_fail = self.revision_store.copy_multi(other.revision_store,
887
assert len(cp_fail) == 0
888
return count, failures
787
891
def commit(self, *args, **kw):
789
892
from bzrlib.commit import commit
790
893
commit(self, *args, **kw)
793
def lookup_revision(self, revno):
794
"""Return revision hash for revision number."""
799
# list is 0-based; revisions are 1-based
800
return self.revision_history()[revno-1]
802
raise BzrError("no such revision %s" % revno)
896
def lookup_revision(self, revision):
897
"""Return the revision identifier for a given revision information."""
898
revno, info = self.get_revision_info(revision)
902
def revision_id_to_revno(self, revision_id):
903
"""Given a revision id, return its revno"""
904
history = self.revision_history()
906
return history.index(revision_id) + 1
908
raise bzrlib.errors.NoSuchRevision(self, revision_id)
911
def get_revision_info(self, revision):
912
"""Return (revno, revision id) for revision identifier.
914
revision can be an integer, in which case it is assumed to be revno (though
915
this will translate negative values into positive ones)
916
revision can also be a string, in which case it is parsed for something like
917
'date:' or 'revid:' etc.
922
try:# Convert to int if possible
923
revision = int(revision)
926
revs = self.revision_history()
927
if isinstance(revision, int):
930
# Mabye we should do this first, but we don't need it if revision == 0
932
revno = len(revs) + revision + 1
935
elif isinstance(revision, basestring):
936
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
937
if revision.startswith(prefix):
938
revno = func(self, revs, revision)
941
raise BzrError('No namespace registered for string: %r' % revision)
943
if revno is None or revno <= 0 or revno > len(revs):
944
raise BzrError("no such revision %s" % revision)
945
return revno, revs[revno-1]
947
def _namespace_revno(self, revs, revision):
948
"""Lookup a revision by revision number"""
949
assert revision.startswith('revno:')
951
return int(revision[6:])
954
REVISION_NAMESPACES['revno:'] = _namespace_revno
956
def _namespace_revid(self, revs, revision):
957
assert revision.startswith('revid:')
959
return revs.index(revision[6:]) + 1
962
REVISION_NAMESPACES['revid:'] = _namespace_revid
964
def _namespace_last(self, revs, revision):
965
assert revision.startswith('last:')
967
offset = int(revision[5:])
972
raise BzrError('You must supply a positive value for --revision last:XXX')
973
return len(revs) - offset + 1
974
REVISION_NAMESPACES['last:'] = _namespace_last
976
def _namespace_tag(self, revs, revision):
977
assert revision.startswith('tag:')
978
raise BzrError('tag: namespace registered, but not implemented.')
979
REVISION_NAMESPACES['tag:'] = _namespace_tag
981
def _namespace_date(self, revs, revision):
982
assert revision.startswith('date:')
984
# Spec for date revisions:
986
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
987
# it can also start with a '+/-/='. '+' says match the first
988
# entry after the given date. '-' is match the first entry before the date
989
# '=' is match the first entry after, but still on the given date.
991
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
992
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
993
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
994
# May 13th, 2005 at 0:00
996
# So the proper way of saying 'give me all entries for today' is:
997
# -r {date:+today}:{date:-tomorrow}
998
# The default is '=' when not supplied
1001
if val[:1] in ('+', '-', '='):
1002
match_style = val[:1]
1005
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1006
if val.lower() == 'yesterday':
1007
dt = today - datetime.timedelta(days=1)
1008
elif val.lower() == 'today':
1010
elif val.lower() == 'tomorrow':
1011
dt = today + datetime.timedelta(days=1)
1014
# This should be done outside the function to avoid recompiling it.
1015
_date_re = re.compile(
1016
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1018
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1020
m = _date_re.match(val)
1021
if not m or (not m.group('date') and not m.group('time')):
1022
raise BzrError('Invalid revision date %r' % revision)
1025
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1027
year, month, day = today.year, today.month, today.day
1029
hour = int(m.group('hour'))
1030
minute = int(m.group('minute'))
1031
if m.group('second'):
1032
second = int(m.group('second'))
1036
hour, minute, second = 0,0,0
1038
dt = datetime.datetime(year=year, month=month, day=day,
1039
hour=hour, minute=minute, second=second)
1043
if match_style == '-':
1045
elif match_style == '=':
1046
last = dt + datetime.timedelta(days=1)
1049
for i in range(len(revs)-1, -1, -1):
1050
r = self.get_revision(revs[i])
1051
# TODO: Handle timezone.
1052
dt = datetime.datetime.fromtimestamp(r.timestamp)
1053
if first >= dt and (last is None or dt >= last):
1056
for i in range(len(revs)):
1057
r = self.get_revision(revs[i])
1058
# TODO: Handle timezone.
1059
dt = datetime.datetime.fromtimestamp(r.timestamp)
1060
if first <= dt and (last is None or dt <= last):
1062
REVISION_NAMESPACES['date:'] = _namespace_date
805
1064
def revision_tree(self, revision_id):
806
1065
"""Return Tree for a revision on this branch.
1212
def revert(self, filenames, old_tree=None, backups=True):
1213
"""Restore selected files to the versions from a previous tree.
1216
If true (default) backups are made of files before
1219
from bzrlib.errors import NotVersionedError, BzrError
1220
from bzrlib.atomicfile import AtomicFile
1221
from bzrlib.osutils import backup_file
1223
inv = self.read_working_inventory()
1224
if old_tree is None:
1225
old_tree = self.basis_tree()
1226
old_inv = old_tree.inventory
1229
for fn in filenames:
1230
file_id = inv.path2id(fn)
1232
raise NotVersionedError("not a versioned file", fn)
1233
if not old_inv.has_id(file_id):
1234
raise BzrError("file not present in old tree", fn, file_id)
1235
nids.append((fn, file_id))
1237
# TODO: Rename back if it was previously at a different location
1239
# TODO: If given a directory, restore the entire contents from
1240
# the previous version.
1242
# TODO: Make a backup to a temporary file.
1244
# TODO: If the file previously didn't exist, delete it?
1245
for fn, file_id in nids:
1248
f = AtomicFile(fn, 'wb')
1250
f.write(old_tree.get_file(file_id).read())
1256
def pending_merges(self):
1257
"""Return a list of pending merges.
1259
These are revisions that have been merged into the working
1260
directory but not yet committed.
1262
cfn = self.controlfilename('pending-merges')
1263
if not os.path.exists(cfn):
1266
for l in self.controlfile('pending-merges', 'r').readlines():
1267
p.append(l.rstrip('\n'))
1271
def add_pending_merge(self, revision_id):
1272
from bzrlib.revision import validate_revision_id
1274
validate_revision_id(revision_id)
1276
p = self.pending_merges()
1277
if revision_id in p:
1279
p.append(revision_id)
1280
self.set_pending_merges(p)
1283
def set_pending_merges(self, rev_list):
1284
from bzrlib.atomicfile import AtomicFile
1287
f = AtomicFile(self.controlfilename('pending-merges'))
954
1299
class ScratchBranch(Branch):
955
1300
"""Special test class: a branch that cleans up after itself.