JGit - Pushing a branch and add upstream (-u option) - java

In JGit, I search a way to push a branch and add the upstream reference (tracking).
It is the option -u or --set-upstream into the push command.
I don't see a method in the class PushCommand which permits to do this.
Please, could you tell me how I can do this ?
PushCommand pushCommand = git.push()
.setRemote(remoteAlias)
.setRefSpecs(spec);

The JGit PushCommand does not offer this functionality (yet), but you can modify the repository configuration like --set-upstream would.
If you pass a remote alias to setRemote() (like the snippet from the question suggests), you need to set the upstream like so:
StoredConfig config = git.getRepository().getConfig();
config.setString(CONFIG_BRANCH_SECTION, "local-branch", "remote", "remote-alias-name");
config.setString(CONFIG_BRANCH_SECTION, "local-branch", "merge", "refs/heads/name-of-branch-on-remote");
config.save();
This will result in this configuration section
[branch "local-branch"]
remote = remote-alias-name
merge = refs/heads/name-of-branch-on-remote
If the remote hasn't been configured yet (i.e. there is no section [remote "remote-alias-name"], you will also have to create such a section. For example, like this:
config.setString(CONFIG_REMOTE_SECTION, "remote-alias-name", "url", "url-of-remote");
config.setString(CONFIG_REMOTE_SECTION, "remote-alias-name", "fetch", "ref-spec");
Constants are defined in class ConfigConstants.

Related

Jgit - checkout from a specific branch

I am using jgit. I want to create and checkout a new branch. I have seen the git.checkout.createnewbranch command. Is there a way to specify the base branch for the checkout, say "test" (or) we have to checkout,pull "test" first and then checkout/create the new branch from it?
Yes, it's possible:
Git git = Git.cloneRepository()
.setURI("https://github.com/eclipse/jgit.git") // just an example uri
.setDirectory("/path/to/repo")
.setCloneAllBranches(true)
.call();
git.checkout()
.setCreateBranch(true)
.setName("new-branch")
.setStartPoint("<base branch>") // if it's a remote base then use origin/ prefix
.call();
Don't forget to call git.close() or use try-with-resources once you are done with git.

JGit: Can I find out whether or not a particular tag exists in a Git repository without checking it out?

I need to write a piece of code in Java which tests whether or not a particular tag exists in a Git repository.
Most obvious way to do it it this:
git = Git.cloneRepository()
.setURI(gitUri)
.setDirectory(dir)
.call();
git.checkout().setName(String.format("refs/tags/%s", version)).call();
If tag version does not exist, an exception will be thrown.
But this way requires me to have a directory (dir) into which the repository will be checked out.
Is it possible to find out whether or not a tag exists in a remote repository without checking it out on disk? If yes, how can I do it?
The LsRemoteCommand is able to list the refs that a remote repository has.
The command can be configured to include tags like this:
LsRemoteCommand ls = Git.lsRemoteRepository();
Collection<Ref> remoteRefs = ls
.setRemote("url-to-remote-repo")
.setHeads(true) // true by default, set to false if not interested in refs/heads/*
.setTags(true) // include tags in result
.call();
If you prefer to get a map of ref-names to Ref objects, you can execute the command with callAsMap().

Get the default branch of a remote repository with JGit

I am working on a Git client, and right now I am trying to implement the checkout of a specific branch. I have a combo box that I populate with branch names, and I would like to find out which branch is the default, so that I can set it as the preselected item in the combo box when connecting to a valid Git repository.
I am listing all the remote branches as you can see below, but I cannot figure out which is the default one.
Map<String, Ref> callAsMap = Git.lsRemoteRepository()
.setRemote("https://github.com/example")
.setCredentialsProvider(credentialsProvider)
.callAsMap();
So, is there a way (standard or "hacky") to detect which Ref object represents the default branch? And how can I get its name?
Repository::getFullBranch returns the current branch of the local repository.
To get the default branch of a remote repository, you need to ask for its HEAD ref. The map that is returned by the snippet that you posted should contain an entry with key HEAD and (if I'm not mistaken) a value that denotes the name of the default branch.
If HEAD refers to an object id, you could obtain a list of all remote refs with repository.getRefDatabase().getRefs(Constants.R_REMOTES) to look up the HEAD id. This approach may be inaccurate as multiple refs could point to the same object id.
Note that it is not required for a remote repository to advertise a default branch.
See also these posts for how C-Git finds the default branch: git - how to get default branch? and What determines default branch after "git clone"?)
After the chain with .get("HEAD"), if it is a symbolic link, you can chain it with .getTarget().getName() to "extract" its name e.g.
Map<String, Ref> callAsMap = Git.lsRemoteRepository()
.setRemote("https://github.com/example")
.setCredentialsProvider(credentialsProvider)
.callAsMap().get("HEAD").getTarget().getName()
Source: https://www.eclipse.org/lists/jgit-dev/msg03320.html

How To Use JGit To Do Equivalent Of "git push --mirror ..."?

I'm trying to create a simple application that executes a "git push --mirror" operation from the Java domain.
The JGit library, specifically the PushCommand class, doesn't seem to support the "--mirror" option even though it supports "--all" and "--tags".
Am I missing something? How do we use JGit to do "git push --mirror ..."?
Try it manually by using the following ref spec:
git.push().setRefSpecs(new RefSpec("+refs/*:refs/*")).call();
There is no exact equivalent to --mirror in JGit yet, but you should be able to emulate this behaviour. To force-push all local refs you can configure the PushCommand with
PushCommand pushCommand = git.push();
pushCommand.setForce(true);
pushCommand.add("refs/*:refs/*");
That would leave the refs that have been deleted locally. Therefore you can obtain a list of remote-refs to determine what has been deleted locally and publish those deletions to the remote:
Collection<Ref> remoteRefs = git.lsRemote().setRemote("origin").setHeads(true).setTags(true).call();
Collection<String> deletedRefs = new ArrayList<String>();
for (Ref remoteRef : remoteRefs) {
if (git.getRepository().getRef(remoteRef.getName()) == null) {
deletedRefs.add(remoteRef.getName());
}
}
for (String deletedRef : deletedRefs) {
pushCommand.add(":" + deletedRef);
}
The git variable references the repository that you want to push from, i.e. the one from the first block. The LsRemoteCommand returns all heads and tags from the remote repository that is configured as origin in the local repository's configuration. In the usual case, the one you cloned from.
Please note that there is a small gap to the approach how deleted local refs are propagated. The LsRemoteCommand only returns refs under heads and tags (e.g. no custom refs like pulls), hence you would not detect a local deletion of e.g. refs/foo/bar.
Does that work for you?

JGit branch checkout Issue

I am checking out a repository from github using the following code .
private String url = "https://github.com/organization/project.git";
Git repo = Git.cloneRepository().setURI(url).setDirectory(directory).setCloneAllBranches(true).call();
for (Ref b : repo.branchList().call()) {
System.out.println("(standard): cloned branch " + b.getName());
}
i am using the code
Git git = Git.open(checkout); //checkout is the folder with .git
git.pull().call(); //succeeds
If i chekout a branch
Git git = Git.open(new File(checkout)); //checkout is the folder with .git
System.out.println(git.getRepository().getFullBranch());
CheckoutCommand checkout = git.checkout();
Ref call = checkout.setName("kalees").call();
It throws org.eclipse.jgit.api.errors.RefNotFoundException: Ref kalees can not be resolved.
What is the issue here, if i specify "master" instead of "kalees", it works fine. what change should i do to checkout a specific branch?
if i use the code
git.checkout().setCreateBranch(true).setName("refs/remotes/origin/kalees");
It checkout the kalees branch. but when i do pull operation
git.pull().call();
it throws org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is detached. What could be the , whether this is a checkout issue or pull issue ?
It should only happen if:
kalees isn't an existing branch (or is incorrectly written, bad case)
kalees is a remote branch you haven tracked yet a a local branch
If so you might need to create it first (a bit like in this example)
git.branchCreate().setForce(true).setName("kalees").setStartPoint("origin/kalees").call();
Following "JGit: Cannot find a tutorial or simple example", I would rather use:
git.branchCreate()
.setName("kalees")
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint("origin/kalees")
.setForce(true)
.call();
I met this question when I want to create a branch with an empty repository, there is no commit in this repository.
It's resolved when I commit something to the repository. Hope it's helpful for you :)
Muthu your code is working you only need to add origin/branch like this to the branch call
Ref call = checkout.setName("origin/kalees").call();

Categories