问题
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
git add .
git commit -a -m "Initial_Source"
Here I get the commit id as
cb96c685a5a4338f852a782631df8d1cf5dca21d
git tag Initial_Source cb96c685a5a4338f852a782631df8d1cf5dca21d
[cb96c685a5a4338f852a782631df8d1cf5dca21d is commitid]
git push
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?
回答1:
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());
}
}
回答2:
- 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()
来源:https://stackoverflow.com/questions/44692822/what-is-the-difference-between-getpeeledobjectid-and-getobjectid-of-ref-obje