JGit: Retrieve tag associated with a git commit

后端 未结 2 792
梦谈多话
梦谈多话 2021-02-08 10:11

I want to use JGit API to retrieve the tags associated with a specific commit hash (if there is any)?

Please provide code snippet for the same.

2条回答
  •  清歌不尽
    2021-02-08 11:02

    If you know that there is exactly one tag for your commit, you could use describe, in more recent versions of JGit (~ November 2013).

    Git.wrap(repository).describe().setTarget(ObjectId.fromString("hash")).call()
    

    You could parse the result, to see if a tag exists, but if there can be multiple tags, you should go with Marcins solution.

提交回复
热议问题