Unable to clone local directory in to remote github repository using jgit
i need to create a github directory in my local machine and i'm trying to clone it with my remote github repository.I tried in a lot of ways to clone a repo with jGit using the below code:
package github;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
import java.io.IOException;
public class PushToRemoteRepository {
private static final String REMOTE_URL = "https://github.com/syndy1989/Modify.git";
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("TestGitRepository", "");
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
UsernamePasswordCredentialsProvider upc = new UsernamePasswordCredentialsProvider("syndy1989", "xxxxx");
// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
Git result = Git.cloneRepository()
.setURI(REMOTE_URL)
.setDirectory(localPath)
.setCredentialsProvider(upc)
.call(); {
System.out.println(result);
// now open the created repository
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(localPath)
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build(); {
Git git = new Git(repository);{
git.push()
.call();
}
System.out.println("Pushed from repository: " + repository.getDirectory() + " to remote repository at " + REMOTE_URL);
}
}
}
}
i get shown the below error:
Cloning from https://github.com/syndy1989/Modify.git to C:UsersADMINI~1AppDataLocalTemp2TestGitRepository9138453641649961455
org.eclipse.jgit.api.Git@255b53dc
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: origin: not found.
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:171)
at github.PushToRemoteRepository.main(PushToRemoteRepository.java:47)
Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: origin: not found.
at org.eclipse.jgit.transport.TransportLocal$1.open(TransportLocal.java:131)
at org.eclipse.jgit.transport.TransportBundleFile$1.open(TransportBundleFile.java:106)
at org.eclipse.jgit.transport.Transport.open(Transport.java:556)
at org.eclipse.jgit.transport.Transport.openAll(Transport.java:374)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:144)
... 1 more
I saw some examples using JGit and some other references like http://www.codeaffine.com/2015/11/30/jgit-clone-repository/ https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CloneRemoteRepository.java
Jar's used:
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>corg.eclipse.jgit</artifactId>
<version>3.4.0.201406041058-rc3</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>javax.validation</artifactId>
<version>1.0.0.GA</version>
</dependency>
raw.json file(located in "C:Gitraw.json" folder inside docker container)All i'm trying to do is to clone Git directory in to remote github repository to push my raw.json file using java code in an automated way. Please help me with the working example to create a git directory on local machine, connecting to remote git hub repository & pushing the .json file in to remote repository using java. Thanks in advance
链接地址: http://www.djcxy.com/p/45226.html上一篇: 随机'关注'文件夹和'.keep'文件