Skip to content

Commit ff5effd

Browse files
peffgitster
authored andcommitted
include agent identifier in capability string
Instead of having the client advertise a particular version number in the git protocol, we have managed extensions and backwards compatibility by having clients and servers advertise capabilities that they support. This is far more robust than having each side consult a table of known versions, and provides sufficient information for the protocol interaction to complete. However, it does not allow servers to keep statistics on which client versions are being used. This information is not necessary to complete the network request (the capabilities provide enough information for that), but it may be helpful to conduct a general survey of client versions in use. We already send the client version in the user-agent header for http requests; adding it here allows us to gather similar statistics for non-http requests. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 745c7c8 commit ff5effd

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

builtin/fetch-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "remote.h"
1111
#include "run-command.h"
1212
#include "transport.h"
13+
#include "version.h"
1314

1415
static int transfer_unpack_limit = -1;
1516
static int fetch_unpack_limit = -1;
@@ -327,6 +328,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
327328
if (args.no_progress) strbuf_addstr(&c, " no-progress");
328329
if (args.include_tag) strbuf_addstr(&c, " include-tag");
329330
if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
331+
strbuf_addf(&c, " agent=%s", git_user_agent_sanitized());
330332
packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
331333
strbuf_release(&c);
332334
} else

builtin/receive-pack.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "string-list.h"
1313
#include "sha1-array.h"
1414
#include "connected.h"
15+
#include "version.h"
1516

1617
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
1718

@@ -121,10 +122,11 @@ static void show_ref(const char *path, const unsigned char *sha1)
121122
if (sent_capabilities)
122123
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
123124
else
124-
packet_write(1, "%s %s%c%s%s\n",
125+
packet_write(1, "%s %s%c%s%s agent=%s\n",
125126
sha1_to_hex(sha1), path, 0,
126127
" report-status delete-refs side-band-64k quiet",
127-
prefer_ofs_delta ? " ofs-delta" : "");
128+
prefer_ofs_delta ? " ofs-delta" : "",
129+
git_user_agent_sanitized());
128130
sent_capabilities = 1;
129131
}
130132

builtin/send-pack.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "send-pack.h"
99
#include "quote.h"
1010
#include "transport.h"
11+
#include "version.h"
1112

1213
static const char send_pack_usage[] =
1314
"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
@@ -306,11 +307,13 @@ int send_pack(struct send_pack_args *args,
306307
int quiet = quiet_supported && (args->quiet || !args->progress);
307308

308309
if (!cmds_sent && (status_report || use_sideband || args->quiet)) {
309-
packet_buf_write(&req_buf, "%s %s %s%c%s%s%s",
310+
packet_buf_write(&req_buf,
311+
"%s %s %s%c%s%s%s agent=%s",
310312
old_hex, new_hex, ref->name, 0,
311313
status_report ? " report-status" : "",
312314
use_sideband ? " side-band-64k" : "",
313-
quiet ? " quiet" : "");
315+
quiet ? " quiet" : "",
316+
git_user_agent_sanitized());
314317
}
315318
else
316319
packet_buf_write(&req_buf, "%s %s %s",

upload-pack.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "list-objects.h"
1212
#include "run-command.h"
1313
#include "sigchain.h"
14+
#include "version.h"
1415

1516
static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
1617

@@ -734,9 +735,11 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
734735
}
735736

736737
if (capabilities)
737-
packet_write(1, "%s %s%c%s%s\n", sha1_to_hex(sha1), refname_nons,
738+
packet_write(1, "%s %s%c%s%s agent=%s\n",
739+
sha1_to_hex(sha1), refname_nons,
738740
0, capabilities,
739-
stateless_rpc ? " no-done" : "");
741+
stateless_rpc ? " no-done" : "",
742+
git_user_agent_sanitized());
740743
else
741744
packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
742745
capabilities = NULL;

version.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "version.h"
3+
#include "strbuf.h"
34

45
const char git_version_string[] = GIT_VERSION;
56

@@ -15,3 +16,23 @@ const char *git_user_agent(void)
1516

1617
return agent;
1718
}
19+
20+
const char *git_user_agent_sanitized(void)
21+
{
22+
static const char *agent = NULL;
23+
24+
if (!agent) {
25+
struct strbuf buf = STRBUF_INIT;
26+
int i;
27+
28+
strbuf_addstr(&buf, git_user_agent());
29+
strbuf_trim(&buf);
30+
for (i = 0; i < buf.len; i++) {
31+
if (buf.buf[i] <= 32 || buf.buf[i] >= 127)
32+
buf.buf[i] = '.';
33+
}
34+
agent = buf.buf;
35+
}
36+
37+
return agent;
38+
}

version.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
extern const char git_version_string[];
55

66
const char *git_user_agent(void);
7+
const char *git_user_agent_sanitized(void);
78

89
#endif /* VERSION_H */

0 commit comments

Comments
 (0)