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

Skip to content

Commit de87053

Browse files
committed
settings: add a setter for a custom user-agent
1 parent 1c34b71 commit de87053

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

include/git2/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ typedef enum {
145145
GIT_OPT_GET_TEMPLATE_PATH,
146146
GIT_OPT_SET_TEMPLATE_PATH,
147147
GIT_OPT_SET_SSL_CERT_LOCATIONS,
148+
GIT_OPT_SET_USER_AGENT,
148149
} git_libgit2_opt_t;
149150

150151
/**
@@ -240,6 +241,8 @@ typedef enum {
240241
* >
241242
* > Either parameter may be `NULL`, but not both.
242243
*
244+
* * opts(GIT_OPT_SET_USER_AGENT, const char *user_agent)
245+
*
243246
* @param option Option key
244247
* @param ... value to set the option
245248
* @return 0 on success, <0 on failure

src/global.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static git_mutex *openssl_locks;
3131
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
3232
static git_atomic git__n_shutdown_callbacks;
3333
static git_atomic git__n_inits;
34+
char *git__user_agent;
3435

3536
void git__on_shutdown(git_global_shutdown_fn callback)
3637
{
@@ -269,6 +270,8 @@ int git_libgit2_shutdown(void)
269270
git_win32__crtdbg_stacktrace_cleanup();
270271
git_win32__stack_cleanup();
271272
#endif
273+
274+
git__free(git__user_agent);
272275
}
273276

274277
/* Exit the lock */
@@ -369,6 +372,7 @@ int git_libgit2_shutdown(void)
369372

370373
git__global_state_cleanup(ptr);
371374
git__free(ptr);
375+
git__free(git__user_agent);
372376

373377
pthread_key_delete(_tls_key);
374378
git_mutex_free(&git__mwindow_mutex);
@@ -423,6 +427,7 @@ int git_libgit2_shutdown(void)
423427
git__shutdown();
424428
git__global_state_cleanup(&__state);
425429
uninit_ssl();
430+
git__free(git__user_agent);
426431

427432
return 0;
428433
}

src/global.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ extern void git__on_shutdown(git_global_shutdown_fn callback);
3535

3636
extern void git__free_tls_data(void);
3737

38+
extern const char *git_libgit2__user_agent(void);
39+
3840
#endif

src/settings.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ static int config_level_to_sysdir(int config_level)
5757
return val;
5858
}
5959

60+
extern char *git__user_agent;
61+
62+
const char *git_libgit2__user_agent()
63+
{
64+
return git__user_agent;
65+
}
66+
6067
int git_libgit2_opts(int key, ...)
6168
{
6269
int error = 0;
@@ -152,6 +159,15 @@ int git_libgit2_opts(int key, ...)
152159
giterr_set(GITERR_NET, "Cannot set certificate locations: OpenSSL is not enabled");
153160
error = -1;
154161
#endif
162+
break;
163+
case GIT_OPT_SET_USER_AGENT:
164+
git__free(git__user_agent);
165+
git__user_agent = git__strdup(va_arg(ap, const char *));
166+
if (!git__user_agent) {
167+
giterr_set_oom();
168+
error = -1;
169+
}
170+
155171
break;
156172
}
157173

tests/core/useragent.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "clar_libgit2.h"
2+
#include "global.h"
3+
4+
void test_core_useragent__get(void)
5+
{
6+
const char *custom_name = "super duper git";
7+
8+
cl_assert_equal_p(NULL, git_libgit2__user_agent());
9+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_USER_AGENT, custom_name));
10+
cl_assert_equal_s(custom_name, git_libgit2__user_agent());
11+
}

0 commit comments

Comments
 (0)