Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support custom git extensions #6031

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 2 commits into from
Sep 14, 2021
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
20 changes: 19 additions & 1 deletion include/git2/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ typedef enum {
GIT_OPT_GET_MWINDOW_FILE_LIMIT,
GIT_OPT_SET_MWINDOW_FILE_LIMIT,
GIT_OPT_SET_ODB_PACKED_PRIORITY,
GIT_OPT_SET_ODB_LOOSE_PRIORITY
GIT_OPT_SET_ODB_LOOSE_PRIORITY,
GIT_OPT_GET_EXTENSIONS,
GIT_OPT_SET_EXTENSIONS
} git_libgit2_opt_t;

/**
Expand Down Expand Up @@ -431,6 +433,22 @@ typedef enum {
* > Override the default priority of the loose ODB backend which
* > is added when default backends are assigned to a repository
*
* opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out)
* > Returns the list of git extensions that are supported. This
* > is the list of built-in extensions supported by libgit2 and
* > custom extensions that have been added with
* > `GIT_OPT_SET_EXTENSIONS`. Extensions that have been negated
* > will not be returned. The returned list should be released
* > with `git_strarray_dispose`.
*
* opts(GIT_OPT_SET_EXTENSIONS, const char **extensions, size_t len)
* > Set that the given git extensions are supported by the caller.
* > Extensions supported by libgit2 may be negated by prefixing
* > them with a `!`. For example: setting extensions to
* > { "!noop", "newext" } indicates that the caller does not want
* > to support repositories with the `noop` extension but does want
* > to support repositories with the `newext` extension.
*
* @param option Option key
* @param ... value to set the option
* @return 0 on success, <0 on failure
Expand Down
23 changes: 23 additions & 0 deletions src/libgit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void libgit2_settings_global_shutdown(void)
{
git__free(git__user_agent);
git__free(git__ssl_ciphers);
git_repository__free_extensions();
}

static int git_libgit2_settings_global_init(void)
Expand Down Expand Up @@ -367,6 +368,28 @@ int git_libgit2_opts(int key, ...)
git_odb__loose_priority = va_arg(ap, int);
break;

case GIT_OPT_SET_EXTENSIONS:
Copy link
Contributor

Choose a reason for hiding this comment

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

(code indent looks incorrect)

{
const char **extensions = va_arg(ap, const char **);
size_t len = va_arg(ap, size_t);
error = git_repository__set_extensions(extensions, len);
}
break;

case GIT_OPT_GET_EXTENSIONS:
{
git_strarray *out = va_arg(ap, git_strarray *);
char **extensions;
size_t len;

if ((error = git_repository__extensions(&extensions, &len)) < 0)
break;

out->strings = extensions;
out->count = len;
}
break;

default:
git_error_set(GIT_ERROR_INVALID, "invalid option key");
error = -1;
Expand Down
115 changes: 112 additions & 3 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,15 +1427,60 @@ static int check_repositoryformatversion(int *version, git_config *config)
return 0;
}

static const char *builtin_extensions[] = {
"noop"
};

static git_vector user_extensions = GIT_VECTOR_INIT;

static int check_valid_extension(const git_config_entry *entry, void *payload)
{
git_buf cfg = GIT_BUF_INIT;
bool reject;
const char *extension;
size_t i;
int error = 0;

GIT_UNUSED(payload);

if (!strcmp(entry->name, "extensions.noop"))
return 0;
git_vector_foreach (&user_extensions, i, extension) {
git_buf_clear(&cfg);

/*
* Users can specify that they don't want to support an
* extension with a '!' prefix.
*/
if ((reject = (extension[0] == '!')) == true)
extension = &extension[1];

if ((error = git_buf_printf(&cfg, "extensions.%s", extension)) < 0)
goto done;

if (strcmp(entry->name, cfg.ptr) == 0) {
if (reject)
goto fail;

goto done;
}
}

for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) {
extension = builtin_extensions[i];

if ((error = git_buf_printf(&cfg, "extensions.%s", extension)) < 0)
goto done;

if (strcmp(entry->name, cfg.ptr) == 0)
goto done;
}

fail:
git_error_set(GIT_ERROR_REPOSITORY, "unsupported extension name %s", entry->name);
return -1;
error = -1;

done:
git_buf_dispose(&cfg);
return error;
}

static int check_extensions(git_config *config, int version)
Expand All @@ -1446,6 +1491,70 @@ static int check_extensions(git_config *config, int version)
return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL);
}

int git_repository__extensions(char ***out, size_t *out_len)
{
git_vector extensions;
const char *builtin, *user;
char *extension;
size_t i, j;

if (git_vector_init(&extensions, 8, NULL) < 0)
return -1;

for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) {
bool match = false;

builtin = builtin_extensions[i];

git_vector_foreach (&user_extensions, j, user) {
if (user[0] == '!' && strcmp(builtin, &user[1]) == 0) {
match = true;
break;
}
}

if (match)
continue;

if ((extension = git__strdup(builtin)) == NULL ||
git_vector_insert(&extensions, extension) < 0)
return -1;
}

git_vector_foreach (&user_extensions, i, user) {
if (user[0] == '!')
continue;

if ((extension = git__strdup(user)) == NULL ||
git_vector_insert(&extensions, extension) < 0)
return -1;
}

*out = (char **)git_vector_detach(out_len, NULL, &extensions);
return 0;
}

int git_repository__set_extensions(const char **extensions, size_t len)
{
char *extension;
size_t i;

git_repository__free_extensions();

for (i = 0; i < len; i++) {
if ((extension = git__strdup(extensions[i])) == NULL ||
git_vector_insert(&user_extensions, extension) < 0)
return -1;
}

return 0;
}

void git_repository__free_extensions(void)
{
git_vector_free_deep(&user_extensions);
}

int git_repository_create_head(const char *git_dir, const char *ref_name)
{
git_buf ref_path = GIT_BUF_INIT;
Expand Down
4 changes: 4 additions & 0 deletions src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,8 @@ int git_repository_initialbranch(git_buf *out, git_repository *repo);
*/
int git_repository_workdir_path(git_buf *out, git_repository *repo, const char *path);

int git_repository__extensions(char ***out, size_t *out_len);
int git_repository__set_extensions(const char **extensions, size_t len);
void git_repository__free_extensions(void);

#endif
46 changes: 46 additions & 0 deletions tests/core/opts.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "clar_libgit2.h"
#include "cache.h"

void test_core_opts__cleanup(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, NULL, 0));
}

void test_core_opts__readwrite(void)
{
size_t old_val = 0;
Expand All @@ -23,3 +28,44 @@ void test_core_opts__invalid_option(void)
cl_git_fail(git_libgit2_opts(-1, "foobar"));
}

void test_core_opts__extensions_query(void)
{
git_strarray out = { 0 };

cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));

cl_assert_equal_sz(out.count, 1);
cl_assert_equal_s("noop", out.strings[0]);

git_strarray_dispose(&out);
}

void test_core_opts__extensions_add(void)
{
const char *in[] = { "foo" };
git_strarray out = { 0 };

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));

cl_assert_equal_sz(out.count, 2);
cl_assert_equal_s("noop", out.strings[0]);
cl_assert_equal_s("foo", out.strings[1]);

git_strarray_dispose(&out);
}

void test_core_opts__extensions_remove(void)
{
const char *in[] = { "bar", "!negate", "!noop", "baz" };
git_strarray out = { 0 };

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));

cl_assert_equal_sz(out.count, 2);
cl_assert_equal_s("bar", out.strings[0]);
cl_assert_equal_s("baz", out.strings[1]);

git_strarray_dispose(&out);
}
72 changes: 72 additions & 0 deletions tests/repo/extensions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "clar_libgit2.h"
#include "futils.h"
#include "sysdir.h"
#include <ctype.h>

git_repository *repo;

void test_repo_extensions__initialize(void)
{
git_config *config;

repo = cl_git_sandbox_init("empty_bare.git");

cl_git_pass(git_repository_config(&config, repo));
cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
git_config_free(config);
}

void test_repo_extensions__cleanup(void)
{
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, NULL, 0));
}

void test_repo_extensions__builtin(void)
{
git_repository *extended;

cl_repo_set_string(repo, "extensions.noop", "foobar");

cl_git_pass(git_repository_open(&extended, "empty_bare.git"));
cl_assert(git_repository_path(extended) != NULL);
cl_assert(git__suffixcmp(git_repository_path(extended), "/") == 0);
git_repository_free(extended);
}

void test_repo_extensions__negate_builtin(void)
{
const char *in[] = { "foo", "!noop", "baz" };
git_repository *extended;

cl_repo_set_string(repo, "extensions.noop", "foobar");

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));

cl_git_fail(git_repository_open(&extended, "empty_bare.git"));
git_repository_free(extended);
}

void test_repo_extensions__unsupported(void)
{
git_repository *extended = NULL;

cl_repo_set_string(repo, "extensions.unknown", "foobar");

cl_git_fail(git_repository_open(&extended, "empty_bare.git"));
git_repository_free(extended);
}

void test_repo_extensions__adds_extension(void)
{
const char *in[] = { "foo", "!noop", "newextension", "baz" };
git_repository *extended;

cl_repo_set_string(repo, "extensions.newextension", "foobar");
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));

cl_git_pass(git_repository_open(&extended, "empty_bare.git"));
cl_assert(git_repository_path(extended) != NULL);
cl_assert(git__suffixcmp(git_repository_path(extended), "/") == 0);
git_repository_free(extended);
}
42 changes: 0 additions & 42 deletions tests/repo/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,6 @@ void test_repo_open__format_version_1(void)
git_repository_free(repo);
}

void test_repo_open__format_version_1_with_valid_extension(void)
{
git_repository *repo;
git_config *config;

repo = cl_git_sandbox_init("empty_bare.git");

cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
cl_git_pass(git_repository_config(&config, repo));

cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
cl_git_pass(git_config_set_int32(config, "extensions.noop", 1));

git_config_free(config);
git_repository_free(repo);

cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
cl_assert(git_repository_path(repo) != NULL);
cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
git_repository_free(repo);
}

void test_repo_open__format_version_1_with_invalid_extension(void)
{
git_repository *repo;
git_config *config;

repo = cl_git_sandbox_init("empty_bare.git");

cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
cl_git_pass(git_repository_config(&config, repo));

cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
cl_git_pass(git_config_set_int32(config, "extensions.invalid", 1));

git_config_free(config);
git_repository_free(repo);

cl_git_fail(git_repository_open(&repo, "empty_bare.git"));
git_repository_free(repo);
}

void test_repo_open__standard_empty_repo_through_gitdir(void)
{
git_repository *repo;
Expand Down