Commit 0c1029be9828cf3a6876efc9e2b1d315b7754d77

Patrick Steinhardt 2019-06-13T11:41:39

Merge pull request #5022 from rcoup/merge-analysis-bare-repo-5017 Merge analysis support for bare repos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
diff --git a/src/merge.c b/src/merge.c
index fbae704..c054ad8 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -3112,9 +3112,6 @@ static int merge_heads(
 	*ancestor_head_out = NULL;
 	*our_head_out = NULL;
 
-	if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
-		goto done;
-
 	if ((error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0)
 		goto done;
 
diff --git a/tests/merge/analysis.c b/tests/merge/analysis.c
new file mode 100644
index 0000000..b3c7b03
--- /dev/null
+++ b/tests/merge/analysis.c
@@ -0,0 +1,176 @@
+/*
+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.
+*/
+
+#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"
+#include "merge_helpers.h"
+#include "refs.h"
+#include "posix.h"
+
+static git_repository *repo;
+static git_index *repo_index;
+
+#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"
+
+
+/* Fixture setup and teardown */
+void testimpl_merge_analysis__initialize(git_repository *t_repo, git_index *t_repo_index)
+{
+	repo = t_repo;
+	repo_index = t_repo_index;
+}
+
+void testimpl_merge_analysis__cleanup(void)
+{
+	repo_index = NULL;
+	repo = NULL;
+}
+
+static void analysis_from_branch(
+	git_merge_analysis_t *merge_analysis,
+	git_merge_preference_t *merge_pref,
+	const char *our_branchname,
+	const char *their_branchname)
+{
+	git_buf our_refname = GIT_BUF_INIT;
+	git_buf their_refname = GIT_BUF_INIT;
+	git_reference *our_ref;
+	git_reference *their_ref;
+	git_annotated_commit *their_head;
+
+	if (our_branchname != NULL) {
+		cl_git_pass(git_buf_printf(&our_refname, "%s%s", GIT_REFS_HEADS_DIR, our_branchname));
+		cl_git_pass(git_reference_lookup(&our_ref, repo, git_buf_cstr(&our_refname)));
+	} else {
+		cl_git_pass(git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE));
+	}
+
+	cl_git_pass(git_buf_printf(&their_refname, "%s%s", GIT_REFS_HEADS_DIR, their_branchname));
+
+	cl_git_pass(git_reference_lookup(&their_ref, repo, git_buf_cstr(&their_refname)));
+	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));
+
+	git_buf_dispose(&our_refname);
+	git_buf_dispose(&their_refname);
+	git_annotated_commit_free(their_head);
+	git_reference_free(our_ref);
+	git_reference_free(their_ref);
+}
+
+void testimpl_merge_analysis__fastforward(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
+}
+
+void testimpl_merge_analysis__no_fastforward(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
+}
+
+void testimpl_merge_analysis__uptodate(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, UPTODATE_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+}
+
+void testimpl_merge_analysis__uptodate_merging_prev_commit(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, PREVIOUS_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+}
+
+void testimpl_merge_analysis__unborn(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+	git_buf master = GIT_BUF_INIT;
+
+	cl_git_pass(git_buf_joinpath(&master, git_repository_path(repo), "refs/heads/master"));
+	p_unlink(git_buf_cstr(&master));
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis);
+
+	git_buf_dispose(&master);
+}
+
+void testimpl_merge_analysis__fastforward_with_config_noff(void)
+{
+	git_config *config;
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	cl_git_pass(git_repository_config(&config, repo));
+	cl_git_pass(git_config_set_string(config, "merge.ff", "false"));
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
+
+	cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD));
+
+	git_config_free(config);
+}
+
+void testimpl_merge_analysis__no_fastforward_with_config_ffonly(void)
+{
+	git_config *config;
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	cl_git_pass(git_repository_config(&config, repo));
+	cl_git_pass(git_config_set_string(config, "merge.ff", "only"));
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
+
+	cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY));
+
+	git_config_free(config);
+}
+
+void testimpl_merge_analysis__between_uptodate_refs(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH, PREVIOUS_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+}
+
+void testimpl_merge_analysis__between_noff_refs(void)
+{
+	git_merge_analysis_t merge_analysis;
+	git_merge_preference_t merge_pref;
+
+	analysis_from_branch(&merge_analysis, &merge_pref, "branch", FASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
+}
diff --git a/tests/merge/analysis.h b/tests/merge/analysis.h
new file mode 100644
index 0000000..f97e73e
--- /dev/null
+++ b/tests/merge/analysis.h
@@ -0,0 +1,16 @@
+#ifndef INCLUDE_cl_merge_analysis_h__
+#define INCLUDE_cl_merge_analysis_h__
+
+void testimpl_merge_analysis__initialize(git_repository *t_repo, git_index *t_repo_index);
+void testimpl_merge_analysis__cleanup(void);
+void testimpl_merge_analysis__fastforward(void);
+void testimpl_merge_analysis__no_fastforward(void);
+void testimpl_merge_analysis__uptodate(void);
+void testimpl_merge_analysis__uptodate_merging_prev_commit(void);
+void testimpl_merge_analysis__unborn(void);
+void testimpl_merge_analysis__fastforward_with_config_noff(void);
+void testimpl_merge_analysis__no_fastforward_with_config_ffonly(void);
+void testimpl_merge_analysis__between_uptodate_refs(void);
+void testimpl_merge_analysis__between_noff_refs(void);
+
+#endif
diff --git a/tests/merge/trees/analysis.c b/tests/merge/trees/analysis.c
new file mode 100644
index 0000000..ce61085
--- /dev/null
+++ b/tests/merge/trees/analysis.c
@@ -0,0 +1,87 @@
+/*
+NOTE: this is essentially duplicated from tests/merge/workdir/analysis.c
+You probably want to make changes to both files.
+*/
+
+#include "clar_libgit2.h"
+#include "git2/repository.h"
+#include "../analysis.h"
+
+static git_repository *repo;
+static git_repository *bare_repo;
+static git_index *repo_index;
+
+#define TEST_REPO_PATH "merge-resolve"
+#define TEST_INDEX_PATH TEST_REPO_PATH "/index"
+
+
+/* Fixture setup and teardown */
+void test_merge_trees_analysis__initialize(void)
+{
+	repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+	cl_git_pass(git_repository_open_ext(&bare_repo, TEST_REPO_PATH "/.git", GIT_REPOSITORY_OPEN_BARE, NULL));
+
+	git_repository_index(&repo_index, bare_repo);
+
+	testimpl_merge_analysis__initialize(bare_repo, repo_index);
+}
+
+void test_merge_trees_analysis__cleanup(void)
+{
+	testimpl_merge_analysis__cleanup();
+
+	git_index_free(repo_index);
+	repo_index = NULL;
+
+	git_repository_free(bare_repo);
+	bare_repo = NULL;
+
+	cl_git_sandbox_cleanup();
+	repo = NULL;
+}
+
+void test_merge_trees_analysis__fastforward(void)
+{
+	testimpl_merge_analysis__fastforward();
+}
+
+void test_merge_trees_analysis__no_fastforward(void)
+{
+	testimpl_merge_analysis__no_fastforward();
+}
+
+void test_merge_trees_analysis__uptodate(void)
+{
+	testimpl_merge_analysis__uptodate();
+}
+
+void test_merge_trees_analysis__uptodate_merging_prev_commit(void)
+{
+	testimpl_merge_analysis__uptodate_merging_prev_commit();
+}
+
+void test_merge_trees_analysis__unborn(void)
+{
+	testimpl_merge_analysis__unborn();
+}
+
+void test_merge_trees_analysis__fastforward_with_config_noff(void)
+{
+	testimpl_merge_analysis__fastforward_with_config_noff();
+}
+
+void test_merge_trees_analysis__no_fastforward_with_config_ffonly(void)
+{
+	testimpl_merge_analysis__no_fastforward_with_config_ffonly();
+}
+
+void test_merge_trees_analysis__between_uptodate_refs(void)
+{
+	testimpl_merge_analysis__between_uptodate_refs();
+}
+
+void test_merge_trees_analysis__between_noff_refs(void)
+{
+	testimpl_merge_analysis__between_noff_refs();
+}
diff --git a/tests/merge/workdir/analysis.c b/tests/merge/workdir/analysis.c
index 27d7dba..08158f8 100644
--- a/tests/merge/workdir/analysis.c
+++ b/tests/merge/workdir/analysis.c
@@ -1,12 +1,11 @@
+/*
+NOTE: this is essentially duplicated with tests/merge/trees/analysis.c
+You probably want to make changes to both files.
+*/
+
 #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"
-#include "../merge_helpers.h"
-#include "refs.h"
-#include "posix.h"
+#include "../analysis.h"
 
 static git_repository *repo;
 static git_index *repo_index;
@@ -14,157 +13,68 @@ static git_index *repo_index;
 #define TEST_REPO_PATH "merge-resolve"
 #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
 
-#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"
-
 
 /* Fixture setup and teardown */
 void test_merge_workdir_analysis__initialize(void)
 {
 	repo = cl_git_sandbox_init(TEST_REPO_PATH);
 	git_repository_index(&repo_index, repo);
+
+	testimpl_merge_analysis__initialize(repo, repo_index);
 }
 
 void test_merge_workdir_analysis__cleanup(void)
 {
+	testimpl_merge_analysis__cleanup();
+
 	git_index_free(repo_index);
-	cl_git_sandbox_cleanup();
-}
+	repo_index = NULL;
 
-static void analysis_from_branch(
-	git_merge_analysis_t *merge_analysis,
-	git_merge_preference_t *merge_pref,
-	const char *our_branchname,
-	const char *their_branchname)
-{
-	git_buf our_refname = GIT_BUF_INIT;
-	git_buf their_refname = GIT_BUF_INIT;
-	git_reference *our_ref;
-	git_reference *their_ref;
-	git_annotated_commit *their_head;
-
-	if (our_branchname != NULL) {
-		cl_git_pass(git_buf_printf(&our_refname, "%s%s", GIT_REFS_HEADS_DIR, our_branchname));
-		cl_git_pass(git_reference_lookup(&our_ref, repo, git_buf_cstr(&our_refname)));
-	} else {
-		cl_git_pass(git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE));
-	}
-
-	cl_git_pass(git_buf_printf(&their_refname, "%s%s", GIT_REFS_HEADS_DIR, their_branchname));
-
-	cl_git_pass(git_reference_lookup(&their_ref, repo, git_buf_cstr(&their_refname)));
-	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));
-
-	git_buf_dispose(&our_refname);
-	git_buf_dispose(&their_refname);
-	git_annotated_commit_free(their_head);
-	git_reference_free(our_ref);
-	git_reference_free(their_ref);
+	cl_git_sandbox_cleanup();
+	repo = NULL;
 }
 
 void test_merge_workdir_analysis__fastforward(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
+	testimpl_merge_analysis__fastforward();
 }
 
 void test_merge_workdir_analysis__no_fastforward(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
+	testimpl_merge_analysis__no_fastforward();
 }
 
 void test_merge_workdir_analysis__uptodate(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, UPTODATE_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+	testimpl_merge_analysis__uptodate();
 }
 
 void test_merge_workdir_analysis__uptodate_merging_prev_commit(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, PREVIOUS_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+	testimpl_merge_analysis__uptodate_merging_prev_commit();
 }
 
 void test_merge_workdir_analysis__unborn(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-	git_buf master = GIT_BUF_INIT;
-
-	git_buf_joinpath(&master, git_repository_path(repo), "refs/heads/master");
-	p_unlink(git_buf_cstr(&master));
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis);
-
-	git_buf_dispose(&master);
+	testimpl_merge_analysis__unborn();
 }
 
 void test_merge_workdir_analysis__fastforward_with_config_noff(void)
 {
-	git_config *config;
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	git_repository_config(&config, repo);
-	git_config_set_string(config, "merge.ff", "false");
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis);
-
-	cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD));
+	testimpl_merge_analysis__fastforward_with_config_noff();
 }
 
 void test_merge_workdir_analysis__no_fastforward_with_config_ffonly(void)
 {
-	git_config *config;
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	git_repository_config(&config, repo);
-	git_config_set_string(config, "merge.ff", "only");
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
-
-	cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY));
+	testimpl_merge_analysis__no_fastforward_with_config_ffonly();
 }
 
 void test_merge_workdir_analysis__between_uptodate_refs(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH, PREVIOUS_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis);
+	testimpl_merge_analysis__between_uptodate_refs();
 }
 
 void test_merge_workdir_analysis__between_noff_refs(void)
 {
-	git_merge_analysis_t merge_analysis;
-	git_merge_preference_t merge_pref;
-
-	analysis_from_branch(&merge_analysis, &merge_pref, "branch", FASTFORWARD_BRANCH);
-	cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis);
+	testimpl_merge_analysis__between_noff_refs();
 }