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

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);
18 19
	signature = NULL;

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

23
	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES));
nulltoken committed
24 25 26 27
}

void test_stash_drop__cannot_drop_from_an_empty_stash(void)
{
28
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
nulltoken committed
29 30 31 32 33 34 35 36 37
}

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));
38
	cl_git_pass(git_index_add_bypath(index, "zero.txt"));
39
	cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
40
	cl_assert(git_path_exists("stash/zero.txt"));
41
	git_index_free(index);
nulltoken committed
42 43

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

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

	cl_git_mkfile("stash/three.txt", "content\n");
56 57
	cl_git_pass(git_stash_save(
		&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
58 59
	cl_assert(!git_path_exists("stash/three.txt"));
	cl_assert(git_path_exists("stash/zero.txt"));
nulltoken committed
60 61 62 63 64 65
}

void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
{
	push_three_states();

66 67 68
	cl_git_fail_with(git_stash_drop(repo, 666), GIT_ENOTFOUND);
	cl_git_fail_with(git_stash_drop(repo, 42), GIT_ENOTFOUND);
	cl_git_fail_with(git_stash_drop(repo, 3), GIT_ENOTFOUND);
nulltoken committed
69 70 71 72 73 74 75 76 77 78
}

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));

79
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
nulltoken committed
80 81 82 83 84 85 86 87 88 89
}

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));

90
	cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND);
nulltoken committed
91 92 93 94 95 96 97 98 99 100 101 102
}

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();

103
	cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE));
104

105
	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
nulltoken committed
106 107
	entry = git_reflog_entry_byindex(reflog, 1);

108
	git_oid_cpy(&oid, git_reflog_entry_id_old(entry));
nulltoken committed
109
	count = git_reflog_entrycount(reflog);
110

nulltoken committed
111 112 113 114
	git_reflog_free(reflog);

	cl_git_pass(git_stash_drop(repo, 1));

115
	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
116
	entry = git_reflog_entry_byindex(reflog, 0);
nulltoken committed
117

118
	cl_assert_equal_oid(&oid, git_reflog_entry_id_old(entry));
119
	cl_assert_equal_sz(count - 1, git_reflog_entrycount(reflog));
nulltoken committed
120 121 122 123 124 125 126 127 128 129 130 131

	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();

132
	cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE));
nulltoken committed
133 134 135 136 137 138
	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));

139 140
	cl_git_fail_with(
		git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE), GIT_ENOTFOUND);
nulltoken committed
141
}
142 143 144

void retrieve_top_stash_id(git_oid *out)
{
145
	git_object *top_stash;
146

147
	cl_git_pass(git_revparse_single(&top_stash, repo, "stash@{0}"));
148 149
	cl_git_pass(git_reference_name_to_id(out, repo, GIT_REFS_STASH_FILE));

150
	cl_assert_equal_oid(out, git_object_id(top_stash));
151 152

	git_object_free(top_stash);
153 154 155 156
}

void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void)
{
157
	git_object *next_top_stash;
158 159 160 161 162 163
	git_oid oid;

	push_three_states();

	retrieve_top_stash_id(&oid);

164
	cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}"));
165
	cl_assert(git_oid_cmp(&oid, git_object_id(next_top_stash)));
166 167 168 169 170

	cl_git_pass(git_stash_drop(repo, 0));

	retrieve_top_stash_id(&oid);

171
	cl_assert_equal_oid(&oid, git_object_id(next_top_stash));
172 173

	git_object_free(next_top_stash);
174
}