Commit ccb308273a8662a9692849115a929bd1a74a15f7

Edward Thomson 2014-03-11T17:19:35

Add `git_merge_status` to provide info about an upcoming merge

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
diff --git a/include/git2/merge.h b/include/git2/merge.h
index 38e3408..1d30a5a 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -235,6 +235,46 @@ GIT_EXTERN(int) git_merge_tree_init_opts(
 	int version);
 
 /**
+ * The results of `git_merge_status` indicate the state of a merge scenario.
+ */
+typedef enum {
+	/**
+	 * A "normal" merge; both HEAD and the given merge input have diverged
+	 * from their common ancestor.  The divergent commits must be merged.
+	 */
+	GIT_MERGE_STATUS_NORMAL = 0,
+
+	/**
+	 * The repository is already up-to-date and no merge needs to be
+	 * performed.  The given merge input already exists as a parent of HEAD.
+	 */
+	GIT_MERGE_STATUS_UP_TO_DATE = (1 << 0),
+
+	/**
+	 * The given merge input is a fast-forward from HEAD and no merge
+	 * needs to be performed.  Instead, the client can check out the
+	 * given merge input.
+	 */
+	GIT_MERGE_STATUS_FASTFORWARD = (1 << 1),
+} git_merge_status_t;
+
+/**
+ * Determine the status of the merge between the given branch(es) and the
+ * HEAD of the repository.
+ *
+ * @param status_out status enumeration that the result is written into
+ * @param repo the repository to merge
+ * @param their_heads the heads to merge into
+ * @param their_heads_len the number of heads to merge
+ * @return 0 on success or error code
+ */
+GIT_EXTERN(int) git_merge_status(
+	git_merge_status_t *status_out,
+	git_repository *repo,
+	const git_merge_head **their_heads,
+	size_t their_heads_len);
+
+/**
  * Option flags for `git_merge`.
  */
 typedef enum {
diff --git a/src/merge.c b/src/merge.c
index 8a33edb..cdc1921 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2497,6 +2497,79 @@ static int merge_state_cleanup(git_repository *repo)
 	return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
 }
 
+static int merge_heads(
+	git_merge_head **ancestor_head_out,
+	git_merge_head **our_head_out,
+	git_repository *repo,
+	const git_merge_head **their_heads,
+	size_t their_heads_len)
+{
+	git_merge_head *ancestor_head = NULL, *our_head = NULL;
+	git_reference *our_ref = NULL;
+	int error = 0;
+
+	*ancestor_head_out = NULL;
+	*our_head_out = NULL;
+
+	if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
+		goto done;
+
+	if ((error = git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE)) < 0 ||
+		(error = git_merge_head_from_ref(&our_head, repo, our_ref)) < 0)
+		goto done;
+
+	if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0) {
+		if (error != GIT_ENOTFOUND)
+			goto done;
+
+		giterr_clear();
+		error = 0;
+	}
+
+	*ancestor_head_out = ancestor_head;
+	*our_head_out = our_head;
+
+done:
+	if (error < 0) {
+		git_merge_head_free(ancestor_head);
+		git_merge_head_free(our_head);
+	}
+
+	git_reference_free(our_ref);
+
+	return error;
+}
+
+int git_merge_status(
+	git_merge_status_t *out,
+	git_repository *repo,
+	const git_merge_head **their_heads,
+	size_t their_heads_len)
+{
+	git_merge_head *ancestor_head = NULL, *our_head = NULL;
+	int error;
+
+	assert(out && repo && their_heads);
+
+	*out = GIT_MERGE_STATUS_NORMAL;
+
+	if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0)
+		goto done;
+
+	if (their_heads_len == 1 && ancestor_head != NULL) {
+		/* We're up-to-date if we're trying to merge our own common ancestor. */
+		if (git_oid_equal(&ancestor_head->oid, &their_heads[0]->oid))
+			*out = GIT_MERGE_STATUS_UP_TO_DATE;
+
+		/* We're fastforwardable if we're our own common ancestor. */
+		else if (git_oid_equal(&ancestor_head->oid, &our_head->oid))
+			*out = GIT_MERGE_STATUS_FASTFORWARD;
+	}
+
+done:
+	return error;
+}
+
 int git_merge(
 	git_merge_result **out,
 	git_repository *repo,
@@ -2530,15 +2603,7 @@ int git_merge(
 	their_trees = git__calloc(their_heads_len, sizeof(git_tree *));
 	GITERR_CHECK_ALLOC(their_trees);
 
-	if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
-		goto on_error;
-
-	if ((error = git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE)) < 0 ||
-		(error = git_merge_head_from_ref(&our_head, repo, our_ref)) < 0)
-		goto on_error;
-
-	if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0 &&
-		error != GIT_ENOTFOUND)
+	if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0)
 		goto on_error;
 
 	if ((error = merge_normalize_opts(repo, &opts, given_opts, ancestor_head, our_head, their_heads_len, their_heads)) < 0)
diff --git a/tests/merge/workdir/fastforward.c b/tests/merge/workdir/fastforward.c
deleted file mode 100644
index d6b3148..0000000
--- a/tests/merge/workdir/fastforward.c
+++ /dev/null
@@ -1,148 +0,0 @@
-#include "clar_libgit2.h"
-#include "git2/repository.h"
-#include "git2/merge.h"
-#include "git2/sys/index.h"
-#include "merge.h"
-#include "../merge_helpers.h"
-#include "refs.h"
-
-static git_repository *repo;
-static git_index *repo_index;
-
-#define TEST_REPO_PATH "merge-resolve"
-#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
-
-#define THEIRS_FASTFORWARD_BRANCH	"ff_branch"
-#define THEIRS_FASTFORWARD_ID		"fd89f8cffb663ac89095a0f9764902e93ceaca6a"
-
-#define THEIRS_NOFASTFORWARD_BRANCH	"branch"
-#define THEIRS_NOFASTFORWARD_ID	"7cb63eed597130ba4abb87b3e544b85021905520"
-
-
-// Fixture setup and teardown
-void test_merge_workdir_fastforward__initialize(void)
-{
-	repo = cl_git_sandbox_init(TEST_REPO_PATH);
-	git_repository_index(&repo_index, repo);
-}
-
-void test_merge_workdir_fastforward__cleanup(void)
-{
-	git_index_free(repo_index);
-	cl_git_sandbox_cleanup();
-}
-
-static git_merge_result *merge_fastforward_branch(int flags)
-{
-	git_reference *their_ref;
-	git_merge_head *their_heads[1];
-	git_merge_result *result;
-	git_merge_opts opts = GIT_MERGE_OPTS_INIT;
-
-	opts.merge_flags = flags;
-
-	cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_REFS_HEADS_DIR THEIRS_FASTFORWARD_BRANCH));
-	cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));
-
-	cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, &opts));
-
-	git_merge_head_free(their_heads[0]);
-	git_reference_free(their_ref);
-
-	return result;
-}
-
-void test_merge_workdir_fastforward__fastforward(void)
-{
-	git_merge_result *result;
-	git_oid expected, ff_oid;
-
-	cl_git_pass(git_oid_fromstr(&expected, THEIRS_FASTFORWARD_ID));
-
-	cl_assert(result = merge_fastforward_branch(0));
-	cl_assert(git_merge_result_is_fastforward(result));
-	cl_git_pass(git_merge_result_fastforward_id(&ff_oid, result));
-	cl_assert(git_oid_cmp(&ff_oid, &expected) == 0);
-
-	git_merge_result_free(result);
-}
-
-void test_merge_workdir_fastforward__fastforward_only(void)
-{
-	git_merge_result *result;
-	git_merge_opts opts = GIT_MERGE_OPTS_INIT;
-	git_reference *their_ref;
-	git_merge_head *their_head;
-	int error;
-
-	opts.merge_flags = GIT_MERGE_FASTFORWARD_ONLY;
-
-	cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_REFS_HEADS_DIR THEIRS_NOFASTFORWARD_BRANCH));
-	cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref));
-
-	cl_git_fail((error = git_merge(&result, repo, (const git_merge_head **)&their_head, 1, &opts)));
-	cl_assert(error == GIT_ENONFASTFORWARD);
-
-	git_merge_head_free(their_head);
-	git_reference_free(their_ref);
-}
-
-void test_merge_workdir_fastforward__no_fastforward(void)
-{
-	git_merge_result *result;
-
-	struct merge_index_entry merge_index_entries[] = {
-		{ 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" },
-		{ 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 0, "automergeable.txt" },
-		{ 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" },
-		{ 0100644, "bd9cb4cd0a770cb9adcb5fce212142ef40ea1c35", 0, "changed-in-master.txt" },
-		{ 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" },
-		{ 0100644, "364bbe4ce80c7bd31e6307dce77d46e3e1759fb3", 0, "new-in-ff.txt" },
-		{ 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" },
-		{ 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" },
-	};
-
-	cl_assert(result = merge_fastforward_branch(GIT_MERGE_NO_FASTFORWARD));
-	cl_assert(!git_merge_result_is_fastforward(result));
-
-	cl_assert(merge_test_index(repo_index, merge_index_entries, 8));
-	cl_assert(git_index_reuc_entrycount(repo_index) == 0);
-
-	git_merge_result_free(result);
-}
-
-void test_merge_workdir_fastforward__uptodate(void)
-{
-	git_reference *their_ref;
-	git_merge_head *their_heads[1];
-	git_merge_result *result;
-
-	cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_HEAD_FILE));
-	cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));
-
-	cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, NULL));
-
-	cl_assert(git_merge_result_is_uptodate(result));
-
-	git_merge_head_free(their_heads[0]);
-	git_reference_free(their_ref);
-	git_merge_result_free(result);
-}
-
-void test_merge_workdir_fastforward__uptodate_merging_prev_commit(void)
-{
-	git_oid their_oid;
-	git_merge_head *their_heads[1];
-	git_merge_result *result;
-
-	cl_git_pass(git_oid_fromstr(&their_oid, "c607fc30883e335def28cd686b51f6cfa02b06ec"));
-	cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oid));
-
-	cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, NULL));
-
-	cl_assert(git_merge_result_is_uptodate(result));
-
-	git_merge_head_free(their_heads[0]);
-	git_merge_result_free(result);
-}
-
diff --git a/tests/merge/workdir/status.c b/tests/merge/workdir/status.c
new file mode 100644
index 0000000..589299e
--- /dev/null
+++ b/tests/merge/workdir/status.c
@@ -0,0 +1,89 @@
+#include "clar_libgit2.h"
+#include "git2/repository.h"
+#include "git2/merge.h"
+#include "git2/sys/index.h"
+#include "merge.h"
+#include "../merge_helpers.h"
+#include "refs.h"
+
+static git_repository *repo;
+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_status__initialize(void)
+{
+	repo = cl_git_sandbox_init(TEST_REPO_PATH);
+	git_repository_index(&repo_index, repo);
+}
+
+void test_merge_workdir_status__cleanup(void)
+{
+	git_index_free(repo_index);
+	cl_git_sandbox_cleanup();
+}
+
+static git_status_t status_from_branch(const char *branchname)
+{
+	git_buf refname = GIT_BUF_INIT;
+	git_reference *their_ref;
+	git_merge_head *their_heads[1];
+	git_status_t status;
+
+	git_buf_printf(&refname, "%s%s", GIT_REFS_HEADS_DIR, branchname);
+
+	cl_git_pass(git_reference_lookup(&their_ref, repo, git_buf_cstr(&refname)));
+	cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));
+
+	cl_git_pass(git_merge_status(&status, repo, their_heads, 1));
+
+	git_buf_free(&refname);
+	git_merge_head_free(their_heads[0]);
+	git_reference_free(their_ref);
+
+	return status;
+}
+
+void test_merge_workdir_status__fastforward(void)
+{
+	git_merge_status_t status;
+
+	status = status_from_branch(FASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_STATUS_FASTFORWARD, status);
+}
+
+void test_merge_workdir_status__no_fastforward(void)
+{
+	git_merge_status_t status;
+
+	status = status_from_branch(NOFASTFORWARD_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_STATUS_NORMAL, status);
+}
+
+void test_merge_workdir_status__uptodate(void)
+{
+	git_merge_status_t status;
+
+	status = status_from_branch(UPTODATE_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_STATUS_UP_TO_DATE, status);
+}
+
+void test_merge_workdir_status__uptodate_merging_prev_commit(void)
+{
+	git_merge_status_t status;
+
+	status = status_from_branch(PREVIOUS_BRANCH);
+	cl_assert_equal_i(GIT_MERGE_STATUS_UP_TO_DATE, status);
+}
diff --git a/tests/resources/merge-resolve/.gitted/refs/heads/previous b/tests/resources/merge-resolve/.gitted/refs/heads/previous
new file mode 100644
index 0000000..7bc1a8d
--- /dev/null
+++ b/tests/resources/merge-resolve/.gitted/refs/heads/previous
@@ -0,0 +1 @@
+c607fc30883e335def28cd686b51f6cfa02b06ec