|
| 1 | +#define USE_THE_REPOSITORY_VARIABLE |
| 2 | + |
1 | 3 | #include "git-compat-util.h"
|
2 | 4 | #include "version.h"
|
3 | 5 | #include "strbuf.h"
|
|
9 | 11 | # include GIT_VERSION_H
|
10 | 12 | #endif
|
11 | 13 |
|
| 14 | +#include "gettext.h" |
| 15 | +#include "config.h" |
| 16 | + |
12 | 17 | const char git_version_string[] = GIT_VERSION;
|
13 | 18 | const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
|
14 | 19 |
|
| 20 | +/* |
| 21 | + * Trim and replace each character with ascii code below 32 or above |
| 22 | + * 127 (included) using a dot '.' character. |
| 23 | + */ |
15 | 24 | void redact_non_printables(struct strbuf *buf)
|
16 | 25 | {
|
17 | 26 | strbuf_trim(buf);
|
@@ -43,8 +52,63 @@ const char *git_user_agent_sanitized(void)
|
43 | 52 |
|
44 | 53 | strbuf_addstr(&buf, git_user_agent());
|
45 | 54 | redact_non_printables(&buf);
|
| 55 | + /* Add os name if the transfer.advertiseosinfo config is true */ |
| 56 | + if (advertise_os_info()) { |
| 57 | + /* Add space to space character after git version string */ |
| 58 | + strbuf_addch(&buf, ' '); |
| 59 | + strbuf_addstr(&buf, os_info_sanitized()); |
| 60 | + } |
46 | 61 | agent = strbuf_detach(&buf, NULL);
|
47 | 62 | }
|
48 | 63 |
|
49 | 64 | return agent;
|
50 | 65 | }
|
| 66 | + |
| 67 | +int get_uname_info(struct strbuf *buf, unsigned int full) |
| 68 | +{ |
| 69 | + struct utsname uname_info; |
| 70 | + |
| 71 | + if (uname(&uname_info)) { |
| 72 | + strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"), |
| 73 | + strerror(errno), |
| 74 | + errno); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + if (full) |
| 78 | + strbuf_addf(buf, "%s %s %s %s\n", |
| 79 | + uname_info.sysname, |
| 80 | + uname_info.release, |
| 81 | + uname_info.version, |
| 82 | + uname_info.machine); |
| 83 | + else |
| 84 | + strbuf_addf(buf, "%s\n", uname_info.sysname); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +const char *os_info_sanitized(void) |
| 89 | +{ |
| 90 | + static const char *os = NULL; |
| 91 | + |
| 92 | + if (!os) { |
| 93 | + struct strbuf buf = STRBUF_INIT; |
| 94 | + |
| 95 | + get_uname_info(&buf, 0); |
| 96 | + /* Sanitize the os information immediately */ |
| 97 | + redact_non_printables(&buf); |
| 98 | + os = strbuf_detach(&buf, NULL); |
| 99 | + } |
| 100 | + |
| 101 | + return os; |
| 102 | +} |
| 103 | + |
| 104 | +int advertise_os_info(void) |
| 105 | +{ |
| 106 | + static int transfer_advertise_os_info= -1; |
| 107 | + |
| 108 | + if (transfer_advertise_os_info == -1) { |
| 109 | + repo_config_get_bool(the_repository, "transfer.advertiseosinfo", &transfer_advertise_os_info); |
| 110 | + /* enabled by default */ |
| 111 | + transfer_advertise_os_info = !!transfer_advertise_os_info; |
| 112 | + } |
| 113 | + return transfer_advertise_os_info; |
| 114 | +} |
0 commit comments