1+ #define USE_THE_REPOSITORY_VARIABLE
2+
13#include "git-compat-util.h"
24#include "version.h"
35#include "version-def.h"
46#include "strbuf.h"
7+ #include "gettext.h"
8+ #include "config.h"
9+ #include "run-command.h"
10+ #include "alias.h"
511
612const char git_version_string [] = GIT_VERSION ;
713const char git_built_from_commit_string [] = GIT_BUILT_FROM_COMMIT ;
814
15+ /*
16+ * Trim and replace each character with ascii code below 32 or above
17+ * 127 (included) using a dot '.' character.
18+ * TODO: ensure consecutive non-printable characters are only replaced once
19+ */
20+ static void redact_non_printables (struct strbuf * buf )
21+ {
22+ strbuf_trim (buf );
23+ for (size_t i = 0 ; i < buf -> len ; i ++ ) {
24+ if (buf -> buf [i ] <= 32 || buf -> buf [i ] >= 127 )
25+ buf -> buf [i ] = '.' ;
26+ }
27+ }
28+
929const char * git_user_agent (void )
1030{
1131 static const char * agent = NULL ;
@@ -27,13 +47,117 @@ const char *git_user_agent_sanitized(void)
2747 struct strbuf buf = STRBUF_INIT ;
2848
2949 strbuf_addstr (& buf , git_user_agent ());
30- strbuf_trim (& buf );
31- for (size_t i = 0 ; i < buf .len ; i ++ ) {
32- if (buf .buf [i ] <= 32 || buf .buf [i ] >= 127 )
33- buf .buf [i ] = '.' ;
34- }
35- agent = buf .buf ;
50+ redact_non_printables (& buf );
51+ agent = strbuf_detach (& buf , NULL );
3652 }
3753
3854 return agent ;
3955}
56+
57+ int get_uname_info (struct strbuf * buf , unsigned int full )
58+ {
59+ struct utsname uname_info ;
60+
61+ if (uname (& uname_info )) {
62+ strbuf_addf (buf , _ ("uname() failed with error '%s' (%d)\n" ),
63+ strerror (errno ),
64+ errno );
65+ return -1 ;
66+ }
67+
68+ if (full )
69+ strbuf_addf (buf , "%s %s %s %s\n" ,
70+ uname_info .sysname ,
71+ uname_info .release ,
72+ uname_info .version ,
73+ uname_info .machine );
74+ else
75+ strbuf_addf (buf , "%s\n" , uname_info .sysname );
76+ return 0 ;
77+ }
78+
79+ /*
80+ * Return -1 if unable to retrieve the osversion.command config or
81+ * if the command is malformed; otherwise, return 0 if successful.
82+ */
83+ static int fill_os_version_command (struct child_process * cmd )
84+ {
85+ const char * os_version_command ;
86+ const char * * argv ;
87+ char * os_version_copy ;
88+ int n ;
89+
90+ if (git_config_get_string_tmp ("osversion.command" , & os_version_command ))
91+ return -1 ;
92+
93+ os_version_copy = xstrdup (os_version_command );
94+ n = split_cmdline (os_version_copy , & argv );
95+
96+ if (n < 0 ) {
97+ warning (_ ("malformed osVersion.command config option: %s" ),
98+ _ (split_cmdline_strerror (n )));
99+ free (os_version_copy );
100+ return -1 ;
101+ }
102+
103+ for (int i = 0 ; i < n ; i ++ )
104+ strvec_push (& cmd -> args , argv [i ]);
105+ free (os_version_copy );
106+ free (argv );
107+
108+ return 0 ;
109+ }
110+
111+ static int capture_os_version (struct strbuf * buf )
112+ {
113+ struct child_process cmd = CHILD_PROCESS_INIT ;
114+
115+ if (fill_os_version_command (& cmd ))
116+ return -1 ;
117+ if (capture_command (& cmd , buf , 0 ))
118+ return -1 ;
119+
120+ return 0 ;
121+ }
122+
123+ const char * os_version (void )
124+ {
125+ static const char * os = NULL ;
126+
127+ if (!os ) {
128+ struct strbuf buf = STRBUF_INIT ;
129+
130+ if (capture_os_version (& buf ))
131+ get_uname_info (& buf , 0 );
132+ os = strbuf_detach (& buf , NULL );
133+ }
134+
135+ return os ;
136+ }
137+
138+ const char * os_version_sanitized (void )
139+ {
140+ static const char * os_sanitized = NULL ;
141+
142+ if (!os_sanitized ) {
143+ struct strbuf buf = STRBUF_INIT ;
144+
145+ strbuf_addstr (& buf , os_version ());
146+ redact_non_printables (& buf );
147+ os_sanitized = strbuf_detach (& buf , NULL );
148+ }
149+
150+ return os_sanitized ;
151+ }
152+
153+ int advertise_os_version (struct repository * r )
154+ {
155+ static int transfer_advertise_os_version = -1 ;
156+
157+ if (transfer_advertise_os_version == -1 ) {
158+ repo_config_get_bool (r , "transfer.advertiseosversion" , & transfer_advertise_os_version );
159+ /* enabled by default */
160+ transfer_advertise_os_version = !!transfer_advertise_os_version ;
161+ }
162+ return transfer_advertise_os_version ;
163+ }
0 commit comments