I am using Jgit 3.7 for importing files from Git repository. But I would like to import only set of folders instead of all. I know Git supports this, but I would like to know Jgit 3.7 supports the same? If so, can someone guide me.
Asked
Active
Viewed 1,514 times
4
-
What have you tried so far? What is the native Git command that you would like to emulate? – Rüdiger Herrmann Apr 08 '15 at 07:00
-
@Rudiger. Thanks for for your quick reply. I have tried below command for getting files from git repository. 'Git.cloneRepository().setURI(uri).setDirectory(new File(dir)).call()'. This command download all the source files. But I am not sure, how to import particular directory and its subdirectories and files? – cooltech Apr 08 '15 at 07:09
2 Answers
5
By design, a cloned git repository always contains all files and folders of the original repository.
With native git, you can create a shallow clone (git clone --depth 1 ...
) but this feature is not yet implemented in JGit.
EDIT 2023-06-13:
As of version 6.3 JGit
supports shallow fetch and clone.
Contradicting its general design, native git (since version 1.7) lets you create partial clones with sparse checkouts but this is also not possible out-of-the-box in JGit.
What you can do in JGit though - once you have cloned a repository - is to checkout only some files of a branch or commit.
git.checkout().setStartPoint("some-branch").addPath("path/to/file").call()

Rüdiger Herrmann
- 20,512
- 11
- 62
- 79
-
Thanks for clarifying that Jgit is yet to implement the solution for sparse checkouts. Better what I can do is, Clone the whole repository and delete the unwanted directory and its files. – cooltech Apr 09 '15 at 00:09
-
adding specific path to the checkout the commit history is dissociated , so you can't push it back to the original branch and if u create a new named branch . The new branch containing only the few specified files can't be merged back to the main branch as they have different commit history – redzedi Apr 07 '21 at 11:34
-
This is very nice; I used it in https://stackoverflow.com/a/76383350 along with the new JGit support in v6.5.0 for `setDepth(1)`. Note though that when `setDepth(1)` along with `setNoCheckout(true)`, this technique does not seem to work with `setStartPoint("main")` (where `main` is the branch). I suppose because there is no checkout, there is no branch. However `setStartPoint(hash)` and `setStartPoint("HEAD")` work. – Garret Wilson Jun 01 '23 at 15:17
-1
Based on @rüdiger-herrmann's answer:
String url = "https://github.com/renaud/solr_noLenghNormSimilarity";
String hash = "802558f58add3a1f5b33f04254194bd7cd59b27f";
String subPath = "src/org";
String dest = "myclone";
File localPath = new File(dest);
Git gitRepo = Git.cloneRepository().setURI(url).setDirectory(localPath).setNoCheckout(true).call();
gitRepo.checkout().setStartPoint(hash).addPath(subPath).call();
gitRepo.getRepository().close();

Renaud
- 16,073
- 6
- 81
- 79