java - 如何在 JGit 中使用 Repository::resolve(String)

标签 java jgit

我正在使用 JGit 中的类存储库用 java 编写程序。

此类具有方法resolve(String revstr)。

基本上,这个方法解析 git 修订字符串并返回一个对象 id。支持这些运算符的特定组合:

HEAD, MERGE_HEAD, FETCH_HEAD
SHA-1: a complete or abbreviated SHA-1
refs/...: a complete reference name
short-name: a short reference name under refs/heads, refs/tags, or refs/remotes namespace
tag-NN-gABBREV: output from describe, parsed by treating ABBREV as an abbreviated SHA-1.
id^: first parent of commit id, this is the same as id^1
id^0: ensure id is a commit
id^n: n-th parent of commit id
id~n: n-th historical ancestor of id, by first parent. id~3 is equivalent to id^1^1^1 or id^^^.
id:path: Lookup path under tree named by id
id^{commit}: ensure id is a commit
id^{tree}: ensure id is a tree
id^{tag}: ensure id is a tag
id^{blob}: ensure id is a blob

我想用这个方法

Repository repo;
ObjectId commit = repo.resolve("cnfuwfxmiazsdixfnsdiufsdhfiusfhsfisfh^{tree}");
System.out.println(commit );

预期输出:

cnfuwfxmiazsdixfnsdiufsdhfiusfhsfisfh

输出:

sjakfshdofcsmdfocsdfjdofdjgdhgfdgfhgf
//Output is another commit in project.

我想要已插入的提交,但它显示了另一个提交。 我想是因为它是提交 id 的第一个父级。

有没有办法获得与我放入方法 resolve() 中的相同提交 ID?

最佳答案

传递给 resolve() 的表达式要求 tree id cnf... (顺便说一句,Git 对象 ID 仅由十六进制字符组成)。这很可能就是您所得到的,即您指定的提交的树 ID。

为了将字符串转换为 JGit ObjectId,请使用

ObjectId objectId = ObjectId.fromString( "..." );

另请参阅:How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

关于java - 如何在 JGit 中使用 Repository::resolve(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37570869/

相关文章:

java - 使用java程序或JGIT克隆PR

java - 单元测试忽略SAX特性失败,但程序运行成功

java slick2D 模拟重力跳跃

java - 渲染前从 webview 获取 Html

android - 无法将JGit导入gradle构建脚本

java - 如何在JGit中编写git log --stat命令

java - 无法在嵌入式 Jetty 服务器中加载 JSTL taglib

java - 在 ARCore 中访问图像数据字节

java - JGit Java Git 库 Unstaging 文件

java - 使用 JGIT,如何检索添加/删除行的行号