Skip to content

Commit a47fcfe

Browse files
committed
Merge branch 'ab/only-single-progress-at-once'
Further tweaks on progress API. * ab/only-single-progress-at-once: pack-bitmap-write.c: don't return without stop_progress() progress API: unify stop_progress{,_msg}(), fix trace2 bug progress.c: refactor stop_progress{,_msg}() to use helpers progress.c: use dereferenced "progress" variable, not "(*p_progress)" progress.h: format and be consistent with progress.c naming progress.c tests: test some invalid usage progress.c tests: make start/stop commands on stdin progress.c test helper: add missing braces leak tests: fix a memory leak in "test-progress" helper
2 parents 6249ce2 + b3118a5 commit a47fcfe

File tree

6 files changed

+170
-76
lines changed

6 files changed

+170
-76
lines changed

pack-bitmap-write.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,15 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,
575575

576576
QSORT(indexed_commits, indexed_commits_nr, date_compare);
577577

578-
if (writer.show_progress)
579-
writer.progress = start_progress("Selecting bitmap commits", 0);
580-
581578
if (indexed_commits_nr < 100) {
582579
for (i = 0; i < indexed_commits_nr; ++i)
583580
push_bitmapped_commit(indexed_commits[i]);
584581
return;
585582
}
586583

584+
if (writer.show_progress)
585+
writer.progress = start_progress("Selecting bitmap commits", 0);
586+
587587
for (;;) {
588588
struct commit *chosen = NULL;
589589

progress.c

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -311,32 +311,39 @@ struct progress *start_delayed_sparse_progress(const char *title,
311311

312312
static void finish_if_sparse(struct progress *progress)
313313
{
314-
if (progress &&
315-
progress->sparse &&
314+
if (progress->sparse &&
316315
progress->last_value != progress->total)
317316
display_progress(progress, progress->total);
318317
}
319318

320-
void stop_progress(struct progress **p_progress)
319+
static void force_last_update(struct progress *progress, const char *msg)
321320
{
322-
if (!p_progress)
323-
BUG("don't provide NULL to stop_progress");
324-
325-
finish_if_sparse(*p_progress);
326-
327-
if (*p_progress) {
328-
trace2_data_intmax("progress", the_repository, "total_objects",
329-
(*p_progress)->total);
321+
char *buf;
322+
struct throughput *tp = progress->throughput;
323+
324+
if (tp) {
325+
uint64_t now_ns = progress_getnanotime(progress);
326+
unsigned int misecs, rate;
327+
misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
328+
rate = tp->curr_total / (misecs ? misecs : 1);
329+
throughput_string(&tp->display, tp->curr_total, rate);
330+
}
331+
progress_update = 1;
332+
buf = xstrfmt(", %s.\n", msg);
333+
display(progress, progress->last_value, buf);
334+
free(buf);
335+
}
330336

331-
if ((*p_progress)->throughput)
332-
trace2_data_intmax("progress", the_repository,
333-
"total_bytes",
334-
(*p_progress)->throughput->curr_total);
337+
static void log_trace2(struct progress *progress)
338+
{
339+
trace2_data_intmax("progress", the_repository, "total_objects",
340+
progress->total);
335341

336-
trace2_region_leave("progress", (*p_progress)->title, the_repository);
337-
}
342+
if (progress->throughput)
343+
trace2_data_intmax("progress", the_repository, "total_bytes",
344+
progress->throughput->curr_total);
338345

339-
stop_progress_msg(p_progress, _("done"));
346+
trace2_region_leave("progress", progress->title, the_repository);
340347
}
341348

342349
void stop_progress_msg(struct progress **p_progress, const char *msg)
@@ -350,23 +357,12 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
350357
if (!progress)
351358
return;
352359
*p_progress = NULL;
353-
if (progress->last_value != -1) {
354-
/* Force the last update */
355-
char *buf;
356-
struct throughput *tp = progress->throughput;
357-
358-
if (tp) {
359-
uint64_t now_ns = progress_getnanotime(progress);
360-
unsigned int misecs, rate;
361-
misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
362-
rate = tp->curr_total / (misecs ? misecs : 1);
363-
throughput_string(&tp->display, tp->curr_total, rate);
364-
}
365-
progress_update = 1;
366-
buf = xstrfmt(", %s.\n", msg);
367-
display(progress, progress->last_value, buf);
368-
free(buf);
369-
}
360+
361+
finish_if_sparse(progress);
362+
if (progress->last_value != -1)
363+
force_last_update(progress, msg);
364+
log_trace2(progress);
365+
370366
clear_progress_signal();
371367
strbuf_release(&progress->counters_sb);
372368
if (progress->throughput)

progress.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef PROGRESS_H
22
#define PROGRESS_H
3+
#include "gettext.h"
34

45
struct progress;
56

@@ -18,7 +19,9 @@ struct progress *start_sparse_progress(const char *title, uint64_t total);
1819
struct progress *start_delayed_progress(const char *title, uint64_t total);
1920
struct progress *start_delayed_sparse_progress(const char *title,
2021
uint64_t total);
21-
void stop_progress(struct progress **progress);
22-
void stop_progress_msg(struct progress **progress, const char *msg);
23-
22+
void stop_progress_msg(struct progress **p_progress, const char *msg);
23+
static inline void stop_progress(struct progress **p_progress)
24+
{
25+
stop_progress_msg(p_progress, _("done"));
26+
}
2427
#endif

t/helper/test-progress.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
*
44
* Reads instructions from standard input, one instruction per line:
55
*
6+
* "start <total>[ <title>]" - Call start_progress(title, total),
7+
* Uses the default title of "Working hard"
8+
* if the " <title>" is omitted.
69
* "progress <items>" - Call display_progress() with the given item count
710
* as parameter.
811
* "throughput <bytes> <millis> - Call display_throughput() with the given
912
* byte count as parameter. The 'millis'
1013
* specify the time elapsed since the
1114
* start_progress() call.
1215
* "update" - Set the 'progress_update' flag.
16+
* "stop" - Call stop_progress().
1317
*
1418
* See 't0500-progress-display.sh' for examples.
1519
*/
@@ -19,34 +23,50 @@
1923
#include "parse-options.h"
2024
#include "progress.h"
2125
#include "strbuf.h"
26+
#include "string-list.h"
2227

2328
int cmd__progress(int argc, const char **argv)
2429
{
25-
int total = 0;
26-
const char *title;
30+
const char *const default_title = "Working hard";
31+
struct string_list titles = STRING_LIST_INIT_DUP;
2732
struct strbuf line = STRBUF_INIT;
28-
struct progress *progress;
33+
struct progress *progress = NULL;
2934

3035
const char *usage[] = {
31-
"test-tool progress [--total=<n>] <progress-title>",
36+
"test-tool progress <stdin",
3237
NULL
3338
};
3439
struct option options[] = {
35-
OPT_INTEGER(0, "total", &total, "total number of items"),
3640
OPT_END(),
3741
};
3842

3943
argc = parse_options(argc, argv, NULL, options, usage, 0);
40-
if (argc != 1)
41-
die("need a title for the progress output");
42-
title = argv[0];
44+
if (argc)
45+
usage_with_options(usage, options);
4346

4447
progress_testing = 1;
45-
progress = start_progress(title, total);
4648
while (strbuf_getline(&line, stdin) != EOF) {
4749
char *end;
4850

49-
if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
51+
if (skip_prefix(line.buf, "start ", (const char **) &end)) {
52+
uint64_t total = strtoull(end, &end, 10);
53+
const char *title;
54+
55+
/*
56+
* We can't use "end + 1" as an argument to
57+
* start_progress(), it doesn't xstrdup() its
58+
* "title" argument. We need to hold onto a
59+
* valid "char *" for it until the end.
60+
*/
61+
if (!*end)
62+
title = default_title;
63+
else if (*end == ' ')
64+
title = string_list_insert(&titles, end + 1)->string;
65+
else
66+
die("invalid input: '%s'\n", line.buf);
67+
68+
progress = start_progress(title, total);
69+
} else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
5070
uint64_t item_count = strtoull(end, &end, 10);
5171
if (*end != '\0')
5272
die("invalid input: '%s'\n", line.buf);
@@ -63,12 +83,16 @@ int cmd__progress(int argc, const char **argv)
6383
die("invalid input: '%s'\n", line.buf);
6484
progress_test_ns = test_ms * 1000 * 1000;
6585
display_throughput(progress, byte_count);
66-
} else if (!strcmp(line.buf, "update"))
86+
} else if (!strcmp(line.buf, "update")) {
6787
progress_test_force_update();
68-
else
88+
} else if (!strcmp(line.buf, "stop")) {
89+
stop_progress(&progress);
90+
} else {
6991
die("invalid input: '%s'\n", line.buf);
92+
}
7093
}
71-
stop_progress(&progress);
94+
strbuf_release(&line);
95+
string_list_clear(&titles, 0);
7296

7397
return 0;
7498
}

0 commit comments

Comments
 (0)