我需要使用JGit Eclipse API从客户端位置创建远程存储库。我还使用HTTPS协议将Apache服务器集成到访问存储库。所以使用HTTPS和JGit我需要从客户端创建远程存储库。
直到现在我已经使用本地存储库完成了所有操作,如
相同的源代码:
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 方法
需要进行哪些类型的更改如果您有任何其他解决方案,请告诉我