From 57890ff99f5bcadbd363a2155433b97bf95052ec Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Sat, 29 Oct 2016 19:19:18 +0100 Subject: [PATCH] fastannotate: add a method to quickly get the last node of the revmap Summary: Previously, to get changeset hashes in the revmap, we have to construct the `revmap` object, which reads all content of a revmap. In the following patches, we want to quickly get the last hash of the revmap to test if the linelog / revmap is up-to-date or not. This patch adds a method to do so. Test Plan: Modified existing tests. Reviewers: #sourcecontrol, stash Reviewed By: stash Subscribers: mjpieters Differential Revision: https://phabricator.intern.facebook.com/D4103898 Signature: t1:4103898:1477988583:15869dfe9028068563b03d1f06d2337562049751 --- fastannotate/revmap.py | 14 ++++++++++++++ tests/test-fastannotate-revmap.py | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/fastannotate/revmap.py b/fastannotate/revmap.py index 8204be7645..818bc5da8c 100644 --- a/fastannotate/revmap.py +++ b/fastannotate/revmap.py @@ -227,3 +227,17 @@ class revmap(object): if path is not None and path != self.rev2path(rev): return False return (self.rev2flag(rev) & sidebranchflag) == 0 + +def getlastnode(path): + """return the last hash in a revmap, without loading its full content. + this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster. + """ + hsh = None + try: + with open(path, 'rb') as f: + f.seek(-_hshlen, 2) + if f.tell() > len(revmap.HEADER): + hsh = f.read(_hshlen) + except IOError: + pass + return hsh diff --git a/tests/test-fastannotate-revmap.py b/tests/test-fastannotate-revmap.py index 05eeca610c..2ddad7d9df 100644 --- a/tests/test-fastannotate-revmap.py +++ b/tests/test-fastannotate-revmap.py @@ -175,7 +175,20 @@ def testcontains(): ensure(fakefctx(genhsh(i), path=str(i // 2)) in rm) ensure(fakefctx(genhsh(i), path='a') not in rm) +def testlastnode(): + path = gettemppath() + ensure(revmap.getlastnode(path) is None) + rm = revmap.revmap(path) + ensure(revmap.getlastnode(path) is None) + for i in xrange(1, 10): + hsh = genhsh(i) + rm.append(hsh, path=str(i // 2), flush=True) + ensure(revmap.getlastnode(path) == hsh) + rm2 = revmap.revmap(path) + ensure(rm2.rev2hsh(rm2.maxrev) == hsh) + testbasicreadwrite() testcorruptformat() testcopyfrom() testcontains() +testlastnode()