Skip to content

Commit

Permalink
progress: fix progress meters when dealing with lots of work
Browse files Browse the repository at this point in the history
The possibility of setting merge.renameLimit beyond 2^16 raises the
possibility that the values passed to progress can exceed 2^32.
Use uint64_t, because it "ought to be enough for anybody".  :-)

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
newren authored and gitster committed Nov 15, 2017
1 parent b520abf commit d6861d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions diffcore-rename.c
Expand Up @@ -534,7 +534,7 @@ void diffcore_rename(struct diff_options *options)
if (options->show_rename_progress) {
progress = start_delayed_progress(
_("Performing inexact rename detection"),
rename_dst_nr * rename_src_nr);
(uint64_t)rename_dst_nr * (uint64_t)rename_src_nr);
}

mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_create), sizeof(*mx));
Expand Down Expand Up @@ -571,7 +571,7 @@ void diffcore_rename(struct diff_options *options)
diff_free_filespec_blob(two);
}
dst_cnt++;
display_progress(progress, (i+1)*rename_src_nr);
display_progress(progress, (uint64_t)(i+1)*(uint64_t)rename_src_nr);
}
stop_progress(&progress);

Expand Down
29 changes: 15 additions & 14 deletions progress.c
Expand Up @@ -30,8 +30,8 @@ struct throughput {

struct progress {
const char *title;
int last_value;
unsigned total;
uint64_t last_value;
uint64_t total;
unsigned last_percent;
unsigned delay;
unsigned delayed_percent_threshold;
Expand Down Expand Up @@ -79,7 +79,7 @@ static int is_foreground_fd(int fd)
return tpgrp < 0 || tpgrp == getpgid(0);
}

static int display(struct progress *progress, unsigned n, const char *done)
static int display(struct progress *progress, uint64_t n, const char *done)
{
const char *eol, *tp;

Expand All @@ -106,18 +106,19 @@ static int display(struct progress *progress, unsigned n, const char *done)
if (percent != progress->last_percent || progress_update) {
progress->last_percent = percent;
if (is_foreground_fd(fileno(stderr)) || done) {
fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
progress->title, percent, n,
progress->total, tp, eol);
fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s",
progress->title, percent,
(uintmax_t)n, (uintmax_t)progress->total,
tp, eol);
fflush(stderr);
}
progress_update = 0;
return 1;
}
} else if (progress_update) {
if (is_foreground_fd(fileno(stderr)) || done) {
fprintf(stderr, "%s: %u%s%s",
progress->title, n, tp, eol);
fprintf(stderr, "%s: %"PRIuMAX"%s%s",
progress->title, (uintmax_t)n, tp, eol);
fflush(stderr);
}
progress_update = 0;
Expand All @@ -127,7 +128,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
return 0;
}

static void throughput_string(struct strbuf *buf, off_t total,
static void throughput_string(struct strbuf *buf, uint64_t total,
unsigned int rate)
{
strbuf_reset(buf);
Expand All @@ -138,7 +139,7 @@ static void throughput_string(struct strbuf *buf, off_t total,
strbuf_addstr(buf, "/s");
}

void display_throughput(struct progress *progress, off_t total)
void display_throughput(struct progress *progress, uint64_t total)
{
struct throughput *tp;
uint64_t now_ns;
Expand Down Expand Up @@ -200,12 +201,12 @@ void display_throughput(struct progress *progress, off_t total)
display(progress, progress->last_value, NULL);
}

int display_progress(struct progress *progress, unsigned n)
int display_progress(struct progress *progress, uint64_t n)
{
return progress ? display(progress, n, NULL) : 0;
}

static struct progress *start_progress_delay(const char *title, unsigned total,
static struct progress *start_progress_delay(const char *title, uint64_t total,
unsigned percent_threshold, unsigned delay)
{
struct progress *progress = malloc(sizeof(*progress));
Expand All @@ -227,12 +228,12 @@ static struct progress *start_progress_delay(const char *title, unsigned total,
return progress;
}

struct progress *start_delayed_progress(const char *title, unsigned total)
struct progress *start_delayed_progress(const char *title, uint64_t total)
{
return start_progress_delay(title, total, 0, 2);
}

struct progress *start_progress(const char *title, unsigned total)
struct progress *start_progress(const char *title, uint64_t total)
{
return start_progress_delay(title, total, 0, 0);
}
Expand Down
8 changes: 4 additions & 4 deletions progress.h
Expand Up @@ -3,10 +3,10 @@

struct progress;

void display_throughput(struct progress *progress, off_t total);
int display_progress(struct progress *progress, unsigned n);
struct progress *start_progress(const char *title, unsigned total);
struct progress *start_delayed_progress(const char *title, unsigned total);
void display_throughput(struct progress *progress, uint64_t total);
int display_progress(struct progress *progress, uint64_t n);
struct progress *start_progress(const char *title, uint64_t total);
struct progress *start_delayed_progress(const char *title, uint64_t total);
void stop_progress(struct progress **progress);
void stop_progress_msg(struct progress **progress, const char *msg);

Expand Down

0 comments on commit d6861d0

Please sign in to comment.