send-pack: factor out capability string generation
A run of 'var ? " var" : ""' fed to a long printf string in a deeply nested block was hard to read. Move it outside the loop and format it into a strbuf. As an added bonus, the trick to add "agent=<agent-name>" by using two conditionals is replaced by a more readable version. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Aug 15, 2014 at 11:37 UTC
887f3533fd764722ffb6e4ea34c01ad9256f0186
1 file changed
+13
-8
send-pack.c
+13
-8
@@ -218,6 +218,7 @@ int send_pack(struct send_pack_args *args,
218
int in = fd[0];
219
int out = fd[1];
220
struct strbuf req_buf = STRBUF_INIT;
221
+ struct strbuf cap_buf = STRBUF_INIT;
222
struct ref *ref;
223
int new_refs;
224
int allow_deleting_refs = 0;
@@ -251,6 +252,15 @@ int send_pack(struct send_pack_args *args,
252
return 0;
253
}
254
255
+ if (status_report)
256
+ strbuf_addstr(&cap_buf, " report-status");
257
+ if (use_sideband)
258
+ strbuf_addstr(&cap_buf, " side-band-64k");
259
+ if (quiet_supported && (args->quiet || !args->progress))
260
+ strbuf_addstr(&cap_buf, " quiet");
261
+ if (agent_supported)
262
+ strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
263
+
264
/*
265
* NEEDSWORK: why does delete-refs have to be so specific to
266
* send-pack machinery that set_ref_status_for_push() cannot
@@ -279,18 +289,12 @@ int send_pack(struct send_pack_args *args,
289
} else {
290
char *old_hex = sha1_to_hex(ref->old_sha1);
291
char *new_hex = sha1_to_hex(ref->new_sha1);
282
- int quiet = quiet_supported && (args->quiet || !args->progress);
292
293
if (!cmds_sent)
294
packet_buf_write(&req_buf,
286
- "%s %s %s%c%s%s%s%s%s",
295
+ "%s %s %s%c%s",
296
old_hex, new_hex, ref->name, 0,
288
- status_report ? " report-status" : "",
289
- use_sideband ? " side-band-64k" : "",
290
- quiet ? " quiet" : "",
291
- agent_supported ? " agent=" : "",
292
- agent_supported ? git_user_agent_sanitized() : ""
293
- );
297
+ cap_buf.buf);
298
else
299
packet_buf_write(&req_buf, "%s %s %s",
300
old_hex, new_hex, ref->name);
@@ -311,6 +315,7 @@ int send_pack(struct send_pack_args *args,
315
packet_flush(out);
316
}
317
strbuf_release(&req_buf);
318
+ strbuf_release(&cap_buf);
319
320
if (use_sideband && cmds_sent) {
321
memset(&demux, 0, sizeof(demux));