what is the difference between getPeeledObjectId() and getObjectId() of Ref Object?

耗尽温柔 提交于 2019-12-06 15:49:17

The getPeeledObjectId() method is always null on a non-annotated (lightweight) tag:

 git tag Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d

That would work with an annotated tag

git tag -a Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d
# or
git tag -m "Initial Source" Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d

Since your tag is a pointer to a commit (referenced by git.getRepository().peel(tag)), getObjectId() gets its id, there is nothing to "peel" again: you already have the commit.
See "What is the difference between an annotated and unannotated tag?"

See porcelain/ListTags.java example: it takes into account the two kinds of tag:

List<Ref> call = git.tagList().call();
for (Ref ref : call) {
    System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

    // fetch all commits for this tag
    LogCommand log = git.log();

    Ref peeledRef = repository.peel(ref);
    if(peeledRef.getPeeledObjectId() != null) {
        // Annotated tag
        log.add(peeledRef.getPeeledObjectId());
    } else {
        // Lightweight tag
        log.add(ref.getObjectId());
    }
}
  • As @VonC said i think we cannot get ObjectId of a lightweight tag(i.e a tag without -a or -m) from getPeeledObjectId().
  • I tried to commit a file and tag that file without -a or -m

    [ git tag Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d ].

  • Then run a java main program to get object Id from getPeeledObjectId() which obviously returns null.

  • Then i commit a file and then tag it with -a and -m (any 1 is enough to act as annotated tag)

    [ git tag Appinterface 523a05f9c486e64eba29786a1b8abfc4da421260 -m "Appinterface_commit_tag" ]

  • Now i am getting objectId from getPeeledObjectId()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!