send-pack: factor out capability string generation
authorJunio C Hamano <gitster@pobox.com>
Fri, 15 Aug 2014 18:37:01 +0000 (11:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 15 Sep 2014 20:23:19 +0000 (13:23 -0700)
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>
send-pack.c
index e81f7412050d51a2cd1d9acb81a1aeb2b72e6835..0cb44aba949f12b9053527c3e1d601c3eae76a1c 100644 (file)
@@ -218,6 +218,7 @@ int send_pack(struct send_pack_args *args,
        int in = fd[0];
        int out = fd[1];
        struct strbuf req_buf = STRBUF_INIT;
+       struct strbuf cap_buf = STRBUF_INIT;
        struct ref *ref;
        int new_refs;
        int allow_deleting_refs = 0;
@@ -251,6 +252,15 @@ int send_pack(struct send_pack_args *args,
                return 0;
        }
 
+       if (status_report)
+               strbuf_addstr(&cap_buf, " report-status");
+       if (use_sideband)
+               strbuf_addstr(&cap_buf, " side-band-64k");
+       if (quiet_supported && (args->quiet || !args->progress))
+               strbuf_addstr(&cap_buf, " quiet");
+       if (agent_supported)
+               strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
+
        /*
         * NEEDSWORK: why does delete-refs have to be so specific to
         * send-pack machinery that set_ref_status_for_push() cannot
@@ -279,18 +289,12 @@ int send_pack(struct send_pack_args *args,
                } else {
                        char *old_hex = sha1_to_hex(ref->old_sha1);
                        char *new_hex = sha1_to_hex(ref->new_sha1);
-                       int quiet = quiet_supported && (args->quiet || !args->progress);
 
                        if (!cmds_sent)
                                packet_buf_write(&req_buf,
-                                                "%s %s %s%c%s%s%s%s%s",
+                                                "%s %s %s%c%s",
                                                 old_hex, new_hex, ref->name, 0,
-                                                status_report ? " report-status" : "",
-                                                use_sideband ? " side-band-64k" : "",
-                                                quiet ? " quiet" : "",
-                                                agent_supported ? " agent=" : "",
-                                                agent_supported ? git_user_agent_sanitized() : ""
-                                               );
+                                                cap_buf.buf);
                        else
                                packet_buf_write(&req_buf, "%s %s %s",
                                                 old_hex, new_hex, ref->name);
@@ -311,6 +315,7 @@ int send_pack(struct send_pack_args *args,
                packet_flush(out);
        }
        strbuf_release(&req_buf);
+       strbuf_release(&cap_buf);
 
        if (use_sideband && cmds_sent) {
                memset(&demux, 0, sizeof(demux));