]> git.proxmox.com Git - libgit2.git/blob - tests/reset/soft.c
aca0834f25a700ffed296aab3426f7ac56bd645b
[libgit2.git] / tests / reset / soft.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "reset_helpers.h"
4 #include "path.h"
5 #include "repo/repo_helpers.h"
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);
18 target = NULL;
19
20 cl_git_sandbox_cleanup();
21 }
22
23 static void assert_reset_soft(bool should_be_detached)
24 {
25 git_oid oid;
26
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));
30
31 cl_assert(git_repository_head_detached(repo) == should_be_detached);
32
33 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
34
35 cl_assert(git_repository_head_detached(repo) == should_be_detached);
36
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));
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
46 void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
47 {
48 git_repository_detach_head(repo);
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
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';
61
62 cl_git_pass(git_revparse_single(&target, repo, raw_head_oid));
63
64 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
65
66 cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
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 */
75 cl_git_pass(git_revparse_single(&target, repo, "b25fa35"));
76
77 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
78
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));
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 */
87 cl_git_pass(git_revparse_single(&target, repo, "53fc32d"));
88
89 cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL));
90 git_object_free(target);
91
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));
95 }
96
97 void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
98 {
99 git_reference *head;
100
101 cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
102
103 make_head_unborn(repo, NON_EXISTING_HEAD);
104
105 cl_assert_equal_i(true, git_repository_head_unborn(repo));
106
107 cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
108
109 cl_assert_equal_i(false, git_repository_head_unborn(repo));
110
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));
113
114 git_reference_free(head);
115 }
116
117 void test_reset_soft__fails_when_merging(void)
118 {
119 git_str merge_head_path = GIT_STR_INIT;
120
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");
124
125 cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
126
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)));
129
130 git_str_dispose(&merge_head_path);
131 }
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;
137 git_str merge_head_path = GIT_STR_INIT;
138
139 cl_git_sandbox_cleanup();
140
141 repo = cl_git_sandbox_init("mergedrepo");
142
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);
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));
152 cl_git_pass(git_reference_peel(&target, head, GIT_OBJECT_COMMIT));
153 git_reference_free(head);
154
155 cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL));
156 }
157
158 void test_reset_soft__reflog_is_correct(void)
159 {
160 git_annotated_commit *annotated;
161 const char *exp_msg = "checkout: moving from br2 to master";
162 const char *master_msg = "commit: checking in";
163
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);
166
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);
173
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);
180
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);
187
188 git_annotated_commit_free(annotated);
189 }