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