Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Cloning LFS repository ignores passed credentials

On 2019-09-11 Jan Tošovský wrote:
> I'm cloning Git repos using this code, however, no LFS content is
> resolved accordingly.
> Git.cloneRepository()
>         .setURI(repositoryUrl)
>         .setCredentialsProvider(new
> UsernamePasswordCredentialsProvider(credentials.getUser(),
> credentials.getPassword()))
>         .setDirectory(folderPath.toFile())
>         .setBranchesToClone(Arrays.asList("refs/heads/" + branchName))
>         .setBranch("refs/heads/" + branchName)
>         .call();
> If native Git+LFS clients are present in the system, the repository is
> cloned until visiting the first LFS item, which triggers the Windows
> Credential window asking to enter remote server (GitLab) credentials.
> I thought native tools are ignored in case of LFS dependency. Anyway,
> if native Git can work with UsernamePasswordCredentialsProvider, why LFS
> doesn't use it as well?
> I am failing to find any documentation to setup LFS correctly. Any
> clarification would be helpful.

It turned out it is necessary to use a special flag to force JGit to use built-in LFS. But when I finally adapted my code, I ended up with another error when connecting to GiLab repository: the server responded with an error code. rc=401

The same error is discussed here: 
The conclusion is LFS doesn't support HTTPS authentication yet:

So built-in LFS cannot be used in my case.

Btw, this is my current code:

private static void cloneRepositoryJGIT(String repositoryUrl, String branchName, Path folderPath, Credentials credentials) throws IOException {

    CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(credentials.getUser(), credentials.getPassword());

    /* this simple method cannot turn off SSL */
            .setBranchesToClone(Arrays.asList("refs/heads/" + branchName))
            .setBranch("refs/heads/" + branchName)


    try (Git git = Git.init().setDirectory(folderPath.toFile()).call()) {

        StoredConfig config = git.getRepository().getConfig();
        config.setBoolean("http", null, "sslVerify", false);

        config.setString(ConfigConstants.CONFIG_SECTION_LFS, null,
                ConfigConstants.CONFIG_KEY_URL, repositoryUrl + "/info/lfs");

                ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
                ConfigConstants.CONFIG_KEY_REQUIRED, true);;

                .setRefSpecs(new RefSpec("+refs/heads/" + branchName + ":refs/remotes/origin/" + branchName))

                .setStartPoint("origin/" + branchName)

    } catch (GitAPIException e) {
        throw new IOException(e);

Back to the top