|
@@ -2,16 +2,14 @@ package info.knacki.pass.ui;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
+import android.support.v7.widget.AppCompatTextView;
|
|
|
import android.widget.ProgressBar;
|
|
import android.widget.ProgressBar;
|
|
|
import android.widget.TextView;
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
|
-import java.security.MessageDigest;
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.Stack;
|
|
import java.util.Stack;
|
|
@@ -22,7 +20,9 @@ import info.knacki.pass.R;
|
|
|
import info.knacki.pass.git.GitInterface;
|
|
import info.knacki.pass.git.GitInterface;
|
|
|
import info.knacki.pass.git.GitInterfaceFactory;
|
|
import info.knacki.pass.git.GitInterfaceFactory;
|
|
|
import info.knacki.pass.git.GitLocal;
|
|
import info.knacki.pass.git.GitLocal;
|
|
|
|
|
+import info.knacki.pass.git.GitSha1;
|
|
|
import info.knacki.pass.git.entities.GitObject;
|
|
import info.knacki.pass.git.entities.GitObject;
|
|
|
|
|
+import info.knacki.pass.git.entities.GitRef;
|
|
|
import info.knacki.pass.io.PathUtils;
|
|
import info.knacki.pass.io.PathUtils;
|
|
|
import info.knacki.pass.settings.SettingsManager;
|
|
import info.knacki.pass.settings.SettingsManager;
|
|
|
|
|
|
|
@@ -32,6 +32,16 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
private GitInterface fGitInterfage;
|
|
private GitInterface fGitInterfage;
|
|
|
private GitObject.GitTree fTree;
|
|
private GitObject.GitTree fTree;
|
|
|
|
|
|
|
|
|
|
+ private void onMsg(final String msg) {
|
|
|
|
|
+ GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ TextView logView = findViewById(R.id.logView);
|
|
|
|
|
+ logView.append(msg +"\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
@@ -50,13 +60,7 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
fGitInterfage.FetchTree(new GitInterface.OnStreamResponseListener<GitObject.GitTree>() {
|
|
fGitInterfage.FetchTree(new GitInterface.OnStreamResponseListener<GitObject.GitTree>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onMsg(final String msg) {
|
|
public void onMsg(final String msg) {
|
|
|
- GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void run() {
|
|
|
|
|
- TextView logView = findViewById(R.id.logView);
|
|
|
|
|
- logView.append(msg +"\n");
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ GitPullActivity.this.onMsg(msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -88,64 +92,29 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public String getMd5OfFile(String filePath)
|
|
|
|
|
- {
|
|
|
|
|
- return getMd5OfFile(new File(PathUtils.GetPassDir(this) +filePath));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public String getMd5OfFile(File f)
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- InputStream input = new FileInputStream(f);
|
|
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
|
|
- MessageDigest md5Hash = MessageDigest.getInstance("MD5");
|
|
|
|
|
- int numRead = 0;
|
|
|
|
|
- while (numRead != -1)
|
|
|
|
|
- {
|
|
|
|
|
- numRead = input.read(buffer);
|
|
|
|
|
- if (numRead > 0)
|
|
|
|
|
- md5Hash.update(buffer, 0, numRead);
|
|
|
|
|
- }
|
|
|
|
|
- input.close();
|
|
|
|
|
-
|
|
|
|
|
- byte [] md5Bytes = md5Hash.digest();
|
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
|
- for (byte i: md5Bytes)
|
|
|
|
|
- sb.append(Integer.toString((i & 0xff) + 0x100, 16).substring(1));
|
|
|
|
|
- return sb.toString();
|
|
|
|
|
- }
|
|
|
|
|
- catch(Throwable t) {
|
|
|
|
|
- t.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- return "";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public static String LOCALGIT_HASH_VERSION_FILE;
|
|
public static String LOCALGIT_HASH_VERSION_FILE;
|
|
|
|
|
|
|
|
protected void OnTreeStructureFetched() {
|
|
protected void OnTreeStructureFetched() {
|
|
|
final GitLocal localVersion = new GitLocal(new File(LOCALGIT_HASH_VERSION_FILE));
|
|
final GitLocal localVersion = new GitLocal(new File(LOCALGIT_HASH_VERSION_FILE));
|
|
|
- HashMap<String, GitObject.GitBlob> filesToDownload = new HashMap<>();
|
|
|
|
|
|
|
+ HashMap<String, GitObject.GitBlob> filesToPull = new HashMap<>();
|
|
|
HashMap<String, GitObject.GitBlob> filesToPush = new HashMap<>();
|
|
HashMap<String, GitObject.GitBlob> filesToPush = new HashMap<>();
|
|
|
HashMap<String, GitObject.GitBlob> conflictingFiles = new HashMap<>();
|
|
HashMap<String, GitObject.GitBlob> conflictingFiles = new HashMap<>();
|
|
|
|
|
|
|
|
for (Map.Entry<String, GitObject.GitBlob>i: fTree.FindAllBlobs().entrySet()) {
|
|
for (Map.Entry<String, GitObject.GitBlob>i: fTree.FindAllBlobs().entrySet()) {
|
|
|
- String localHash = localVersion.GetLocalHash(i.getKey());
|
|
|
|
|
- String remoteHash = localVersion.GetRemoteHash(i.getKey());
|
|
|
|
|
- String currentHash = getMd5OfFile(i.getKey());
|
|
|
|
|
|
|
+ String remoteLocalHash = localVersion.GetHash(i.getKey());
|
|
|
|
|
+ String currentHash = GitSha1.getSha1OfFile(PathUtils.GetPassDir(this), i.getKey());
|
|
|
|
|
|
|
|
- if (localHash == null) localHash = "";
|
|
|
|
|
- if (remoteHash == null) remoteHash = "";
|
|
|
|
|
|
|
+ if (remoteLocalHash == null) remoteLocalHash = "";
|
|
|
|
|
|
|
|
- final boolean remoteChanged = !remoteHash.equals(i.getValue().fSha1);
|
|
|
|
|
- final boolean localChanged = !localHash.equals(currentHash);
|
|
|
|
|
|
|
+ final boolean remoteChanged = !remoteLocalHash.equals(i.getValue().GetHash());
|
|
|
|
|
+ final boolean localChanged = !remoteLocalHash.equals(currentHash);
|
|
|
|
|
|
|
|
if (remoteChanged && localChanged) {
|
|
if (remoteChanged && localChanged) {
|
|
|
// Conflict
|
|
// Conflict
|
|
|
conflictingFiles.put(i.getKey(), i.getValue());
|
|
conflictingFiles.put(i.getKey(), i.getValue());
|
|
|
} else if (remoteChanged) {
|
|
} else if (remoteChanged) {
|
|
|
// remote changed
|
|
// remote changed
|
|
|
- filesToDownload.put(i.getKey(), i.getValue());
|
|
|
|
|
|
|
+ filesToPull.put(i.getKey(), i.getValue());
|
|
|
} else if (localChanged) {
|
|
} else if (localChanged) {
|
|
|
// local changed
|
|
// local changed
|
|
|
filesToPush.put(i.getKey(), i.getValue());
|
|
filesToPush.put(i.getKey(), i.getValue());
|
|
@@ -153,26 +122,40 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
}
|
|
}
|
|
|
for (String i: localVersion.FileNames()) {
|
|
for (String i: localVersion.FileNames()) {
|
|
|
if (fTree.GetObjectFullPath(i) == null) {
|
|
if (fTree.GetObjectFullPath(i) == null) {
|
|
|
- final boolean localChanged = !getMd5OfFile(i).equals(localVersion.GetLocalHash(i));
|
|
|
|
|
|
|
+ log.severe("Cannot find obj on remote tree " +i);
|
|
|
|
|
+ final boolean localChanged = !GitSha1.getSha1OfFile(PathUtils.GetPassDir(this), i).equals(localVersion.GetHash(i));
|
|
|
if (localChanged)
|
|
if (localChanged)
|
|
|
conflictingFiles.put(i, null);
|
|
conflictingFiles.put(i, null);
|
|
|
else
|
|
else
|
|
|
- filesToDownload.put(i, null);
|
|
|
|
|
|
|
+ filesToPull.put(i, null);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
// FIXME new file on local
|
|
// FIXME new file on local
|
|
|
|
|
|
|
|
- AskForConflicts(localVersion, conflictingFiles, filesToDownload, filesToPush);
|
|
|
|
|
|
|
+ AskForConflicts(localVersion, conflictingFiles, filesToPull, filesToPush);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void AskForConflicts(GitLocal localVersion, HashMap<String, GitObject.GitBlob> conflicts, HashMap<String, GitObject.GitBlob> filesToDownload, HashMap<String, GitObject.GitBlob> filesToPush) {
|
|
|
|
|
|
|
+ void AskForConflicts(GitLocal localVersion, HashMap<String, GitObject.GitBlob> conflicts, HashMap<String, GitObject.GitBlob> filesToPull, HashMap<String, GitObject.GitBlob> filesToPush) {
|
|
|
// FIXME
|
|
// FIXME
|
|
|
- filesToDownload.putAll(conflicts);
|
|
|
|
|
- SyncFiles(localVersion, filesToDownload, filesToPush);
|
|
|
|
|
|
|
+ filesToPull.putAll(conflicts);
|
|
|
|
|
+ SyncFiles(localVersion, filesToPull, filesToPush);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void RmEmptyDirs(File dir, boolean isRoot) {
|
|
|
|
|
+ File[] content = dir.listFiles();
|
|
|
|
|
+
|
|
|
|
|
+ if (null == content || content.length == 0) {
|
|
|
|
|
+ if (!isRoot)
|
|
|
|
|
+ dir.delete();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (File i: content)
|
|
|
|
|
+ if (i.isDirectory())
|
|
|
|
|
+ RmEmptyDirs(i, false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void SyncFiles(final GitLocal localVersion, final HashMap<String, GitObject.GitBlob> filesToDownload, final HashMap<String, GitObject.GitBlob> filesToPush) {
|
|
|
|
|
- final GitInterface.OnResponseListener<Void> allDone = new GitInterface.OnResponseListener<Void>() {
|
|
|
|
|
|
|
+ void SyncFiles(final GitLocal localVersion, final HashMap<String, GitObject.GitBlob> filesToPull, final HashMap<String, GitObject.GitBlob> filesToPush) {
|
|
|
|
|
+ final GitInterface.OnStreamResponseListener<Void> allDone = new GitInterface.OnStreamResponseListener<Void>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onResponse(Void result) {
|
|
public void onResponse(Void result) {
|
|
|
GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
@@ -183,6 +166,8 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
pg.setMax(1);
|
|
pg.setMax(1);
|
|
|
pg.setProgress(1);
|
|
pg.setProgress(1);
|
|
|
localVersion.Write(new File(LOCALGIT_HASH_VERSION_FILE));
|
|
localVersion.Write(new File(LOCALGIT_HASH_VERSION_FILE));
|
|
|
|
|
+ RmEmptyDirs(new File(PathUtils.GetPassDir(GitPullActivity.this)), true);
|
|
|
|
|
+ fGitInterfage.debugTree(PathUtils.GetPassDir(GitPullActivity.this) +"/", fTree);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -199,9 +184,14 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onMsg(String message) {
|
|
|
|
|
+ GitPullActivity.this.onMsg(message);
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- DownloadBlobs(filesToDownload, localVersion, new GitInterface.OnResponseListener<Void>(){
|
|
|
|
|
|
|
+ DownloadBlobs(filesToPull, localVersion, new GitInterface.OnResponseListener<Void>(){
|
|
|
@Override
|
|
@Override
|
|
|
public void onResponse(Void result) {
|
|
public void onResponse(Void result) {
|
|
|
if (filesToPush.size() > 0)
|
|
if (filesToPush.size() > 0)
|
|
@@ -223,7 +213,7 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
final TextView logView = findViewById(R.id.logView);
|
|
final TextView logView = findViewById(R.id.logView);
|
|
|
- logView.append(blobs.size() +" files to Download\n");
|
|
|
|
|
|
|
+ logView.append(blobs.size() +" files to update locally\n");
|
|
|
for (String i: blobs.keySet())
|
|
for (String i: blobs.keySet())
|
|
|
logView.append(" > " +i +"\n");
|
|
logView.append(" > " +i +"\n");
|
|
|
final Stack<Map.Entry<String, GitObject.GitBlob>> files = new Stack<>();
|
|
final Stack<Map.Entry<String, GitObject.GitBlob>> files = new Stack<>();
|
|
@@ -235,7 +225,6 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
final GitObject.GitBlob blob = files.peek().getValue();
|
|
final GitObject.GitBlob blob = files.peek().getValue();
|
|
|
final GitInterface.OnResponseListener<byte[]> _this = this;
|
|
final GitInterface.OnResponseListener<byte[]> _this = this;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
@@ -278,6 +267,7 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
new File(PathUtils.GetPassDir(this) +filename).delete();
|
|
new File(PathUtils.GetPassDir(this) +filename).delete();
|
|
|
localCache.remove(filename);
|
|
localCache.remove(filename);
|
|
|
DownloadNext(files, localCache, downloader, resp);
|
|
DownloadNext(files, localCache, downloader, resp);
|
|
|
|
|
+ log.info("Removed file " +filename);
|
|
|
} else {
|
|
} else {
|
|
|
fGitInterfage.FetchBlob(files.peek().getValue(), downloader);
|
|
fGitInterfage.FetchBlob(files.peek().getValue(), downloader);
|
|
|
}
|
|
}
|
|
@@ -302,9 +292,22 @@ public class GitPullActivity extends AppCompatActivity {
|
|
|
log.log(Level.SEVERE, e.getMessage(), e);
|
|
log.log(Level.SEVERE, e.getMessage(), e);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- localVersion.SetHashes(filename, blob.GetHash(), getMd5OfFile(f));
|
|
|
|
|
|
|
+ localVersion.SetHash(filename, blob.GetHash());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void PushBlobs(Map<String, GitObject.GitBlob> blobs, GitInterface.OnResponseListener<Void> resp) {
|
|
|
|
|
|
|
+ void PushBlobs(final Map<String, GitObject.GitBlob> blobs, final GitInterface.OnStreamResponseListener<Void> resp) {
|
|
|
|
|
+ GitPullActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ ((AppCompatTextView)(findViewById(R.id.logView))).append("Committing " +blobs.size() +" files\n");
|
|
|
|
|
+ File[] files = new File[blobs.size()];
|
|
|
|
|
+ int i =0;
|
|
|
|
|
+ for (Map.Entry blob: blobs.entrySet()) {
|
|
|
|
|
+ files[i++] = new File(PathUtils.GetPassDir(GitPullActivity.this) +blob.getKey());
|
|
|
|
|
+ }
|
|
|
|
|
+ // public void PushBlobs(final GitRef prev, final Map<String, GitObject.GitBlob> blobs, final File[] files, final GitInterface.OnResponseListener<Void> resp);
|
|
|
|
|
+ fGitInterfage.PushBlobs(PathUtils.GetPassDir(GitPullActivity.this), fTree, blobs, files, resp);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|