upload-pack: advertise session ID in v0 capabilities

When transfer.advertiseSID is true, advertise upload-pack's session ID via the new session-id capability. Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Josh Steadmon committed Nov 11, 2020 at 15:29 UTC 791e1adf2236a9bb3e6a40da64c00614e39e15b7
1 file changed +14 -1
upload-pack.c
+14 -1
@@ -110,6 +110,7 @@ struct upload_pack_data {
110 unsigned done : 1; /* v2 only */
111 unsigned allow_ref_in_want : 1; /* v2 only */
112 unsigned allow_sideband_all : 1; /* v2 only */
113 + unsigned advertise_sid : 1;
114 };
115
116 static void upload_pack_data_init(struct upload_pack_data *data)
@@ -141,6 +142,7 @@ static void upload_pack_data_init(struct upload_pack_data *data)
142 packet_writer_init(&data->writer, 1);
143
144 data->keepalive = 5;
145 + data->advertise_sid = 0;
146 }
147
148 static void upload_pack_data_clear(struct upload_pack_data *data)
@@ -1178,6 +1180,11 @@ static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1180 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1181 }
1182
1183 +static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1184 + if (d->advertise_sid)
1185 + strbuf_addf(buf, " session-id=%s", trace2_session_id());
1186 +}
1187 +
1188 static int send_ref(const char *refname, const struct object_id *oid,
1189 int flag, void *cb_data)
1190 {
@@ -1193,9 +1200,11 @@ static int send_ref(const char *refname, const struct object_id *oid,
1200
1201 if (capabilities) {
1202 struct strbuf symref_info = STRBUF_INIT;
1203 + struct strbuf session_id = STRBUF_INIT;
1204
1205 format_symref_info(&symref_info, &data->symref);
1198 - packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
1206 + format_session_id(&session_id, data);
1207 + packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1208 oid_to_hex(oid), refname_nons,
1209 0, capabilities,
1210 (data->allow_uor & ALLOW_TIP_SHA1) ?
@@ -1205,9 +1214,11 @@ static int send_ref(const char *refname, const struct object_id *oid,
1214 data->stateless_rpc ? " no-done" : "",
1215 symref_info.buf,
1216 data->allow_filter ? " filter" : "",
1217 + session_id.buf,
1218 the_hash_algo->name,
1219 git_user_agent_sanitized());
1220 strbuf_release(&symref_info);
1221 + strbuf_release(&session_id);
1222 } else {
1223 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1224 }
@@ -1299,6 +1310,8 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
1310 data->allow_sideband_all = git_config_bool(var, value);
1311 } else if (!strcmp("core.precomposeunicode", var)) {
1312 precomposed_unicode = git_config_bool(var, value);
1313 + } else if (!strcmp("transfer.advertisesid", var)) {
1314 + data->advertise_sid = git_config_bool(var, value);
1315 }
1316
1317 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&