Mercurial 存储库识别
我需要能够唯一地标识 Mercurial 存储库,并将该标识符放置在克隆时包含的文件中。如果我可以将标识符放入 .hg 文件夹中的文件中,这比简单地将普通文件添加到存储库更好。
我知道我可以从提交的第一个更改中获得几乎确定的标识符。我知道 hgrc 文件不能用于存储标识符,因为它没有被克隆。
所以,我的问题是:克隆的 .hg 文件夹中是否还有另一个文件可以用来放置标识符?谢谢。
I need to be able to uniquely identify a Mercurial repository and have that identifier placed in a file that is included when cloned. If I can put the identifier in a file in the .hg folder that is preferable to simply adding a normal file to the repo.
I understand that I can get a near certain identifier from the first changes that are committed. I know that the hgrc file cannot be used to store the identifier, because it is not cloned.
So, my question is: Is there another file in the .hg folder that is cloned that I can use to put the identifier? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从第一次阅读开始,听起来您希望能够确保存储库的克隆是正确存储库的克隆,而不是某个替代者的克隆。但是,如果您正在考虑使用的身份信息与其他所有信息一起克隆,那么冒名顶替者仍然可以通过此测试。您需要将该标识符分开,以便可以将其与克隆中的信息进行比较。
无论这是否是您的目的,您可能不想编辑
.hg
中克隆的任何文件。您必须在存储库的其他区域(.hg
之外)添加一个要跟踪的文件。但是,您实际上根本不需要额外的文件,因为变更集哈希不仅接近确定,而且非常确定,因此用于轻松识别存储库的信息内置于存储库中本身。在命令行上,您可以获得第一个变更集的哈希标识符的简短或完整版本:
如果我对您比较 2 个标识符的愿望是正确的,以便您或其他人知道存储库的克隆是真正的克隆,而不是如果是假克隆,您将单独获得相同的变更集 ID,以便有人可以使用上述命令之一来查看其克隆的 ID,并将其与您所说的进行比较。这很像许多具有可下载可执行文件的网站在下载链接旁边显示哈希标识符,以便您可以自己对文件进行哈希并将结果与网站上的哈希进行比较。
编辑关于阐明此目的的评论:
由于您需要能够从文件中读取它,因此有几个选项:
存储库中的跟踪文件root
除了创建自己的文件之外,您还可以考虑一个文件:
.hgtags
。...将标记第一个修订版,允许您使用
ident
作为对该变更集的引用,而不是-r0
。 Mercurial 始终使用最新版本.hgtags
中的标签信息,无论工作目录更新到什么变更集,但这对您的应用程序来说可能并不重要。hg tag
将这样的一行附加到.hgtags
文件中,如果文件不存在则创建该文件:如果您没有标签,这是最方便的文件尚未在您的存储库中,因为它将是文件中的第一行以便于查找。您可能认为可以简单地自己编写此文件,但您仍然需要调用
hg
来获取变更集 id,并在某个时候再次将其添加到跟踪中,然后提交:hg tag
为您完成这一切。如果已经有可能需要考虑标签文件,那也没关系,因为它们往往相对较短,您只需要查找以您选择的标签名称结尾的 1 行。 Mercurial 专为
.hgtags
进行仅追加操作而设计,但如果.hgtags
已存在,则如果将此标记的行插入为第一行,一切仍然可以正常工作因为: 1. 标签永远不会被移动或移除。 2. 您将使用文件中尚未使用的标签名称。读取
hg
的内容通常只有 Mercurial 本身才能深入了解
.hg
中的一些文件,可以读取这些文件来获取第一个变更集的哈希值。我研究了 Mercurial 的文件格式,Revlog 和 RevlogNG,至少对于我自己的 2 个存储库,.hg\store\00changelog.i
包含第一个变更集的哈希值,位于偏移量 0x20(20 字节长度)处。也许,至少从 Mercurial 0.9 开始,所有存储库中的情况都是相同的。 RevlogNG 还指出该文件的前 4 个字节将指示 Revlog 版本号和标志。虽然变更集 ID 目前只有 20 字节长,但它的实际字段长 32 字节,可能是为了将来扩展到更长的哈希值。由于此选项不需要更改现有存储库,并且只涉及读取主索引的前 52-64 个字节,因此我可能会选择它。如果我在产品的早期阶段在它管理的任何存储库出现之前就抓住了这个需求,我会倾向于自定义文件方法,因为我可能会从存储库的开头创建和添加自己的元数据文件。
From first read, it sounds like you want to be able to make sure that a clone of the repository is a clone of the correct repository and not some stand-in impostor. However, if the identification information you're thinking of using is cloned with everything else, then an impostor would still pass this test. You'd need to keep that identifier separate so that it can be compared against information in the clone.
Whether that is your purpose or not, any file in
.hg
that is cloned you may not want to edit. You'd have to add a file to be tracked in the other areas of the repo, outside of.hg
. However, you don't really need an extra file at all, as the changeset hash is not just near certain, but very certain, so the information for handily identifying a repository is built-in to the repository itself.On the commandline, you can get either the short or full versions of the very first changeset's hash identifier:
If I am correct about your desire to compare 2 identifiers so that you or someone else knows a clone of the repository is a true clone and not a false clone, you would have the same changset id available separately so that someone can use one of the above commands to see the id of their clone and compare it to what you say it should be. This is much like how many websites with downloadable executable files show a hash identifier next to the download link so that you can hash the file yourself and compare the result to the hash on the website.
Edit regarding your comment that sheds light on the purpose of this:
Since you need to be able to read it from a file, there are a couple options:
Tracked file in repository root
There is one file you might consider, other than creating your own:
.hgtags
....would tag the very first revision, allowing you to use
ident
as a reference to that changeset rather than-r0
. Mercurial always uses tag information from the latest version of.hgtags
, no matter what changeset the working directory is updated to, but that may not matter to your app.hg tag
appends a line such as this to the.hgtags
file, creating the file if it doesn't exist:This is most handy if you don't have a tags files yet in your repos, because it will be the first line in the file for easy finding. You might think could simply write this file yourself, but then you'd still have to call
hg
to get the changeset id and again at some point for adding it to tracking and then committing:hg tag
does all that for you.If there is already the possibility of a tags file to consider, that's ok, too, because they tend to be relatively short and you just need to look for the 1 line that ends with your chosen tag name. Mercurial is designed for append-only operations to
.hgtags
, but everything would still work fine if you inserted the line for this tag as the very first line if.hgtags
already exists because: 1. The tag will never be moved or removed. 2. You'll be using a tag name not already used in the file.Reading
hg
's gutsThere are files that normally only Mercurial itself touches deeper in
.hg
that can be read to get the first changeset's hash. I looked into Mercurial's File Formats, Revlog, and RevlogNG, and at least for 2 of my own repos,.hg\store\00changelog.i
contains the first changeset's hash at offset 0x20 (20 byte length). Probably, at least since Mercurial 0.9, it will be the same in all repos. RevlogNG also notes the first 4 bytes of that file will indicate Revlog version number and flags. While the changeset id is only 20 bytes long currently, the actual field for it is 32 bytes long, probably for future expansion to a longer hash.Since this option requires no alteration of existing repositories and only involves reading the first 52-64 bytes of the main index, it's the one I'd probably go with. If I was catching this requirement in the early stages of the product before any repos it manages were out in the wild, I would lean toward the custom file approach because I would probably have my own metadata file created and added from the beginning of the repo.
错误:存储库不相关
消息来自mercurial/treediscovery.py
:base
变量存储两个存储库的最后一个公共部分。通过给出推/拉检查的想法,我们可以假设存储库如果具有共同的根,则它们是相关的,因此从命令检查哈希值:出于我未知的原因
hg log -r 0
始终显示相同的根,但您可能会遇到FIRST_REPO保留SECOND_REPO历史记录的情况,但SECOND_REPO的明显0
转数与SECOND_REPO不同FIRST_REPO 但 Mercurial 检查已通过。您可能不会通过精心制作存储库来欺骗根检查,因为构建两个存储库看起来像这样(具有共同部分但根不同):
不可能,因为这意味着您反转 SHA-256,因为每个后续哈希都取决于先前的值。
error: repository is unrelated
message come frommercurial/treediscovery.py
:base
variable store last common parts of two repositories. By giving this idea of push/pull checks we may assume that repositories are related if they have common roots, so check hashes from command:For unknown to me reason
hg log -r 0
always shown same root, but you may have situation that FIRST_REPO hold SECOND_REPO history, but obviously0
revs of SECOND_REPO different from FIRST_REPO but Mercurial check is passed.You may not trick roots checking by carefully crafting repositories because building two repositories looks like these (with common parts but different roots):
impossible because that mean you reverse SHA-256 as each subsequent hash depends on previous values.