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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jun 12, 2016 at 17:53 UTC
0d789a5bc1aeef2d6d0d3120efc4b85766a0a327
1 file changed
+42
-46
fetch-pack.c
+42
-46
@@ -50,6 +50,21 @@ static int non_common_revs, multi_ack, use_sideband;
50
#define ALLOW_REACHABLE_SHA1 02
51
static unsigned int allow_unadvertised_object_request;
52
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
+
68
static void rev_list_push(struct commit *commit, int mark)
69
{
70
if (!(commit->object.flags & mark)) {
@@ -375,8 +390,7 @@ static int find_common(struct fetch_pack_args *args,
390
retval = -1;
391
while ((sha1 = get_rev())) {
392
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));
394
in_vain++;
395
if (flush_at <= ++count) {
396
int ack;
@@ -397,9 +411,9 @@ static int find_common(struct fetch_pack_args *args,
411
consume_shallow_list(args, fd[0]);
412
do {
413
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));
417
switch (ack) {
418
case ACK:
419
flushes = 0;
@@ -438,8 +452,7 @@ static int find_common(struct fetch_pack_args *args,
452
} while (ack);
453
flushes--;
454
if (got_continue && MAX_IN_VAIN < in_vain) {
441
- if (args->verbose)
442
- fprintf(stderr, "giving up\n");
455
+ print_verbose(args, "giving up");
456
break; /* give up */
457
}
458
}
@@ -449,8 +462,7 @@ done:
462
packet_buf_write(&req_buf, "done\n");
463
send_request(args, fd[1], &req_buf);
464
}
452
- if (args->verbose)
453
- fprintf(stderr, "done\n");
465
+ print_verbose(args, "done");
466
if (retval != 0) {
467
multi_ack = 0;
468
flushes++;
@@ -462,9 +474,8 @@ done:
474
while (flushes || multi_ack) {
475
int ack = get_ack(fd[0], result_sha1);
476
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));
479
if (ack == ACK)
480
return 0;
481
multi_ack = 1;
@@ -509,9 +520,8 @@ static void mark_recent_complete_commits(struct fetch_pack_args *args,
520
unsigned long cutoff)
521
{
522
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));
525
pop_most_recent_commit(&complete, COMPLETE);
526
}
527
}
@@ -652,18 +662,12 @@ static int everything_local(struct fetch_pack_args *args,
662
o = lookup_object(remote);
663
if (!o || !(o->flags & COMPLETE)) {
664
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);
667
continue;
668
}
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);
671
}
672
return retval;
673
}
@@ -810,39 +814,32 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
814
if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
815
die("Server does not support shallow clients");
816
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");
818
multi_ack = 2;
819
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");
821
if (args->stateless_rpc)
822
no_done = 1;
823
}
824
}
825
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");
827
multi_ack = 1;
828
}
829
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");
831
use_sideband = 2;
832
}
833
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");
835
use_sideband = 1;
836
}
837
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");
839
allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
840
}
841
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");
843
allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
844
}
845
if (!server_supports("thin-pack"))
@@ -851,17 +848,16 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
848
args->no_progress = 0;
849
if (!server_supports("include-tag"))
850
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
854
prefer_ofs_delta = 0;
855
856
if ((agent_feature = server_feature_value("agent", &agent_len))) {
857
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);
861
}
862
863
if (everything_local(args, &ref, sought, nr_sought)) {