Blame tests/stash/drop.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "stash_helpers.h"
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *repo;
Packit Service 20376f
static git_signature *signature;
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_repository_init(&repo, "stash", 0));
Packit Service 20376f
	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_signature_free(signature);
Packit Service 20376f
	signature = NULL;
Packit Service 20376f
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
	repo = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__cannot_drop_from_an_empty_stash(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void push_three_states(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("stash/zero.txt", "content\n");
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, repo));
Packit Service 20376f
	cl_git_pass(git_index_add_bypath(index, "zero.txt"));
Packit Service 20376f
	cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
Packit Service 20376f
	cl_assert(git_path_exists("stash/zero.txt"));
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("stash/one.txt", "content\n");
Packit Service 20376f
	cl_git_pass(git_stash_save(
Packit Service 20376f
		&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
Packit Service 20376f
	cl_assert(!git_path_exists("stash/one.txt"));
Packit Service 20376f
	cl_assert(git_path_exists("stash/zero.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("stash/two.txt", "content\n");
Packit Service 20376f
	cl_git_pass(git_stash_save(
Packit Service 20376f
		&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
Packit Service 20376f
	cl_assert(!git_path_exists("stash/two.txt"));
Packit Service 20376f
	cl_assert(git_path_exists("stash/zero.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("stash/three.txt", "content\n");
Packit Service 20376f
	cl_git_pass(git_stash_save(
Packit Service 20376f
		&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
Packit Service 20376f
	cl_assert(!git_path_exists("stash/three.txt"));
Packit Service 20376f
	cl_assert(git_path_exists("stash/zero.txt"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
Packit Service 20376f
{
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 666), GIT_ENOTFOUND);
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 42), GIT_ENOTFOUND);
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 3), GIT_ENOTFOUND);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__can_purge_the_stash_from_the_top(void)
Packit Service 20376f
{
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__can_purge_the_stash_from_the_bottom(void)
Packit Service 20376f
{
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 2));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 1));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *stash;
Packit Service 20376f
	git_reflog *reflog;
Packit Service 20376f
	const git_reflog_entry *entry;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	size_t count;
Packit Service 20376f
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
Packit Service 20376f
	entry = git_reflog_entry_byindex(reflog, 1);
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&oid, git_reflog_entry_id_old(entry));
Packit Service 20376f
	count = git_reflog_entrycount(reflog);
Packit Service 20376f
Packit Service 20376f
	git_reflog_free(reflog);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 1));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
Packit Service 20376f
	entry = git_reflog_entry_byindex(reflog, 0);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_oid(&oid, git_reflog_entry_id_old(entry));
Packit Service 20376f
	cl_assert_equal_sz(count - 1, git_reflog_entrycount(reflog));
Packit Service 20376f
Packit Service 20376f
	git_reflog_free(reflog);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(stash);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__dropping_the_last_entry_removes_the_stash(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *stash;
Packit Service 20376f
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE));
Packit Service 20376f
	git_reference_free(stash);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail_with(
Packit Service 20376f
		git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE), GIT_ENOTFOUND);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void retrieve_top_stash_id(git_oid *out)
Packit Service 20376f
{
Packit Service 20376f
	git_object *top_stash;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&top_stash, repo, "stash@{0}"));
Packit Service 20376f
	cl_git_pass(git_reference_name_to_id(out, repo, GIT_REFS_STASH_FILE));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_oid(out, git_object_id(top_stash));
Packit Service 20376f
Packit Service 20376f
	git_object_free(top_stash);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void)
Packit Service 20376f
{
Packit Service 20376f
	git_object *next_top_stash;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
Packit Service 20376f
	push_three_states();
Packit Service 20376f
Packit Service 20376f
	retrieve_top_stash_id(&oid;;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}"));
Packit Service 20376f
	cl_assert(git_oid_cmp(&oid, git_object_id(next_top_stash)));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_stash_drop(repo, 0));
Packit Service 20376f
Packit Service 20376f
	retrieve_top_stash_id(&oid;;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_oid(&oid, git_object_id(next_top_stash));
Packit Service 20376f
Packit Service 20376f
	git_object_free(next_top_stash);
Packit Service 20376f
}