Skip to content

Commit 0d789a5

Browse files
pcloudsgitster
authored andcommitted
fetch-pack: use a common function for verbose printing
This reduces the number of "if (verbose)" which makes it a bit easier to read imo. It also makes it easier to redirect all these printouts, to a file for example. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45a3e52 commit 0d789a5

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

fetch-pack.c

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ static int non_common_revs, multi_ack, use_sideband;
5050
#define ALLOW_REACHABLE_SHA1 02
5151
static unsigned int allow_unadvertised_object_request;
5252

53+
__attribute__((format (printf, 2, 3)))
54+
static inline void print_verbose(const struct fetch_pack_args *args,
55+
const char *fmt, ...)
56+
{
57+
va_list params;
58+
59+
if (!args->verbose)
60+
return;
61+
62+
va_start(params, fmt);
63+
vfprintf(stderr, fmt, params);
64+
va_end(params);
65+
fputc('\n', stderr);
66+
}
67+
5368
static void rev_list_push(struct commit *commit, int mark)
5469
{
5570
if (!(commit->object.flags & mark)) {
@@ -375,8 +390,7 @@ static int find_common(struct fetch_pack_args *args,
375390
retval = -1;
376391
while ((sha1 = get_rev())) {
377392
packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
378-
if (args->verbose)
379-
fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
393+
print_verbose(args, "have %s", sha1_to_hex(sha1));
380394
in_vain++;
381395
if (flush_at <= ++count) {
382396
int ack;
@@ -397,9 +411,9 @@ static int find_common(struct fetch_pack_args *args,
397411
consume_shallow_list(args, fd[0]);
398412
do {
399413
ack = get_ack(fd[0], result_sha1);
400-
if (args->verbose && ack)
401-
fprintf(stderr, "got ack %d %s\n", ack,
402-
sha1_to_hex(result_sha1));
414+
if (ack)
415+
print_verbose(args, "got ack %d %s", ack,
416+
sha1_to_hex(result_sha1));
403417
switch (ack) {
404418
case ACK:
405419
flushes = 0;
@@ -438,8 +452,7 @@ static int find_common(struct fetch_pack_args *args,
438452
} while (ack);
439453
flushes--;
440454
if (got_continue && MAX_IN_VAIN < in_vain) {
441-
if (args->verbose)
442-
fprintf(stderr, "giving up\n");
455+
print_verbose(args, "giving up");
443456
break; /* give up */
444457
}
445458
}
@@ -449,8 +462,7 @@ static int find_common(struct fetch_pack_args *args,
449462
packet_buf_write(&req_buf, "done\n");
450463
send_request(args, fd[1], &req_buf);
451464
}
452-
if (args->verbose)
453-
fprintf(stderr, "done\n");
465+
print_verbose(args, "done");
454466
if (retval != 0) {
455467
multi_ack = 0;
456468
flushes++;
@@ -462,9 +474,8 @@ static int find_common(struct fetch_pack_args *args,
462474
while (flushes || multi_ack) {
463475
int ack = get_ack(fd[0], result_sha1);
464476
if (ack) {
465-
if (args->verbose)
466-
fprintf(stderr, "got ack (%d) %s\n", ack,
467-
sha1_to_hex(result_sha1));
477+
print_verbose(args, "got ack (%d) %s", ack,
478+
sha1_to_hex(result_sha1));
468479
if (ack == ACK)
469480
return 0;
470481
multi_ack = 1;
@@ -509,9 +520,8 @@ static void mark_recent_complete_commits(struct fetch_pack_args *args,
509520
unsigned long cutoff)
510521
{
511522
while (complete && cutoff <= complete->item->date) {
512-
if (args->verbose)
513-
fprintf(stderr, "Marking %s as complete\n",
514-
oid_to_hex(&complete->item->object.oid));
523+
print_verbose(args, "Marking %s as complete",
524+
oid_to_hex(&complete->item->object.oid));
515525
pop_most_recent_commit(&complete, COMPLETE);
516526
}
517527
}
@@ -652,18 +662,12 @@ static int everything_local(struct fetch_pack_args *args,
652662
o = lookup_object(remote);
653663
if (!o || !(o->flags & COMPLETE)) {
654664
retval = 0;
655-
if (!args->verbose)
656-
continue;
657-
fprintf(stderr,
658-
"want %s (%s)\n", sha1_to_hex(remote),
659-
ref->name);
665+
print_verbose(args, "want %s (%s)", sha1_to_hex(remote),
666+
ref->name);
660667
continue;
661668
}
662-
if (!args->verbose)
663-
continue;
664-
fprintf(stderr,
665-
"already have %s (%s)\n", sha1_to_hex(remote),
666-
ref->name);
669+
print_verbose(args, "already have %s (%s)", sha1_to_hex(remote),
670+
ref->name);
667671
}
668672
return retval;
669673
}
@@ -810,39 +814,32 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
810814
if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
811815
die("Server does not support shallow clients");
812816
if (server_supports("multi_ack_detailed")) {
813-
if (args->verbose)
814-
fprintf(stderr, "Server supports multi_ack_detailed\n");
817+
print_verbose(args, "Server supports multi_ack_detailed");
815818
multi_ack = 2;
816819
if (server_supports("no-done")) {
817-
if (args->verbose)
818-
fprintf(stderr, "Server supports no-done\n");
820+
print_verbose(args, "Server supports no-done");
819821
if (args->stateless_rpc)
820822
no_done = 1;
821823
}
822824
}
823825
else if (server_supports("multi_ack")) {
824-
if (args->verbose)
825-
fprintf(stderr, "Server supports multi_ack\n");
826+
print_verbose(args, "Server supports multi_ack");
826827
multi_ack = 1;
827828
}
828829
if (server_supports("side-band-64k")) {
829-
if (args->verbose)
830-
fprintf(stderr, "Server supports side-band-64k\n");
830+
print_verbose(args, "Server supports side-band-64k");
831831
use_sideband = 2;
832832
}
833833
else if (server_supports("side-band")) {
834-
if (args->verbose)
835-
fprintf(stderr, "Server supports side-band\n");
834+
print_verbose(args, "Server supports side-band");
836835
use_sideband = 1;
837836
}
838837
if (server_supports("allow-tip-sha1-in-want")) {
839-
if (args->verbose)
840-
fprintf(stderr, "Server supports allow-tip-sha1-in-want\n");
838+
print_verbose(args, "Server supports allow-tip-sha1-in-want");
841839
allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
842840
}
843841
if (server_supports("allow-reachable-sha1-in-want")) {
844-
if (args->verbose)
845-
fprintf(stderr, "Server supports allow-reachable-sha1-in-want\n");
842+
print_verbose(args, "Server supports allow-reachable-sha1-in-want");
846843
allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
847844
}
848845
if (!server_supports("thin-pack"))
@@ -851,17 +848,16 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
851848
args->no_progress = 0;
852849
if (!server_supports("include-tag"))
853850
args->include_tag = 0;
854-
if (server_supports("ofs-delta")) {
855-
if (args->verbose)
856-
fprintf(stderr, "Server supports ofs-delta\n");
857-
} else
851+
if (server_supports("ofs-delta"))
852+
print_verbose(args, "Server supports ofs-delta");
853+
else
858854
prefer_ofs_delta = 0;
859855

860856
if ((agent_feature = server_feature_value("agent", &agent_len))) {
861857
agent_supported = 1;
862-
if (args->verbose && agent_len)
863-
fprintf(stderr, "Server version is %.*s\n",
864-
agent_len, agent_feature);
858+
if (agent_len)
859+
print_verbose(args, "Server version is %.*s",
860+
agent_len, agent_feature);
865861
}
866862

867863
if (everything_local(args, &ref, sought, nr_sought)) {

0 commit comments

Comments
 (0)