Skip to content

Support for git_merge_analysis_for_ref() #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,72 @@ Repository_merge_analysis(Repository *self, PyObject *py_id)
return Py_BuildValue("(ii)", analysis, preference);
}

PyDoc_STRVAR(Repository_merge_analysis_for_ref__doc__,
"merge_analysis_for_ref(our_ref, their_head) -> (Integer, Integer)\n"
"\n"
"Analyzes the given branch and determines the opportunities for\n"
"merging it into a reference.\n"
"\n"
"Parameters:\n"
"\n"
"our_ref\n"
" The reference name (String) to perform the analysis from\n"
"\n"
"their_head\n"
" Head (commit Oid) to merge into\n"
"\n"
"The first returned value is a mixture of the GIT_MERGE_ANALYSIS_NONE, _NORMAL,\n"
"_UP_TO_DATE, _FASTFORWARD and _UNBORN flags.\n"
"The second value is the user's preference from 'merge.ff'");

PyObject *
Repository_merge_analysis_for_ref(Repository *self, PyObject *args)
{
char *our_ref_name = NULL;
PyObject *py_their_head;
PyObject *py_result = NULL;
git_oid head_id;
git_reference *our_ref;
git_annotated_commit *commit;
git_merge_analysis_t analysis;
git_merge_preference_t preference;
int err = 0;

if (!PyArg_ParseTuple(args, "zO",
&our_ref_name,
&py_their_head))
return NULL;

err = git_reference_lookup(&our_ref, self->repo, our_ref_name);
if (err < 0) {
PyObject *py_err = Error_set_str(err, our_ref_name);
return py_err;
}

err = py_oid_to_git_oid_expand(self->repo, py_their_head, &head_id);
if (err < 0)
goto out;

err = git_annotated_commit_lookup(&commit, self->repo, &head_id);
if (err < 0) {
py_result = Error_set(err);
goto out;
}

err = git_merge_analysis_for_ref(&analysis, &preference, self->repo, our_ref, (const git_annotated_commit **) &commit, 1);
git_annotated_commit_free(commit);
if (err < 0) {
py_result = Error_set(err);
goto out;
}

py_result = Py_BuildValue("(ii)", analysis, preference);

out:
git_reference_free(our_ref);
return py_result;
}

PyDoc_STRVAR(Repository_merge__doc__,
"merge(id)\n"
"\n"
Expand Down Expand Up @@ -1944,6 +2010,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, descendant_of, METH_VARARGS),
METHOD(Repository, merge_base, METH_VARARGS),
METHOD(Repository, merge_analysis, METH_O),
METHOD(Repository, merge_analysis_for_ref, METH_VARARGS),
METHOD(Repository, merge, METH_O),
METHOD(Repository, cherrypick, METH_O),
METHOD(Repository, apply, METH_O),
Expand Down
2 changes: 2 additions & 0 deletions src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ PyObject* Repository_blame(Repository *self, PyObject *args, PyObject *kwds);
PyObject* Repository_merge(Repository *self, PyObject *py_oid);
PyObject* Repository_cherrypick(Repository *self, PyObject *py_oid);
PyObject* Repository_apply(Repository *self, PyObject *py_diff);
PyObject* Repository_merge_analysis(Repository *self, PyObject *py_id);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't see why Repository_merge_analysis was missing here already (oversight?) so I added it.

PyObject* Repository_merge_analysis_for_ref(Repository *self, PyObject *args);

#endif
11 changes: 11 additions & 0 deletions test/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,31 @@ def test_merge_none(self):
def test_merge_analysis_uptodate(self):
branch_head_hex = '5ebeeebb320790caf276b9fc8b24546d63316533'
branch_id = self.repo.get(branch_head_hex).id

analysis, preference = self.repo.merge_analysis(branch_id)
assert analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE
assert not analysis & GIT_MERGE_ANALYSIS_FASTFORWARD
assert {} == self.repo.status()

analysis, preference = self.repo.merge_analysis_for_ref('HEAD', branch_id)
assert analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE
assert not analysis & GIT_MERGE_ANALYSIS_FASTFORWARD
assert {} == self.repo.status()

def test_merge_analysis_fastforward(self):
branch_head_hex = 'e97b4cfd5db0fb4ebabf4f203979ca4e5d1c7c87'
branch_id = self.repo.get(branch_head_hex).id

analysis, preference = self.repo.merge_analysis(branch_id)
assert not analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE
assert analysis & GIT_MERGE_ANALYSIS_FASTFORWARD
assert {} == self.repo.status()

analysis, preference = self.repo.merge_analysis_for_ref('HEAD', branch_id)
assert not analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE
assert analysis & GIT_MERGE_ANALYSIS_FASTFORWARD
assert {} == self.repo.status()

def test_merge_no_fastforward_no_conflicts(self):
branch_head_hex = '03490f16b15a09913edb3a067a3dc67fbb8d41f1'
branch_id = self.repo.get(branch_head_hex).id
Expand Down