[Pkg-bazaar-commits] ./bzr/unstable r703: - split out a new 'bzr upgrade' command separate from
Martin Pool
mbp at sourcefrog.net
Fri Apr 10 08:20:46 UTC 2009
------------------------------------------------------------
revno: 703
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Mon 2005-06-20 12:50:08 +1000
message:
- split out a new 'bzr upgrade' command separate from
(but based on) 'bzr check', so that the code in each is simpler
added:
bzrlib/upgrade.py
modified:
NEWS
bzrlib/check.py
bzrlib/commands.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS 2005-06-15 06:03:16 +0000
+++ b/NEWS 2005-06-20 02:50:08 +0000
@@ -1,3 +1,12 @@
+DEVELOPMENT HEAD
+
+ CHANGES:
+
+ * New ``bzr upgrade`` command to upgrade the format of a branch,
+ replacing ``bzr check --update``.
+
+
+
bzr-0.0.5 2005-06-15
CHANGES:
=== modified file 'bzrlib/check.py'
--- a/bzrlib/check.py 2005-06-11 01:58:46 +0000
+++ b/bzrlib/check.py 2005-06-20 02:50:08 +0000
@@ -18,29 +18,17 @@
-def check(branch, update=False):
+def check(branch):
"""Run consistency checks on a branch.
-
- If update is True, for revisions missing certain information
- (right now this is inventory_sha1 and revision_sha1),
- update them to include the appropriate values.
"""
- import sys
-
from bzrlib.trace import mutter
from bzrlib.errors import BzrCheckError
from bzrlib.osutils import fingerprint_file
from bzrlib.progress import ProgressBar
- if update:
- branch.lock_write()
- else:
- branch.lock_read()
+ branch.lock_read()
try:
-
- out = sys.stdout
-
pb = ProgressBar(show_spinner=True)
last_ptr = None
checked_revs = {}
@@ -53,104 +41,38 @@
checked_texts = {}
- updated_revisions = []
-
- # Set to True in the case that the previous revision entry
- # was updated, since this will affect future revision entries
- updated_previous_revision = False
-
- for rid in history:
+ for rev_id in history:
revno += 1
pb.update('checking revision', revno, revcount)
- mutter(' revision {%s}' % rid)
- rev = branch.get_revision(rid)
- if rev.revision_id != rid:
- raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
+ mutter(' revision {%s}' % rev_id)
+ rev = branch.get_revision(rev_id)
+ if rev.revision_id != rev_id:
+ raise BzrCheckError('wrong internal revision id in revision {%s}' % rev_id)
if rev.precursor != last_ptr:
- raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
- last_ptr = rid
- if rid in checked_revs:
- raise BzrCheckError('repeated revision {%s}' % rid)
- checked_revs[rid] = True
+ raise BzrCheckError('mismatched precursor in revision {%s}' % rev_id)
+ last_ptr = rev_id
+ if rev_id in checked_revs:
+ raise BzrCheckError('repeated revision {%s}' % rev_id)
+ checked_revs[rev_id] = True
## TODO: Check all the required fields are present on the revision.
- updated = False
if rev.inventory_sha1:
- #mutter(' checking inventory hash {%s}' % rev.inventory_sha1)
inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
if inv_sha1 != rev.inventory_sha1:
raise BzrCheckError('Inventory sha1 hash doesn\'t match'
- ' value in revision {%s}' % rid)
- elif update:
- inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
- rev.inventory_sha1 = inv_sha1
- updated = True
+ ' value in revision {%s}' % rev_id)
else:
missing_inventory_sha_cnt += 1
- mutter("no inventory_sha1 on revision {%s}" % rid)
+ mutter("no inventory_sha1 on revision {%s}" % rev_id)
if rev.precursor:
if rev.precursor_sha1:
precursor_sha1 = branch.get_revision_sha1(rev.precursor)
- if updated_previous_revision:
- # we don't expect the hashes to match, because
- # we had to modify the previous revision_history entry.
- rev.precursor_sha1 = precursor_sha1
- updated = True
- else:
- #mutter(' checking precursor hash {%s}' % rev.precursor_sha1)
- if rev.precursor_sha1 != precursor_sha1:
- raise BzrCheckError('Precursor sha1 hash doesn\'t match'
- ' value in revision {%s}' % rid)
- elif update:
- precursor_sha1 = branch.get_revision_sha1(rev.precursor)
- rev.precursor_sha1 = precursor_sha1
- updated = True
-
- if updated:
- updated_previous_revision = True
- # We had to update this revision entries hashes
- # Now we need to write out a new value
- # This is a little bit invasive, since we are *rewriting* a
- # revision entry. I'm not supremely happy about it, but
- # there should be *some* way of making old entries have
- # the full meta information.
- import tempfile, os, errno
- rev_tmp = tempfile.TemporaryFile()
- rev.write_xml(rev_tmp)
- rev_tmp.seek(0)
-
- tmpfd, tmp_path = tempfile.mkstemp(prefix=rid, suffix='.gz',
- dir=branch.controlfilename('revision-store'))
- os.close(tmpfd)
- def special_rename(p1, p2):
- if sys.platform == 'win32':
- try:
- os.remove(p2)
- except OSError, e:
- if e.errno != e.ENOENT:
- raise
- os.rename(p1, p2)
-
- try:
- # TODO: We may need to handle the case where the old revision
- # entry was not compressed (and thus did not end with .gz)
-
- # Remove the old revision entry out of the way
- rev_path = branch.controlfilename(['revision-store', rid+'.gz'])
- special_rename(rev_path, tmp_path)
- branch.revision_store.add(rev_tmp, rid) # Add the new one
- os.remove(tmp_path) # Remove the old name
- mutter(' Updated revision entry {%s}' % rid)
- except:
- # On any exception, restore the old entry
- special_rename(tmp_path, rev_path)
- raise
- rev_tmp.close()
- updated_revisions.append(rid)
- else:
- updated_previous_revision = False
+ #mutter(' checking precursor hash {%s}' % rev.precursor_sha1)
+ if rev.precursor_sha1 != precursor_sha1:
+ raise BzrCheckError('Precursor sha1 hash doesn\'t match'
+ ' value in revision {%s}' % rev_id)
inv = branch.get_inventory(rev.inventory_id)
seen_ids = {}
@@ -161,11 +83,10 @@
if file_id in seen_ids:
raise BzrCheckError('duplicated file_id {%s} '
'in inventory for revision {%s}'
- % (file_id, rid))
+ % (file_id, rev_id))
seen_ids[file_id] = True
i = 0
- len_inv = len(inv)
for file_id in inv:
i += 1
if i & 31 == 0:
@@ -176,7 +97,7 @@
if ie.parent_id != None:
if ie.parent_id not in seen_ids:
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
- % (ie.parent_id, rid))
+ % (ie.parent_id, rev_id))
if ie.kind == 'file':
if ie.text_id in checked_texts:
@@ -196,14 +117,14 @@
elif ie.kind == 'directory':
if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
raise BzrCheckError('directory {%s} has text in revision {%s}'
- % (file_id, rid))
+ % (file_id, rev_id))
pb.tick()
for path, ie in inv.iter_entries():
if path in seen_names:
- raise BzrCheckError('duplicated path %r '
+ raise BzrCheckError('duplicated path %s '
'in inventory for revision {%s}'
- % (path, revid))
+ % (path, rev_id))
seen_names[path] = True
finally:
@@ -212,8 +133,6 @@
pb.clear()
print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
- if updated_revisions:
- print '%d revisions updated to current format' % len(updated_revisions)
if missing_inventory_sha_cnt:
print '%d revisions are missing inventory_sha1' % missing_inventory_sha_cnt
- print ' (use bzr check --update to fix them)'
+ print ' (use "bzr upgrade" to fix them)'
=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py 2005-06-20 01:32:42 +0000
+++ b/bzrlib/commands.py 2005-06-20 02:50:08 +0000
@@ -1122,11 +1122,24 @@
to help ensure data consistency.
"""
takes_args = ['dir?']
- takes_options = ['update']
- def run(self, dir='.', update=False):
+ def run(self, dir='.'):
import bzrlib.check
- bzrlib.check.check(Branch(dir), update=update)
+ bzrlib.check.check(Branch(dir))
+
+
+
+class cmd_upgrade(Command):
+ """Upgrade branch storage to current format.
+
+ This should normally be used only after the check command tells
+ you to run it.
+ """
+ takes_args = ['dir?']
+
+ def run(self, dir='.'):
+ from bzrlib.upgrade import upgrade
+ upgrade(Branch(dir))
=== added file 'bzrlib/upgrade.py'
--- a/bzrlib/upgrade.py 1970-01-01 00:00:00 +0000
+++ b/bzrlib/upgrade.py 2005-06-20 02:50:08 +0000
@@ -0,0 +1,150 @@
+# Copyright (C) 2004, 2005 by Martin Pool
+# Copyright (C) 2005 by Canonical Ltd
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+
+
+def upgrade(branch):
+ """
+ Upgrade branch to current format.
+
+ This causes objects to be rewritten into the current format.
+
+ If they change, their SHA-1 will of course change, which might
+ break any later signatures, or backreferences that check the
+ SHA-1.
+ """
+ import sys
+
+ from bzrlib.trace import mutter
+ from bzrlib.errors import BzrCheckError
+ from bzrlib.progress import ProgressBar
+
+ branch.lock_write()
+
+ try:
+ pb = ProgressBar(show_spinner=True)
+ last_ptr = None
+ checked_revs = {}
+
+ history = branch.revision_history()
+ revno = 0
+ revcount = len(history)
+
+ updated_revisions = []
+
+ # Set to True in the case that the previous revision entry
+ # was updated, since this will affect future revision entries
+ updated_previous_revision = False
+
+ for rid in history:
+ revno += 1
+ pb.update('upgrading revision', revno, revcount)
+ mutter(' revision {%s}' % rid)
+ rev = branch.get_revision(rid)
+ if rev.revision_id != rid:
+ raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
+ if rev.precursor != last_ptr:
+ raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
+ last_ptr = rid
+ if rid in checked_revs:
+ raise BzrCheckError('repeated revision {%s}' % rid)
+ checked_revs[rid] = True
+
+ ## TODO: Check all the required fields are present on the revision.
+
+ updated = False
+ if rev.inventory_sha1:
+ #mutter(' checking inventory hash {%s}' % rev.inventory_sha1)
+ inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
+ if inv_sha1 != rev.inventory_sha1:
+ raise BzrCheckError('Inventory sha1 hash doesn\'t match'
+ ' value in revision {%s}' % rid)
+ else:
+ inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
+ rev.inventory_sha1 = inv_sha1
+ updated = True
+
+ if rev.precursor:
+ if rev.precursor_sha1:
+ precursor_sha1 = branch.get_revision_sha1(rev.precursor)
+ if updated_previous_revision:
+ # we don't expect the hashes to match, because
+ # we had to modify the previous revision_history entry.
+ rev.precursor_sha1 = precursor_sha1
+ updated = True
+ else:
+ #mutter(' checking precursor hash {%s}' % rev.precursor_sha1)
+ if rev.precursor_sha1 != precursor_sha1:
+ raise BzrCheckError('Precursor sha1 hash doesn\'t match'
+ ' value in revision {%s}' % rid)
+ else:
+ precursor_sha1 = branch.get_revision_sha1(rev.precursor)
+ rev.precursor_sha1 = precursor_sha1
+ updated = True
+
+ if updated:
+ updated_previous_revision = True
+ # We had to update this revision entries hashes
+ # Now we need to write out a new value
+ # This is a little bit invasive, since we are *rewriting* a
+ # revision entry. I'm not supremely happy about it, but
+ # there should be *some* way of making old entries have
+ # the full meta information.
+ import tempfile, os, errno
+ rev_tmp = tempfile.TemporaryFile()
+ rev.write_xml(rev_tmp)
+ rev_tmp.seek(0)
+
+ tmpfd, tmp_path = tempfile.mkstemp(prefix=rid, suffix='.gz',
+ dir=branch.controlfilename('revision-store'))
+ os.close(tmpfd)
+ def special_rename(p1, p2):
+ if sys.platform == 'win32':
+ try:
+ os.remove(p2)
+ except OSError, e:
+ if e.errno != errno.ENOENT:
+ raise
+ os.rename(p1, p2)
+
+ try:
+ # TODO: We may need to handle the case where the old revision
+ # entry was not compressed (and thus did not end with .gz)
+
+ # Remove the old revision entry out of the way
+ rev_path = branch.controlfilename(['revision-store', rid+'.gz'])
+ special_rename(rev_path, tmp_path)
+ branch.revision_store.add(rev_tmp, rid) # Add the new one
+ os.remove(tmp_path) # Remove the old name
+ mutter(' Updated revision entry {%s}' % rid)
+ except:
+ # On any exception, restore the old entry
+ special_rename(tmp_path, rev_path)
+ raise
+ rev_tmp.close()
+ updated_revisions.append(rid)
+ else:
+ updated_previous_revision = False
+
+ finally:
+ branch.unlock()
+
+ pb.clear()
+
+ if updated_revisions:
+ print '%d revisions updated to current format' % len(updated_revisions)
More information about the Pkg-bazaar-commits
mailing list