Skip to content

Instantly share code, notes, and snippets.

@TNick
Created March 30, 2013 15:37
Show Gist options
  • Save TNick/5277132 to your computer and use it in GitHub Desktop.
Save TNick/5277132 to your computer and use it in GitHub Desktop.
git_stash_pop() and git_stash_apply()
int git_stash_apply(
git_repository *repo,
size_t index)
{
git_reference *stash;
const git_reflog_entry *ref_e;
git_reflog *reflog = NULL;
size_t max;
int error;
const git_oid * oidw;
git_commit *p_commits[4] = {NULL};
size_t n_parents = 0;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
return error;
if ((error = git_reflog_read(&reflog, stash)) < 0)
goto cleanup;
max = git_reflog_entrycount(reflog);
if (index > max - 1) {
error = GIT_ENOTFOUND;
giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
goto cleanup;
}
ref_e = git_reflog_entry_byindex(reflog,index);
assert(ref_e!=NULL);
oidw = git_reflog_entry_id_new(ref_e);
if ((error = git_commit_lookup(&p_commits[0], repo, oidw)) < 0)
goto cleanup;
n_parents = 1;
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
n_parents = git_commit_parentcount(p_commits[0]);
assert(n_parents<4);
for ( size_t i = 0; i < n_parents; i++ ) {
if ((error = git_commit_parent(&p_commits[i+1], p_commits[0], i)) < 0)
goto cleanup;
}
if ( p_commits[3] != NULL )
{
if ((error = git_checkout_tree(
repo,(git_object*)p_commits[3], &opts)) < 0)
goto cleanup;
}
if ((error = git_checkout_tree(
repo,(git_object*)p_commits[0], &opts)) < 0)
goto cleanup;
error = 0;
cleanup:
if ( error < 0 )
{
const git_error * error = giterr_last();
qDebug() << error->message;
}
for( size_t i = 0; i < 4; i++ )
git_commit_free(p_commits[i]);
git_reference_free(stash);
git_reflog_free(reflog);
return error;
}
int git_stash_pop(
git_repository *repo,
size_t index)
{
int error;
if ((error = git_stash_apply(repo,index)) < 0)
return error;
return git_stash_drop(repo,index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment