drop.c 3.71 KB
Newer Older
nulltoken committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "clar_libgit2.h"
#include "fileops.h"
#include "stash_helpers.h"

static git_repository *repo;
static git_signature *signature;

void test_stash_drop__initialize(void)
{
	cl_git_pass(git_repository_init(&repo, "stash", 0));
	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
}

void test_stash_drop__cleanup(void)
{
	git_signature_free(signature);
17 18
	signature = NULL;

nulltoken committed
19
	git_repository_free(repo);
20 21
	repo = NULL;

22
	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES));
nulltoken committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36
}

void test_stash_drop__cannot_drop_from_an_empty_stash(void)
{
	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
}

static void push_three_states(void)
{
	git_oid oid;
	git_index *index;

	cl_git_mkfile("stash/zero.txt", "content\n");
	cl_git_pass(git_repository_index(&index, repo));
Edward Thomson committed
37
	cl_git_pass(git_index_add_from_workdir(index, "zero.txt"));
nulltoken committed
38
	commit_staged_files(&oid, index, signature);
39
	cl_assert(git_path_exists("stash/zero.txt"));
nulltoken committed
40 41 42

	cl_git_mkfile("stash/one.txt", "content\n");
	cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
43 44
	cl_assert(!git_path_exists("stash/one.txt"));
	cl_assert(git_path_exists("stash/zero.txt"));
nulltoken committed
45 46 47

	cl_git_mkfile("stash/two.txt", "content\n");
	cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
48 49
	cl_assert(!git_path_exists("stash/two.txt"));
	cl_assert(git_path_exists("stash/zero.txt"));
nulltoken committed
50 51 52

	cl_git_mkfile("stash/three.txt", "content\n");
	cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
53 54
	cl_assert(!git_path_exists("stash/three.txt"));
	cl_assert(git_path_exists("stash/zero.txt"));
nulltoken committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	git_index_free(index);
}

void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
{
	push_three_states();

	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 666));
	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 42));
	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 3));
}

void test_stash_drop__can_purge_the_stash_from_the_top(void)
{
	push_three_states();

	cl_git_pass(git_stash_drop(repo, 0));
	cl_git_pass(git_stash_drop(repo, 0));
	cl_git_pass(git_stash_drop(repo, 0));

	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
}

void test_stash_drop__can_purge_the_stash_from_the_bottom(void)
{
	push_three_states();

	cl_git_pass(git_stash_drop(repo, 2));
	cl_git_pass(git_stash_drop(repo, 1));
	cl_git_pass(git_stash_drop(repo, 0));

	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
}

void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
{
	git_reference *stash;
	git_reflog *reflog;
	const git_reflog_entry *entry;
	git_oid oid;
	size_t count;

	push_three_states();

	cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash"));
101

nulltoken committed
102 103 104
	cl_git_pass(git_reflog_read(&reflog, stash));
	entry = git_reflog_entry_byindex(reflog, 1);

105
	git_oid_cpy(&oid, git_reflog_entry_id_old(entry));
nulltoken committed
106
	count = git_reflog_entrycount(reflog);
107

nulltoken committed
108 109 110 111 112
	git_reflog_free(reflog);

	cl_git_pass(git_stash_drop(repo, 1));

	cl_git_pass(git_reflog_read(&reflog, stash));
113
	entry = git_reflog_entry_byindex(reflog, 0);
nulltoken committed
114

115
	cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_id_old(entry)));
116
	cl_assert_equal_sz(count - 1, git_reflog_entrycount(reflog));
nulltoken committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	git_reflog_free(reflog);

	git_reference_free(stash);
}

void test_stash_drop__dropping_the_last_entry_removes_the_stash(void)
{
	git_reference *stash;

	push_three_states();

	cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash"));
	git_reference_free(stash);

	cl_git_pass(git_stash_drop(repo, 0));
	cl_git_pass(git_stash_drop(repo, 0));
	cl_git_pass(git_stash_drop(repo, 0));

	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&stash, repo, "refs/stash"));
}