Clone一个git仓库到本地,使用Java从Azure DevOps。

huangapple go评论89阅读模式
英文:

Clone a git repository to local from Azure devops using java

问题

使用以下代码片段从 ADO 克隆存储库。

File file = new File("本地文件夹路径");
CredentialsProvider cp = new UsernamePasswordCredentialsProvider("用户名", "密码");
try {
    Git.cloneRepository()
       .setURI("存储库路径")
       .setCredentialsProvider(cp)
       .setDirectory(file)
       .call();
} catch (GitAPIException e) {
    e.printStackTrace();
}

如果我们只尝试克隆存储库一次,它能够正常工作。第二次尝试时,会抛出如下错误:

Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: 目标路径"path"已经存在且不是空目录
	at org.eclipse.jgit.api.CloneCommand.verifyDirectories(CloneCommand.java:253)
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:189)

请提供一个解决方案:

  1. 如果本地不存在存储库,则像上面的代码一样克隆它。
  2. 如果存储库已经存在,仅拉取最新的更改。

如果在 Java 中还有其他解决方案(任何其他 API 等),也请提供。

谢谢

英文:

Used the following piece of code to clone the repo from 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. 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.

Thanks

答案1

得分: 1

请检查您的文件夹是否存在,如果存在,请使用 pull 命令

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

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

pullCommand.call();
英文:

Please check if your folder exists and if then use pull command

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

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

pullCommand.call();

答案2

得分: 1

根据git clone命令,如果要克隆到一个已存在的目录,该目录必须是空的,因此您不能第二次克隆该仓库。

关于您的第2个请求:“如果仓库已经存在,只拉取最新的更改。”,您应该使用git pull而不是git clone

英文:

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.

Regarding your 2# request "If it is already existing, pull the latest changes only.", you should use git pull instead of git clone.

答案3

得分: 1

如果您想尝试使用 git24j,您可以按照以下步骤操作:

String urlThatNeedsAuth = "https://github.com/a-private-repo.git";
File localDir = new File("/tmp/localPath");
// libgit2 通过回调设置凭证
Clone.Options opts = Clone.Options.defaultOpts();
opts.getFetchOpts()
    .getCallbacks()
    .setCredAcquireCb(
        (url, usernameFromUrl, allowedTypes) ->
            Credential.userpassPlaintextNew("fake-user", "fake-passwd"));

// 如果本地克隆存在,则拉取;否则进行克隆
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);
}
英文:

If you would like to give git24j a try, you can do this:

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

huangapple
  • 本文由 发表于 2020年9月10日 23:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63832513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定