added possibility to pull and push after having initialized a repository

This commit is contained in:
knuthy 2014-08-10 19:39:55 +01:00
parent bee81ba5f2
commit 2ec3047e18
3 changed files with 56 additions and 9 deletions

View file

@ -36,6 +36,7 @@ import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshSessionFactory;
@ -432,12 +433,21 @@ public class GitHandler extends Activity {
}) })
.show(); .show();
else else {
// check that the remote origin is here, else add it
PasswordRepository.addRemote("origin", settings.getString("git_remote_username", "user")
+ "@" +
settings.getString("git_remote_server", "server.com")
+ ":" +
settings.getString("git_remote_location", "path/to/repository"));
new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File(""))) new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File("")))
.pull() .pull()
.setRebase(true) .setRebase(true)
.setRemote("origin")
.setCredentialsProvider(provider)); .setCredentialsProvider(provider));
} }
}
public void pushOperation(UsernamePasswordCredentialsProvider provider) { public void pushOperation(UsernamePasswordCredentialsProvider provider) {
@ -463,11 +473,21 @@ public class GitHandler extends Activity {
}) })
.show(); .show();
else else {
// check that the remote origin is here, else add it
PasswordRepository.addRemote("origin", settings.getString("git_remote_username", "user")
+ "@" +
settings.getString("git_remote_server", "server.com")
+ ":" +
settings.getString("git_remote_location", "path/to/repository"));
new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File(""))) new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File("")))
.push() .push()
.setPushAll()
.setRemote("origin")
.setCredentialsProvider(provider)); .setCredentialsProvider(provider));
} }
}
/** Finds the method and provides it with authentication paramters via invokeWithAuthentication */ /** Finds the method and provides it with authentication paramters via invokeWithAuthentication */
private void authenticateAndRun(String operation) { private void authenticateAndRun(String operation) {

View file

@ -27,6 +27,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshSessionFactory;

View file

@ -2,13 +2,18 @@ package com.zeapo.pwdstore.utils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import static java.util.Collections.sort; import static java.util.Collections.sort;
@ -56,9 +61,30 @@ public class PasswordRepository {
} }
} }
// TODO add remote branch // TODO add remote edition later-on
public static void addRemote() { // TODO add multiple remotes support for pull/push
public static void addRemote(String name, String url) {
StoredConfig storedConfig = repository.getConfig();
Set<String> remotes = storedConfig.getSubsections("remote");
if (!remotes.contains(name)) {
try {
URIish uri = new URIish(url);
RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/"+ name + "/*");
RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name);
remoteConfig.addFetchRefSpec(refSpec);
remoteConfig.addPushRefSpec(refSpec);
remoteConfig.addURI(uri);
remoteConfig.addPushURI(uri);
remoteConfig.update(storedConfig);
storedConfig.save();
} catch (Exception e) {
}
}
} }
public static void closeRepository() { public static void closeRepository() {