How to log the commits between two release tags with JGit - jgit

I have one case which is for example to run git command like
$ git log 1.0.201802090918...1.0.201802071240"
to get a differed commits list between release tag 1.0.201802090918 and 1.0.201802071240 under my repo. So I wonder how to code with JGit to gain the same here.

The LogCommand allows to specify the range of commits that it will include. The range need to be given as ObjectIds. And if tags mark the start and end points, the commit IDs that they reference need to be extracted first.
The snippet below illustrates the necessary steps:
ObjectId from = repo.resolve("refs/tags/start-tag");
ObjectId to = repo.resolve("refs/tags/end-tag");
git.log().addRange(from, to).call();
If annotated tags are used, they may have to be unpeeled first as described here: what is the difference between getPeeledObjectId() and getObjectId() of Ref Object?

Related

Has there been a change in Create DMP Segment API response header wrt segment ID : "X-LinkedIn-Id"?

As We implemented our code long back where we used to get the value of SegmentID from "X-LinkedIn-Id" Attributes and now this attribute has been changed to all small letters and our code is not working due to this. Is this a temporary change or a permanent change? Depending on this we need to do backport to our previous releases so its very important to get this clarity. And also we did not get any supporting doc related to this change over the official doc.
Can you please let us know about this so that we can make our final changes And also can you please share a doc wrt change logs on this API ?
And there is also a discrepancy around "x-linkedin-id" as in Docs, where its small letters and in git repo its still caps which we are currently using and facing the issue. Ref below:
In the Doc related to Create DMP Segment
https://learn.microsoft.com/en-us/linkedin/marketing/integrations/matched-audiences/create-and-manage-segments?tabs=http#create-dmp-segment , "x-linkedin-id" attribute is used.
In Git Repo
https://linkedin.github.io/rest.li/spec/protocol , "X-LinkedIn-Id" attribute is used.

JavaParser SymbolSolver and JGit to find method qualified name for a given commit

Consider the following code:
mt.resolve().getQualifiedSignature();
Here mt is of type MethodDeclaration, and it might come from a MethodCallExpr.
Now in order it to work accurately, I need to set the following:
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(new File("src/java1/")));
combinedTypeSolver.add(new JavaParserTypeSolver(new File("src/java2/
")));
This is easy, except I have two issues considering my scenario.
1) I can not set the root source directories manually. I need to find them automatically.
2) I can not give path like the above, because I am using jGit to checkout to different commits. So the paths are not fixed and might vary based on the different checkout commits. So the path should be accessed using JGit tree.
Any help would be highly appreciated.

How to programmatically run arc diff --verbatim if test plan is required

We want to run arc diff using a jenkins job which will put out a diff programmatically.
We can use --verbatim option, but it's complaining about Invalid or missing field "Test Plan". We don't want to disable test plan requirement but arc diff doesn't seem to have an option to provide test plan from cli. So the question is how do we prevent arc diff prompt window?
to have arc diff --create --verbatim to work, the last commit before you create the revision must have all the needed fields, eg. :
Title of commit
Summary: Summary of commit
Test Plan: A test plan
Say, if you want to do all of that programatically, you have to create this commit first.

Is there a JGit method equivalent to git rev-parse --short?

Does JGit include an implementation of git rev-parse --short? I could not find one in perusing the documentation.
There is no direct equivalent to git rev-parse in JGit.
However, JGit provides API that may help to achieve what rev-parse does.
ObjectId::isId() to determine if the given string represents a SHA-1
ObjectId::fromString() to create an ObjectId from a string
To shorten a given object id, use ObjectId::abbreviate()
The following example abbreviates the given 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).
To verify an id with a repository Repository::resolve can be used. The method accepts expressions like fb808b5^{commit} and returns an ObjectId, or null if the string can't be resolved. an AmbiguousObjectException is thrown if the repository contains more than one match. 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)
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.
Just to make a straightforward example for what #RĂ¼diger Herrmann mentioned above
// git rev-parse HEAD
git.getRepository().exactRef("refs/heads/master").getObjectId().name()
// git rev-parse --short HEAD
git.getRepository().exactRef("refs/heads/master").getObjectId().abbreviate(7).name()

JGit: Retrieve tag associated with a git commit

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.
Git object model describes tag as an object containing information about specific object ie. commit (among other things) thus it's impossible in pure git to get information you want (commit object don't have information about related tags). This should be done "backwards", take tag object and then refer to specific commit.
So if you want get information about tags specified for particular commit you should iterate over them (tags) and choose appropriate.
List<RevTag> list = git.tagList().call();
ObjectId commitId = ObjectId.fromString("hash");
Collection<ObjectId> commits = new LinkedList<ObjectId>();
for (RevTag tag : list) {
RevObject object = tag.getObject();
if (object.getId().equals(commitId)) {;
commits.add(object.getId());
}
}
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.

Resources