3

JGit でファイルを追加または更新する簡単な方法は次のとおりです。

  git.add().addFilepattern(file).call()

ただし、ファイルが Git 作業ディレクトリに存在することを前提としています。マルチスレッド設定 (Scala と Akka を使用) を使用している場合、データを JGit に直接書き込み、最初に作業ディレクトリにファイルを書き込む必要がないように、裸のリポジトリでのみ作業する方法はありますか?

ファイルを取得するには、次のように動作するようです:

  git.getRepository().open(objId).getBytes()

ファイルの追加または更新に似たものはありますか?

4

1 に答える 1

3

「追加」は、ファイルをインデックスに配置する高レベルの抽象化です。裸のリポジトリではインデックスがないため、機能間の 1 対 1 の対応ではありません。代わりに、新しいコミットでファイルを作成できます。これを行うには、 を使用ObjectInserterしてリポジトリにオブジェクトを追加します (スレッドごとに 1 つにしてください)。次に、次のことを行います。

  1. ファイルの内容を blob としてリポジトリに追加するには、そのバイトを挿入 (またはInputStream.

  2. を使用して、新しいファイルを含むツリーを作成しますTreeFormatter

  3. を使用して、ツリーを指すコミットを作成しますCommitBuilder

たとえば、ファイルのみを含む新しいコミット (親なし) を作成するには:

ObjectInserter repoInserter = repository.newObjectInserter();
ObjectId blobId;

try
{
    // Add a blob to the repository
    ObjectId blobId = repoInserter.insert(OBJ_BLOB, "Hello World!\n".getBytes());

    // Create a tree that contains the blob as file "hello.txt"
    TreeFormatter treeFormatter = new TreeFormatter();
    treeFormatter.append("hello.txt", FileMode.TYPE_FILE, blobId);
    ObjectId treeId = treeFormatter.insertTo(repoInserter);

    // Create a commit that contains this tree
    CommitBuilder commit = new CommitBuilder();
    PersonIdent ident = new PersonIdent("Me", "me@example.com");
    commit.setCommitter(ident);
    commit.setAuthor(ident);
    commit.setMessage("This is a new commit!");
    commit.setTreeId(treeId);

    ObjectId commitId = repositoryInserter.insert(commit);

    repoInserter.flush();
}
finally
{
    repoInserter.release();
}

git checkoutこれで、コミット ID を として返すことができますcommitId

于 2013-05-17T13:47:37.257 に答える