]>
Commit | Line | Data |
---|---|---|
edebceff | 1 | #include "clar_libgit2.h" |
632d8b23 | 2 | #include "posix.h" |
edebceff | 3 | #include "reset_helpers.h" |
632d8b23 | 4 | #include "path.h" |
c436ed26 | 5 | #include "repo/repo_helpers.h" |
edebceff | 6 | |
7 | static git_repository *repo; | |
8 | static git_object *target; | |
9 | ||
10 | void test_reset_soft__initialize(void) | |
11 | { | |
12 | repo = cl_git_sandbox_init("testrepo.git"); | |
13 | } | |
14 | ||
15 | void test_reset_soft__cleanup(void) | |
16 | { | |
17 | git_object_free(target); | |
9094d30b SC |
18 | target = NULL; |
19 | ||
edebceff | 20 | cl_git_sandbox_cleanup(); |
21 | } | |
22 | ||
23 | static void assert_reset_soft(bool should_be_detached) | |
24 | { | |
25 | git_oid oid; | |
26 | ||
2508cc66 | 27 | cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); |
edebceff | 28 | cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); |
0d847a31 | 29 | cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); |
edebceff | 30 | |
31 | cl_assert(git_repository_head_detached(repo) == should_be_detached); | |
32 | ||
23a17803 | 33 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
edebceff | 34 | |
35 | cl_assert(git_repository_head_detached(repo) == should_be_detached); | |
36 | ||
2508cc66 | 37 | cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); |
edebceff | 38 | cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); |
39 | } | |
40 | ||
41 | void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) | |
42 | { | |
43 | assert_reset_soft(false); | |
44 | } | |
45 | ||
edebceff | 46 | void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) |
47 | { | |
4e498646 | 48 | git_repository_detach_head(repo); |
edebceff | 49 | |
50 | assert_reset_soft(true); | |
51 | } | |
52 | ||
53 | void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) | |
54 | { | |
55 | git_oid oid; | |
56 | char raw_head_oid[GIT_OID_HEXSZ + 1]; | |
57 | ||
2508cc66 | 58 | cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); |
edebceff | 59 | git_oid_fmt(raw_head_oid, &oid); |
60 | raw_head_oid[GIT_OID_HEXSZ] = '\0'; | |
61 | ||
0d847a31 | 62 | cl_git_pass(git_revparse_single(&target, repo, raw_head_oid)); |
edebceff | 63 | |
23a17803 | 64 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
edebceff | 65 | |
2508cc66 | 66 | cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); |
edebceff | 67 | cl_git_pass(git_oid_streq(&oid, raw_head_oid)); |
68 | } | |
69 | ||
70 | void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) | |
71 | { | |
72 | git_oid oid; | |
73 | ||
74 | /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ | |
0d847a31 | 75 | cl_git_pass(git_revparse_single(&target, repo, "b25fa35")); |
edebceff | 76 | |
23a17803 | 77 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
edebceff | 78 | |
79 | cl_assert(git_repository_head_detached(repo) == false); | |
2508cc66 | 80 | cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); |
edebceff | 81 | cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); |
82 | } | |
83 | ||
84 | void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) | |
85 | { | |
86 | /* 53fc32d is the tree of commit e90810b */ | |
0d847a31 | 87 | cl_git_pass(git_revparse_single(&target, repo, "53fc32d")); |
edebceff | 88 | |
23a17803 | 89 | cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
edebceff | 90 | git_object_free(target); |
91 | ||
92 | /* 521d87c is an annotated tag pointing to a blob */ | |
0d847a31 | 93 | cl_git_pass(git_revparse_single(&target, repo, "521d87c")); |
23a17803 | 94 | cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
edebceff | 95 | } |
c436ed26 | 96 | |
605da51a | 97 | void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void) |
c436ed26 | 98 | { |
99 | git_reference *head; | |
100 | ||
0d847a31 | 101 | cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); |
c436ed26 | 102 | |
605da51a | 103 | make_head_unborn(repo, NON_EXISTING_HEAD); |
c436ed26 | 104 | |
605da51a | 105 | cl_assert_equal_i(true, git_repository_head_unborn(repo)); |
c436ed26 | 106 | |
23a17803 | 107 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
c436ed26 | 108 | |
605da51a | 109 | cl_assert_equal_i(false, git_repository_head_unborn(repo)); |
c436ed26 | 110 | |
111 | cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD)); | |
2508cc66 | 112 | cl_assert_equal_i(0, git_oid_streq(git_reference_target(head), KNOWN_COMMIT_IN_BARE_REPO)); |
c436ed26 | 113 | |
114 | git_reference_free(head); | |
115 | } | |
632d8b23 ET |
116 | |
117 | void test_reset_soft__fails_when_merging(void) | |
118 | { | |
e579e0f7 | 119 | git_str merge_head_path = GIT_STR_INIT; |
632d8b23 | 120 | |
4e498646 | 121 | cl_git_pass(git_repository_detach_head(repo)); |
e579e0f7 MB |
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"); | |
632d8b23 | 124 | |
0d847a31 | 125 | cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); |
03bdb2ad | 126 | |
23a17803 | 127 | cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
e579e0f7 | 128 | cl_git_pass(p_unlink(git_str_cstr(&merge_head_path))); |
00e161b9 | 129 | |
e579e0f7 | 130 | git_str_dispose(&merge_head_path); |
632d8b23 | 131 | } |
9a0d5904 | 132 | |
133 | void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void) | |
134 | { | |
135 | git_index *index; | |
136 | git_reference *head; | |
e579e0f7 | 137 | git_str merge_head_path = GIT_STR_INIT; |
9a0d5904 | 138 | |
139 | cl_git_sandbox_cleanup(); | |
140 | ||
141 | repo = cl_git_sandbox_init("mergedrepo"); | |
142 | ||
e579e0f7 MB |
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); | |
9a0d5904 | 146 | |
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); | |
150 | ||
151 | cl_git_pass(git_repository_head(&head, repo)); | |
ac3d33df | 152 | cl_git_pass(git_reference_peel(&target, head, GIT_OBJECT_COMMIT)); |
9a0d5904 | 153 | git_reference_free(head); |
154 | ||
23a17803 | 155 | cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
9a0d5904 | 156 | } |
86746b4b | 157 | |
a5815a2a | 158 | void test_reset_soft__reflog_is_correct(void) |
86746b4b | 159 | { |
a5815a2a CMN |
160 | git_annotated_commit *annotated; |
161 | const char *exp_msg = "checkout: moving from br2 to master"; | |
162 | const char *master_msg = "commit: checking in"; | |
86746b4b | 163 | |
a5815a2a CMN |
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); | |
86746b4b BS |
166 | |
167 | /* Branch not moving, no reflog entry */ | |
168 | cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); | |
23a17803 | 169 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
a5815a2a CMN |
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); | |
86746b4b | 173 | |
a5815a2a CMN |
174 | /* Moved branch, expect id in message */ |
175 | exp_msg = "reset: moving to be3563ae3f795b2b4353bcce3a527ad0a4f7f644"; | |
86746b4b | 176 | cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); |
23a17803 | 177 | cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); |
a5815a2a CMN |
178 | reflog_check(repo, "HEAD", 8, "yoram.harmelin@gmail.com", exp_msg); |
179 | reflog_check(repo, "refs/heads/master", 3, NULL, exp_msg); | |
86746b4b | 180 | |
a5815a2a CMN |
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)); | |
86746b4b | 185 | reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); |
a5815a2a CMN |
186 | reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg); |
187 | ||
188 | git_annotated_commit_free(annotated); | |
86746b4b | 189 | } |