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: https://www.eclipse.org/lists/egit-dev/msg04624.html 
The conclusion is LFS doesn't support HTTPS authentication yet: https://www.eclipse.org/lists/egit-dev/msg04630.html

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 */
    /*
    Git.cloneRepository()
            .setURI(repositoryUrl)
            .setCredentialsProvider(credentialsProvider)
            .setDirectory(folderPath.toFile())
            .setBranchesToClone(Arrays.asList("refs/heads/" + branchName))
            .setBranch("refs/heads/" + branchName)
            .call();
     */

    BuiltinLFS.register();

    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");

        config.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
                ConfigConstants.CONFIG_SECTION_LFS,
                ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
        config.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
                ConfigConstants.CONFIG_SECTION_LFS,
                ConfigConstants.CONFIG_KEY_REQUIRED, true);

        config.save();

        git.fetch()
                .setRemote(repositoryUrl)
                .setRefSpecs(new RefSpec("+refs/heads/" + branchName + ":refs/remotes/origin/" + branchName))
                .setCredentialsProvider(credentialsProvider)
                .call();

        git.checkout()
                .setCreateBranch(true)
                .setName(branchName)
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
                .setStartPoint("origin/" + branchName)
                .call();

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




Back to the top