1 #include "clar_libgit2.h"
2 #include "git2/rebase.h"
3 #include "merge.h"
4 #include "posix.h"
5 #include "annotated_commit.h"
6 
7 #include <fcntl.h>
8 
9 static git_repository *repo;
10 
11 /* Fixture setup and teardown */
test_rebase_abort__initialize(void)12 void test_rebase_abort__initialize(void)
13 {
14 	repo = cl_git_sandbox_init("rebase");
15 }
16 
test_rebase_abort__cleanup(void)17 void test_rebase_abort__cleanup(void)
18 {
19 	cl_git_sandbox_cleanup();
20 }
21 
ensure_aborted(git_annotated_commit * branch,git_annotated_commit * onto)22 static void ensure_aborted(
23 	git_annotated_commit *branch,
24 	git_annotated_commit *onto)
25 {
26 	git_reference *head_ref, *branch_ref = NULL;
27 	git_status_list *statuslist;
28 	git_reflog *reflog;
29 	const git_reflog_entry *reflog_entry;
30 
31 	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
32 
33 	/* Make sure the refs are updated appropriately */
34 	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
35 
36 	if (branch->ref_name == NULL)
37 		cl_assert_equal_oid(git_annotated_commit_id(branch), git_reference_target(head_ref));
38 	else {
39 		cl_assert_equal_s("refs/heads/beef", git_reference_symbolic_target(head_ref));
40 		cl_git_pass(git_reference_lookup(&branch_ref, repo, git_reference_symbolic_target(head_ref)));
41 		cl_assert_equal_oid(git_annotated_commit_id(branch), git_reference_target(branch_ref));
42 	}
43 
44 	git_status_list_new(&statuslist, repo, NULL);
45 	cl_assert_equal_i(0, git_status_list_entrycount(statuslist));
46 	git_status_list_free(statuslist);
47 
48 	/* Make sure the reflogs are updated appropriately */
49 	cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
50 
51 	cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
52 	cl_assert_equal_oid(git_annotated_commit_id(onto), git_reflog_entry_id_old(reflog_entry));
53 	cl_assert_equal_oid(git_annotated_commit_id(branch), git_reflog_entry_id_new(reflog_entry));
54 	cl_assert_equal_s("rebase: aborting", git_reflog_entry_message(reflog_entry));
55 
56 	git_reflog_free(reflog);
57 	git_reference_free(head_ref);
58 	git_reference_free(branch_ref);
59 }
60 
test_abort(git_annotated_commit * branch,git_annotated_commit * onto)61 static void test_abort(
62 	git_annotated_commit *branch, git_annotated_commit *onto)
63 {
64 	git_rebase *rebase;
65 
66 	cl_git_pass(git_rebase_open(&rebase, repo, NULL));
67 	cl_git_pass(git_rebase_abort(rebase));
68 
69 	ensure_aborted(branch, onto);
70 
71 	git_rebase_free(rebase);
72 }
73 
test_rebase_abort__merge(void)74 void test_rebase_abort__merge(void)
75 {
76 	git_rebase *rebase;
77 	git_reference *branch_ref, *onto_ref;
78 	git_annotated_commit *branch_head, *onto_head;
79 
80 	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
81 	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));
82 
83 	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
84 	cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));
85 
86 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
87 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
88 
89 	test_abort(branch_head, onto_head);
90 
91 	git_annotated_commit_free(branch_head);
92 	git_annotated_commit_free(onto_head);
93 
94 	git_reference_free(branch_ref);
95 	git_reference_free(onto_ref);
96 	git_rebase_free(rebase);
97 }
98 
test_rebase_abort__merge_immediately_after_init(void)99 void test_rebase_abort__merge_immediately_after_init(void)
100 {
101 	git_rebase *rebase;
102 	git_reference *branch_ref, *onto_ref;
103 	git_annotated_commit *branch_head, *onto_head;
104 
105 	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
106 	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));
107 
108 	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
109 	cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));
110 
111 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
112 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
113 
114 	cl_git_pass(git_rebase_abort(rebase));
115 	ensure_aborted(branch_head, onto_head);
116 
117 	git_annotated_commit_free(branch_head);
118 	git_annotated_commit_free(onto_head);
119 
120 	git_reference_free(branch_ref);
121 	git_reference_free(onto_ref);
122 	git_rebase_free(rebase);
123 }
124 
test_rebase_abort__merge_by_id(void)125 void test_rebase_abort__merge_by_id(void)
126 {
127 	git_rebase *rebase;
128 	git_oid branch_id, onto_id;
129 	git_annotated_commit *branch_head, *onto_head;
130 
131 	cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
132 	cl_git_pass(git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));
133 
134 	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
135 	cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));
136 
137 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
138 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
139 
140 	test_abort(branch_head, onto_head);
141 
142 	git_annotated_commit_free(branch_head);
143 	git_annotated_commit_free(onto_head);
144 
145 	git_rebase_free(rebase);
146 }
147 
test_rebase_abort__merge_by_revspec(void)148 void test_rebase_abort__merge_by_revspec(void)
149 {
150 	git_rebase *rebase;
151 	git_annotated_commit *branch_head, *onto_head;
152 
153 	cl_git_pass(git_annotated_commit_from_revspec(&branch_head, repo, "b146bd7"));
154 	cl_git_pass(git_annotated_commit_from_revspec(&onto_head, repo, "efad0b1"));
155 
156 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
157 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
158 
159 	test_abort(branch_head, onto_head);
160 
161 	git_annotated_commit_free(branch_head);
162 	git_annotated_commit_free(onto_head);
163 
164 	git_rebase_free(rebase);
165 }
166 
test_rebase_abort__merge_by_id_immediately_after_init(void)167 void test_rebase_abort__merge_by_id_immediately_after_init(void)
168 {
169 	git_rebase *rebase;
170 	git_oid branch_id, onto_id;
171 	git_annotated_commit *branch_head, *onto_head;
172 
173 	cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
174 	cl_git_pass(git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));
175 
176 	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
177 	cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));
178 
179 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
180 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
181 
182 	cl_git_pass(git_rebase_abort(rebase));
183 	ensure_aborted(branch_head, onto_head);
184 
185 	git_annotated_commit_free(branch_head);
186 	git_annotated_commit_free(onto_head);
187 
188 	git_rebase_free(rebase);
189 }
190 
test_rebase_abort__detached_head(void)191 void test_rebase_abort__detached_head(void)
192 {
193 	git_rebase *rebase;
194 	git_oid branch_id, onto_id;
195 	git_signature *signature;
196 	git_annotated_commit *branch_head, *onto_head;
197 
198 	git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64");
199     git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00");
200 
201 	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
202 	cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));
203 
204 	cl_git_pass(git_signature_new(&signature, "Rebaser", "rebaser@example.com", 1404157834, -400));
205 
206 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
207 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
208 
209 	test_abort(branch_head, onto_head);
210 
211 	git_signature_free(signature);
212 
213 	git_annotated_commit_free(branch_head);
214 	git_annotated_commit_free(onto_head);
215 
216 	git_rebase_free(rebase);
217 }
218 
test_rebase_abort__old_style_head_file(void)219 void test_rebase_abort__old_style_head_file(void)
220 {
221 	git_rebase *rebase;
222 	git_reference *branch_ref, *onto_ref;
223 	git_signature *signature;
224 	git_annotated_commit *branch_head, *onto_head;
225 
226 	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
227 	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));
228 
229 	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
230 	cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));
231 
232 	cl_git_pass(git_signature_new(&signature, "Rebaser", "rebaser@example.com", 1404157834, -400));
233 
234 	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
235 	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
236 
237 	p_rename("rebase-merge/.git/rebase-merge/orig-head",
238 		"rebase-merge/.git/rebase-merge/head");
239 
240 	test_abort(branch_head, onto_head);
241 
242 	git_signature_free(signature);
243 
244 	git_annotated_commit_free(branch_head);
245 	git_annotated_commit_free(onto_head);
246 
247 	git_reference_free(branch_ref);
248 	git_reference_free(onto_ref);
249 	git_rebase_free(rebase);
250 }
251