Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

jgit - 是否有一个等效于git rev-parse --short的JGit方法?(Is there a JGit method equivalent to git rev-parse --short?)

Does JGit include an implementation of git rev-parse --short ?

(JGit是否包括git rev-parse --short ?)

I could not find one in perusing the documentation.

(我在仔细阅读文档时找不到。)

  ask by bmargulies translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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的功能。)

  • ObjectId::isId() to determine if the given string represents a SHA-1

    (ObjectId::isId()确定给定的字符串是否代表SHA-1)

  • ObjectId::fromString() to create an ObjectId from a string

    (ObjectId::fromString()从字符串创建一个ObjectId)

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 ,但是找到的修订版与给定类型不匹配。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...