Skip to content

Commit 0c1029b

Browse files
authored
Merge pull request libgit2#5022 from rcoup/merge-analysis-bare-repo-5017
Merge analysis support for bare repos
2 parents 758d1b9 + 438c995 commit 0c1029b

File tree

5 files changed

+301
-115
lines changed

5 files changed

+301
-115
lines changed

src/merge.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,9 +3112,6 @@ static int merge_heads(
31123112
*ancestor_head_out = NULL;
31133113
*our_head_out = NULL;
31143114

3115-
if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
3116-
goto done;
3117-
31183115
if ((error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0)
31193116
goto done;
31203117

tests/merge/analysis.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
NOTE: this is the implementation for both merge/trees/analysis.c and merge/workdir/analysis.c
3+
You probably want to make changes to both files.
4+
*/
5+
6+
#include "clar_libgit2.h"
7+
#include "git2/repository.h"
8+
#include "git2/merge.h"
9+
#include "git2/annotated_commit.h"
10+
#include "git2/sys/index.h"
11+
#include "merge.h"
12+
#include "merge_helpers.h"
13+
#include "refs.h"
14+
#include "posix.h"
15+
16+
static git_repository *repo;
17+
static git_index *repo_index;
18+
19+
#define UPTODATE_BRANCH "master"
20+
#define PREVIOUS_BRANCH "previous"
21+
22+
#define FASTFORWARD_BRANCH "ff_branch"
23+
#define FASTFORWARD_ID "fd89f8cffb663ac89095a0f9764902e93ceaca6a"
24+
25+
#define NOFASTFORWARD_BRANCH "branch"
26+
#define NOFASTFORWARD_ID "7cb63eed597130ba4abb87b3e544b85021905520"
27+
28+
29+
/* Fixture setup and teardown */
30+
void testimpl_merge_analysis__initialize(git_repository *t_repo, git_index *t_repo_index)
31+
{
32+
repo = t_repo;
33+
repo_index = t_repo_index;
34+
}
35+
36+
void testimpl_merge_analysis__cleanup(void)
37+
{
38+
repo_index = NULL;
39+
repo = NULL;
40+
}
41+
42+
static void analysis_from_branch(
43+
git_merge_analysis_t *merge_analysis,
44+
git_merge_preference_t *merge_pref,
45+
const char *our_branchname,
46+
const char *their_branchname)
47+
{
48+
git_buf our_refname = GIT_BUF_INIT;
49+
git_buf their_refname = GIT_BUF_INIT;
50+
git_reference *our_ref;
51+
git_reference *their_ref;
52+
git_annotated_commit *their_head;
53+
54+
if (our_branchname != NULL) {
55+
cl_git_pass(git_buf_printf(&our_refname, "%s%s", GIT_REFS_HEADS_DIR, our_branchname));
56+
cl_git_pass(git_reference_lookup(&our_ref, repo, git_buf_cstr(&our_refname)));
57+
} else {
58+
cl_git_pass(git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE));
59+
}
60+
61+
cl_git_pass(git_buf_printf(&their_refname, "%s%s", GIT_REFS_HEADS_DIR, their_branchname));
62+
63+
cl_git_pass(git_reference_lookup(&their_ref, repo, git_buf_cstr(&their_refname)));
64+
cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref));
65+
66+
cl_git_pass(git_merge_analysis_for_ref(merge_analysis, merge_pref, repo, our_ref, (const git_annotated_commit **)&their_head, 1));
67+
68+
git_buf_dispose(&our_refname);
69+
git_buf_dispose(&their_refname);
70+
git_annotated_commit_free(their_head);
71+
git_reference_free(our_ref);
72+
git_reference_free(their_ref);
73+
}
74+
75+
void testimpl_merge_analysis__fastforward(void)
76+
{
77+
git_merge_analysis_t merge_analysis;
78+
git_merge_preference_t merge_pref;
79+
80+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
81+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
82+
}
83+
84+
void testimpl_merge_analysis__no_fastforward(void)
85+
{
86+
git_merge_analysis_t merge_analysis;
87+
git_merge_preference_t merge_pref;
88+
89+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
90+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
91+
}
92+
93+
void testimpl_merge_analysis__uptodate(void)
94+
{
95+
git_merge_analysis_t merge_analysis;
96+
git_merge_preference_t merge_pref;
97+
98+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, UPTODATE_BRANCH);
99+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
100+
}
101+
102+
void testimpl_merge_analysis__uptodate_merging_prev_commit(void)
103+
{
104+
git_merge_analysis_t merge_analysis;
105+
git_merge_preference_t merge_pref;
106+
107+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, PREVIOUS_BRANCH);
108+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
109+
}
110+
111+
void testimpl_merge_analysis__unborn(void)
112+
{
113+
git_merge_analysis_t merge_analysis;
114+
git_merge_preference_t merge_pref;
115+
git_buf master = GIT_BUF_INIT;
116+
117+
cl_git_pass(git_buf_joinpath(&master, git_repository_path(repo), "refs/heads/master"));
118+
p_unlink(git_buf_cstr(&master));
119+
120+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
121+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis);
122+
123+
git_buf_dispose(&master);
124+
}
125+
126+
void testimpl_merge_analysis__fastforward_with_config_noff(void)
127+
{
128+
git_config *config;
129+
git_merge_analysis_t merge_analysis;
130+
git_merge_preference_t merge_pref;
131+
132+
cl_git_pass(git_repository_config(&config, repo));
133+
cl_git_pass(git_config_set_string(config, "merge.ff", "false"));
134+
135+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
136+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
137+
138+
cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD));
139+
140+
git_config_free(config);
141+
}
142+
143+
void testimpl_merge_analysis__no_fastforward_with_config_ffonly(void)
144+
{
145+
git_config *config;
146+
git_merge_analysis_t merge_analysis;
147+
git_merge_preference_t merge_pref;
148+
149+
cl_git_pass(git_repository_config(&config, repo));
150+
cl_git_pass(git_config_set_string(config, "merge.ff", "only"));
151+
152+
analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
153+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
154+
155+
cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY));
156+
157+
git_config_free(config);
158+
}
159+
160+
void testimpl_merge_analysis__between_uptodate_refs(void)
161+
{
162+
git_merge_analysis_t merge_analysis;
163+
git_merge_preference_t merge_pref;
164+
165+
analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH, PREVIOUS_BRANCH);
166+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
167+
}
168+
169+
void testimpl_merge_analysis__between_noff_refs(void)
170+
{
171+
git_merge_analysis_t merge_analysis;
172+
git_merge_preference_t merge_pref;
173+
174+
analysis_from_branch(&merge_analysis, &merge_pref, "branch", FASTFORWARD_BRANCH);
175+
cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
176+
}

tests/merge/analysis.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef INCLUDE_cl_merge_analysis_h__
2+
#define INCLUDE_cl_merge_analysis_h__
3+
4+
void testimpl_merge_analysis__initialize(git_repository *t_repo, git_index *t_repo_index);
5+
void testimpl_merge_analysis__cleanup(void);
6+
void testimpl_merge_analysis__fastforward(void);
7+
void testimpl_merge_analysis__no_fastforward(void);
8+
void testimpl_merge_analysis__uptodate(void);
9+
void testimpl_merge_analysis__uptodate_merging_prev_commit(void);
10+
void testimpl_merge_analysis__unborn(void);
11+
void testimpl_merge_analysis__fastforward_with_config_noff(void);
12+
void testimpl_merge_analysis__no_fastforward_with_config_ffonly(void);
13+
void testimpl_merge_analysis__between_uptodate_refs(void);
14+
void testimpl_merge_analysis__between_noff_refs(void);
15+
16+
#endif

tests/merge/trees/analysis.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
NOTE: this is essentially duplicated from tests/merge/workdir/analysis.c
3+
You probably want to make changes to both files.
4+
*/
5+
6+
#include "clar_libgit2.h"
7+
#include "git2/repository.h"
8+
#include "../analysis.h"
9+
10+
static git_repository *repo;
11+
static git_repository *bare_repo;
12+
static git_index *repo_index;
13+
14+
#define TEST_REPO_PATH "merge-resolve"
15+
#define TEST_INDEX_PATH TEST_REPO_PATH "/index"
16+
17+
18+
/* Fixture setup and teardown */
19+
void test_merge_trees_analysis__initialize(void)
20+
{
21+
repo = cl_git_sandbox_init(TEST_REPO_PATH);
22+
23+
cl_git_pass(git_repository_open_ext(&bare_repo, TEST_REPO_PATH "/.git", GIT_REPOSITORY_OPEN_BARE, NULL));
24+
25+
git_repository_index(&repo_index, bare_repo);
26+
27+
testimpl_merge_analysis__initialize(bare_repo, repo_index);
28+
}
29+
30+
void test_merge_trees_analysis__cleanup(void)
31+
{
32+
testimpl_merge_analysis__cleanup();
33+
34+
git_index_free(repo_index);
35+
repo_index = NULL;
36+
37+
git_repository_free(bare_repo);
38+
bare_repo = NULL;
39+
40+
cl_git_sandbox_cleanup();
41+
repo = NULL;
42+
}
43+
44+
void test_merge_trees_analysis__fastforward(void)
45+
{
46+
testimpl_merge_analysis__fastforward();
47+
}
48+
49+
void test_merge_trees_analysis__no_fastforward(void)
50+
{
51+
testimpl_merge_analysis__no_fastforward();
52+
}
53+
54+
void test_merge_trees_analysis__uptodate(void)
55+
{
56+
testimpl_merge_analysis__uptodate();
57+
}
58+
59+
void test_merge_trees_analysis__uptodate_merging_prev_commit(void)
60+
{
61+
testimpl_merge_analysis__uptodate_merging_prev_commit();
62+
}
63+
64+
void test_merge_trees_analysis__unborn(void)
65+
{
66+
testimpl_merge_analysis__unborn();
67+
}
68+
69+
void test_merge_trees_analysis__fastforward_with_config_noff(void)
70+
{
71+
testimpl_merge_analysis__fastforward_with_config_noff();
72+
}
73+
74+
void test_merge_trees_analysis__no_fastforward_with_config_ffonly(void)
75+
{
76+
testimpl_merge_analysis__no_fastforward_with_config_ffonly();
77+
}
78+
79+
void test_merge_trees_analysis__between_uptodate_refs(void)
80+
{
81+
testimpl_merge_analysis__between_uptodate_refs();
82+
}
83+
84+
void test_merge_trees_analysis__between_noff_refs(void)
85+
{
86+
testimpl_merge_analysis__between_noff_refs();
87+
}

0 commit comments

Comments
 (0)