「追加」は、ファイルをインデックスに配置する高レベルの抽象化です。裸のリポジトリではインデックスがないため、機能間の 1 対 1 の対応ではありません。代わりに、新しいコミットでファイルを作成できます。これを行うには、 を使用ObjectInserter
してリポジトリにオブジェクトを追加します (スレッドごとに 1 つにしてください)。次に、次のことを行います。
ファイルの内容を blob としてリポジトリに追加するには、そのバイトを挿入 (またはInputStream
.
を使用して、新しいファイルを含むツリーを作成しますTreeFormatter
。
を使用して、ツリーを指すコミットを作成します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
。