I created local branch 12345 from origin/master that relevant to gerrit
How I can find the remote name refs/for/master for the local branch 12345
The input is the branch name refs/heads/12345 or part of the string
I calculated it branch Name in :
public String getCurrentBranch() throws GitException {
try {
Ref headRef;
headRef = repository.getRef(Constants.HEAD);
return Repository.shortenRefName(headRef.getLeaf().getName());
} catch (IOException e) {
throw new GitException(e.getMessage(), e);
}
}
How I Can find the result of the remote refs/for/master to res/heads/12345 ?
Do I need to do it from the config ?
You've cross-tagged this as both git and jgit (and then also java, which seems completely inappropriate here, but I'll leave that to others) and I can only give a pure-git answer, which may be unhelpful.
In Git, a branch—by which I mean the branch name, not the structure—can have at most one upstream set. When a branch has an upstream set, the branch is said to track the upstream. This upstream is normally a remote-tracking branch name, e.g., master may track origin/master. The name origin/master is a remote-tracking branch, so we say that "branch master tracks remote-tracking branch origin/master": this has far too many occurrences of the words "branch" and "tracks", each with a different meaning, but we're kind of stuck with it.
A branch that tracks another branch need not track (i.e., have as an upstream) a remote-tracking branch at all. For instance, local branch feature/zorg might track local branch develop. The only things that having an upstream does for you are to automate more of fetch, merge, and rebase (and hence pull which is just fetch followed by one of the other two), and give you more information when running git branch -v (or -vv, etc) and git status. Of course, these are fairly significant, and hence are a reason to set an upstream.
The upstream—the name of the other branch that the given branch is tracking—is set in two parts: a remote, which is the name of a remote if the upstream is a remote-tracking branch, and a branch (name). If the remote is set to ., the branch is tracking another local branch.
These two parts may be obtained individually with git config or git config --get (both do the same thing):
$ git config --get branch.master.remote
origin
$ git config --get branch.master.merge
refs/heads/master
Note that the second part has not undergone the mapping(s) specified by the fetch rule for the given remote:
$ git config --get-all remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
Since this mapping says that refs/heads/* becomes refs/remotes/origin/*, we can determine that our name for the remote-tracking branch that corresponds to refs/heads/master on remote origin is in fact refs/remotes/origin/master. (Of course, this mapping is pretty standard and some software might just assume it, but it's better practice to actually do the mapping using the setting(s) for remotes.origin.fetch. There may be more than one, hence the --get-all here.)
Again, if the remote part is set to ., the upstream is actually a local branch, and no mapping should be applied to the merge part.
In shell script, then, you could do something like:
branch="${1:-master}"
has_upstream=true
remote="$(git config --get branch.$branch.remote)" || has_upstream=false
merge="$(git config --get branch.$branch.merge)" || has_upstream=false
if ! $has_upstream; then
echo "branch $branch has no upstream"
else
case "$remote" in
.) echo "branch $branch tracks local branch $merge";;
*) echo "branch $branch tracks $merge on $remote";;
esac
fi
This does not help with the mapping, if you'd like to check the remote-tracking branch (you may of course need to run git fetch to update it), but you can get that using git rev-parse:
$ git rev-parse --symbolic-full-name master#{u}
refs/remotes/origin/master
Note that you need to strip refs/remotes/ from the result when it is a remote-tracking branch, if you want to use the short form. Of course if the upstream is a local branch, the full name will begin with refs/heads/ rather than refs/remotes/. If you want the current branch's upstream, a simple #{u} suffices; the name#{u} syntax allows you to find other branches' upstream names.
The #{u} suffix makes rev-parse fail if there is no configured upstream:
$ git rev-parse --symbolic-full-name target#{u}
fatal: no upstream configured for branch 'target'
which offers another way to tell if an upstream is configured.
Sorry to be late, but here you go:
private static Ref findCurrentBranch(Git git) throws IOException, GitAPIException {
String currentBranch = git.getRepository().getFullBranch();
return git.branchList()
.setListMode(ListBranchCommand.ListMode.ALL)
.call()
.stream()
.filter(e -> e.getName().equals(currentBranch))
.findFirst()
.get();
}
private static Optional<Ref> findRemoteBranch(Git git, Ref branch) throws GitAPIException {
Optional<Ref> remoteBranch = git.branchList()
.setListMode(ListBranchCommand.ListMode.REMOTE)
.call()
.stream()
.filter(e -> e.getObjectId().getName().equals(branch.getObjectId().getName()))
.findFirst();
return remoteBranch;
}
Related
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().
I want a way to be able to return "unknown" as my git revision if I have uncommitted changes locally. If not, it should return the correct git revision.
String getGitRevision() {
String gitRevision = "unknown"
try {
def cmd = "git rev-parse HEAD"
def proc = cmd.execute()
gitRevision = proc.text.trim()
}
catch (IOException ioe) {
println "Fetch revision failed."
}
return gitRevision;
}
I expect that when I have uncommitted changes locally, getGitRevision() returns unknown.
But even when I have uncommitted changes, it grabs the correct revision id. I'm not really sure how to add that logic in.
HEAD is parseable as a commit as long as there is a current commit, and there is almost always a current commit. "Uncommitted changes" is not relevant to whether there is a current commit.
To compare the index and work-tree to the current commit, use git status. To do this reliably, in your own software, consider using git --no-optional-locks status --porcelain=v2, perhaps with -z as well, and reading its output, which is shown in the git status documentation. Your code can then choose what to consider "uncommitted changes" (including untracked files, and, if you add --ignored, ignored files).
For this particular case, you can just use git describe --always --dirty and check for the -dirty suffix. Or—since this is what git describe itself does—run git diff-index --quiet HEAD and check its exit status: 0 means "not dirty", 1 means "dirty", anything else means "unable to tell, something went wrong". The method using git status --porcelain... is the most flexible, as it lets you define what you mean by "dirty", but the other two are much simpler.
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
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?
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();