15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
from bzrlib.trace import mutter, note
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
23
25
sha_file, appendpath, file_kind
24
from bzrlib.errors import BzrError
26
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
28
from bzrlib.textui import show_status
29
from bzrlib.revision import Revision
30
from bzrlib.xml import unpack_xml
31
from bzrlib.delta import compare_trees
32
from bzrlib.tree import EmptyTree, RevisionTree
26
34
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
27
35
## TODO: Maybe include checks for common corruption of newlines, etc?
38
# TODO: Some operations like log might retrieve the same revisions
39
# repeatedly to calculate deltas. We could perhaps have a weakref
40
# cache in memory to make this faster.
42
# TODO: please move the revision-string syntax stuff out of the branch
43
# object; it's clutter
31
46
def find_branch(f, **args):
32
47
if f and (f.startswith('http://') or f.startswith('https://')):
118
133
Exception.__init__(self, "These branches have diverged.")
121
class NoSuchRevision(BzrError):
122
def __init__(self, branch, revision):
124
self.revision = revision
125
msg = "Branch %s has no revision %d" % (branch, revision)
126
BzrError.__init__(self, msg)
129
136
######################################################################
312
319
self.controlfile(f, 'w').write('')
313
320
mutter('created control directory in ' + self.base)
315
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
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
pack_xml(Inventory(), self.controlfile('inventory','w'))
318
328
def _check_format(self):
422
432
add all non-ignored children. Perhaps do that in a
423
433
higher-level method.
425
from bzrlib.textui import show_status
426
435
# TODO: Re-adding a file that is removed in the working copy
427
436
# should probably put it back with the previous ID.
428
437
if isinstance(files, basestring):
501
510
is the opposite of add. Removing it is consistent with most
502
511
other tools. Maybe an option.
504
from bzrlib.textui import show_status
505
513
## TODO: Normalize names
506
514
## TODO: Remove nested loops; better scalability
507
515
if isinstance(files, basestring):
593
def get_revision_xml(self, revision_id):
594
"""Return XML file object for revision object."""
595
if not revision_id or not isinstance(revision_id, basestring):
596
raise InvalidRevisionId(revision_id)
601
return self.revision_store[revision_id]
603
raise bzrlib.errors.NoSuchRevision(self, revision_id)
585
608
def get_revision(self, revision_id):
586
609
"""Return the Revision object for a named revision"""
587
from bzrlib.revision import Revision
588
from bzrlib.xml import unpack_xml
610
xml_file = self.get_revision_xml(revision_id)
592
if not revision_id or not isinstance(revision_id, basestring):
593
raise ValueError('invalid revision-id: %r' % revision_id)
594
r = unpack_xml(Revision, self.revision_store[revision_id])
613
r = unpack_xml(Revision, xml_file)
614
except SyntaxError, e:
615
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
598
619
assert r.revision_id == revision_id
623
def get_revision_delta(self, revno):
624
"""Return the delta for one revision.
626
The delta is relative to its mainline predecessor, or the
627
empty tree for revision 1.
629
assert isinstance(revno, int)
630
rh = self.revision_history()
631
if not (1 <= revno <= len(rh)):
632
raise InvalidRevisionNumber(revno)
634
# revno is 1-based; list is 0-based
636
new_tree = self.revision_tree(rh[revno-1])
638
old_tree = EmptyTree()
640
old_tree = self.revision_tree(rh[revno-2])
642
return compare_trees(old_tree, new_tree)
602
646
def get_revision_sha1(self, revision_id):
607
651
# the revision, (add signatures/remove signatures) and still
608
652
# have all hash pointers stay consistent.
609
653
# But for now, just hash the contents.
610
return sha_file(self.revision_store[revision_id])
654
return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
613
657
def get_inventory(self, inventory_id):
619
663
from bzrlib.inventory import Inventory
620
664
from bzrlib.xml import unpack_xml
622
return unpack_xml(Inventory, self.inventory_store[inventory_id])
666
return unpack_xml(Inventory, self.get_inventory_xml(inventory_id))
669
def get_inventory_xml(self, inventory_id):
670
"""Get inventory XML as a file object."""
671
return self.inventory_store[inventory_id]
625
674
def get_inventory_sha1(self, inventory_id):
626
675
"""Return the sha1 hash of the inventory entry
628
return sha_file(self.inventory_store[inventory_id])
677
return sha_file(self.get_inventory_xml(inventory_id))
631
680
def get_revision_inventory(self, revision_id):
697
746
return r+1, my_history[r]
698
747
return None, None
700
def enum_history(self, direction):
701
"""Return (revno, revision_id) for history of branch.
704
'forward' is from earliest to latest
705
'reverse' is from latest to earliest
707
rh = self.revision_history()
708
if direction == 'forward':
713
elif direction == 'reverse':
719
raise ValueError('invalid history direction', direction)
723
751
"""Return current revision number for this branch.
1019
1047
`revision_id` may be None for the null revision, in which case
1020
1048
an `EmptyTree` is returned."""
1021
from bzrlib.tree import EmptyTree, RevisionTree
1022
1049
# TODO: refactor this to use an existing revision object
1023
1050
# so we don't need to read it in twice.
1024
1051
if revision_id == None:
1025
return EmptyTree(self.get_root_id())
1027
1054
inv = self.get_revision_inventory(revision_id)
1028
1055
return RevisionTree(self.text_store, inv)
1040
1067
If there are no revisions yet, return an `EmptyTree`.
1042
from bzrlib.tree import EmptyTree, RevisionTree
1043
1069
r = self.last_patch()
1045
return EmptyTree(self.get_root_id())
1047
1073
return RevisionTree(self.text_store, self.get_revision_inventory(r))