Add get user agent functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 44d868e..32925d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ v0.24 + 1
### API additions
+* You can now get the user-agent used by libgit2 using the
+ `GIT_OPT_GET_USER_AGENT` option with `git_libgit2_opts()`.
+ It is the counterpart to `GIT_OPT_SET_USER_AGENT`.
+
* `git_commit_create_buffer()` creates a commit and writes it into a
user-provided buffer instead of writing it into the object db.
diff --git a/include/git2/common.h b/include/git2/common.h
index d7428d8..18abe46 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -158,6 +158,7 @@ typedef enum {
GIT_OPT_SET_USER_AGENT,
GIT_OPT_ENABLE_STRICT_OBJECT_CREATION,
GIT_OPT_SET_SSL_CIPHERS,
+ GIT_OPT_GET_USER_AGENT,
} git_libgit2_opt_t;
/**
diff --git a/src/settings.c b/src/settings.c
index 00a3ef0..4a6e0f3 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -209,6 +209,14 @@ int git_libgit2_opts(int key, ...)
#endif
break;
+ case GIT_OPT_GET_USER_AGENT:
+ {
+ git_buf *out = va_arg(ap, git_buf *);
+ git_buf_sanitize(out);
+ error = git_buf_sets(out, git__user_agent);
+ }
+ break;
+
default:
giterr_set(GITERR_INVALID, "invalid option key");
error = -1;
diff --git a/tests/core/useragent.c b/tests/core/useragent.c
index 6d06693..5c09223 100644
--- a/tests/core/useragent.c
+++ b/tests/core/useragent.c
@@ -4,8 +4,14 @@
void test_core_useragent__get(void)
{
const char *custom_name = "super duper git";
+ git_buf buf = GIT_BUF_INIT;
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());
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_GET_USER_AGENT, &buf));
+ cl_assert_equal_s(custom_name, buf.ptr);
+
+ git_buf_free(&buf);
}