git_stash_save()
允许保存更改,类似于git stash
. 回退有什么功能git stash pop
吗?
我可以看到git_stash_foreach()
和git_stash_drop()
。有没有办法使用它们来实现这个功能?
编辑:根据nulltoken 的回答,我预计以下代码可以工作:
void tstStashPop ( const char * repo_path )
{
git_repository *repo;
git_commit * top_cmt;
git_oid saved_stash;
git_tree * top_tree;
git_signature *signature;
// open a repository
if ( git_repository_open(&repo, repo_path) != 0 )
{
assert(false);
}
else
{
// create a signature
git_signature_new(&signature, "no name", "no.name@gmail.com", 1323847743, 60);
if ( git_stash_save( &saved_stash, repo, signature,
"message for this stash", /*GIT_STASH_INCLUDE_UNTRACKED*/0)
!= GIT_ENOTFOUND )
{
// get the commit that was saved by git stash save
if ( git_commit_lookup( &top_cmt, repo, &saved_stash ) != 0 )
{
assert(false);
}
else
{
// get the tree for this commit
if ( git_commit_tree( &top_tree, top_cmt ) != 0 )
{
assert(false);
}
else
{
// checkout the tree
git_checkout_opts opts;
opts = GIT_CHECKOUT_OPTS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
if ( git_checkout_tree( repo, (git_object*)top_tree, &opts ) != 0 )
{
assert(false);
}
}
}
// remove the stashed commit
git_stash_drop( repo, 0 );
}
// free signature
git_signature_free(signature);
// free repo
git_repository_free(repo);
}
}
没有报告错误,但未恢复更改。git_stash_save()
有效(我可以看到消息git stash list
)并且git_stash_drop()
也有效。但是,git_checkout_tree()
不会产生任何效果。
另外,我应该免费top_tree
吗top_cmt
?