Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to clone a single branch in JGit
How to clone a single branch in JGit [message #1691173] Thu, 02 April 2015 19:28 Go to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
Hello,

Is it possible in JGit to clone a single branch?

I am using CloneCommand.setBranchesToClone() to specify my branch but I still get all the branches history cloned!!

I tried CloneCommand.setCloneAllBranches(false) but it didn't help.

Thanks,
Re: How to clone a single branch in JGit [message #1691608 is a reply to message #1691173] Wed, 08 April 2015 13:00 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
Any thoughts?!
Re: How to clone a single branch in JGit [message #1691770 is a reply to message #1691608] Thu, 09 April 2015 14:37 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
You can specify to clone only one branch. Commits done on other branches are not included during the transport. But for the branch which you want to clone the complete history of that branch is transported. Means: the commit the branch is pointing to and all it's predecessor branches are transported. See my example code

			Git r = Git.cloneRepository().setDirectory(tmpDir)
					.setURI("https://github.com/chalstrick/dondalfi.git").setBranchesToClone(Arrays.asList("refs/heads/test")).setNoCheckout(true)
					.call();
			for (Ref f : r.branchList().setListMode(ListMode.ALL).call())
				System.out.println("found branch " + f.getName());

Afterwards the cloned repository knows only about one branch "remotes/origin/test" . If in the cloned repository you say "git show 228ae29" then you will not find anything although 228ae29 is the id of the tip of master on the remote repository


Ciao
Chris
Re: How to clone a single branch in JGit [message #1691955 is a reply to message #1691770] Fri, 10 April 2015 19:55 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
I was doing
setBranchesToClone(Arrays.asList("test")
instead of
setBranchesToClone(Arrays.asList("refs/heads/test")

When JGit doesn't find a branch "test" on the remote side, it clones everything.

I don't know if it makes sense to you guys if JGit would fail or transform "test" to "refs/heads/test", instead of cloning all the branches silently??

Thanks,
Re: How to clone a single branch in JGit [message #1692918 is a reply to message #1691955] Mon, 20 April 2015 15:11 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
The javadoc of the method CloneCommand.setBranchesToClone() explicitly tells that you should the full ref name and even gives an example. That's a quite low-level operation not visible in native gits "clone" command. In my eyes it's ok that it requires full refnames and not their abbreviated forms.

Ciao
Chris
icon4.gif  Re: How to clone a single branch in JGit [message #1694504 is a reply to message #1692918] Tue, 05 May 2015 17:06 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
On the same flow of idea, now that I have a single-branched clone, when I pull, all the remote branches are fetched, and I don't have a single-branched repo anymore!

I found out the cause is that CloneCommand sets the following fetch ref spec, regardless of whether this is a single branch clone or not:

fetch = +refs/heads/*:refs/remotes/origin/*


This is actually not consistent with native git where if you create a single branch clone you get the expected:

fetch = +refs/heads/master:refs/remotes/origin/master


Subsequently, if you do a pull, only the changes in your branch are fetched, which is the desired behavior.

If you agree with this, CloneCommand needs to be fixed. What do you think?
Re: How to clone a single branch in JGit [message #1694586 is a reply to message #1694504] Wed, 06 May 2015 13:15 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
I think you are right. CloneCommand should be fixed.

A small problem is that CloneCommand and "git clone" offer different functioniality in this area. "git clone" offers the "--branch" (aka "-b") and the "--single-branch" options. "--branch" is no problem: CloneCommand.setBranch does the same. But JGit offers the "setBranchesToClone" method which allows to fetch a set of remote branches. That doesn't exist in native git where you have "--single-branch" and can specify at most one remote branch to clone. Whats definitly wrong with JGit is that if setBranchesToClone() is used to calculate the refspecs for the initial fetch that this refspec isn't stored in remote.<name>.fetch. If this would be fixed then a method "setSingleBranch(String name)" could be introduced which forwards to "setBranchesToClone". Do you want to contribute?


Ciao
Chris
Re: How to clone a single branch in JGit [message #1694873 is a reply to message #1694586] Fri, 08 May 2015 15:42 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
I can contribute. Bug created https://bugs.eclipse.org/bugs/show_bug.cgi?id=466858
Re: How to clone a single branch in JGit [message #1694893 is a reply to message #1694873] Fri, 08 May 2015 18:59 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
Could you please fix EGit.setup? The api baseline uri is erronuous!
Re: How to clone a single branch in JGit [message #1695136 is a reply to message #1694893] Tue, 12 May 2015 08:55 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
what is EGit.setup ?
Re: How to clone a single branch in JGit [message #1702650 is a reply to message #1695136] Thu, 23 July 2015 18:23 Go to previous messageGo to next message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
Sorry I missed on your question because I was not subscribed to the topic Sad
EGit.setup is the file used to provision an IDE in order to contribute to the project. Now it seems to be fine Smile

[Updated on: Thu, 23 July 2015 18:33]

Report message to a moderator

Re: How to clone a single branch in JGit [message #1702651 is a reply to message #1694586] Thu, 23 July 2015 18:33 Go to previous message
Julian Enoch is currently offline Julian EnochFriend
Messages: 24
Registered: January 2014
Junior Member
Christian Halstrick wrote on Wed, 06 May 2015 09:15
I think you are right. CloneCommand should be fixed.
...
Whats definitly wrong with JGit is that if setBranchesToClone() is used to calculate the refspecs for the initial fetch that this refspec isn't stored in remote.<name>.fetch.


A fix for this has now been submitted in https://bugs.eclipse.org/bugs/show_bug.cgi?id=466858


Quote:
... a method "setSingleBranch(String name)" could be introduced which forwards to "setBranchesToClone"...


This can be considered as an enhancement, although IMHO it is not necessary, as Jgit is right now just more flexible in the way to clone a single branch or multiple branches, using the same method setBranchesToClone, which would not nave been made possible on a line command with git.
Previous Topic:cannot open git-receive-pack: Server redirected too many times (20)
Next Topic:NPE on checkout if there are two branches on the commit
Goto Forum:
  


Current Time: Fri Apr 26 07:08:23 GMT 2024

Powered by FUDForum. Page generated in 0.04305 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top