Skip to content

Commit c3231ae

Browse files
Unique-Usmangitster
authored andcommitted
connect: advertise OS version
As some issues that can happen with a Git client can be operating system specific, it can be useful for a server to know which OS a client is using. In the same way it can be useful for a client to know which OS a server is using. Let's introduce a new protocol (`os-version`) allowing Git clients and servers to exchange operating system information. The protocol is controlled by the new `transfer.advertiseOSVersion` config option. Add the `transfer.advertiseOSVersion` config option to address privacy concerns issue. It defaults to `true` and can be changed to `false`. When enabled, this option makes clients and servers send each other the OS name (e.g., "Linux" or "Windows"). The information is retrieved using the 'sysname' field of the `uname(2)` system call. However, there are differences between `uname(1)` (command-line utility) and `uname(2)` (system call) outputs on Windows. These discrepancies complicate testing on Windows platforms. For example: - `uname(1)` output: MINGW64_NT-10.0-20348.3.4.10-87d57229.x86_64\ .2024-02-14.20:17.UTC.x86_64 - `uname(2)` output: Windows.10.0.20348 Until a good way to test the feature on Windows is found, the transfer.advertiseOSVersion is set to false on Windows during testing. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 54db285 commit c3231ae

File tree

9 files changed

+122
-2
lines changed

9 files changed

+122
-2
lines changed

Documentation/config/transfer.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,10 @@ transfer.bundleURI::
125125
transfer.advertiseObjectInfo::
126126
When `true`, the `object-info` capability is advertised by
127127
servers. Defaults to false.
128+
129+
transfer.advertiseOSVersion::
130+
When `true`, the `os-version` capability is advertised by clients and
131+
servers. It makes clients and servers send to each other a string
132+
representing the operating system name, like "Linux" or "Windows".
133+
This string is retrieved from the 'sysname' field of the struct returned
134+
by the uname(2) system call. Defaults to true.

Documentation/gitprotocol-v2.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ printable ASCII characters except space (i.e., the byte range 32 < x <
190190
and debugging purposes, and MUST NOT be used to programmatically assume
191191
the presence or absence of particular features.
192192

193+
os-version
194+
~~~~~~~~~~
195+
196+
In the same way as the `agent` capability above, the server can
197+
advertise the `os-version` capability with a value `X` (in the form
198+
`os-version=X`) to notify the client that the server is running an
199+
operating system that can be identified by `X`. The client may
200+
optionally send its own `os-version` string by including the
201+
`os-version` capability with a value `Y` (in the form `os-version=Y`)
202+
in its request to the server (but it MUST NOT do so if the server did
203+
not advertise the os-version capability). The `X` and `Y` strings may
204+
contain any printable ASCII characters except space (i.e., the byte
205+
range 32 < x < 127), and are typically made from the result of
206+
`uname -s`(OS name e.g Linux). The os-version capability can be disabled
207+
entirely by setting the `transfer.advertiseOSVersion` config option
208+
to `false`. The `os-version` strings are purely informative for
209+
statistics and debugging purposes, and MUST NOT be used to
210+
programmatically assume the presence or absence of particular
211+
features.
212+
193213
ls-refs
194214
~~~~~~~
195215

connect.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ static void send_capabilities(int fd_out, struct packet_reader *reader)
492492
if (server_supports_v2("agent"))
493493
packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
494494

495+
if (server_supports_v2("os-version") && advertise_os_version(the_repository))
496+
packet_write_fmt(fd_out, "os-version=%s", os_version_sanitized());
497+
495498
if (server_feature_v2("object-format", &hash_name)) {
496499
int hash_algo = hash_algo_by_name(hash_name);
497500
if (hash_algo == GIT_HASH_UNKNOWN)

serve.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ static int agent_advertise(struct repository *r UNUSED,
3131
return 1;
3232
}
3333

34+
static int os_version_advertise(struct repository *r,
35+
struct strbuf *value)
36+
{
37+
if (!advertise_os_version(r))
38+
return 0;
39+
if (value)
40+
strbuf_addstr(value, os_version_sanitized());
41+
return 1;
42+
}
43+
3444
static int object_format_advertise(struct repository *r,
3545
struct strbuf *value)
3646
{
@@ -123,6 +133,10 @@ static struct protocol_capability capabilities[] = {
123133
.name = "agent",
124134
.advertise = agent_advertise,
125135
},
136+
{
137+
.name = "os-version",
138+
.advertise = os_version_advertise,
139+
},
126140
{
127141
.name = "ls-refs",
128142
.advertise = ls_refs_advertise,

t/t5555-http-smart-common.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,19 @@ test_expect_success 'git receive-pack --advertise-refs: v1' '
123123
'
124124

125125
test_expect_success 'git upload-pack --advertise-refs: v2' '
126+
printf "agent=FAKE" >agent_and_os_name &&
127+
if test_have_prereq WINDOWS
128+
then
129+
# We do not use test_config here so that any tests below can reuse
130+
# the "expect" file from this test
131+
git config transfer.advertiseOSVersion false
132+
else
133+
printf "\nos-version=%s\n" $(uname -s | test_redact_non_printables) >>agent_and_os_name
134+
fi &&
135+
126136
cat >expect <<-EOF &&
127137
version 2
128-
agent=FAKE
138+
$(cat agent_and_os_name)
129139
ls-refs=unborn
130140
fetch=shallow wait-for-done
131141
server-option

t/t5701-git-serve.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
88
. ./test-lib.sh
99

1010
test_expect_success 'test capability advertisement' '
11+
printf "agent=git/$(git version | cut -d" " -f3)" >agent_and_os_name &&
12+
if test_have_prereq WINDOWS
13+
then
14+
# We do not use test_config here so that tests below will be able to reuse
15+
# the expect.base and expect.trailer files
16+
git config transfer.advertiseOSVersion false
17+
else
18+
printf "\nos-version=%s\n" $(uname -s | test_redact_non_printables) >>agent_and_os_name
19+
fi &&
20+
1121
test_oid_cache <<-EOF &&
1222
wrong_algo sha1:sha256
1323
wrong_algo sha256:sha1
1424
EOF
1525
cat >expect.base <<-EOF &&
1626
version 2
17-
agent=git/$(git version | cut -d" " -f3)
27+
$(cat agent_and_os_name)
1828
ls-refs=unborn
1929
fetch=shallow wait-for-done
2030
server-option

t/test-lib-functions.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,3 +2007,11 @@ test_trailing_hash () {
20072007
test-tool hexdump |
20082008
sed "s/ //g"
20092009
}
2010+
2011+
# Trim and replace each character with ascii code below 32 or above
2012+
# 127 (included) using a dot '.' character.
2013+
# Octal intervals \001-\040 and \177-\377
2014+
# corresponds to decimal intervals 1-32 and 127-255
2015+
test_redact_non_printables () {
2016+
tr -d "\n" | tr "[\001-\040][\177-\377]" "."
2017+
}

version.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "version-def.h"
44
#include "strbuf.h"
55
#include "gettext.h"
6+
#include "config.h"
67

78
const char git_version_string[] = GIT_VERSION;
89
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
@@ -70,3 +71,44 @@ int get_uname_info(struct strbuf *buf, unsigned int full)
7071
strbuf_addf(buf, "%s\n", uname_info.sysname);
7172
return 0;
7273
}
74+
75+
const char *os_version(void)
76+
{
77+
static const char *os = NULL;
78+
79+
if (!os) {
80+
struct strbuf buf = STRBUF_INIT;
81+
82+
get_uname_info(&buf, 0);
83+
os = strbuf_detach(&buf, NULL);
84+
}
85+
86+
return os;
87+
}
88+
89+
const char *os_version_sanitized(void)
90+
{
91+
static const char *os_sanitized = NULL;
92+
93+
if (!os_sanitized) {
94+
struct strbuf buf = STRBUF_INIT;
95+
96+
strbuf_addstr(&buf, os_version());
97+
redact_non_printables(&buf);
98+
os_sanitized = strbuf_detach(&buf, NULL);
99+
}
100+
101+
return os_sanitized;
102+
}
103+
104+
int advertise_os_version(struct repository *r)
105+
{
106+
static int transfer_advertise_os_version = -1;
107+
108+
if (transfer_advertise_os_version == -1) {
109+
repo_config_get_bool(r, "transfer.advertiseosversion", &transfer_advertise_os_version);
110+
/* enabled by default */
111+
transfer_advertise_os_version = !!transfer_advertise_os_version;
112+
}
113+
return transfer_advertise_os_version;
114+
}

version.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef VERSION_H
22
#define VERSION_H
33

4+
struct repository;
5+
46
extern const char git_version_string[];
57
extern const char git_built_from_commit_string[];
68

@@ -14,4 +16,8 @@ const char *git_user_agent_sanitized(void);
1416
*/
1517
int get_uname_info(struct strbuf *buf, unsigned int full);
1618

19+
const char *os_version(void);
20+
const char *os_version_sanitized(void);
21+
int advertise_os_version(struct repository *r);
22+
1723
#endif /* VERSION_H */

0 commit comments

Comments
 (0)