简体   繁体   English

如何使用Rugged(libgit2)Ruby gem通过标记/引用查找提交?

[英]How to lookup commit by tag/ref using the Rugged (libgit2) Ruby gem?

I'm using Rugged, the libgit2 binding for Ruby. 我正在使用Rugged,Ruby的libgit2绑定。 I have seen that you can call the Repository#lookup method to get the object at a given SHA hash in a git repository. 我已经看到您可以调用Repository#lookup方法来在git存储库中的给定SHA哈希处获取对象。

The thing is, I don't know the hash of the commit I'm looking for. 问题是,我不知道我要查找的提交的哈希值。 I do know the tag id though (eg v1.4.2 ). 我确实知道标签ID(例如v1.4.2 )。 I'd like something similar to: 我想要类似的东西:

# not real API!
my_repo.lookup('v1.4.2')

Or if it's the best I can do, something like this is fine: 或者,如果这是我能做的最好的事情,那么可以这样做:

# not real API!
sha = my_repo.get_sha_from_tag('v1.4.2')
my_repo.lookup(sha)

The trouble is that although I'm looking through the documentation, I'm unfamiliar with a lot of the low-level Git terminology being used (I had never heard of an oid until 1 hour ago, for example). 麻烦的是,尽管我在浏览文档,但对许多底层Git术语不熟悉(例如,直到1个小时前我才听说过oid )。 So it's difficult to find what I need in the docs. 因此,很难在文档中找到我需要的东西。

I am new to Ruby and to Rugged and to libgit2, so apologies if it seems like I missed something obvious. 我是Ruby,Rugged和libgit2的新手,因此,如果我错过明显的内容,我深表歉意。 Thanks! 谢谢!

我在这里找到了一种执行此操作的方法

my_repo.tags.find{|tag| tag.name == "v1.4.2"}.target

Background 背景

"OID" is not really in Git's parlance: this is rather a contraption of the authors of Rugged. 实际上,“ OID”并不是Git的用语:这是Rugged作者的一种悖论。 Still, this term colloquially stands for "object ID", where "ID" stands for "identifier". 尽管如此,该术语通俗地表示“对象ID”,其中“ ID”表示“标识符”。 Git presently uses SHA-1 to identify all types of objects it stores (which are: commits, trees and blobs). Git目前使用SHA-1来识别它存储的所有类型的对象(即:提交,树和Blob)。 Hence presumably Repository#lookup expects a SHA-1 of a valid object (presumably formatted as a string containing hex-representation of that SHA-1). 因此,大概Repository#lookup期望一个有效对象的SHA-1(大概格式化为包含该SHA-1的十六进制表示形式的字符串)。

Contrary to SHA-1 names identifying various objects in the Git database, Git also has "symbolic names" for branches and tags. 与在Git数据库中标识各种对象的SHA-1名称相反,Git还具有用于分支和标签的“符号名称”。 Those things combined are called "refs", which is short for "references" (since they, well, refer to other stuff — typically commits, but tags can refer to any kind of object at all). 那些组合在一起的东西称为“引用”,是“引用”的缩写(因为它们很好地引用了其他内容-通常是提交,但是标记根本可以引用任何种类的对象)。 Also note that internally Git uses the term "heads" to refer to branches. 还要注意,Git在内部使用术语“ heads”来指代分支。

What is more, Git implements a "mini language" which can be used to refer to specific stuff using combinations of ref names, SHA-1 names and operators. 而且,Git实现了一种“迷你语言”,该语言可以使用引用名称,SHA-1名称和运算符的组合来引用特定内容。 This is documented in the gitrevisions(7) manual page, and it should be your go-to documentation to start with. 这在gitrevisions(7)手册页中进行了说明,并且应该作为gitrevisions(7)文档。 A "revision" is hence anything which may be resolved to some kind of object stored in a Git database — typically commit — using that Git's minilanguage. 因此,“修订”是指可以使用该Git的最小语言解析为Git数据库中存储的某种对象(通常是提交)的任何内容。

Consider reading it through comptelely, but at the very least read the 3rd paragraph of the "SPECIFYING REVISIONS" block, which starts with <refname> ... — from there, you'll learn that a full name of a tag named, say, "v1.2.3" actually is "refs/tags/v1.2.3", and the fact you may use that short name is due to Git applying its ref resolution rules detailed in that manual page. 考虑完全阅读它,但至少要阅读“ SPECIFYING REVISIONS”块的第3段,该段以<refname> ...开头<refname> ...从那里,您将了解到一个名为的标签的全名,例如,“ v1.2.3”实际上是“ refs / tags / v1.2.3”,您之所以可以使用该简称,是因为Git应用了该手册页中详细说明的ref解析规则。

Solution

So, with that knowledge at hand, a quick skim through the doc you're using brings back at least Repository#rev_parse which looks like what you need — pass it "refs/tags/v1.4.2" and be done with that. 因此,掌握了这些知识之后,快速浏览一下您正在使用的文档 ,至少可以找到看起来像您所需的Repository#rev_parse其传递给“ refs / tags / v1.4.2”并完成该操作。

Note that passing it bare "v1.4.2" will also work but might have surprising result if there is no tag named like that but exists a branch with that name. 请注意,将其裸露地传递给“ v1.4.2”也可以,但是如果没有像这样命名的标签,但是存在一个使用该名称的分支,则可能会产生令人惊讶的结果。 So think your solution through. 因此,请仔细考虑您的解决方案。

The way you found yourself will also work OK but it's a bit unnatural IMO. 您发现自己的方式也可以正常工作,但是IMO有点不自然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM