There is no direct equivalent to git rev-parse
in JGit.
(在JGit中没有直接等效于git rev-parse
的文件。)
However, JGit provides API that may help to achieve what rev-parse does.
(但是,JGit提供的API可能有助于实现rev-parse的功能。)
To shorten a given object id, use ObjectId::abbreviate()
(要缩短给定的对象ID,请使用ObjectId::abbreviate()
)
The following example abbreviates the given SHA-1:
(下面的示例缩写给定的SHA-1:)
ObjectId objectId = ObjectId.fromString( "fb808b5b01cdfaedda0bd1d304c7115ce959b286" );
AbbreviatedObjectId abbreviatedId = objectId.abbreviate( 7 ); // returns fb808b5
Note that the methods outlined above operate independent of a repository and thus cannot detect ambiguous object ids or verify if a given id exists in a repository or if it has the expected type (things that git rev-parse --verify
would do).
(请注意,上面概述的方法独立于存储库运行,因此无法检测到模棱两可的对象ID,也无法验证给定ID是否存在于存储库中或是否具有预期的类型( git rev-parse --verify
会执行的操作)。)
To verify an id with a repository Repository::resolve
can be used.
(要使用存储Repository::resolve
验证ID,可以使用Repository::resolve
。)
The method accepts expressions like fb808b5^{commit}
and returns an ObjectId
, or null
if the string can't be resolved. (该方法接受fb808b5^{commit}
类的表达式,并返回ObjectId
;如果无法解析字符串,则返回null
。)
an AmbiguousObjectException
is thrown if the repository contains more than one match. (如果存储库包含多个匹配项,则抛出AmbiguousObjectException
。)
See the JavaDoc for a list of supported expressions: http://download.eclipse.org/jgit/site/4.2.0-SNAPSHOT/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String) (有关受支持的表达式的列表,请参见JavaDoc: http : //download.eclipse.org/jgit/site/4.2.0-SNAPSHOT/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang。串))
Be aware, that an IncorrectObjectTypeException
is thrown if an object type is specified, but the revision that was found does not match the given type.
(请注意,如果指定了对象类型,则抛出IncorrectObjectTypeException
,但是找到的修订版与给定类型不匹配。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…