1

我正在尝试使用 JGit 库将我的 android 手机上的文档同步到服务器。

它几乎可以工作......但我无法承诺工作。每当我尝试提交时,我都会收到异常“数据错误”。我将问题追溯到 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;
    }
}

行 fd.read(dst, off, len) 将抛出执行。我认为 fd 参数是 InflaterInputStream 所以这段代码试图从压缩档案中读取。

我试图在装有 Android 2.2 的 HTC Desire Z 上运行它。

我正在使用 org.eclipse.jgit-0.12.1.jar 和 com.jcraft.jsch_0.1.31.jar 来访问 jgit 功能。

我已经做了一些测试示例来尝试理解问题

以下代码在我的 Windows XP 机器上运行没有问题:

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);
}

}

以下代码将在我的 android 手机上引发异常:

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);
}
}

关于我可以做些什么来完成这项工作的任何想法?

问候,安德斯

4

1 回答 1

0

凉爽的!

试试这个补丁:(它可能是空白损坏了,所以按原样应用它可能不起作用)

   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;
                }
于 2011-05-23T17:41:26.043 回答