git/version.c
Christian Couder 31c5b4b16f version: refactor strbuf_sanitize()
The git_user_agent_sanitized() function performs some sanitizing to
avoid special characters being sent over the line and possibly messing
up with the protocol or with the parsing on the other side.

Let's extract this sanitizing into a new strbuf_sanitize() function, as
we will want to reuse it in a following patch, and let's put it into
strbuf.{c,h}.

While at it, let's also make a few small improvements:
  - use 'size_t' for 'i' instead of 'int',
  - move the declaration of 'i' inside the 'for ( ... )',
  - use strbuf_detach() to explicitly detach the string contained by
    the 'sb' strbuf.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-09-10 10:40:56 -07:00

35 lines
625 B
C

#include "git-compat-util.h"
#include "version.h"
#include "strbuf.h"
const char git_version_string[] = GIT_VERSION;
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
const char *git_user_agent(void)
{
static const char *agent = NULL;
if (!agent) {
agent = getenv("GIT_USER_AGENT");
if (!agent)
agent = GIT_USER_AGENT;
}
return agent;
}
const char *git_user_agent_sanitized(void)
{
static const char *agent = NULL;
if (!agent) {
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, git_user_agent());
strbuf_sanitize(&buf);
agent = strbuf_detach(&buf, NULL);
}
return agent;
}