1 #include "clar_libgit2.h"
3 #include "stash_helpers.h"
6 static git_repository
*repo
;
7 static git_signature
*signature
;
9 void test_stash_drop__initialize(void)
11 cl_git_pass(git_repository_init(&repo
, "stash", 0));
12 cl_git_pass(git_signature_new(&signature
, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
15 void test_stash_drop__cleanup(void)
17 git_signature_free(signature
);
20 git_repository_free(repo
);
23 cl_git_pass(git_futils_rmdir_r("stash", NULL
, GIT_RMDIR_REMOVE_FILES
));
26 void test_stash_drop__cannot_drop_from_an_empty_stash(void)
28 cl_git_fail_with(git_stash_drop(repo
, 0), GIT_ENOTFOUND
);
31 static void push_three_states(void)
36 cl_git_mkfile("stash/zero.txt", "content\n");
37 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_fs_path_exists("stash/zero.txt"));
41 git_index_free(index
);
43 cl_git_mkfile("stash/one.txt", "content\n");
44 cl_git_pass(git_stash_save(
45 &oid
, repo
, signature
, "First", GIT_STASH_INCLUDE_UNTRACKED
));
46 cl_assert(!git_fs_path_exists("stash/one.txt"));
47 cl_assert(git_fs_path_exists("stash/zero.txt"));
49 cl_git_mkfile("stash/two.txt", "content\n");
50 cl_git_pass(git_stash_save(
51 &oid
, repo
, signature
, "Second", GIT_STASH_INCLUDE_UNTRACKED
));
52 cl_assert(!git_fs_path_exists("stash/two.txt"));
53 cl_assert(git_fs_path_exists("stash/zero.txt"));
55 cl_git_mkfile("stash/three.txt", "content\n");
56 cl_git_pass(git_stash_save(
57 &oid
, repo
, signature
, "Third", GIT_STASH_INCLUDE_UNTRACKED
));
58 cl_assert(!git_fs_path_exists("stash/three.txt"));
59 cl_assert(git_fs_path_exists("stash/zero.txt"));
62 void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
66 cl_git_fail_with(git_stash_drop(repo
, 666), GIT_ENOTFOUND
);
67 cl_git_fail_with(git_stash_drop(repo
, 42), GIT_ENOTFOUND
);
68 cl_git_fail_with(git_stash_drop(repo
, 3), GIT_ENOTFOUND
);
71 void test_stash_drop__can_purge_the_stash_from_the_top(void)
75 cl_git_pass(git_stash_drop(repo
, 0));
76 cl_git_pass(git_stash_drop(repo
, 0));
77 cl_git_pass(git_stash_drop(repo
, 0));
79 cl_git_fail_with(git_stash_drop(repo
, 0), GIT_ENOTFOUND
);
82 void test_stash_drop__can_purge_the_stash_from_the_bottom(void)
86 cl_git_pass(git_stash_drop(repo
, 2));
87 cl_git_pass(git_stash_drop(repo
, 1));
88 cl_git_pass(git_stash_drop(repo
, 0));
90 cl_git_fail_with(git_stash_drop(repo
, 0), GIT_ENOTFOUND
);
93 void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
97 const git_reflog_entry
*entry
;
103 cl_git_pass(git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
));
105 cl_git_pass(git_reflog_read(&reflog
, repo
, GIT_REFS_STASH_FILE
));
106 entry
= git_reflog_entry_byindex(reflog
, 1);
108 git_oid_cpy(&oid
, git_reflog_entry_id_old(entry
));
109 count
= git_reflog_entrycount(reflog
);
111 git_reflog_free(reflog
);
113 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);
118 cl_assert_equal_oid(&oid
, git_reflog_entry_id_old(entry
));
119 cl_assert_equal_sz(count
- 1, git_reflog_entrycount(reflog
));
121 git_reflog_free(reflog
);
123 git_reference_free(stash
);
126 void test_stash_drop__dropping_the_last_entry_removes_the_stash(void)
128 git_reference
*stash
;
132 cl_git_pass(git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
));
133 git_reference_free(stash
);
135 cl_git_pass(git_stash_drop(repo
, 0));
136 cl_git_pass(git_stash_drop(repo
, 0));
137 cl_git_pass(git_stash_drop(repo
, 0));
140 git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
), GIT_ENOTFOUND
);
143 static void retrieve_top_stash_id(git_oid
*out
)
145 git_object
*top_stash
;
147 cl_git_pass(git_revparse_single(&top_stash
, repo
, "stash@{0}"));
148 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
));
152 git_object_free(top_stash
);
155 void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void)
157 git_object
*next_top_stash
;
162 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
)));
167 cl_git_pass(git_stash_drop(repo
, 0));
169 retrieve_top_stash_id(&oid
);
171 cl_assert_equal_oid(&oid
, git_object_id(next_top_stash
));
173 git_object_free(next_top_stash
);