From de870533e02505f3868403dabd7699da01e4ceda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 2 Oct 2015 03:43:11 +0200 Subject: [PATCH] settings: add a setter for a custom user-agent --- include/git2/common.h | 3 +++ src/global.c | 5 +++++ src/global.h | 2 ++ src/settings.c | 16 ++++++++++++++++ tests/core/useragent.c | 11 +++++++++++ 5 files changed, 37 insertions(+) create mode 100644 tests/core/useragent.c diff --git a/include/git2/common.h b/include/git2/common.h index 577906115..bf341e79f 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -145,6 +145,7 @@ typedef enum { GIT_OPT_GET_TEMPLATE_PATH, GIT_OPT_SET_TEMPLATE_PATH, GIT_OPT_SET_SSL_CERT_LOCATIONS, + GIT_OPT_SET_USER_AGENT, } git_libgit2_opt_t; /** @@ -240,6 +241,8 @@ typedef enum { * > * > Either parameter may be `NULL`, but not both. * + * * opts(GIT_OPT_SET_USER_AGENT, const char *user_agent) + * * @param option Option key * @param ... value to set the option * @return 0 on success, <0 on failure diff --git a/src/global.c b/src/global.c index 3d37ee4de..0eab8d552 100644 --- a/src/global.c +++ b/src/global.c @@ -31,6 +31,7 @@ static git_mutex *openssl_locks; static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB]; static git_atomic git__n_shutdown_callbacks; static git_atomic git__n_inits; +char *git__user_agent; void git__on_shutdown(git_global_shutdown_fn callback) { @@ -269,6 +270,8 @@ int git_libgit2_shutdown(void) git_win32__crtdbg_stacktrace_cleanup(); git_win32__stack_cleanup(); #endif + + git__free(git__user_agent); } /* Exit the lock */ @@ -369,6 +372,7 @@ int git_libgit2_shutdown(void) git__global_state_cleanup(ptr); git__free(ptr); + git__free(git__user_agent); pthread_key_delete(_tls_key); git_mutex_free(&git__mwindow_mutex); @@ -423,6 +427,7 @@ int git_libgit2_shutdown(void) git__shutdown(); git__global_state_cleanup(&__state); uninit_ssl(); + git__free(git__user_agent); return 0; } diff --git a/src/global.h b/src/global.h index 37e909ac6..9fdcee573 100644 --- a/src/global.h +++ b/src/global.h @@ -35,4 +35,6 @@ extern void git__on_shutdown(git_global_shutdown_fn callback); extern void git__free_tls_data(void); +extern const char *git_libgit2__user_agent(void); + #endif diff --git a/src/settings.c b/src/settings.c index 2097ca314..030d28537 100644 --- a/src/settings.c +++ b/src/settings.c @@ -57,6 +57,13 @@ static int config_level_to_sysdir(int config_level) return val; } +extern char *git__user_agent; + +const char *git_libgit2__user_agent() +{ + return git__user_agent; +} + int git_libgit2_opts(int key, ...) { int error = 0; @@ -152,6 +159,15 @@ int git_libgit2_opts(int key, ...) giterr_set(GITERR_NET, "Cannot set certificate locations: OpenSSL is not enabled"); error = -1; #endif + break; + case GIT_OPT_SET_USER_AGENT: + git__free(git__user_agent); + git__user_agent = git__strdup(va_arg(ap, const char *)); + if (!git__user_agent) { + giterr_set_oom(); + error = -1; + } + break; } diff --git a/tests/core/useragent.c b/tests/core/useragent.c new file mode 100644 index 000000000..6d06693a8 --- /dev/null +++ b/tests/core/useragent.c @@ -0,0 +1,11 @@ +#include "clar_libgit2.h" +#include "global.h" + +void test_core_useragent__get(void) +{ + const char *custom_name = "super duper git"; + + cl_assert_equal_p(NULL, git_libgit2__user_agent()); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_USER_AGENT, custom_name)); + cl_assert_equal_s(custom_name, git_libgit2__user_agent()); +}