Skip to content
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

refs: make git_reference_cmp consider the name #6346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libgit2/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1054,10 +1054,14 @@ int git_reference_cmp(
const git_reference *ref2)
{
git_reference_t type1, type2;
int ret;

GIT_ASSERT_ARG(ref1);
GIT_ASSERT_ARG(ref2);

if ((ret = strcmp(ref1->name, ref2->name)) != 0)
Copy link

Choose a reason for hiding this comment

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

Why is it necessary to continue inspecting the references if they have identical names? Is it so that "outdated" git_reference values compare as different?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, that's the most likely scenario, I would expect. You load a reference, mutate it, and then check an existing reference to the new reference.

return ret;

type1 = git_reference_type(ref1);
type2 = git_reference_type(ref2);

Expand Down
27 changes: 27 additions & 0 deletions tests/libgit2/refs/cmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "clar_libgit2.h"
#include "refs.h"

static git_repository *g_repo;

void test_refs_cmp__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo2");
}

void test_refs_cmp__cleanup(void)
{
cl_git_sandbox_cleanup();
}

void test_refs_cmp__symbolic(void)
{
git_reference *one, *two;

cl_git_pass(git_reference_lookup(&one, g_repo, "refs/heads/symbolic-one"));
cl_git_pass(git_reference_lookup(&two, g_repo, "refs/heads/symbolic-two"));

cl_assert(git_reference_cmp(one, two) != 0);

git_reference_free(one);
git_reference_free(two);
}
1 change: 1 addition & 0 deletions tests/resources/testrepo2/.gitted/refs/heads/symbolic-one
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
1 change: 1 addition & 0 deletions tests/resources/testrepo2/.gitted/refs/heads/symbolic-two
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master