abort.c 7.86 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "git2/rebase.h"
#include "merge.h"
#include "posix.h"
5
#include "annotated_commit.h"
6 7 8 9 10

#include <fcntl.h>

static git_repository *repo;

11
/* Fixture setup and teardown */
12 13 14 15 16 17 18 19 20 21
void test_rebase_abort__initialize(void)
{
	repo = cl_git_sandbox_init("rebase");
}

void test_rebase_abort__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

22 23 24
static void ensure_aborted(
	git_annotated_commit *branch,
	git_annotated_commit *onto)
25 26 27 28 29 30 31 32 33 34 35 36
{
	git_reference *head_ref, *branch_ref = NULL;
	git_status_list *statuslist;
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	/* Make sure the refs are updated appropriately */
	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));

	if (branch->ref_name == NULL)
37
		cl_assert_equal_oid(git_annotated_commit_id(branch), git_reference_target(head_ref));
38 39 40
	else {
		cl_assert_equal_s("refs/heads/beef", git_reference_symbolic_target(head_ref));
		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 45 46 47 48 49 50 51
	}

	git_status_list_new(&statuslist, repo, NULL);
	cl_assert_equal_i(0, git_status_list_entrycount(statuslist));
	git_status_list_free(statuslist);

	/* Make sure the reflogs are updated appropriately */
	cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));

	cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
52 53
	cl_assert_equal_oid(git_annotated_commit_id(onto), git_reflog_entry_id_old(reflog_entry));
	cl_assert_equal_oid(git_annotated_commit_id(branch), git_reflog_entry_id_new(reflog_entry));
54 55 56 57 58
	cl_assert_equal_s("rebase: aborting", git_reflog_entry_message(reflog_entry));

	git_reflog_free(reflog);
	git_reference_free(head_ref);
	git_reference_free(branch_ref);
59 60 61 62 63 64 65 66 67 68 69 70
}

static void test_abort(
	git_annotated_commit *branch, git_annotated_commit *onto)
{
	git_rebase *rebase;

	cl_git_pass(git_rebase_open(&rebase, repo, NULL));
	cl_git_pass(git_rebase_abort(rebase));

	ensure_aborted(branch, onto);

71
	git_rebase_free(rebase);
72 73 74 75
}

void test_rebase_abort__merge(void)
{
76
	git_rebase *rebase;
77
	git_reference *branch_ref, *onto_ref;
78
	git_annotated_commit *branch_head, *onto_head;
79 80 81 82

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));

83 84
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	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 88 89 90
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));

	test_abort(branch_head, onto_head);

91 92
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);
93 94 95

	git_reference_free(branch_ref);
	git_reference_free(onto_ref);
96
	git_rebase_free(rebase);
97 98
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
void test_rebase_abort__merge_immediately_after_init(void)
{
	git_rebase *rebase;
	git_reference *branch_ref, *onto_ref;
	git_annotated_commit *branch_head, *onto_head;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));

	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));

	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));

	cl_git_pass(git_rebase_abort(rebase));
	ensure_aborted(branch_head, onto_head);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);

	git_reference_free(branch_ref);
	git_reference_free(onto_ref);
	git_rebase_free(rebase);
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138
void test_rebase_abort__merge_by_id(void)
{
	git_rebase *rebase;
	git_oid branch_id, onto_id;
	git_annotated_commit *branch_head, *onto_head;

	cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
	cl_git_pass(git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));

	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
	cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));

	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	test_abort(branch_head, onto_head);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);

	git_rebase_free(rebase);
}

void test_rebase_abort__merge_by_revspec(void)
{
	git_rebase *rebase;
	git_annotated_commit *branch_head, *onto_head;

	cl_git_pass(git_annotated_commit_from_revspec(&branch_head, repo, "b146bd7"));
	cl_git_pass(git_annotated_commit_from_revspec(&onto_head, repo, "efad0b1"));
	
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
158 159 160 161 162

	test_abort(branch_head, onto_head);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	git_rebase_free(rebase);
}

void test_rebase_abort__merge_by_id_immediately_after_init(void)
{
	git_rebase *rebase;
	git_oid branch_id, onto_id;
	git_annotated_commit *branch_head, *onto_head;

	cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
	cl_git_pass(git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));

	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
	cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));

	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));

	cl_git_pass(git_rebase_abort(rebase));
	ensure_aborted(branch_head, onto_head);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);
187 188 189 190

	git_rebase_free(rebase);
}

191 192
void test_rebase_abort__detached_head(void)
{
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 205

	cl_git_pass(git_signature_new(&signature, "Rebaser", "rebaser@example.com", 1404157834, -400));

206
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
207 208 209 210 211 212
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));

	test_abort(branch_head, onto_head);

	git_signature_free(signature);

213 214
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);
215

216
	git_rebase_free(rebase);
217 218 219 220
}

void test_rebase_abort__old_style_head_file(void)
{
221
	git_rebase *rebase;
222 223
	git_reference *branch_ref, *onto_ref;
	git_signature *signature;
224
	git_annotated_commit *branch_head, *onto_head;
225 226 227 228

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));

229 230
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));
231 232 233

	cl_git_pass(git_signature_new(&signature, "Rebaser", "rebaser@example.com", 1404157834, -400));

234
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
235 236 237 238 239 240 241 242 243
	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));

	p_rename("rebase-merge/.git/rebase-merge/orig-head",
		"rebase-merge/.git/rebase-merge/head");

	test_abort(branch_head, onto_head);

	git_signature_free(signature);

244 245
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(onto_head);
246 247 248

	git_reference_free(branch_ref);
	git_reference_free(onto_ref);
249
	git_rebase_free(rebase);
250
}