libgit2 / libgit2

A cross-platform, linkable library implementation of Git that you can use in your application.

Home Page:https://libgit2.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot delete or rename a branch whose config section contains multivar entries

samueltardieu opened this issue · comments

If a branch configuration contains duplicated variables (multivar), the branch can neither be removed nor renamed using libgit2. This has been noticed using git branchless which uses git2-rs which uses libgit2, in combination with git publish. git publish stores several gitpublishto and gitpublishcc entries when a patch series is sent via email, with the various recipients. When a branch is merged upstream, git branchless sync --pull will attempt to remove it and fail because of multiple entries.

Reproduction steps

Create a repository and set multivar config entries for this branch, for example:

[branch.br2]
gitpublishto = example1@example.com
gitpublishto = example2@example.com

Then delete this branch using libgit2.

All this can be done with a test in tests/libgit2/branches/delete.c:

void test_refs_branches_delete__can_delete_a_local_branch_with_multivar(void)
{
	git_reference *branch;
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_set_multivar(
	        cfg, "branch.br2.gitpublishto", "^$", "example1@example.com"));
	cl_git_pass(git_config_set_multivar(
	        cfg, "branch.br2.gitpublishto", "^$", "example2@example.com"));
	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
	git_reference_free(branch);
}

Expected behavior

The branch can be deleted.

Actual behavior

The branch cannot be deleted because of an error: "entry is not unique due to being a multivar". This is due to the config section deleting (or renaming).

Version of libgit2 (release number or SHA1)

25e2b9d

Operating system(s) tested

NixOS (Linux)

Resolution

A PR follows.