soft.c 6.09 KB
Newer Older
nulltoken committed
1
#include "clar_libgit2.h"
Edward Thomson committed
2
#include "posix.h"
nulltoken committed
3
#include "reset_helpers.h"
Edward Thomson committed
4
#include "path.h"
5
#include "repo/repo_helpers.h"
nulltoken committed
6 7 8 9 10 11 12 13 14 15 16 17

static git_repository *repo;
static git_object *target;

void test_reset_soft__initialize(void)
{
	repo = cl_git_sandbox_init("testrepo.git");
}

void test_reset_soft__cleanup(void)
{
	git_object_free(target);
18 19
	target = NULL;

nulltoken committed
20 21 22 23 24 25 26
	cl_git_sandbox_cleanup();
}

static void assert_reset_soft(bool should_be_detached)
{
	git_oid oid;

27
	cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
nulltoken committed
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));
nulltoken committed
30 31 32

	cl_assert(git_repository_head_detached(repo) == should_be_detached);

33
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
nulltoken committed
34 35 36

	cl_assert(git_repository_head_detached(repo) == should_be_detached);

37
	cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
nulltoken committed
38 39 40 41 42 43 44 45 46 47
	cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
}

void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void)
{
	assert_reset_soft(false);
}

void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
{
48
	git_repository_detach_head(repo);
nulltoken committed
49 50 51 52 53 54 55 56 57

	assert_reset_soft(true);
}

void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void)
{
	git_oid oid;
	char raw_head_oid[GIT_OID_HEXSZ + 1];

58
	cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
nulltoken committed
59 60 61
	git_oid_fmt(raw_head_oid, &oid);
	raw_head_oid[GIT_OID_HEXSZ] = '\0';

62
	cl_git_pass(git_revparse_single(&target, repo, raw_head_oid));
nulltoken committed
63

64
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
nulltoken committed
65

66
	cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
nulltoken committed
67 68 69 70 71 72 73 74
	cl_git_pass(git_oid_streq(&oid, raw_head_oid));
}

void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void)
{
	git_oid oid;

	/* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
75
	cl_git_pass(git_revparse_single(&target, repo, "b25fa35"));
nulltoken committed
76

77
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
nulltoken committed
78 79

	cl_assert(git_repository_head_detached(repo) == false);
80
	cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
nulltoken committed
81 82 83 84 85 86
	cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
}

void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
{
	/* 53fc32d is the tree of commit e90810b */
87
	cl_git_pass(git_revparse_single(&target, repo, "53fc32d"));
nulltoken committed
88

89
	cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL));
nulltoken committed
90 91 92
	git_object_free(target);

	/* 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));
nulltoken committed
95
}
96

97
void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
98 99 100
{
	git_reference *head;

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 115

	git_reference_free(head);
}
Edward Thomson committed
116 117 118 119 120

void test_reset_soft__fails_when_merging(void)
{
	git_buf merge_head_path = GIT_BUF_INIT;

121
	cl_git_pass(git_repository_detach_head(repo));
Edward Thomson committed
122 123 124
	cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
	cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");

125
	cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
Edward Thomson committed
126

127
	cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL));
Edward Thomson committed
128
	cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
nulltoken committed
129 130

	git_buf_free(&merge_head_path);
Edward Thomson committed
131
}
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void)
{
	git_index *index;
	git_reference *head;
	git_buf merge_head_path = GIT_BUF_INIT;

	cl_git_sandbox_cleanup();

	repo = cl_git_sandbox_init("mergedrepo");

	cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
	cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
	git_buf_free(&merge_head_path);

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert_equal_i(true, git_index_has_conflicts(index));
	git_index_free(index);

	cl_git_pass(git_repository_head(&head, repo));
	cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT));
	git_reference_free(head);

155
	cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL));
156
}
Ben Straub committed
157

158
void test_reset_soft__reflog_is_correct(void)
Ben Straub committed
159
{
160 161 162
	git_annotated_commit *annotated;
	const char *exp_msg = "checkout: moving from br2 to master";
	const char *master_msg = "commit: checking in";
Ben Straub committed
163

164 165
	reflog_check(repo, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg);
	reflog_check(repo, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg);
Ben Straub committed
166 167 168

	/* Branch not moving, no reflog entry */
	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
169
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
170 171 172
	reflog_check(repo, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg);
	reflog_check(repo, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg);
	git_object_free(target);
Ben Straub committed
173

174 175
	/* Moved branch, expect id in message */
	exp_msg = "reset: moving to be3563ae3f795b2b4353bcce3a527ad0a4f7f644";
Ben Straub committed
176
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
177
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
178 179
	reflog_check(repo, "HEAD", 8, "yoram.harmelin@gmail.com", exp_msg);
	reflog_check(repo, "refs/heads/master", 3, NULL, exp_msg);
Ben Straub committed
180

181 182 183 184
	/* Moved branch, expect message with annotated string */
	exp_msg = "reset: moving to HEAD~^{commit}";
	cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}"));
	cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_SOFT, NULL));
Ben Straub committed
185
	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
186 187 188
	reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg);

	git_annotated_commit_free(annotated);
Ben Straub committed
189
}