analysis.c 5.73 KB
Newer Older
Robert Coup committed
1 2 3 4
/*
NOTE: this is the implementation for both merge/trees/analysis.c and merge/workdir/analysis.c
You probably want to make changes to both files.
*/
Robert Coup committed
5 6 7 8 9 10 11

#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "git2/annotated_commit.h"
#include "git2/sys/index.h"
#include "merge.h"
Robert Coup committed
12
#include "merge_helpers.h"
Robert Coup committed
13 14 15
#include "refs.h"
#include "posix.h"

16
#define TEST_REPO_PATH "merge-resolve"
Robert Coup committed
17 18 19 20 21 22 23 24 25 26

#define UPTODATE_BRANCH         "master"
#define PREVIOUS_BRANCH         "previous"

#define FASTFORWARD_BRANCH      "ff_branch"
#define FASTFORWARD_ID          "fd89f8cffb663ac89095a0f9764902e93ceaca6a"

#define NOFASTFORWARD_BRANCH    "branch"
#define NOFASTFORWARD_ID        "7cb63eed597130ba4abb87b3e544b85021905520"

27 28 29 30 31 32 33 34 35
static git_repository *sandbox;
static git_repository *repo;

void test_merge_analysis__initialize_with_bare_repository(void)
{
	sandbox = cl_git_sandbox_init(TEST_REPO_PATH);
	cl_git_pass(git_repository_open_ext(&repo, git_repository_path(sandbox),
					    GIT_REPOSITORY_OPEN_BARE, NULL));
}
Robert Coup committed
36

37
void test_merge_analysis__initialize_with_nonbare_repository(void)
Robert Coup committed
38
{
39 40 41
	sandbox = cl_git_sandbox_init(TEST_REPO_PATH);
	cl_git_pass(git_repository_open_ext(&repo, git_repository_workdir(sandbox),
					    0, NULL));
Robert Coup committed
42 43
}

44
void test_merge_analysis__cleanup(void)
Robert Coup committed
45
{
46 47
	git_repository_free(repo);
	cl_git_sandbox_cleanup();
Robert Coup committed
48 49 50
}

static void analysis_from_branch(
Robert Coup committed
51 52 53 54
	git_merge_analysis_t *merge_analysis,
	git_merge_preference_t *merge_pref,
	const char *our_branchname,
	const char *their_branchname)
Robert Coup committed
55
{
56 57
	git_str our_refname = GIT_STR_INIT;
	git_str their_refname = GIT_STR_INIT;
Robert Coup committed
58 59 60 61 62
	git_reference *our_ref;
	git_reference *their_ref;
	git_annotated_commit *their_head;

	if (our_branchname != NULL) {
63 64
		cl_git_pass(git_str_printf(&our_refname, "%s%s", GIT_REFS_HEADS_DIR, our_branchname));
		cl_git_pass(git_reference_lookup(&our_ref, repo, git_str_cstr(&our_refname)));
Robert Coup committed
65 66 67 68
	} else {
		cl_git_pass(git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE));
	}

69
	cl_git_pass(git_str_printf(&their_refname, "%s%s", GIT_REFS_HEADS_DIR, their_branchname));
Robert Coup committed
70

71
	cl_git_pass(git_reference_lookup(&their_ref, repo, git_str_cstr(&their_refname)));
Robert Coup committed
72 73 74 75
	cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref));

	cl_git_pass(git_merge_analysis_for_ref(merge_analysis, merge_pref, repo, our_ref, (const git_annotated_commit **)&their_head, 1));

76 77
	git_str_dispose(&our_refname);
	git_str_dispose(&their_refname);
Robert Coup committed
78 79 80
	git_annotated_commit_free(their_head);
	git_reference_free(our_ref);
	git_reference_free(their_ref);
Robert Coup committed
81 82
}

83
void test_merge_analysis__fastforward(void)
Robert Coup committed
84
{
Robert Coup committed
85 86
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
87

Robert Coup committed
88 89
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
Robert Coup committed
90 91
}

92
void test_merge_analysis__no_fastforward(void)
Robert Coup committed
93
{
Robert Coup committed
94 95
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
96

Robert Coup committed
97 98
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
Robert Coup committed
99 100
}

101
void test_merge_analysis__uptodate(void)
Robert Coup committed
102
{
Robert Coup committed
103 104
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
105

Robert Coup committed
106 107
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, UPTODATE_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
Robert Coup committed
108 109
}

110
void test_merge_analysis__uptodate_merging_prev_commit(void)
Robert Coup committed
111
{
Robert Coup committed
112 113
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
114

Robert Coup committed
115 116
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, PREVIOUS_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
Robert Coup committed
117 118
}

119
void test_merge_analysis__unborn(void)
Robert Coup committed
120
{
Robert Coup committed
121 122
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
123
	git_str master = GIT_STR_INIT;
Robert Coup committed
124

125 126
	cl_git_pass(git_str_joinpath(&master, git_repository_path(repo), "refs/heads/master"));
	cl_must_pass(p_unlink(git_str_cstr(&master)));
Robert Coup committed
127

Robert Coup committed
128 129
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis);
Robert Coup committed
130

131
	git_str_dispose(&master);
Robert Coup committed
132 133
}

134
void test_merge_analysis__fastforward_with_config_noff(void)
Robert Coup committed
135
{
Robert Coup committed
136 137 138
	git_config *config;
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
139

140 141
	cl_git_pass(git_repository_config(&config, repo));
	cl_git_pass(git_config_set_string(config, "merge.ff", "false"));
Robert Coup committed
142

Robert Coup committed
143 144
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
Robert Coup committed
145

Robert Coup committed
146
	cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD));
147 148

	git_config_free(config);
Robert Coup committed
149 150
}

151
void test_merge_analysis__no_fastforward_with_config_ffonly(void)
Robert Coup committed
152
{
Robert Coup committed
153 154 155
	git_config *config;
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
156

157 158
	cl_git_pass(git_repository_config(&config, repo));
	cl_git_pass(git_config_set_string(config, "merge.ff", "only"));
Robert Coup committed
159

Robert Coup committed
160 161
	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
Robert Coup committed
162

Robert Coup committed
163
	cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY));
164 165

	git_config_free(config);
Robert Coup committed
166 167
}

168
void test_merge_analysis__between_uptodate_refs(void)
Robert Coup committed
169
{
Robert Coup committed
170 171
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
172

Robert Coup committed
173 174
	analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH, PREVIOUS_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
Robert Coup committed
175 176
}

177
void test_merge_analysis__between_noff_refs(void)
Robert Coup committed
178
{
Robert Coup committed
179 180
	git_merge_analysis_t merge_analysis;
	git_merge_preference_t merge_pref;
Robert Coup committed
181

Robert Coup committed
182 183
	analysis_from_branch(&merge_analysis, &merge_pref, "branch", FASTFORWARD_BRANCH);
	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
Robert Coup committed
184
}