JGit commit problem on Android
I am trying to use the JGit library to开发者_JAVA百科 synch documents on my android phone to a server.
It almost works... but I can not get the commit to work. Whenever I try a commit I get an exception "data error". I tracked down the problem to the following statements in org.eclipse.jgit.util.IO:
public static void readFully(final InputStream fd, final byte[] dst,
int off, int len) throws IOException {
while (len > 0) {
final int r = fd.read(dst, off, len);
if (r <= 0)
throw new EOFException(JGitText.get().shortReadOfBlock);
off += r;
len -= r;
}
}
The line fd.read(dst, off, len) will throw the execption. I think the fd parameter is an InflaterInputStream so this code tries to read from a compressed archive.
A am trying to run this on an HTC Desire Z with Android 2.2.
I am using org.eclipse.jgit-0.12.1.jar and com.jcraft.jsch_0.1.31.jar to access the jgit functionality.
I have done some test examples to try to understand the problem
The following code runs without problems on my Windows XP machine:
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
// Create a new directory
File dirF = new File("C:\\Test\\TestDir");
dirF.mkdir();
log(">>> Created directory.\n");
// Initialize git repository
InitCommand init = Git.init();
File initFile = new File("C:\\Test\\TestDir");
init.setDirectory(initFile);
init.call();
log(">>> Git Init done.\n");
// Create a file
File newfile = new File("C:\\Test\\TestDir\\myfile.txt");
newfile.createNewFile();
PrintStream os = new PrintStream(newfile);
os.println("Some text");
os.close();
log(">>> File created.\n");
// Add to git
FileRepositoryBuilder builder = new FileRepositoryBuilder();
File f = new File("C:\\Test\\TestDir\\.git");
Repository db = builder.setGitDir(f)
.findGitDir() // scan up the file system tree
.build();
Git git = new Git(db);
AddCommand add = git.add();
add.addFilepattern(".").call();
log(">>> Git Add done.\n");
// Commit the change
CommitCommand commit = git.commit();
commit.setAll(true);
commit.setMessage("A JGit message");
commit.call();
log(">>> Git Commit done.\n");
// Check the log
for (RevCommit c : git.log().call()) {
log(c.getId() + "/" + c.getAuthorIdent().getName() + "/"
+ c.getShortMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void log(String s) {
System.out.print(s);
}
}
The following code will throw an exception on my android phone:
public class JGitSimpleAndroidActivity extends Activity {
StringBuilder sb;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sb = new StringBuilder();
tv = new TextView(this);
try {
// Create a new directory
File sdCard;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
sdCard = Environment.getExternalStorageDirectory();
} else {
log(">>> SD card is not available.");
return;
}
File dirF = new File(sdCard,"TestDir");
dirF.mkdir();
log(">>> Created directory.\n");
// Initialize git repository
InitCommand init = Git.init();
init.setDirectory(dirF);
init.call();
log(">>> Git Init done.\n");
// Create a file
File newfile = new File(dirF,"myfile.txt");
newfile.createNewFile();
PrintStream os = new PrintStream(newfile);
os.println("Some text");
os.close();
log(">>> File created.\n");
// Add to git
FileRepositoryBuilder builder = new FileRepositoryBuilder();
File f = new File(dirF,".git");
Repository db = builder.setGitDir(f)
.findGitDir() // scan up the file system tree
.build();
Git git = new Git(db);
AddCommand add = git.add();
add.addFilepattern(".").call();
log(">>> Git Add done.\n");
// Commit the change
CommitCommand commit = git.commit();
commit.setAll(true);
commit.setMessage("A JGit message");
commit.call(); // >>>>>>>> EXCEPTION THROWN HERE <<<<<<<
log(">>> Git Commit done.\n");
// Check the log
for (RevCommit c : git.log().call()) {
log(c.getId() + "/" + c.getAuthorIdent().getName() + "/"
+ c.getShortMessage());
}
} catch (Exception e) {
log(e.getMessage());
}
}
private void log(String s) {
sb.append(s);
sb.append('\n');
tv.setText(sb);
setContentView(tv);
}
}
Any ideas of what I can do to make this work?
Regards, Anders
Cool!
Try this patch: (it's probably white-space damaged, so applying it as-is may not work)
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java index 4b9c572..4e38747 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java @@ -214,7 +214,7 @@ public static void readFully(final InputStream fd, final byte[] dst,
int off, int len) throws IOException {
while (len > 0) {
final int r = fd.read(dst, off, len);
- if (r <= 0)
+ if (r < 0)
throw new EOFException(JGitText.get().shortReadOfBlock);
off += r;
len -= r;
@@ -242,7 +242,7 @@ public static void skipFully(final InputStream fd, long toSkip)
throws IOException {
while (toSkip > 0) {
final long r = fd.skip(toSkip);
- if (r <= 0)
+ if (r < 0)
throw new EOFException(JGitText.get().shortSkipOfBlock);
toSkip -= r;
}
精彩评论