Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List branches of a git remote repo without cloning it

Tags:

java

jgit

I was wondering hows it would be possible to list all the branches of a remote Git repo with jgit but without cloning it.

While going through the jgit's javadoc, I found the ListBranchCommand but that only seem to work with an already opened Repository object. But I was not able to find how to create a Repository object over HTTP without cloning it in local.

Is it possible ? Thanks

like image 470
Augier Avatar asked Mar 25 '14 10:03

Augier


People also ask

How do I see remote files in git?

You can list all references on remote with "git ls-remote <URL>". "git archive --remote=<URL> HEAD". "git clone --depth=1 <URL>". If server provides git web interface to repository, you can use it to browse.

How do I track a remote branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

Does cloning a repo clone all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.


2 Answers

There is the LsRemoteCommand to list the branches of a remote repository. To obtain the command, use either

Git.wrap( repo ).lsRemote()

or

Git.lsRemoteRepository()

The statically created LsRemoteCommand has its limitations. For certain transport protocols, a local repository is necessary to obtain configuration settings. Therefore I usually create an empty temporary local repository with Git.init() and then use the first approach.

If you want to avoid creating the extra repository, you can test with Transport.open() if it succeeds without a repository. It throws a NotSupportedException otherwise.

like image 55
Rüdiger Herrmann Avatar answered Sep 29 '22 04:09

Rüdiger Herrmann


Ok, never mind, after hours searching for the answer, I find it right before asking it there...

So, the answer is in the CookBook :

Collection<Ref> refs = Git.lsRemoteRepository()
            .setHeads(true)
            .setTags(true)
            .setRemote(REMOTE_URL)
            .call();
like image 43
Augier Avatar answered Sep 29 '22 03:09

Augier