英文:
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)
请提供一个解决方案:
- 如果本地不存在存储库,则像上面的代码一样克隆它。
- 如果存储库已经存在,仅拉取最新的更改。
如果在 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:
- If the repository is not there in local, Clone it like above.
- 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论