简体   繁体   English

使用 java 从 Azure devops 将 git 存储库克隆到本地

[英]Clone a git repository to local from Azure devops using java

Used the following piece of code to clone the repo from ADO.使用以下代码从 ADO 克隆存储库。

        File file = new File("local_folder_location");
        CredentialsProvider cp = new UsernamePasswordCredentialsProvider("user_id", "password");
        try {
            Git.cloneRepository()
                      .setURI("repo_path")
                      .setCredentialsProvider(cp)
                      .setDirectory(file)
                      .call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }

It is working fine if we try to clone the repo only once.如果我们只尝试克隆 repo 一次,它就可以正常工作。 On the second time it will throw an error like:第二次它会抛出一个错误,如:

Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: Destination path "path" already exists and is not an empty directory
    at org.eclipse.jgit.api.CloneCommand.verifyDirectories(CloneCommand.java:253)
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:189)

Please suggest a solution in which:请提出一个解决方案,其中:

  1. If the repository is not there in local, Clone it like above.如果存储库不在本地,请像上面一样克隆它。
  2. If it is already existing, pull the latest changes only.如果它已经存在,则仅拉取最新的更改。

Also if there's any other solution to this in Java (any other API or something), it would work.此外,如果在 Java(任何其他 API 或其他东西)中有任何其他解决方案,它会起作用。

Thanks谢谢

Please check if your folder exists and if then use pull command请检查您的文件夹是否存在,然后使用pull命令

Git git = Git.open(new File("/path/to/repo/.git"));

PullCommand pullCommand = git.pull();
    pullCommand.setCredentialsProvider(
      new UsernamePasswordCredentialsProvider("user_id", "password" )
    );

pullCommand.call();

According to git clone command, cloning into an existing directory is only allowed if the directory is empty , so you can not clone the repo second time.根据git clone命令,只有当目录为时才允许克隆到现有目录,因此您不能第二次克隆 repo。

Regarding your 2# request "If it is already existing, pull the latest changes only.", you should use git pull instead of git clone .关于您的 2# 请求“如果它已经存在,则仅提取最新的更改。”,您应该使用git pull而不是git clone

If you would like to give git24j a try, you can do this:如果你想尝试一下git24j ,你可以这样做:

String urlThatNeedsAuth = "https://github.com/a-private-repo.git";
File localDir = new File("/tmp/localPath");
// libgit2 sets credential through callbacks
Clone.Options opts = Clone.Options.defaultOpts();
opts.getFetchOpts()
    .getCallbacks()
    .setCredAcquireCb(
        (url, usernameFromUrl, allowedTypes) ->
            Credential.userpassPlaintextNew("fake-user", "fake-passwd"));

// pull if local clone exits, clone otherwise
if (localDir.exists()) {
    Repository repo = Repository.open(localDir.getPath());
    Remote.lookup(repo, "origin").fetch(null, opts.getFetchOpts(), null);
} else {
    Clone.cloneRepo(aUrlThatNeedsAuth, localDir.getPath(), opts);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM