From 2ec3047e180121cebe86415165ab378bab28c08c Mon Sep 17 00:00:00 2001 From: knuthy Date: Sun, 10 Aug 2014 19:39:55 +0100 Subject: added possibility to pull and push after having initialized a repository --- .../main/java/com/zeapo/pwdstore/GitHandler.java | 34 +++++++++++++++++----- .../java/com/zeapo/pwdstore/PasswordStore.java | 1 + .../zeapo/pwdstore/utils/PasswordRepository.java | 30 +++++++++++++++++-- 3 files changed, 56 insertions(+), 9 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java index fe6cdb27..aed6b776 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -36,6 +36,7 @@ import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.SshSessionFactory; @@ -432,11 +433,20 @@ public class GitHandler extends Activity { }) .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(""))) - .pull() - .setRebase(true) - .setCredentialsProvider(provider)); + .pull() + .setRebase(true) + .setRemote("origin") + .setCredentialsProvider(provider)); + } } @@ -463,10 +473,20 @@ public class GitHandler extends Activity { }) .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(""))) - .push() - .setCredentialsProvider(provider)); + .push() + .setPushAll() + .setRemote("origin") + .setCredentialsProvider(provider)); + } } /** Finds the method and provides it with authentication paramters via invokeWithAuthentication */ diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 72df9ba8..6d51858b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -27,6 +27,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.SshSessionFactory; diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java index 1ff90436..1c238f95 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -2,13 +2,18 @@ package com.zeapo.pwdstore.utils; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.lib.Repository; 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.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; import static java.util.Collections.sort; @@ -56,9 +61,30 @@ public class PasswordRepository { } } - // TODO add remote branch - public static void addRemote() { + // TODO add remote edition later-on + // TODO add multiple remotes support for pull/push + public static void addRemote(String name, String url) { + StoredConfig storedConfig = repository.getConfig(); + Set 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() { -- cgit v1.2.3