Jonse Jonse
Jonse Jonse

Reputation: 65

How to Commit files by creating a memory index in libgit2?

Create your own memory index and use it instead of the index of the repository. but I can't initialize it.the error message is could not initialize index entry, Index is not backed up by an existing repository,do something like this:

...
git_repository_open(&repo, ...); // same as before
git_index_new(&index); // create in-memory index
git_revparse_single(headTree, repo, "HEAD^{tree}");  // get head Tree
git_index_read_tree(index, headTree); // initialize to the current HEAD
git_index_add_by_path(index, "1.txt"); //update the nominated file(s).but it not work 
git_index_write_tree_to(&oid, index, repo); // write the tree into the repo
git_tree_lookup(&tree, repo, &oid); // same as before
...

Upvotes: 2

Views: 568

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78703

Your in-memory index is not bound to a repository. Therefore you can’t do things that implicitly touch the working directory - because that’s a concept tied to a repository object and there isn’t one for your index.

Thus git_index_add_by_path will fail when operating on an in-memory index. It can’t find that path, it has no concept of where to look for it.

git_index_add_by_path works by reading the file on disk to create a git_index_entry. It’s a convenience function.

Instead, you can use git_index_add with the index entry data.

Upvotes: 3

Related Questions