3

、、およびその他すべてのようなgit 配管コマンド ( git book の第 9 章で使用されているコマンドなど) を使用する必要があります。JGit でこれらを行うための優れた API はありますか (見つけられないようです)、または出力ストリームまたはファイルから blob への書き込みなどの基本的なことをどのように行いますか / git コマンドの代わりに何を使用しますか?git hash-objectgit write-treegit commit-tree

4

4 に答える 4

3

JGit APIへようこそ。パッケージ内の高レベルの磁器 API を除いてorg.eclipse.jgit.api、低レベル API は、ネイティブ git の配管コマンドと密接に関連して構築されていません。これは、JGit が Java ライブラリであり、コマンド ライン インターフェイスではないためです。

例が必要な場合は、まず 2000 を超える JGit テスト ケースを参照してください。次に、EGit が JGit をどのように使用しているかを調べます。これで解決しない場合は、戻ってきて、より具体的な質問をしてください。

于 2012-08-23T18:34:08.043 に答える
1

git ハッシュオブジェクト <ファイル>

import java.io.*;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectInserter.Formatter;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;

public class GitHashObject {
    public static void main(String[] args) throws IOException {
        File file = File.createTempFile("foobar", ".txt");
        FileOutputStream out = new FileOutputStream(file);
        out.write("foobar\n".getBytes());
        out.close();
        System.out.println(gitHashObject(file));
    }

    public static String gitHashObject(File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        Formatter formatter = new ObjectInserter.Formatter();
        ObjectId objectId = formatter.idFor(OBJ_BLOB, file.length(), in);
        in.close();
        return objectId.getName(); // or objectId.name()
    }
}

予想される出力: 323fae03f4606ea9991df8befbb2fca795e648fa (Git を使用しない Git SHA1 の割り当て
で説明)

于 2013-11-05T13:11:05.403 に答える
1

ある場合は、JGitにあるはずです。

たとえば、DirCacheオブジェクト (つまり、Git インデックス) にはWriteTree 関数があります。

/**
 * Write all index trees to the object store, returning the root tree.
 *
 * @param ow
 *   the writer to use when serializing to the store. The caller is
 *   responsible for flushing the inserter before trying to use the
 *   returned tree identity.
 * @return identity for the root tree.
 * @throws UnmergedPathException
 *   one or more paths contain higher-order stages (stage > 0),
 *   which cannot be stored in a tree object.
 * @throws IllegalStateException
 *   one or more paths contain an invalid mode which should never
 *   appear in a tree object.
 * @throws IOException
 *   an unexpected error occurred writing to the object store.
 */
public ObjectId writeTree(final ObjectInserter ow)
于 2012-07-12T09:25:40.843 に答える