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

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()

Related

Corda NodeDriver MissingContractAttachments for Cash even with packages to scan set

Getting the MissingContractAttachments exception:
java.util.concurrent.ExecutionException: net.corda.core.transactions.MissingContractAttachments: Cannot find contract attachments for [net.corda.finance.contracts.asset.Cash]
Have my NodeDriver's packages set to:
...
withExtraCordappPackagesToScan(
Arrays.asList.of("com.cordatemplate.cordapp, net.corda.finance,
net.corda.finance.contracts.asset")
)
...
But the error remains. Also tried it with:
...
.withExtraCordappPackagesToScan(
Arrays.asList("com.cordatemplate.cordapp,
net.corda.finance, net.corda.finance.contracts.asset,
net.corda.finance.schemas, net.corda.finance.contracts.asset.Cash,
net.corda.finance.contracts.asset.Cash.Commands.Issue")
)
...
Each package needs to be passed as an individual string, rather than a stringified list.
So instead of:
withExtraCordappPackagesToScan(
Arrays.asList.of("com.cordatemplate.cordapp, net.corda.finance,
net.corda.finance.contracts.asset")
)
You would write:
withExtraCordappPackagesToScan(
Arrays.asList.of(
"com.cordatemplate.cordapp",
"net.corda.finance",
"net.corda.finance.contracts.asset")
)
According to the corda documentation, the two main sources of MissingContractAttachments errors is from either not setting up the Cordapp packages in tests or from having the wrong fully qualified contract name.
Since it seems that you have already attempted to include the packages, you likely have the incorrect fully qualified contract name.
From the docs:
For example, you’ve defined MyContract in the package com.mycompany.myapp.contracts, but the fully-qualified contract name you pass to the TransactionBuilder is com.mycompany.myapp.MyContract (instead of com.mycompany.myapp.contracts.MyContract).

How to log the commits between two release tags with 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?

Filter property based searches in Artifactory

I'm looking to use the Artifactory property search
https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-ArtifactSearch%28QuickSearch%29
Currently this will pull json listing any artifact that matches my properties.
"results" : [
{
"uri": "http://localhost:8080/artifactory/api/storage/libs-release-local/org/acme/lib/ver/lib-ver.pom"
},{
"uri": "http://localhost:8080/artifactory/api/storage/libs-release-local/org/acme/lib/ver2/lib-ver2.pom"
}
]
I need to be able to filter the artifacts I get back as i'm only interested in a certain classifier. The GAVC Search has this with &c=classifier
I can do it in code if this isn't possible via the interface
Any help appreciated
Since the release of AQL in Artifactory 3.5, it's now the official and the preferred way to find artifacts.
Here's an example similar to your needs:
items.find
(
{
"$and":[
{"#license":{"$eq":"GPL"}},
{"#version":{"$match":"1.1.*"}},
{"name":{"$match":"*.jar"}}
]
}
)
To run the query in Artifactory, copy the query to a file and name it aql.query
Run the following command from the directory that contains the aql.query file
curl -X POST -uUSER:PASSWORD 'http://HOST:PORT/artifactory/api/search/aql' -Taql.query
Don't forget to replace the templates (USER, PASSWORD,HOST and PORT) to real values.
In the example
The first two criteria are used to filter items by properties.
The third criteria filters items by the artifact name (in our case the artifact name should end with .jar)
For more details on how to write AQL query are in AQL
Old answer
Currently you can't combine the property search with GAVC search.
So you have two options:
Executing one of them (whichever gives you more precise results) and then filter the JSON list on the client by a script
Writing an execution user plugin that will execute the search by using the Searches service and then filter the results on the server side.
Of course, the later is preferable.

jGit CheckoutCommand results in a strange behaviour

I'm trying to use CheckoutCommand the following way:
Git git = new Git(repository);
CheckoutCommand checkoutCommand = git.checkout();
checkoutCommand.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM);
checkoutCommand.setStartPoint("origin/" + branchName);
checkoutCommand.setCreateBranch(true);
checkoutCommand.setForce(true);
checkoutCommand.call();
I tried using SetupUpstreamMode.TRACK as well but it still failed.
This results in a strange behaviour:
the repository contents are deleted and instead a clone from each remote branch is created.
Can you please advise?
You are not setting the name of the branch to create, which should result in an exception on call() (is it possible that you are swallowing that?). Add a call like this:
checkoutCommand.setName(branchName);
See the documentation of CheckoutCommand (the above is mentioned here).
Also note that these calls can be chained, so you could also write it like this:
git.checkout().setCreateBranch(true).setName(branchName) // ...

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