libgit2: git checkout <hash>

Viewed 51

How to make similar on git checkout hash via libgit2? I was trying as below:

git_checkout_options opts;
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
opts.paths.strings = "*";
opts.paths.count = 1;
...
git_checkout_tree(repo, (const git_object *) commit, &opts);
...

It has no effect. Files in repo stay in final state.

1 Answers

Answer: Initialize opts structure with GIT_CHECKOUT_OPTIONS_INIT as:

git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

Reason: A struct in libgit2 like git_checkout_options has a member variable unsigned int version for a validation. Uninitialized struct causes error on the validation with the macro GIT_ERROR_CHECK_VERSION which is called inside of git_checkout_tree().

Related