|
@@ -3,8 +3,6 @@ package info.knacki.pass.git;
|
|
|
import android.os.AsyncTask;
|
|
import android.os.AsyncTask;
|
|
|
import android.util.Base64;
|
|
import android.util.Base64;
|
|
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
|
-import java.io.DataOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
@@ -17,11 +15,11 @@ import java.net.URLConnection;
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.SortedSet;
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Logger;
|
|
|
import java.util.zip.InflaterInputStream;
|
|
import java.util.zip.InflaterInputStream;
|
|
@@ -31,6 +29,7 @@ import info.knacki.pass.git.entities.GitObject;
|
|
|
import info.knacki.pass.git.entities.GitPackable;
|
|
import info.knacki.pass.git.entities.GitPackable;
|
|
|
import info.knacki.pass.git.entities.GitPackableUtil;
|
|
import info.knacki.pass.git.entities.GitPackableUtil;
|
|
|
import info.knacki.pass.git.entities.GitRef;
|
|
import info.knacki.pass.git.entities.GitRef;
|
|
|
|
|
+import info.knacki.pass.io.OutputStreamWithCheckSum;
|
|
|
import info.knacki.pass.settings.SettingsManager;
|
|
import info.knacki.pass.settings.SettingsManager;
|
|
|
|
|
|
|
|
public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
@@ -135,7 +134,7 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
} catch (Throwable e) {
|
|
} catch (Throwable e) {
|
|
|
Map<String, List<String>> headers = in.getHeaderFields();
|
|
Map<String, List<String>> headers = in.getHeaderFields();
|
|
|
lp: for (List<String> i: headers.values()) {
|
|
lp: for (List<String> i: headers.values()) {
|
|
|
- for (String status: (List<String>) i) {
|
|
|
|
|
|
|
+ for (String status: i) {
|
|
|
log.severe(in.getURL().toString() + ": " + status);
|
|
log.severe(in.getURL().toString() + ": " + status);
|
|
|
break lp;
|
|
break lp;
|
|
|
}
|
|
}
|
|
@@ -159,7 +158,7 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
httpClient.setRequestProperty(header.getKey(), header.getValue());
|
|
httpClient.setRequestProperty(header.getKey(), header.getValue());
|
|
|
}
|
|
}
|
|
|
if (fConfig.HasAuthentification()) {
|
|
if (fConfig.HasAuthentification()) {
|
|
|
- httpClient.setRequestProperty("Authorization", "basic " +Base64.encode("fConfig.GetUser():fConfig.GetPassword()".getBytes(), Base64.NO_WRAP));
|
|
|
|
|
|
|
+ httpClient.setRequestProperty("Authorization", "basic " +Base64.encodeToString("fConfig.GetUser():fConfig.GetPassword()".getBytes(), Base64.NO_WRAP));
|
|
|
}
|
|
}
|
|
|
fOnReadyWrite.onResponse(httpClient.getOutputStream());
|
|
fOnReadyWrite.onResponse(httpClient.getOutputStream());
|
|
|
InputStream in = GetInputFilter(httpClient);
|
|
InputStream in = GetInputFilter(httpClient);
|
|
@@ -167,7 +166,7 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
int totalRead = 0;
|
|
int totalRead = 0;
|
|
|
byte[] buffer = new byte[1024];
|
|
byte[] buffer = new byte[1024];
|
|
|
int currentRead;
|
|
int currentRead;
|
|
|
- while ((currentRead = in.read(buffer, 0, 1024)) != 0) {
|
|
|
|
|
|
|
+ while ((currentRead = in.read(buffer, 0, 1024)) > 0) {
|
|
|
fullBuffer.add(buffer);
|
|
fullBuffer.add(buffer);
|
|
|
buffer = new byte[1024];
|
|
buffer = new byte[1024];
|
|
|
totalRead += currentRead;
|
|
totalRead += currentRead;
|
|
@@ -175,15 +174,19 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
in.close();
|
|
in.close();
|
|
|
- buffer = new byte[totalRead];
|
|
|
|
|
- int i =0;
|
|
|
|
|
- for (byte []currentBuf: fullBuffer) {
|
|
|
|
|
- for (int j =0; j < currentBuf.length && i*1024+j < totalRead; ++j) {
|
|
|
|
|
- buffer[i*1024 +j] = currentBuf[j];
|
|
|
|
|
|
|
+ if (totalRead >= 0) {
|
|
|
|
|
+ buffer = new byte[totalRead];
|
|
|
|
|
+ int i = 0;
|
|
|
|
|
+ for (byte[] currentBuf : fullBuffer) {
|
|
|
|
|
+ for (int j = 0; j < currentBuf.length && i * 1024 + j < totalRead; ++j) {
|
|
|
|
|
+ buffer[i * 1024 + j] = currentBuf[j];
|
|
|
|
|
+ }
|
|
|
|
|
+ ++i;
|
|
|
}
|
|
}
|
|
|
- ++i;
|
|
|
|
|
|
|
+ ManageResponse(buffer);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ManageResponse(new byte[] {});
|
|
|
}
|
|
}
|
|
|
- ManageResponse(buffer);
|
|
|
|
|
}
|
|
}
|
|
|
catch (IOException e) {
|
|
catch (IOException e) {
|
|
|
log.log(Level.WARNING, e.getMessage(), e);
|
|
log.log(Level.WARNING, e.getMessage(), e);
|
|
@@ -290,7 +293,7 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
PullHash(ref.GetHash(), new OnResponseListener<byte[]>() {
|
|
PullHash(ref.GetHash(), new OnResponseListener<byte[]>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onResponse(byte[] result) {
|
|
public void onResponse(byte[] result) {
|
|
|
- response.onResponse(new GitCommit(new String(result, Charset.defaultCharset())));
|
|
|
|
|
|
|
+ response.onResponse(new GitCommit(ref.GetHash(), new String(result, Charset.defaultCharset())));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -446,17 +449,22 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
protoInflateGet(url, response);
|
|
protoInflateGet(url, response);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public byte[] makePack(List<GitPackable> objectsToPack) throws IOException
|
|
|
|
|
|
|
+ public boolean makePack(SortedSet<GitPackable> objectsToPack, OutputStream out) throws IOException
|
|
|
{
|
|
{
|
|
|
- ByteArrayOutputStream msg = new ByteArrayOutputStream();
|
|
|
|
|
- msg.write("PACK".getBytes());
|
|
|
|
|
- msg.write(new byte[] { 0, 0, 0, 2 } );
|
|
|
|
|
- msg.write(ByteBuffer.allocate(4).putInt(objectsToPack.size()).array());
|
|
|
|
|
|
|
+ OutputStreamWithCheckSum msg = new OutputStreamWithCheckSum(out);
|
|
|
|
|
+ msg.write("PACK")
|
|
|
|
|
+ .write(new byte[] { 0, 0, 0, 2 } )
|
|
|
|
|
+ .write(ByteBuffer.allocate(4).putInt(objectsToPack.size()).array());
|
|
|
for (GitPackable i: objectsToPack) {
|
|
for (GitPackable i: objectsToPack) {
|
|
|
byte[] pack = i.GetPack();
|
|
byte[] pack = i.GetPack();
|
|
|
- byte[] header = GitPackableUtil.getObjHeader(i.GetPackableType(), 16711680);
|
|
|
|
|
|
|
+ if (null == pack)
|
|
|
|
|
+ return false;
|
|
|
|
|
+ msg.write(GitPackableUtil.getObjHeader(i.GetPackableType(), pack.length))
|
|
|
|
|
+ .write(GitPackableUtil.deflate(pack));
|
|
|
|
|
+ log.severe("Writing pack " +i.GetPackableType() +", " +GitSha1.getSha1OfPackable(i) +new String(i.GetPack()));
|
|
|
}
|
|
}
|
|
|
- return msg.toByteArray();
|
|
|
|
|
|
|
+ msg.writeSha1();
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void PushBlobs(final GitCommit.Builder commit, final GitInterface.OnStreamResponseListener<Void> response) {
|
|
public void PushBlobs(final GitCommit.Builder commit, final GitInterface.OnStreamResponseListener<Void> response) {
|
|
@@ -470,37 +478,35 @@ public class HttpGitProtocol implements GitInterface { // FIXME package-private
|
|
|
}
|
|
}
|
|
|
if (myref != null) {
|
|
if (myref != null) {
|
|
|
final GitRef finalRef = myref;
|
|
final GitRef finalRef = myref;
|
|
|
- response.onMsg("Pushing over " +myref.GetBranchName() +" revision " +myref.GetHash());
|
|
|
|
|
- ByteArrayOutputStream msg = new ByteArrayOutputStream();
|
|
|
|
|
|
|
+ response.onMsg("Pushing over " +myref.GetBranch() +" revision " +myref.GetHash());
|
|
|
|
|
+ log.severe("Pack: " +GitSha1.getSha1OfPackable(commit.Build()) +": " +new String(commit.Build().GetPack()));
|
|
|
try {
|
|
try {
|
|
|
HashMap<String, String> headers = new HashMap<>();
|
|
HashMap<String, String> headers = new HashMap<>();
|
|
|
headers.put("Content-Type", "application/x-git-receive-pack-request");
|
|
headers.put("Content-Type", "application/x-git-receive-pack-request");
|
|
|
ProtoPost(new URL(fConfig.GetUrl() +"/git-receive-pack"), headers, new OnResponseListener<byte[]>() {
|
|
ProtoPost(new URL(fConfig.GetUrl() +"/git-receive-pack"), headers, new OnResponseListener<byte[]>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onResponse(byte[] result) {
|
|
public void onResponse(byte[] result) {
|
|
|
- Logger.getAnonymousLogger().severe(new String(result, Charset.defaultCharset()));
|
|
|
|
|
|
|
+ Logger.getAnonymousLogger().severe("git-receive-pack " +new String(result, Charset.defaultCharset()));
|
|
|
|
|
+ response.onResponse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onError(String msg, Throwable e) {
|
|
public void onError(String msg, Throwable e) {
|
|
|
|
|
+ Logger.getAnonymousLogger().severe("git-receive-pack ERROR");
|
|
|
response.onError(msg, e);
|
|
response.onError(msg, e);
|
|
|
}
|
|
}
|
|
|
}, new OnResponseListener<OutputStream>() {
|
|
}, new OnResponseListener<OutputStream>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onResponse(OutputStream result) {
|
|
public void onResponse(OutputStream result) {
|
|
|
- StringBuilder msgBuilder = new StringBuilder();
|
|
|
|
|
- GitCommit ci = commit.Build();
|
|
|
|
|
- byte[] ciRaw = ci.getRawFormat().getBytes();
|
|
|
|
|
- msgBuilder.append(finalRef.GetHash() + " " + GitSha1.BytesToString(GitSha1.getCommitHash(ci)) +" " +finalRef.GetBranchName());
|
|
|
|
|
- byte[] msgLine = msgBuilder.toString().getBytes();
|
|
|
|
|
|
|
+ byte[] msgLine = (finalRef.GetHash() + " " + GitSha1.BytesToString(GitSha1.getRawSha1OfPackable(commit.Build())) +" " +finalRef.GetBranch()).getBytes();
|
|
|
try {
|
|
try {
|
|
|
- result.write(String.format(Locale.US, "%04d", msgLine.length).getBytes());
|
|
|
|
|
|
|
+ result.write(String.format(Locale.US, "%04X", msgLine.length +4).getBytes());
|
|
|
result.write(msgLine);
|
|
result.write(msgLine);
|
|
|
- result.write("\n0000\n".getBytes());
|
|
|
|
|
-
|
|
|
|
|
- result.write(String.format(Locale.US, "%04d", ciRaw.length).getBytes());
|
|
|
|
|
- result.write(ciRaw);
|
|
|
|
|
- result.write(makePack(commit.PreparePack()));
|
|
|
|
|
|
|
+ result.write("0000".getBytes());
|
|
|
|
|
+ if (!makePack(commit.PreparePack(), result)) {
|
|
|
|
|
+ onError("Pack error", null);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
catch (IOException e) {
|
|
catch (IOException e) {
|
|
|
log.log(Level.SEVERE, "Cannot git-upload-pack: " +e.getMessage(), e);
|
|
log.log(Level.SEVERE, "Cannot git-upload-pack: " +e.getMessage(), e);
|