1

I am using jgit api for my project's build, deployment functionalities (in Local Machine). I commited whole source (java project) via command prompt by following commands

  1. git add .
  2. git commit -a -m "Initial_Source"

    Here I get the commit id as cb96c685a5a4338f852a782631df8d1cf5dca21d

  3. git tag Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d

    [cb96c685a5a4338f852a782631df8d1cf5dca21d is commitid]

  4. git push

  5. git push --tags

but when i tried to get commit id via getPeeledObjectId() it is returning null

my code is

Ref tag = git.getRepository().getRef("Initial_Source");
Ref peeledRef = git.getRepository().peel(tag);
return peeledRef.getPeeledObjectId(); -- this is returning null

but instead of getPeeledObjectId() I tried using getObjectId(). It's giving the commitId. But I wanna know when to use getPeelObjectId() and getObjectId().

What are those methods?

jdno
  • 4,104
  • 1
  • 22
  • 27
Jothivignesh
  • 45
  • 1
  • 6

2 Answers2

0

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());
    }
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • wat is peel? where should i use getPeelObjectId() nd normal getObjectId() – Jothivignesh Jun 22 '17 at 08:41
  • @Jothivignesh peel is dereferencing the object: for a lightweight tag, there is nothing to dereference: it already points to the commit. For an annotated tag, you need `getPeelObjectId()` in order to access the commit, because `getObjectId()` would give you the tag itself. That is part of the difference between lightweight and annotated tag, as described in the answers of https://stackoverflow.com/q/11514075/6309. – VonC Jun 22 '17 at 08:43
  • so if i put a tag with -a or -m(annotated tag) then i can get the commitId from getPeelObjectId() otherwise i have to use getObjectId().. rit? – Jothivignesh Jun 22 '17 at 12:03
  • @Jothivignesh Yes, that is the idea. – VonC Jun 22 '17 at 15:06
0
  • 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()

Jothivignesh
  • 45
  • 1
  • 6