Java 在远程位置使用https的git存储库
我需要使用JGit Eclipse API从客户端位置创建远程存储库。我还集成了Apache服务器,以使用HTTPS协议访问存储库。所以,使用HTTPS和JGit,我需要从客户端创建远程存储库 到目前为止,我已经使用本地存储库完成了所有操作,如Java 在远程位置使用https的git存储库,java,git,https,apache2,jgit,Java,Git,Https,Apache2,Jgit,我需要使用JGit Eclipse API从客户端位置创建远程存储库。我还集成了Apache服务器,以使用HTTPS协议访问存储库。所以,使用HTTPS和JGit,我需要从客户端创建远程存储库 到目前为止,我已经使用本地存储库完成了所有操作,如 创建裸存储库 创建本地存储库 打开现有存储库 在本地存储库中添加文件或文件夹 在本地存储库中提交 将数据从本地存储库推送到主存储库 将数据从主存储库拉到本地存储库 相同的源代码: public void createBareRepository(File
public void createBareRepository(File bareRepoLocation) throws IllegalStateException, GitAPIException, IOException {
bareRepoLocation.delete();
Git.init().setBare(true).setDirectory(bareRepoLocation).call();
Repository repository = FileRepositoryBuilder.create(new File(bareRepoLocation.getAbsolutePath(), Constants.DOT_GIT));
repository.close();
}
public void createLocalRepository(File localRepoLocation) throws IllegalStateException, GitAPIException, IOException {
localRepoLocation.delete();
Git.init().setBare(false).setDirectory(localRepoLocation).call();
Repository repository = FileRepositoryBuilder.create(new File(localRepoLocation.getAbsolutePath(), Constants.DOT_GIT));
System.out.println("Created a new repository at " + repository.getDirectory());
repository.close();
}
private Repository openRepository(String localRepoLocation) throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File(localRepoLocation)).readEnvironment()
.findGitDir() // scan up the file system tree
.build();
return repository;
}
public void addFilesToLocalRepository(String localRepoLocation, File needToAdd) throws IOException, NoFilepatternException, GitAPIException {
Repository repository = openRepository(localRepoLocation+File.separator+Constants.DOT_GIT);
Git git = new Git(repository);
git.add().addFilepattern(needToAdd.getName()).call();
}
public void commitInLocalRepository(String localRepoLocation, String commitMsg) throws IOException, NoHeadException, NoMessageException, UnmergedPathsException,
ConcurrentRefUpdateException, WrongRepositoryStateException, RejectCommitException, GitAPIException {
Repository repository = openRepository(localRepoLocation+File.separator+Constants.DOT_GIT);
Git git = new Git(repository);
RevCommit commitCommand = git.commit().setMessage(commitMsg).call();
}
public void pushDataToMasterRepository(File currentRepoLocation, String masterRepoLocation, String userName, String password) throws IOException, InvalidRemoteException,
TransportException, GitAPIException {
Repository repository = openRepository(currentRepoLocation.getAbsolutePath() + "/" + Constants.DOT_GIT);
Git git = new Git(repository);
StoredConfig config = repository.getConfig();
config.setString("remote", "origin", "url", masterRepoLocation);
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
// fetch = +refs/heads/*:refs/remotes/origin/*
config.save();
git.push().setProgressMonitor(new TextProgressMonitor()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password)).call();
}
public void pullDataFromMasterRepository(File currentRepoLocation, String masterRepoLocation, String userName, String password) throws IOException,
WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException,
NoHeadException, TransportException, GitAPIException, URISyntaxException {
Repository repository = openRepository(currentRepoLocation.getAbsolutePath() + "/" + Constants.DOT_GIT);
StoredConfig config = repository.getConfig();
config.setString("remote", "origin", "url", masterRepoLocation);
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
// fetch = +refs/heads/*:refs/remotes/origin/*
config.save();
Git git = new Git(repository);
PullCommand pull = git.pull();
// pull.setProgressMonitor(new TextProgressMonitor());
PullResult result = pull.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password)).call();
}
使用上面的方法,我可以在本地机器上使用裸存储库和本地存储库执行所有操作
我面临的问题是:如果我能够使用HTTPS将数据推送到现有的远程存储库(这意味着我能够在该目录中写入),那么为什么我不能在远程位置创建新的存储库呢。我需要一个使用HTTPS在远程服务器上创建存储库的代码
在createBareRepository方法中需要进行哪些类型的更改
如果您有其他解决方案,请告诉我
请写下你迄今为止所取得的成就(代码示例,…),否则没有人愿意帮助你。如果有人知道,请让我帮助。JGit不支持你试图实现的目标。它可以克隆现有的远程存储库,但由于它在本地运行,因此无法初始化远程存储库。您需要创建一个可以在远程服务器上运行的web服务。然后,该web服务可以使用JGit创建本地存储库(使用您在问题中发布的代码)。