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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 3, 2012 at 12:19 UTC ff5effdf455bd6e694ff0bb3e0793515a2214adb
6 files changed +38 -6
builtin/fetch-pack.c
+2
@@ -10,6 +10,7 @@
10 #include "remote.h"
11 #include "run-command.h"
12 #include "transport.h"
13 +#include "version.h"
14
15 static int transfer_unpack_limit = -1;
16 static int fetch_unpack_limit = -1;
@@ -327,6 +328,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
328 if (args.no_progress) strbuf_addstr(&c, " no-progress");
329 if (args.include_tag) strbuf_addstr(&c, " include-tag");
330 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
331 + strbuf_addf(&c, " agent=%s", git_user_agent_sanitized());
332 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
333 strbuf_release(&c);
334 } else
builtin/receive-pack.c
+4 -2
@@ -12,6 +12,7 @@
12 #include "string-list.h"
13 #include "sha1-array.h"
14 #include "connected.h"
15 +#include "version.h"
16
17 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
18
@@ -121,10 +122,11 @@ static void show_ref(const char *path, const unsigned char *sha1)
122 if (sent_capabilities)
123 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
124 else
124 - packet_write(1, "%s %s%c%s%s\n",
125 + packet_write(1, "%s %s%c%s%s agent=%s\n",
126 sha1_to_hex(sha1), path, 0,
127 " 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());
130 sent_capabilities = 1;
131 }
132
builtin/send-pack.c
+5 -2
@@ -8,6 +8,7 @@
8 #include "send-pack.h"
9 #include "quote.h"
10 #include "transport.h"
11 +#include "version.h"
12
13 static const char send_pack_usage[] =
14 "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,
307 int quiet = quiet_supported && (args->quiet || !args->progress);
308
309 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",
312 old_hex, new_hex, ref->name, 0,
313 status_report ? " report-status" : "",
314 use_sideband ? " side-band-64k" : "",
313 - quiet ? " quiet" : "");
315 + quiet ? " quiet" : "",
316 + git_user_agent_sanitized());
317 }
318 else
319 packet_buf_write(&req_buf, "%s %s %s",
upload-pack.c
+5 -2
@@ -11,6 +11,7 @@
11 #include "list-objects.h"
12 #include "run-command.h"
13 #include "sigchain.h"
14 +#include "version.h"
15
16 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
17
@@ -734,9 +735,11 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
735 }
736
737 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,
740 0, capabilities,
739 - stateless_rpc ? " no-done" : "");
741 + stateless_rpc ? " no-done" : "",
742 + git_user_agent_sanitized());
743 else
744 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
745 capabilities = NULL;
version.c
+21
@@ -1,5 +1,6 @@
1 #include "git-compat-util.h"
2 #include "version.h"
3 +#include "strbuf.h"
4
5 const char git_version_string[] = GIT_VERSION;
6
@@ -15,3 +16,23 @@ const char *git_user_agent(void)
16
17 return agent;
18 }
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
+1
@@ -4,5 +4,6 @@
4 extern const char git_version_string[];
5
6 const char *git_user_agent(void);
7 +const char *git_user_agent_sanitized(void);
8
9 #endif /* VERSION_H */