1 #include "clar_libgit2.h"
3 #include "reset_helpers.h"
5 #include "repo/repo_helpers.h"
7 static git_repository
*repo
;
8 static git_object
*target
;
10 void test_reset_soft__initialize(void)
12 repo
= cl_git_sandbox_init("testrepo.git");
15 void test_reset_soft__cleanup(void)
17 git_object_free(target
);
20 cl_git_sandbox_cleanup();
23 static void assert_reset_soft(bool should_be_detached
)
27 cl_git_pass(git_reference_name_to_id(&oid
, repo
, "HEAD"));
28 cl_git_fail(git_oid_streq(&oid
, KNOWN_COMMIT_IN_BARE_REPO
));
29 cl_git_pass(git_revparse_single(&target
, repo
, KNOWN_COMMIT_IN_BARE_REPO
));
31 cl_assert(git_repository_head_detached(repo
) == should_be_detached
);
33 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
35 cl_assert(git_repository_head_detached(repo
) == should_be_detached
);
37 cl_git_pass(git_reference_name_to_id(&oid
, repo
, "HEAD"));
38 cl_git_pass(git_oid_streq(&oid
, KNOWN_COMMIT_IN_BARE_REPO
));
41 void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void)
43 assert_reset_soft(false);
46 void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
48 git_repository_detach_head(repo
);
50 assert_reset_soft(true);
53 void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void)
56 char raw_head_oid
[GIT_OID_HEXSZ
+ 1];
58 cl_git_pass(git_reference_name_to_id(&oid
, repo
, "HEAD"));
59 git_oid_fmt(raw_head_oid
, &oid
);
60 raw_head_oid
[GIT_OID_HEXSZ
] = '\0';
62 cl_git_pass(git_revparse_single(&target
, repo
, raw_head_oid
));
64 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
66 cl_git_pass(git_reference_name_to_id(&oid
, repo
, "HEAD"));
67 cl_git_pass(git_oid_streq(&oid
, raw_head_oid
));
70 void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void)
74 /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
75 cl_git_pass(git_revparse_single(&target
, repo
, "b25fa35"));
77 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
79 cl_assert(git_repository_head_detached(repo
) == false);
80 cl_git_pass(git_reference_name_to_id(&oid
, repo
, "HEAD"));
81 cl_git_pass(git_oid_streq(&oid
, KNOWN_COMMIT_IN_BARE_REPO
));
84 void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
86 /* 53fc32d is the tree of commit e90810b */
87 cl_git_pass(git_revparse_single(&target
, repo
, "53fc32d"));
89 cl_git_fail(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
90 git_object_free(target
);
92 /* 521d87c is an annotated tag pointing to a blob */
93 cl_git_pass(git_revparse_single(&target
, repo
, "521d87c"));
94 cl_git_fail(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
97 void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
101 cl_git_pass(git_revparse_single(&target
, repo
, KNOWN_COMMIT_IN_BARE_REPO
));
103 make_head_unborn(repo
, NON_EXISTING_HEAD
);
105 cl_assert_equal_i(true, git_repository_head_unborn(repo
));
107 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
109 cl_assert_equal_i(false, git_repository_head_unborn(repo
));
111 cl_git_pass(git_reference_lookup(&head
, repo
, NON_EXISTING_HEAD
));
112 cl_assert_equal_i(0, git_oid_streq(git_reference_target(head
), KNOWN_COMMIT_IN_BARE_REPO
));
114 git_reference_free(head
);
117 void test_reset_soft__fails_when_merging(void)
119 git_str merge_head_path
= GIT_STR_INIT
;
121 cl_git_pass(git_repository_detach_head(repo
));
122 cl_git_pass(git_str_joinpath(&merge_head_path
, git_repository_path(repo
), "MERGE_HEAD"));
123 cl_git_mkfile(git_str_cstr(&merge_head_path
), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
125 cl_git_pass(git_revparse_single(&target
, repo
, KNOWN_COMMIT_IN_BARE_REPO
));
127 cl_assert_equal_i(GIT_EUNMERGED
, git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
128 cl_git_pass(p_unlink(git_str_cstr(&merge_head_path
)));
130 git_str_dispose(&merge_head_path
);
133 void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void)
137 git_str merge_head_path
= GIT_STR_INIT
;
139 cl_git_sandbox_cleanup();
141 repo
= cl_git_sandbox_init("mergedrepo");
143 cl_git_pass(git_str_joinpath(&merge_head_path
, git_repository_path(repo
), "MERGE_HEAD"));
144 cl_git_pass(p_unlink(git_str_cstr(&merge_head_path
)));
145 git_str_dispose(&merge_head_path
);
147 cl_git_pass(git_repository_index(&index
, repo
));
148 cl_assert_equal_i(true, git_index_has_conflicts(index
));
149 git_index_free(index
);
151 cl_git_pass(git_repository_head(&head
, repo
));
152 cl_git_pass(git_reference_peel(&target
, head
, GIT_OBJECT_COMMIT
));
153 git_reference_free(head
);
155 cl_assert_equal_i(GIT_EUNMERGED
, git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
158 void test_reset_soft__reflog_is_correct(void)
160 git_annotated_commit
*annotated
;
161 const char *exp_msg
= "checkout: moving from br2 to master";
162 const char *master_msg
= "commit: checking in";
164 reflog_check(repo
, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg
);
165 reflog_check(repo
, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg
);
167 /* Branch not moving, no reflog entry */
168 cl_git_pass(git_revparse_single(&target
, repo
, "HEAD^{commit}"));
169 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
170 reflog_check(repo
, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg
);
171 reflog_check(repo
, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg
);
172 git_object_free(target
);
174 /* Moved branch, expect id in message */
175 exp_msg
= "reset: moving to be3563ae3f795b2b4353bcce3a527ad0a4f7f644";
176 cl_git_pass(git_revparse_single(&target
, repo
, "HEAD~^{commit}"));
177 cl_git_pass(git_reset(repo
, target
, GIT_RESET_SOFT
, NULL
));
178 reflog_check(repo
, "HEAD", 8, "yoram.harmelin@gmail.com", exp_msg
);
179 reflog_check(repo
, "refs/heads/master", 3, NULL
, exp_msg
);
181 /* Moved branch, expect message with annotated string */
182 exp_msg
= "reset: moving to HEAD~^{commit}";
183 cl_git_pass(git_annotated_commit_from_revspec(&annotated
, repo
, "HEAD~^{commit}"));
184 cl_git_pass(git_reset_from_annotated(repo
, annotated
, GIT_RESET_SOFT
, NULL
));
185 reflog_check(repo
, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg
);
186 reflog_check(repo
, "refs/heads/master", 4, NULL
, exp_msg
);
188 git_annotated_commit_free(annotated
);