Add the possibility to edit git server information

Also uses jgit 3.6
This commit is contained in:
zeapo 2014-12-04 21:43:27 +01:00
parent 173248e1bb
commit d98a30ee48
8 changed files with 127 additions and 52 deletions

View file

@ -4,7 +4,7 @@ apply plugin: 'eclipse'
android { android {
compileSdkVersion 21 compileSdkVersion 21
buildToolsVersion "21.0.1" buildToolsVersion "21.1.1"
defaultConfig { defaultConfig {
applicationId "com.zeapo.pwdstore" applicationId "com.zeapo.pwdstore"
minSdkVersion 15 minSdkVersion 15
@ -25,12 +25,12 @@ android {
} }
dependencies { dependencies {
compile "com.android.support:appcompat-v7:21.+" compile "com.android.support:appcompat-v7:21.0.2"
compile "com.android.support:recyclerview-v7:21.+" compile "com.android.support:recyclerview-v7:21.0.2"
//compile fileTree(dir: 'libs', include: ['*.jar']) //compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':libraries:openpgp-api-lib') compile project(':libraries:openpgp-api-lib')
compile 'org.eclipse.jgit:org.eclipse.jgit:3.5.+' compile 'org.eclipse.jgit:org.eclipse.jgit:3.6.0.201411121045-m1'
compile 'org.apache.commons:commons-io:1.3.2' compile 'org.apache.commons:commons-io:1.3.2'
} }
tasks.findAll { // make all tasks whose name starts with 'assemble'... tasks.findAll { // make all tasks whose name starts with 'assemble'...

View file

@ -12,8 +12,6 @@ import android.support.v7.app.ActionBarActivity;
import android.text.Editable; import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -37,7 +35,6 @@ import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.PullCommand;
import org.eclipse.jgit.api.PushCommand; import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.diff.Edit;
import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem; import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;
@ -52,8 +49,6 @@ import org.eclipse.jgit.util.FS;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -78,6 +73,7 @@ public class GitHandler extends ActionBarActivity {
public static final int REQUEST_PUSH = 102; public static final int REQUEST_PUSH = 102;
public static final int REQUEST_CLONE = 103; public static final int REQUEST_CLONE = 103;
public static final int REQUEST_INIT = 104; public static final int REQUEST_INIT = 104;
public static final int EDIT_SERVER = 105;
private static final int GET_SSH_KEY_FROM_CLONE = 201; private static final int GET_SSH_KEY_FROM_CLONE = 201;
@ -92,9 +88,13 @@ public class GitHandler extends ActionBarActivity {
protocol = settings.getString("git_remote_protocol", "ssh://"); protocol = settings.getString("git_remote_protocol", "ssh://");
connectionMode = settings.getString("git_remote_auth", "ssh-key"); connectionMode = settings.getString("git_remote_auth", "ssh-key");
int operationCode = getIntent().getExtras().getInt("Operation");
switch (getIntent().getExtras().getInt("Operation")) { getSupportActionBar().setDisplayHomeAsUpEnabled(true);
switch (operationCode) {
case REQUEST_CLONE: case REQUEST_CLONE:
case EDIT_SERVER:
setContentView(R.layout.activity_git_clone); setContentView(R.layout.activity_git_clone);
final Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol); final Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol);
@ -245,6 +245,16 @@ public class GitHandler extends ActionBarActivity {
} }
}); });
if (operationCode == EDIT_SERVER)
{
findViewById(R.id.clone_button).setVisibility(View.INVISIBLE);
findViewById(R.id.save_button).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.clone_button).setVisibility(View.VISIBLE);
findViewById(R.id.save_button).setVisibility(View.INVISIBLE);
}
break; break;
case REQUEST_PULL: case REQUEST_PULL:
authenticateAndRun("pullOperation"); authenticateAndRun("pullOperation");
@ -453,6 +463,10 @@ public class GitHandler extends ActionBarActivity {
} }
/**
* Clones the repository, the directory exists, deletes it
* @param view
*/
public void cloneRepository(View view) { public void cloneRepository(View view) {
localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store");
@ -532,8 +546,12 @@ public class GitHandler extends ActionBarActivity {
} }
} }
public void cloneOperation(UsernamePasswordCredentialsProvider provider) {
/**
* Save the repository information to the shared preferences settings
* @param view
*/
public void saveConfiguration(View view) {
// remember the settings // remember the settings
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
@ -545,6 +563,13 @@ public class GitHandler extends ActionBarActivity {
editor.putString("git_remote_port", port); editor.putString("git_remote_port", port);
editor.commit(); editor.commit();
PasswordRepository.addRemote("origin", ((EditText) findViewById(R.id.clone_uri)).getText().toString(), true);
}
public void cloneOperation(UsernamePasswordCredentialsProvider provider) {
saveConfiguration(null);
CloneCommand cmd = Git.cloneRepository(). CloneCommand cmd = Git.cloneRepository().
setCredentialsProvider(provider). setCredentialsProvider(provider).
setCloneAllBranches(true). setCloneAllBranches(true).
@ -584,7 +609,7 @@ public class GitHandler extends ActionBarActivity {
+ "@" + + "@" +
settings.getString("git_remote_server", "server.com").trim() settings.getString("git_remote_server", "server.com").trim()
+ ":" + + ":" +
settings.getString("git_remote_location", "path/to/repository")); settings.getString("git_remote_location", "path/to/repository"), false);
GitCommand cmd; GitCommand cmd;
if (provider != null) if (provider != null)
@ -633,7 +658,7 @@ public class GitHandler extends ActionBarActivity {
+ "@" + + "@" +
settings.getString("git_remote_server", "server.com").trim() settings.getString("git_remote_server", "server.com").trim()
+ ":" + + ":" +
settings.getString("git_remote_location", "path/to/repository")); settings.getString("git_remote_location", "path/to/repository"), false);
GitCommand cmd; GitCommand cmd;
if (provider != null) if (provider != null)

View file

@ -31,6 +31,9 @@ import java.io.InputStream;
import java.net.URI; import java.net.URI;
public class UserPreference extends ActionBarActivity implements Preference.OnPreferenceClickListener { public class UserPreference extends ActionBarActivity implements Preference.OnPreferenceClickListener {
private final static int IMPORT_SSH_KEY = 1;
private final static int IMPORT_PGP_KEY = 2;
private final static int EDIT_GIT_INFO = 3;
public static class PrefsFragment extends PreferenceFragment { public static class PrefsFragment extends PreferenceFragment {
@Override @Override
@ -40,6 +43,7 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr
addPreferencesFromResource(R.xml.preference); addPreferencesFromResource(R.xml.preference);
findPreference("openpgp_key_id").setOnPreferenceClickListener((UserPreference) getActivity()); findPreference("openpgp_key_id").setOnPreferenceClickListener((UserPreference) getActivity());
findPreference("ssh_key").setOnPreferenceClickListener((UserPreference) getActivity()); findPreference("ssh_key").setOnPreferenceClickListener((UserPreference) getActivity());
findPreference("git_server_info").setOnPreferenceClickListener((UserPreference) getActivity());
} }
} }
@ -79,7 +83,7 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr
public void getSshKey() { public void getSshKey() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); intent.setType("*/*");
startActivityForResult(intent, 1); startActivityForResult(intent, IMPORT_SSH_KEY);
} }
@ -92,21 +96,38 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr
@Override @Override
public boolean onPreferenceClick(Preference pref) { public boolean onPreferenceClick(Preference pref) {
if (pref.getKey().equals("openpgp_key_id")) { switch (pref.getKey())
{
case "openpgp_key_id":
{
Intent intent = new Intent(this, PgpHandler.class); Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("Operation", "GET_KEY_ID"); intent.putExtra("Operation", "GET_KEY_ID");
startActivityForResult(intent, 0); startActivityForResult(intent, IMPORT_PGP_KEY);
} else if (pref.getKey().equals("ssh_key")) { }
break;
case "ssh_key":
{
getSshKey(); getSshKey();
} }
break;
case "git_server_info":
{
Intent intent = new Intent(this, GitHandler.class);
intent.putExtra("Operation", GitHandler.EDIT_SERVER);
startActivityForResult(intent, EDIT_GIT_INFO);
}
break;
}
return true; return true;
} }
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,
Intent data) { Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == 1) { switch (requestCode)
// Uri sshFile = data.getData(); {
case IMPORT_SSH_KEY:
{
try { try {
copySshKey(data.getData()); copySshKey(data.getData());
Log.i("PREF", "Got key"); Log.i("PREF", "Got key");
@ -125,6 +146,15 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr
}).show(); }).show();
} }
} }
break;
case EDIT_GIT_INFO:
{
}
break;
default:
break;
}
} }
} }
} }

View file

@ -63,16 +63,15 @@ public class PasswordRepository {
getRepository(localDir); getRepository(localDir);
} }
// TODO add remote edition later-on
// TODO add multiple remotes support for pull/push // TODO add multiple remotes support for pull/push
public static void addRemote(String name, String url) { public static void addRemote(String name, String url, Boolean replace) {
StoredConfig storedConfig = repository.getConfig(); StoredConfig storedConfig = repository.getConfig();
Set<String> remotes = storedConfig.getSubsections("remote"); Set<String> remotes = storedConfig.getSubsections("remote");
if (!remotes.contains(name)) { if (!remotes.contains(name)) {
try { try {
URIish uri = new URIish(url); URIish uri = new URIish(url);
RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/"+ name + "/*"); RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/" + name + "/*");
RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name); RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name);
remoteConfig.addFetchRefSpec(refSpec); remoteConfig.addFetchRefSpec(refSpec);
@ -84,7 +83,29 @@ public class PasswordRepository {
storedConfig.save(); storedConfig.save();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
}
} else if (replace) {
try {
URIish uri = new URIish(url);
RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name);
// remove the first and eventually the only uri
if (remoteConfig.getURIs().size() > 0) {
remoteConfig.removeURI(remoteConfig.getURIs().get(0));
}
if (remoteConfig.getPushURIs().size() > 0) {
remoteConfig.removePushURI(remoteConfig.getPushURIs().get(0));
}
remoteConfig.addURI(uri);
remoteConfig.addPushURI(uri);
remoteConfig.update(storedConfig);
storedConfig.save();
} catch (Exception e) {
e.printStackTrace();
} }
} }
} }

View file

@ -205,6 +205,12 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="cloneRepository"/> android:onClick="cloneRepository"/>
<Button
android:id="@+id/save_button"
android:text="Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="saveConfiguration"/>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View file

@ -93,7 +93,8 @@
<string name="pref_remote_hint">path/to/repository</string> <string name="pref_remote_hint">path/to/repository</string>
<string name="pref_git_username_title">Username</string> <string name="pref_git_username_title">Username</string>
<string name="pref_git_username_hint">username</string> <string name="pref_git_username_hint">username</string>
<string name="pref_ssh_title">SSH Key</string> <string name="pref_edit_server_info">Edit git server settings</string>
<string name="pref_ssh_title">Import ssh-key</string>
<string name="pref_crypto_title">Crypto</string> <string name="pref_crypto_title">Crypto</string>
<string name="pref_provider_title">Select OpenPGP Provider!</string> <string name="pref_provider_title">Select OpenPGP Provider!</string>
<string name="pref_provider_account_title">Set your OpenPGP account</string> <string name="pref_provider_account_title">Set your OpenPGP account</string>

View file

@ -1,15 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_git_title"> <PreferenceCategory android:title="@string/pref_git_title">
<EditTextPreference android:title= "@string/pref_server_title" <Preference android:title="@string/pref_edit_server_info" android:key="git_server_info"/>
android:key="git_remote_server" android:hint="@string/pref_server_hint"
android:inputType="textUri" />
<EditTextPreference android:title="@string/pref_remote_title"
android:key="git_remote_location" android:hint="@string/pref_remote_hint"
android:inputType="textUri" />
<EditTextPreference android:title="@string/pref_git_username_title"
android:key="git_remote_username" android:hint="@string/pref_git_username_hint"
android:inputType="textPersonName" />
<Preference android:title="@string/pref_ssh_title" android:key="ssh_key" /> <Preference android:title="@string/pref_ssh_title" android:key="ssh_key" />
</PreferenceCategory> </PreferenceCategory>

View file

@ -1,6 +1,6 @@
#Tue Nov 25 21:49:03 CET 2014 #Thu Dec 04 19:46:18 CET 2014
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip