Git fork
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "version.h"
5#include "strbuf.h"
6#include "gettext.h"
7
8#ifndef GIT_VERSION_H
9# include "version-def.h"
10#else
11# include GIT_VERSION_H
12#endif
13
14const char git_version_string[] = GIT_VERSION;
15const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
16
17/*
18 * Trim and replace each character with ascii code below 32 or above
19 * 127 (included) using a dot '.' character.
20 */
21static void redact_non_printables(struct strbuf *buf)
22{
23 strbuf_trim(buf);
24 for (size_t i = 0; i < buf->len; i++) {
25 if (!isprint(buf->buf[i]) || buf->buf[i] == ' ')
26 buf->buf[i] = '.';
27 }
28}
29
30const char *git_user_agent(void)
31{
32 static const char *agent = NULL;
33
34 if (!agent) {
35 agent = getenv("GIT_USER_AGENT");
36 if (!agent)
37 agent = GIT_USER_AGENT;
38 }
39
40 return agent;
41}
42
43/*
44 Retrieve, sanitize and cache operating system info for subsequent
45 calls. Return a pointer to the sanitized operating system info
46 string.
47*/
48static const char *os_info(void)
49{
50 static const char *os = NULL;
51
52 if (!os) {
53 struct strbuf buf = STRBUF_INIT;
54
55 get_uname_info(&buf, 0);
56 /* Sanitize the os information immediately */
57 redact_non_printables(&buf);
58 os = strbuf_detach(&buf, NULL);
59 }
60
61 return os;
62}
63
64const char *git_user_agent_sanitized(void)
65{
66 static const char *agent = NULL;
67
68 if (!agent) {
69 struct strbuf buf = STRBUF_INIT;
70
71 strbuf_addstr(&buf, git_user_agent());
72
73 if (!getenv("GIT_USER_AGENT")) {
74 strbuf_addch(&buf, '-');
75 strbuf_addstr(&buf, os_info());
76 }
77 redact_non_printables(&buf);
78 agent = strbuf_detach(&buf, NULL);
79 }
80
81 return agent;
82}
83
84int get_uname_info(struct strbuf *buf, unsigned int full)
85{
86 struct utsname uname_info;
87
88 if (uname(&uname_info)) {
89 strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"),
90 strerror(errno),
91 errno);
92 return -1;
93 }
94 if (full)
95 strbuf_addf(buf, "%s %s %s %s\n",
96 uname_info.sysname,
97 uname_info.release,
98 uname_info.version,
99 uname_info.machine);
100 else
101 strbuf_addf(buf, "%s\n", uname_info.sysname);
102 return 0;
103}