15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
21
from bzrlib.trace import mutter, note
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
25
23
sha_file, appendpath, file_kind
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
24
from bzrlib.errors import BzrError
34
26
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
35
27
## 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.
43
31
def find_branch(f, **args):
44
32
if f and (f.startswith('http://') or f.startswith('https://')):
130
118
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)
133
129
######################################################################
316
312
self.controlfile(f, 'w').write('')
317
313
mutter('created control directory in ' + self.base)
319
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
315
pack_xml(Inventory(), self.controlfile('inventory','w'))
322
318
def _check_format(self):
337
333
['use a different bzr version',
338
334
'or remove the .bzr directory and "bzr init" again'])
340
def get_root_id(self):
341
"""Return the id of this branches root"""
342
inv = self.read_working_inventory()
343
return inv.root.file_id
345
def set_root_id(self, file_id):
346
inv = self.read_working_inventory()
347
orig_root_id = inv.root.file_id
348
del inv._byid[inv.root.file_id]
349
inv.root.file_id = file_id
350
inv._byid[inv.root.file_id] = inv.root
353
if entry.parent_id in (None, orig_root_id):
354
entry.parent_id = inv.root.file_id
355
self._write_inventory(inv)
357
338
def read_working_inventory(self):
358
339
"""Read the working inventory."""
365
346
# ElementTree does its own conversion from UTF-8, so open in
367
348
inv = unpack_xml(Inventory,
368
self.controlfile('inventory', 'rb'))
349
self.controlfile('inventory', 'rb'))
369
350
mutter("loaded inventory of %d items in %f"
370
351
% (len(inv), time() - before))
426
407
add all non-ignored children. Perhaps do that in a
427
408
higher-level method.
410
from bzrlib.textui import show_status
429
411
# TODO: Re-adding a file that is removed in the working copy
430
412
# should probably put it back with the previous ID.
431
413
if isinstance(files, basestring):
504
486
is the opposite of add. Removing it is consistent with most
505
487
other tools. Maybe an option.
489
from bzrlib.textui import show_status
507
490
## TODO: Normalize names
508
491
## TODO: Remove nested loops; better scalability
509
492
if isinstance(files, basestring):
538
521
# FIXME: this doesn't need to be a branch method
539
522
def set_inventory(self, new_inventory_list):
540
523
from bzrlib.inventory import Inventory, InventoryEntry
541
inv = Inventory(self.get_root_id())
542
525
for path, file_id, parent, kind in new_inventory_list:
543
526
name = os.path.basename(path)
566
549
return self.working_tree().unknowns()
569
def append_revision(self, *revision_ids):
552
def append_revision(self, revision_id):
570
553
from bzrlib.atomicfile import AtomicFile
572
for revision_id in revision_ids:
573
mutter("add {%s} to revision-history" % revision_id)
575
rev_history = self.revision_history()
576
rev_history.extend(revision_ids)
555
mutter("add {%s} to revision-history" % revision_id)
556
rev_history = self.revision_history() + [revision_id]
578
558
f = AtomicFile(self.controlfilename('revision-history'))
587
def get_revision_xml(self, revision_id):
588
"""Return XML file object for revision object."""
589
if not revision_id or not isinstance(revision_id, basestring):
590
raise InvalidRevisionId(revision_id)
595
return self.revision_store[revision_id]
597
raise bzrlib.errors.NoSuchRevision(revision_id)
602
567
def get_revision(self, revision_id):
603
568
"""Return the Revision object for a named revision"""
604
xml_file = self.get_revision_xml(revision_id)
569
from bzrlib.revision import Revision
570
from bzrlib.xml import unpack_xml
607
r = unpack_xml(Revision, xml_file)
608
except SyntaxError, e:
609
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
574
if not revision_id or not isinstance(revision_id, basestring):
575
raise ValueError('invalid revision-id: %r' % revision_id)
576
r = unpack_xml(Revision, self.revision_store[revision_id])
613
580
assert r.revision_id == revision_id
617
def get_revision_delta(self, revno):
618
"""Return the delta for one revision.
620
The delta is relative to its mainline predecessor, or the
621
empty tree for revision 1.
623
assert isinstance(revno, int)
624
rh = self.revision_history()
625
if not (1 <= revno <= len(rh)):
626
raise InvalidRevisionNumber(revno)
628
# revno is 1-based; list is 0-based
630
new_tree = self.revision_tree(rh[revno-1])
632
old_tree = EmptyTree()
634
old_tree = self.revision_tree(rh[revno-2])
636
return compare_trees(old_tree, new_tree)
640
584
def get_revision_sha1(self, revision_id):
645
589
# the revision, (add signatures/remove signatures) and still
646
590
# have all hash pointers stay consistent.
647
591
# But for now, just hash the contents.
648
return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
592
return sha_file(self.revision_store[revision_id])
651
595
def get_inventory(self, inventory_id):
672
616
# must be the same as its revision, so this is trivial.
673
617
if revision_id == None:
674
618
from bzrlib.inventory import Inventory
675
return Inventory(self.get_root_id())
677
621
return self.get_inventory(revision_id)
735
679
return r+1, my_history[r]
736
680
return None, None
682
def enum_history(self, direction):
683
"""Return (revno, revision_id) for history of branch.
686
'forward' is from earliest to latest
687
'reverse' is from latest to earliest
689
rh = self.revision_history()
690
if direction == 'forward':
695
elif direction == 'reverse':
701
raise ValueError('invalid history direction', direction)
740
705
"""Return current revision number for this branch.
1036
1004
`revision_id` may be None for the null revision, in which case
1037
1005
an `EmptyTree` is returned."""
1006
from bzrlib.tree import EmptyTree, RevisionTree
1038
1007
# TODO: refactor this to use an existing revision object
1039
1008
# so we don't need to read it in twice.
1040
1009
if revision_id == None:
1381
1351
s = hexlify(rand_bytes(8))
1382
1352
return '-'.join((name, compact_date(time()), s))
1386
"""Return a new tree-root file id."""
1387
return gen_file_id('TREE_ROOT')