| 1 |
# Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
# Use of this source code is governed by a BSD-style |
| 3 |
# license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
# Mercurial extension to add a 'goreposum' command that |
| 6 |
# computes a hash of a remote repo's tag state. |
| 7 |
# Tag definitions can come from the .hgtags file stored in |
| 8 |
# any head of any branch, and the server protocol does not |
| 9 |
# expose the tags directly. However, the protocol does expose |
| 10 |
# the hashes of all the branch heads, so we can use a hash of |
| 11 |
# all those branch names and heads as a conservative snapshot |
| 12 |
# of the entire remote repo state, and use that as the tag sum. |
| 13 |
# Any change on the server then invalidates the tag sum, |
| 14 |
# even if it didn't have anything to do with tags, but at least |
| 15 |
# we will avoid re-cloning a server when there have been no |
| 16 |
# changes at all. |
| 17 |
# |
| 18 |
# Separately, this extension also adds a 'golookup' command that |
| 19 |
# returns the hash of a specific reference, like 'default' or a tag. |
| 20 |
# And golookup of a hash confirms that it still exists on the server. |
| 21 |
# We can use that to revalidate that specific versions still exist and |
| 22 |
# have the same meaning they did the last time we checked. |
| 23 |
# |
| 24 |
# Usage: |
| 25 |
# |
| 26 |
# hg --config "extensions.goreposum=$GOROOT/lib/hg/goreposum.py" goreposum REPOURL |
| 27 |
|
| 28 |
import base64, hashlib, sys |
| 29 |
from mercurial import registrar, ui, hg, node |
| 30 |
from mercurial.i18n import _ |
| 31 |
cmdtable = {} |
| 32 |
command = registrar.command(cmdtable) |
| 33 |
@command(b'goreposum', [], _('url'), norepo=True) |
| 34 |
def goreposum(ui, url): |
| 35 |
""" |
| 36 |
goreposum computes a checksum of all the named state in the remote repo. |
| 37 |
It hashes together all the branch names and hashes |
| 38 |
and then all the bookmark names and hashes. |
| 39 |
Tags are stored in .hgtags files in any of the branches, |
| 40 |
so the branch metadata includes the tags as well. |
| 41 |
""" |
| 42 |
h = hashlib.sha256() |
| 43 |
peer = hg.peer(ui, {}, url) |
| 44 |
for name, revs in peer.branchmap().items(): |
| 45 |
h.update(name) |
| 46 |
for r in revs: |
| 47 |
h.update(b' ') |
| 48 |
h.update(r) |
| 49 |
h.update(b'\n') |
| 50 |
if (b'bookmarks' in peer.listkeys(b'namespaces')): |
| 51 |
for name, rev in peer.listkeys(b'bookmarks').items(): |
| 52 |
h.update(name) |
| 53 |
h.update(b'=') |
| 54 |
h.update(rev) |
| 55 |
h.update(b'\n') |
| 56 |
print('r1:'+base64.standard_b64encode(h.digest()).decode('utf-8')) |
| 57 |
|
| 58 |
@command(b'golookup', [], _('url rev'), norepo=True) |
| 59 |
def golookup(ui, url, rev): |
| 60 |
""" |
| 61 |
golookup looks up a single identifier in the repo, |
| 62 |
printing its hash. |
| 63 |
""" |
| 64 |
print(node.hex(hg.peer(ui, {}, url).lookup(rev)).decode('utf-8')) |