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
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.delta import compare_trees
32
from bzrlib.tree import EmptyTree, RevisionTree
33
from bzrlib.inventory import Inventory
34
from bzrlib.weavestore import WeaveStore
40
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
41
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
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
35
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
42
36
## TODO: Maybe include checks for common corruption of newlines, etc?
45
# TODO: Some operations like log might retrieve the same revisions
46
# repeatedly to calculate deltas. We could perhaps have a weakref
47
# cache in memory to make this faster.
49
# TODO: please move the revision-string syntax stuff out of the branch
50
# object; it's clutter
53
40
def find_branch(f, **args):
54
41
if f and (f.startswith('http://') or f.startswith('https://')):
304
280
raise BzrError("invalid controlfile mode %r" % mode)
306
284
def _make_control(self):
307
285
os.mkdir(self.controlfilename([]))
308
286
self.controlfile('README', 'w').write(
309
287
"This is a Bazaar-NG control directory.\n"
310
288
"Do not change any files in this directory.\n")
311
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT_5)
312
for d in ('text-store', 'inventory-store', 'revision-store',
289
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT)
290
for d in ('text-store', 'inventory-store', 'revision-store'):
314
291
os.mkdir(self.controlfilename(d))
315
292
for f in ('revision-history', 'merged-patches',
316
293
'pending-merged-patches', 'branch-name',
319
295
self.controlfile(f, 'w').write('')
320
296
mutter('created control directory in ' + self.base)
322
# if we want per-tree root ids then this is the place to set
323
# them; they're not needed for now and so ommitted for
325
f = self.controlfile('inventory','w')
326
bzrlib.xml5.serializer_v5.write_inventory(Inventory(), f)
297
Inventory().write_xml(self.controlfile('inventory','w'))
329
300
def _check_format(self):
330
301
"""Check this branch format is supported.
332
The format level is stored, as an integer, in
333
self._branch_format for code that needs to check it later.
303
The current tool only supports the current unstable format.
335
305
In the future, we might need different in-memory Branch
336
306
classes to support downlevel branches. But not yet.
308
# This ignores newlines so that we can open branches created
309
# on Windows from Linux and so on. I think it might be better
310
# to always make all internal files in unix format.
338
311
fmt = self.controlfile('branch-format', 'r').read()
339
if fmt == BZR_BRANCH_FORMAT_5:
340
self._branch_format = 5
342
raise BzrError('sorry, branch format "%s" not supported; '
343
'use a different bzr version, '
344
'or run "bzr upgrade", '
345
'or remove the .bzr directory and "bzr init" again'
346
% fmt.rstrip('\n\r'))
348
def get_root_id(self):
349
"""Return the id of this branches root"""
350
inv = self.read_working_inventory()
351
return inv.root.file_id
353
def set_root_id(self, file_id):
354
inv = self.read_working_inventory()
355
orig_root_id = inv.root.file_id
356
del inv._byid[inv.root.file_id]
357
inv.root.file_id = file_id
358
inv._byid[inv.root.file_id] = inv.root
361
if entry.parent_id in (None, orig_root_id):
362
entry.parent_id = inv.root.file_id
363
self._write_inventory(inv)
312
fmt.replace('\r\n', '')
313
if fmt != BZR_BRANCH_FORMAT:
314
raise BzrError('sorry, branch format %r not supported' % fmt,
315
['use a different bzr version',
316
'or remove the .bzr directory and "bzr init" again'])
365
320
def read_working_inventory(self):
366
321
"""Read the working inventory."""
323
# ElementTree does its own conversion from UTF-8, so open in
369
# ElementTree does its own conversion from UTF-8, so open in
371
f = self.controlfile('inventory', 'rb')
372
return bzrlib.xml5.serializer_v5.read_inventory(f)
327
inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
328
mutter("loaded inventory of %d items in %f"
329
% (len(inv), time.time() - before))
559
520
return self.working_tree().unknowns()
562
def append_revision(self, *revision_ids):
563
from bzrlib.atomicfile import AtomicFile
565
for revision_id in revision_ids:
566
mutter("add {%s} to revision-history" % revision_id)
523
def append_revision(self, revision_id):
524
mutter("add {%s} to revision-history" % revision_id)
568
525
rev_history = self.revision_history()
569
rev_history.extend(revision_ids)
571
f = AtomicFile(self.controlfilename('revision-history'))
573
for rev_id in rev_history:
580
def get_revision_xml_file(self, revision_id):
581
"""Return XML file object for revision object."""
582
if not revision_id or not isinstance(revision_id, basestring):
583
raise InvalidRevisionId(revision_id)
588
return self.revision_store[revision_id]
590
raise bzrlib.errors.NoSuchRevision(self, revision_id)
596
get_revision_xml = get_revision_xml_file
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)
599
542
def get_revision(self, revision_id):
600
543
"""Return the Revision object for a named revision"""
601
xml_file = self.get_revision_xml_file(revision_id)
604
r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
605
except SyntaxError, e:
606
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
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])
610
547
assert r.revision_id == revision_id
614
def get_revision_delta(self, revno):
615
"""Return the delta for one revision.
617
The delta is relative to its mainline predecessor, or the
618
empty tree for revision 1.
620
assert isinstance(revno, int)
621
rh = self.revision_history()
622
if not (1 <= revno <= len(rh)):
623
raise InvalidRevisionNumber(revno)
625
# revno is 1-based; list is 0-based
627
new_tree = self.revision_tree(rh[revno-1])
629
old_tree = EmptyTree()
631
old_tree = self.revision_tree(rh[revno-2])
633
return compare_trees(old_tree, new_tree)
637
550
def get_revision_sha1(self, revision_id):
638
551
"""Hash the stored value of a revision, and return it."""
639
552
# In the future, revision entries will be signed. At that
642
555
# the revision, (add signatures/remove signatures) and still
643
556
# have all hash pointers stay consistent.
644
557
# But for now, just hash the contents.
645
return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
648
def get_inventory(self, revision_id):
558
return sha_file(self.revision_store[revision_id])
561
def get_inventory(self, inventory_id):
649
562
"""Get Inventory object by hash.
651
564
TODO: Perhaps for this and similar methods, take a revision
652
565
parameter which can be either an integer revno or a
654
f = self.get_inventory_xml_file(revision_id)
655
return bzrlib.xml5.serializer_v5.read_inventory(f)
658
def get_inventory_xml(self, revision_id):
659
"""Get inventory XML as a file object."""
661
assert isinstance(revision_id, basestring), type(revision_id)
662
return self.inventory_store[revision_id]
664
raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
666
get_inventory_xml_file = get_inventory_xml
669
def get_inventory_sha1(self, revision_id):
567
i = Inventory.read_xml(self.inventory_store[inventory_id])
570
def get_inventory_sha1(self, inventory_id):
670
571
"""Return the sha1 hash of the inventory entry
672
return sha_file(self.get_inventory_xml_file(revision_id))
573
return sha_file(self.inventory_store[inventory_id])
675
576
def get_revision_inventory(self, revision_id):
676
577
"""Return inventory of a past revision."""
677
# bzr 0.0.6 imposes the constraint that the inventory_id
678
# must be the same as its revision, so this is trivial.
679
578
if revision_id == None:
680
return Inventory(self.get_root_id())
682
return self.get_inventory(revision_id)
581
return self.get_inventory(self.get_revision(revision_id).inventory_id)
685
584
def revision_history(self):
799
719
if stop_revision is None:
800
720
stop_revision = other_len
801
721
elif stop_revision > other_len:
802
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
722
raise NoSuchRevision(self, stop_revision)
804
724
return other_history[self_len:stop_revision]
807
727
def update_revisions(self, other, stop_revision=None):
808
728
"""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()
810
from bzrlib.fetch import greedy_fetch
812
pb = bzrlib.ui.ui_factory.progress_bar()
750
from bzrlib.progress import ProgressBar
813
754
pb.update('comparing histories')
815
755
revision_ids = self.missing_revisions(other, stop_revision)
817
if len(revision_ids) > 0:
818
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
821
self.append_revision(*revision_ids)
822
## note("Added %d revisions." % count)
825
def install_revisions(self, other, revision_ids, pb):
826
if hasattr(other.revision_store, "prefetch"):
827
other.revision_store.prefetch(revision_ids)
828
if hasattr(other.inventory_store, "prefetch"):
829
inventory_ids = [other.get_revision(r).inventory_id
830
for r in revision_ids]
831
other.inventory_store.prefetch(inventory_ids)
834
pb = bzrlib.ui.ui_factory.progress_bar()
757
needed_texts = sets.Set()
841
for i, rev_id in enumerate(revision_ids):
842
pb.update('fetching revision', i+1, len(revision_ids))
844
rev = other.get_revision(rev_id)
845
except bzrlib.errors.NoSuchRevision:
759
for rev_id in revision_ids:
761
pb.update('fetching revision', i, len(revision_ids))
762
rev = other.get_revision(rev_id)
849
763
revisions.append(rev)
850
764
inv = other.get_inventory(str(rev.inventory_id))
851
765
for key, entry in inv.iter_entries():
859
count, cp_fail = self.text_store.copy_multi(other.text_store,
861
#print "Added %d texts." % count
773
count = self.text_store.copy_multi(other.text_store, needed_texts)
774
print "Added %d texts." % count
862
775
inventory_ids = [ f.inventory_id for f in revisions ]
863
count, cp_fail = self.inventory_store.copy_multi(other.inventory_store,
865
#print "Added %d inventories." % count
776
count = self.inventory_store.copy_multi(other.inventory_store,
778
print "Added %d inventories." % count
866
779
revision_ids = [ f.revision_id for f in revisions]
868
count, cp_fail = self.revision_store.copy_multi(other.revision_store,
871
assert len(cp_fail) == 0
872
return count, failures
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
875
787
def commit(self, *args, **kw):
876
from bzrlib.commit import Commit
877
Commit().commit(self, *args, **kw)
789
from bzrlib.commit import commit
790
commit(self, *args, **kw)
880
def lookup_revision(self, revision):
881
"""Return the revision identifier for a given revision information."""
882
revno, info = self._get_revision_info(revision)
886
def revision_id_to_revno(self, revision_id):
887
"""Given a revision id, return its revno"""
888
history = self.revision_history()
890
return history.index(revision_id) + 1
892
raise bzrlib.errors.NoSuchRevision(self, revision_id)
895
def get_revision_info(self, revision):
896
"""Return (revno, revision id) for revision identifier.
898
revision can be an integer, in which case it is assumed to be revno (though
899
this will translate negative values into positive ones)
900
revision can also be a string, in which case it is parsed for something like
901
'date:' or 'revid:' etc.
903
revno, rev_id = self._get_revision_info(revision)
905
raise bzrlib.errors.NoSuchRevision(self, revision)
908
def get_rev_id(self, revno, history=None):
909
"""Find the revision id of the specified revno."""
793
def lookup_revision(self, revno):
794
"""Return revision hash for revision number."""
913
history = self.revision_history()
914
elif revno <= 0 or revno > len(history):
915
raise bzrlib.errors.NoSuchRevision(self, revno)
916
return history[revno - 1]
918
def _get_revision_info(self, revision):
919
"""Return (revno, revision id) for revision specifier.
921
revision can be an integer, in which case it is assumed to be revno
922
(though this will translate negative values into positive ones)
923
revision can also be a string, in which case it is parsed for something
924
like 'date:' or 'revid:' etc.
926
A revid is always returned. If it is None, the specifier referred to
927
the null revision. If the revid does not occur in the revision
928
history, revno will be None.
934
try:# Convert to int if possible
935
revision = int(revision)
938
revs = self.revision_history()
939
if isinstance(revision, int):
941
revno = len(revs) + revision + 1
944
rev_id = self.get_rev_id(revno, revs)
945
elif isinstance(revision, basestring):
946
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
947
if revision.startswith(prefix):
948
result = func(self, revs, revision)
950
revno, rev_id = result
953
rev_id = self.get_rev_id(revno, revs)
956
raise BzrError('No namespace registered for string: %r' %
959
raise TypeError('Unhandled revision type %s' % revision)
963
raise bzrlib.errors.NoSuchRevision(self, revision)
966
def _namespace_revno(self, revs, revision):
967
"""Lookup a revision by revision number"""
968
assert revision.startswith('revno:')
970
return (int(revision[6:]),)
973
REVISION_NAMESPACES['revno:'] = _namespace_revno
975
def _namespace_revid(self, revs, revision):
976
assert revision.startswith('revid:')
977
rev_id = revision[len('revid:'):]
979
return revs.index(rev_id) + 1, rev_id
982
REVISION_NAMESPACES['revid:'] = _namespace_revid
984
def _namespace_last(self, revs, revision):
985
assert revision.startswith('last:')
987
offset = int(revision[5:])
992
raise BzrError('You must supply a positive value for --revision last:XXX')
993
return (len(revs) - offset + 1,)
994
REVISION_NAMESPACES['last:'] = _namespace_last
996
def _namespace_tag(self, revs, revision):
997
assert revision.startswith('tag:')
998
raise BzrError('tag: namespace registered, but not implemented.')
999
REVISION_NAMESPACES['tag:'] = _namespace_tag
1001
def _namespace_date(self, revs, revision):
1002
assert revision.startswith('date:')
1004
# Spec for date revisions:
1006
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
1007
# it can also start with a '+/-/='. '+' says match the first
1008
# entry after the given date. '-' is match the first entry before the date
1009
# '=' is match the first entry after, but still on the given date.
1011
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
1012
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
1013
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
1014
# May 13th, 2005 at 0:00
1016
# So the proper way of saying 'give me all entries for today' is:
1017
# -r {date:+today}:{date:-tomorrow}
1018
# The default is '=' when not supplied
1021
if val[:1] in ('+', '-', '='):
1022
match_style = val[:1]
1025
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1026
if val.lower() == 'yesterday':
1027
dt = today - datetime.timedelta(days=1)
1028
elif val.lower() == 'today':
1030
elif val.lower() == 'tomorrow':
1031
dt = today + datetime.timedelta(days=1)
1034
# This should be done outside the function to avoid recompiling it.
1035
_date_re = re.compile(
1036
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1038
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1040
m = _date_re.match(val)
1041
if not m or (not m.group('date') and not m.group('time')):
1042
raise BzrError('Invalid revision date %r' % revision)
1045
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1047
year, month, day = today.year, today.month, today.day
1049
hour = int(m.group('hour'))
1050
minute = int(m.group('minute'))
1051
if m.group('second'):
1052
second = int(m.group('second'))
1056
hour, minute, second = 0,0,0
1058
dt = datetime.datetime(year=year, month=month, day=day,
1059
hour=hour, minute=minute, second=second)
1063
if match_style == '-':
1065
elif match_style == '=':
1066
last = dt + datetime.timedelta(days=1)
1069
for i in range(len(revs)-1, -1, -1):
1070
r = self.get_revision(revs[i])
1071
# TODO: Handle timezone.
1072
dt = datetime.datetime.fromtimestamp(r.timestamp)
1073
if first >= dt and (last is None or dt >= last):
1076
for i in range(len(revs)):
1077
r = self.get_revision(revs[i])
1078
# TODO: Handle timezone.
1079
dt = datetime.datetime.fromtimestamp(r.timestamp)
1080
if first <= dt and (last is None or dt <= last):
1082
REVISION_NAMESPACES['date:'] = _namespace_date
799
# list is 0-based; revisions are 1-based
800
return self.revision_history()[revno-1]
802
raise BzrError("no such revision %s" % revno)
1084
805
def revision_tree(self, revision_id):
1085
806
"""Return Tree for a revision on this branch.
1231
def revert(self, filenames, old_tree=None, backups=True):
1232
"""Restore selected files to the versions from a previous tree.
1235
If true (default) backups are made of files before
1238
from bzrlib.errors import NotVersionedError, BzrError
1239
from bzrlib.atomicfile import AtomicFile
1240
from bzrlib.osutils import backup_file
1242
inv = self.read_working_inventory()
1243
if old_tree is None:
1244
old_tree = self.basis_tree()
1245
old_inv = old_tree.inventory
1248
for fn in filenames:
1249
file_id = inv.path2id(fn)
1251
raise NotVersionedError("not a versioned file", fn)
1252
if not old_inv.has_id(file_id):
1253
raise BzrError("file not present in old tree", fn, file_id)
1254
nids.append((fn, file_id))
1256
# TODO: Rename back if it was previously at a different location
1258
# TODO: If given a directory, restore the entire contents from
1259
# the previous version.
1261
# TODO: Make a backup to a temporary file.
1263
# TODO: If the file previously didn't exist, delete it?
1264
for fn, file_id in nids:
1267
f = AtomicFile(fn, 'wb')
1269
f.write(old_tree.get_file(file_id).read())
1275
def pending_merges(self):
1276
"""Return a list of pending merges.
1278
These are revisions that have been merged into the working
1279
directory but not yet committed.
1281
cfn = self.controlfilename('pending-merges')
1282
if not os.path.exists(cfn):
1285
for l in self.controlfile('pending-merges', 'r').readlines():
1286
p.append(l.rstrip('\n'))
1290
def add_pending_merge(self, revision_id):
1291
from bzrlib.revision import validate_revision_id
1293
validate_revision_id(revision_id)
1295
p = self.pending_merges()
1296
if revision_id in p:
1298
p.append(revision_id)
1299
self.set_pending_merges(p)
1302
def set_pending_merges(self, rev_list):
1303
from bzrlib.atomicfile import AtomicFile
1306
f = AtomicFile(self.controlfilename('pending-merges'))
1317
def get_parent(self):
1318
"""Return the parent location of the branch.
1320
This is the default location for push/pull/missing. The usual
1321
pattern is that the user can override it by specifying a
1325
_locs = ['parent', 'pull', 'x-pull']
1328
return self.controlfile(l, 'r').read().strip('\n')
1330
if e.errno != errno.ENOENT:
1335
def set_parent(self, url):
1336
# TODO: Maybe delete old location files?
1337
from bzrlib.atomicfile import AtomicFile
1340
f = AtomicFile(self.controlfilename('parent'))
1349
def check_revno(self, revno):
1351
Check whether a revno corresponds to any revision.
1352
Zero (the NULL revision) is considered valid.
1355
self.check_real_revno(revno)
1357
def check_real_revno(self, revno):
1359
Check whether a revno corresponds to a real revision.
1360
Zero (the NULL revision) is considered invalid
1362
if revno < 1 or revno > self.revno():
1363
raise InvalidRevisionNumber(revno)
1368
954
class ScratchBranch(Branch):
1481
1059
name = re.sub(r'[^\w.]', '', name)
1483
1061
s = hexlify(rand_bytes(8))
1484
return '-'.join((name, compact_date(time()), s))
1488
"""Return a new tree-root file id."""
1489
return gen_file_id('TREE_ROOT')
1492
def pull_loc(branch):
1493
# TODO: Should perhaps just make attribute be 'base' in
1494
# RemoteBranch and Branch?
1495
if hasattr(branch, "baseurl"):
1496
return branch.baseurl
1501
def copy_branch(branch_from, to_location, revision=None):
1502
"""Copy branch_from into the existing directory to_location.
1505
If not None, only revisions up to this point will be copied.
1506
The head of the new branch will be that revision.
1509
The name of a local directory that exists but is empty.
1511
from bzrlib.merge import merge
1512
from bzrlib.branch import Branch
1514
assert isinstance(branch_from, Branch)
1515
assert isinstance(to_location, basestring)
1517
br_to = Branch(to_location, init=True)
1518
br_to.set_root_id(branch_from.get_root_id())
1519
if revision is None:
1520
revno = branch_from.revno()
1522
revno, rev_id = branch_from.get_revision_info(revision)
1523
br_to.update_revisions(branch_from, stop_revision=revno)
1524
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1525
check_clean=False, ignore_zero=True)
1527
from_location = pull_loc(branch_from)
1528
br_to.set_parent(pull_loc(branch_from))
1062
return '-'.join((name, compact_date(time.time()), s))