Skip to content

Commit 887f353

Browse files
committed
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 <[email protected]>
1 parent 64de20a commit 887f353

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

send-pack.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ int send_pack(struct send_pack_args *args,
218218
int in = fd[0];
219219
int out = fd[1];
220220
struct strbuf req_buf = STRBUF_INIT;
221+
struct strbuf cap_buf = STRBUF_INIT;
221222
struct ref *ref;
222223
int new_refs;
223224
int allow_deleting_refs = 0;
@@ -251,6 +252,15 @@ int send_pack(struct send_pack_args *args,
251252
return 0;
252253
}
253254

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+
254264
/*
255265
* NEEDSWORK: why does delete-refs have to be so specific to
256266
* send-pack machinery that set_ref_status_for_push() cannot
@@ -279,18 +289,12 @@ int send_pack(struct send_pack_args *args,
279289
} else {
280290
char *old_hex = sha1_to_hex(ref->old_sha1);
281291
char *new_hex = sha1_to_hex(ref->new_sha1);
282-
int quiet = quiet_supported && (args->quiet || !args->progress);
283292

284293
if (!cmds_sent)
285294
packet_buf_write(&req_buf,
286-
"%s %s %s%c%s%s%s%s%s",
295+
"%s %s %s%c%s",
287296
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);
294298
else
295299
packet_buf_write(&req_buf, "%s %s %s",
296300
old_hex, new_hex, ref->name);
@@ -311,6 +315,7 @@ int send_pack(struct send_pack_args *args,
311315
packet_flush(out);
312316
}
313317
strbuf_release(&req_buf);
318+
strbuf_release(&cap_buf);
314319

315320
if (use_sideband && cmds_sent) {
316321
memset(&demux, 0, sizeof(demux));

0 commit comments

Comments
 (0)