Introduce GIT_MERGE_ANALYSIS_UNBORN
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
diff --git a/include/git2/merge.h b/include/git2/merge.h
index 2cb8df3..491f831 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -259,6 +259,13 @@ typedef enum {
* given merge input.
*/
GIT_MERGE_ANALYSIS_FASTFORWARD = (1 << 2),
+
+ /**
+ * The HEAD of the current repository is "unborn" and does not point to
+ * a valid commit. No merge can be performed, but the caller may wish
+ * to simply set HEAD to the target commit(s).
+ */
+ GIT_MERGE_ANALYSIS_UNBORN = (1 << 3),
} git_merge_analysis_t;
/**
diff --git a/src/merge.c b/src/merge.c
index 6b416a3..852043c 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2524,26 +2524,41 @@ int git_merge_analysis(
size_t their_heads_len)
{
git_merge_head *ancestor_head = NULL, *our_head = NULL;
- int error;
+ int error = 0;
assert(out && repo && their_heads);
- *out = GIT_MERGE_ANALYSIS_NORMAL;
+ *out = GIT_MERGE_ANALYSIS_NONE;
+
+ if (git_repository_head_unborn(repo)) {
+ *out = GIT_MERGE_ANALYSIS_UNBORN;
+ goto done;
+ }
+
+ if (their_heads_len != 1) {
+ giterr_set(GITERR_MERGE, "Can only merge a single branch");
+ error = -1;
+ goto done;
+ }
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_ANALYSIS_UP_TO_DATE;
+ /* We're up-to-date if we're trying to merge our own common ancestor. */
+ if (ancestor_head && git_oid_equal(&ancestor_head->oid, &their_heads[0]->oid))
+ *out = GIT_MERGE_ANALYSIS_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_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_NORMAL;
- }
+ /* We're fastforwardable if we're our own common ancestor. */
+ else if (ancestor_head && git_oid_equal(&ancestor_head->oid, &our_head->oid))
+ *out = GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_NORMAL;
+
+ /* Otherwise, just a normal merge is possible. */
+ else
+ *out = GIT_MERGE_ANALYSIS_NORMAL;
done:
+ git_merge_head_free(ancestor_head);
+ git_merge_head_free(our_head);
return error;
}
diff --git a/tests/merge/workdir/analysis.c b/tests/merge/workdir/analysis.c
index 6a4b86d..86b50b7 100644
--- a/tests/merge/workdir/analysis.c
+++ b/tests/merge/workdir/analysis.c
@@ -5,6 +5,7 @@
#include "merge.h"
#include "../merge_helpers.h"
#include "refs.h"
+#include "posix.h"
static git_repository *repo;
static git_index *repo_index;
@@ -39,18 +40,18 @@ static git_merge_analysis_t analysis_from_branch(const char *branchname)
{
git_buf refname = GIT_BUF_INIT;
git_reference *their_ref;
- git_merge_head *their_heads[1];
+ git_merge_head *their_head;
git_merge_analysis_t analysis;
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_head_from_ref(&their_head, repo, their_ref));
- cl_git_pass(git_merge_analysis(&analysis, repo, their_heads, 1));
+ cl_git_pass(git_merge_analysis(&analysis, repo, (const git_merge_head **)&their_head, 1));
git_buf_free(&refname);
- git_merge_head_free(their_heads[0]);
+ git_merge_head_free(their_head);
git_reference_free(their_ref);
return analysis;
@@ -88,3 +89,18 @@ void test_merge_workdir_analysis__uptodate_merging_prev_commit(void)
analysis = analysis_from_branch(PREVIOUS_BRANCH);
cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, analysis);
}
+
+void test_merge_workdir_analysis__unborn(void)
+{
+ git_merge_analysis_t analysis;
+ git_buf master = GIT_BUF_INIT;
+
+ git_buf_joinpath(&master, git_repository_path(repo), "refs/heads/master");
+ p_unlink(git_buf_cstr(&master));
+
+ analysis = analysis_from_branch(NOFASTFORWARD_BRANCH);
+ cl_assert_equal_i(GIT_MERGE_ANALYSIS_UNBORN, analysis);
+
+ git_buf_free(&master);
+}
+