Home » Eclipse Projects » EGit / JGit » Simple Git client using JGit has trouble with fetch/pull
Simple Git client using JGit has trouble with fetch/pull [message #1067086] |
Fri, 05 July 2013 18:29 |
Tim Braun Messages: 8 Registered: July 2013 |
Junior Member |
|
|
I use JGit to implement a very simple Git client that is only able to do two things:
- Add a file to a branch in a repository
- Retrieve a file of a branch in a repository.
The client doesn't have to worry about conflicts because it is assumed that files are never modified, only added or deleted.
The code I have for this is below.
My problem is, that the fetch in getFile() does not fetch any updates from the repository. If I replace fetch with pull I get a
org.eclipse.jgit.api.errors.InvalidConfigurationException: No value for key branch.development.merge found in configuration
I am not sure how the configuration is supposed to look like, but after execution of main() it looks as given below, and I would assume that an entry is missing for branch "development"?
Can anybody help?
public class GitClient implements IGitClient{
private String remoteRepositoryUI;
private String localRepositoryPath;
private Git localGit;
private UsernamePasswordCredentialsProvider credentialsProvider;
public GitClient(String remoteRepositoryURI, String localRepositoryPath,
String user, String password) {
this.credentialsProvider = new UsernamePasswordCredentialsProvider(user, password);
this.remoteRepositoryUI = remoteRepositoryURI;
this.localRepositoryPath = localRepositoryPath;
}
public void addFile(String sourceFilePath, String repositoryFilePath, Branch branch, String message) throws Exception {
if(!isInitialized())
initialize(branch);
switchBranch(branch);
FileUtils.copyFile(new File(sourceFilePath),
new File(this.localRepositoryPath + File.separator + repositoryFilePath));
localGit.add().addFilepattern(repositoryFilePath).call();
localGit.commit().setMessage(message).call();
localGit.push().setCredentialsProvider(this.credentialsProvider).call();
}
public File getFile(String repositoryFilePath, Branch branch) throws Exception {
if(!isInitialized())
initialize(branch);
switchBranch(branch);
localGit.fetch().call(); //<-- Problematic part
//localGit.pull().call();
return new File(this.localRepositoryPath + File.separator + repositoryFilePath);
}
private void switchBranch(Branch branch) throws InvalidRemoteException, TransportException, IOException, GitAPIException {
if(!isInitialized())
initialize(branch);
localGit.checkout()
.setName(branch.toString())
.setUpstreamMode(SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + branch.toString()).setForce(true)
.call();
}
private boolean isInitialized() {
return this.localGit != null;
}
private void initialize(Branch branch) throws IOException,
InvalidRemoteException, TransportException, GitAPIException {
File localRepositoryDirectory = new File(this.localRepositoryPath);
boolean localRepositoryExists = localRepositoryDirectory.exists() && localRepositoryDirectory.isDirectory() && localRepositoryDirectory.listFiles().length != 0;
if(!localRepositoryExists) {
Git.cloneRepository()
.setCloneAllBranches(true)
.setBranch("refs/heads/" + branch.toString())
.setURI(this.remoteRepositoryUI)
.setDirectory(new File(this.localRepositoryPath))
.call();
}
this.localGit = Git.open(new File(this.localRepositoryPath));
if(!localRepositoryExists) {
for(Branch aBranch : Branch.values()) {
if(!branch.equals(aBranch)) {
localGit.checkout().setCreateBranch(true)
.setName(aBranch.toString())
.setUpstreamMode(SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + aBranch.toString())
.call();
}
}
}
}
public static void main(String[] args) throws Exception {
GitClient clientA = new GitClient("URLToRepository.git",
"local", "username", "password");
GitClient clientB = new GitClient("URLToRepository.git",
"localB", "username", "password");
clientA.addFile("fileA1", "fileA1", Branch.master, "clientA1");
clientB.addFile("fileB1", "fileB1", Branch.master, "clientB1");
clientB.addFile("fileB2", "fileB2", Branch.development, "clientB2");
clientA.addFile("fileA2", "fileA2", Branch.development, "clientA2");
clientA.addFile("fileA3", "fileA3", Branch.master, "clientA3");
File file = clientA.getFile("fileA1", Branch.master);
System.out.println(file.getAbsolutePath() + " " + file.exists());
file = clientA.getFile("fileA2", Branch.development);
System.out.println(file.getAbsolutePath() + " " + file.exists());
file = clientA.getFile("fileA3", Branch.master);
System.out.println(file.getAbsolutePath() + " " + file.exists());
file = clientA.getFile("fileB1", Branch.master);
System.out.println(file.getAbsolutePath() + " " + file.exists());
file = clientA.getFile("fileB2", Branch.development);
System.out.println(file.getAbsolutePath() + " " + file.exists());
}
}
[core]
repositoryformatversion = 0
filemode = false
logallrefupdates = true
[remote "origin"]
url = URLToRepository.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
|
|
|
Goto Forum:
Current Time: Wed Sep 25 18:20:41 GMT 2024
Powered by FUDForum. Page generated in 0.04971 seconds
|