Skip to content

Commit dc6a075

Browse files
Nicolas Pitregitster
authored andcommitted
make struct progress an opaque type
This allows for better management of progress "object" existence, as well as making the progress display implementation more independent from its callers. Signed-off-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e54913 commit dc6a075

File tree

7 files changed

+57
-45
lines changed

7 files changed

+57
-45
lines changed

builtin-pack-objects.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int depth = 50;
7373
static int delta_search_threads = 1;
7474
static int pack_to_stdout;
7575
static int num_preferred_base;
76-
static struct progress progress_state;
76+
static struct progress *progress_state;
7777
static int pack_compression_level = Z_DEFAULT_COMPRESSION;
7878
static int pack_compression_seen;
7979

@@ -598,7 +598,7 @@ static void write_pack_file(void)
598598
uint32_t nr_remaining = nr_result;
599599

600600
if (do_progress)
601-
start_progress(&progress_state, "Writing objects", nr_result);
601+
progress_state = start_progress("Writing objects", nr_result);
602602
written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
603603

604604
do {
@@ -630,7 +630,7 @@ static void write_pack_file(void)
630630
break;
631631
offset = offset_one;
632632
if (do_progress)
633-
display_progress(&progress_state, written);
633+
display_progress(progress_state, written);
634634
}
635635

636636
/*
@@ -854,7 +854,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
854854
object_ix[-1 - ix] = nr_objects;
855855

856856
if (progress)
857-
display_progress(&progress_state, nr_objects);
857+
display_progress(progress_state, nr_objects);
858858

859859
if (name && no_try_delta(name))
860860
entry->no_try_delta = 1;
@@ -1518,7 +1518,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
15181518
progress_lock();
15191519
(*processed)++;
15201520
if (progress)
1521-
display_progress(&progress_state, *processed);
1521+
display_progress(progress_state, *processed);
15221522
progress_unlock();
15231523

15241524
/*
@@ -1718,8 +1718,8 @@ static void prepare_pack(int window, int depth)
17181718
if (nr_deltas && n > 1) {
17191719
unsigned nr_done = 0;
17201720
if (progress)
1721-
start_progress(&progress_state, "Compressing objects",
1722-
nr_deltas);
1721+
progress_state = start_progress("Compressing objects",
1722+
nr_deltas);
17231723
qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
17241724
ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
17251725
if (progress)
@@ -2135,7 +2135,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
21352135
prepare_packed_git();
21362136

21372137
if (progress)
2138-
start_progress(&progress_state, "Counting objects", 0);
2138+
progress_state = start_progress("Counting objects", 0);
21392139
if (!use_internal_rev_list)
21402140
read_object_list_from_stdin();
21412141
else {

builtin-prune-packed.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ static const char prune_packed_usage[] =
88
#define DRY_RUN 01
99
#define VERBOSE 02
1010

11-
static struct progress progress;
11+
static struct progress *progress;
1212

1313
static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
1414
{
1515
struct dirent *de;
1616
char hex[40];
1717

1818
if (opts == VERBOSE)
19-
display_progress(&progress, i + 1);
19+
display_progress(progress, i + 1);
2020

2121
sprintf(hex, "%02x", i);
2222
while ((de = readdir(dir)) != NULL) {
@@ -46,8 +46,7 @@ void prune_packed_objects(int opts)
4646
int len = strlen(dir);
4747

4848
if (opts == VERBOSE)
49-
start_progress_delay(&progress,
50-
"Removing duplicate objects",
49+
progress = start_progress_delay("Removing duplicate objects",
5150
256, 95, 2);
5251

5352
if (len > PATH_MAX - 42)

builtin-unpack-objects.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static void unpack_one(unsigned nr)
311311
static void unpack_all(void)
312312
{
313313
int i;
314-
struct progress progress;
314+
struct progress *progress = NULL;
315315
struct pack_header *hdr = fill(sizeof(struct pack_header));
316316
unsigned nr_objects = ntohl(hdr->hdr_entries);
317317

@@ -322,12 +322,12 @@ static void unpack_all(void)
322322
use(sizeof(struct pack_header));
323323

324324
if (!quiet)
325-
start_progress(&progress, "Unpacking objects", nr_objects);
325+
progress = start_progress("Unpacking objects", nr_objects);
326326
obj_list = xmalloc(nr_objects * sizeof(*obj_list));
327327
for (i = 0; i < nr_objects; i++) {
328328
unpack_one(i);
329329
if (!quiet)
330-
display_progress(&progress, i + 1);
330+
display_progress(progress, i + 1);
331331
}
332332
if (!quiet)
333333
stop_progress(&progress);

index-pack.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int nr_resolved_deltas;
4646
static int from_stdin;
4747
static int verbose;
4848

49-
static struct progress progress;
49+
static struct progress *progress;
5050

5151
/* We always read in 4kB chunks. */
5252
static unsigned char input_buffer[4096];
@@ -406,7 +406,7 @@ static void parse_pack_objects(unsigned char *sha1)
406406
* - remember base (SHA1 or offset) for all deltas.
407407
*/
408408
if (verbose)
409-
start_progress(&progress, "Indexing objects", nr_objects);
409+
progress = start_progress("Indexing objects", nr_objects);
410410
for (i = 0; i < nr_objects; i++) {
411411
struct object_entry *obj = &objects[i];
412412
data = unpack_raw_entry(obj, &delta->base);
@@ -419,7 +419,7 @@ static void parse_pack_objects(unsigned char *sha1)
419419
sha1_object(data, obj->size, obj->type, obj->idx.sha1);
420420
free(data);
421421
if (verbose)
422-
display_progress(&progress, i+1);
422+
display_progress(progress, i+1);
423423
}
424424
objects[i].idx.offset = consumed_bytes;
425425
if (verbose)
@@ -455,7 +455,7 @@ static void parse_pack_objects(unsigned char *sha1)
455455
* for some more deltas.
456456
*/
457457
if (verbose)
458-
start_progress(&progress, "Resolving deltas", nr_deltas);
458+
progress = start_progress("Resolving deltas", nr_deltas);
459459
for (i = 0; i < nr_objects; i++) {
460460
struct object_entry *obj = &objects[i];
461461
union delta_base base;
@@ -487,7 +487,7 @@ static void parse_pack_objects(unsigned char *sha1)
487487
}
488488
free(data);
489489
if (verbose)
490-
display_progress(&progress, nr_resolved_deltas);
490+
display_progress(progress, nr_resolved_deltas);
491491
}
492492
}
493493

@@ -595,7 +595,7 @@ static void fix_unresolved_deltas(int nr_unresolved)
595595
append_obj_to_pack(d->base.sha1, data, size, type);
596596
free(data);
597597
if (verbose)
598-
display_progress(&progress, nr_resolved_deltas);
598+
display_progress(progress, nr_resolved_deltas);
599599
}
600600
free(sorted_by_pos);
601601
}

progress.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#include "git-compat-util.h"
22
#include "progress.h"
33

4+
struct progress {
5+
const char *title;
6+
int last_value;
7+
unsigned total;
8+
unsigned last_percent;
9+
unsigned delay;
10+
unsigned delayed_percent_treshold;
11+
};
12+
413
static volatile sig_atomic_t progress_update;
514

615
static void progress_interval(int signum)
@@ -76,32 +85,44 @@ static int display(struct progress *progress, unsigned n, int done)
7685

7786
int display_progress(struct progress *progress, unsigned n)
7887
{
79-
return display(progress, n, 0);
88+
return progress ? display(progress, n, 0) : 0;
8089
}
8190

82-
void start_progress_delay(struct progress *progress, const char *title,
83-
unsigned total, unsigned percent_treshold, unsigned delay)
91+
struct progress *start_progress_delay(const char *title, unsigned total,
92+
unsigned percent_treshold, unsigned delay)
8493
{
94+
struct progress *progress = malloc(sizeof(*progress));
95+
if (!progress) {
96+
/* unlikely, but here's a good fallback */
97+
fprintf(stderr, "%s...\n", title);
98+
return NULL;
99+
}
85100
progress->title = title;
86101
progress->total = total;
87102
progress->last_value = -1;
88103
progress->last_percent = -1;
89104
progress->delayed_percent_treshold = percent_treshold;
90105
progress->delay = delay;
91106
set_progress_signal();
107+
return progress;
92108
}
93109

94-
void start_progress(struct progress *progress, const char *title, unsigned total)
110+
struct progress *start_progress(const char *title, unsigned total)
95111
{
96-
start_progress_delay(progress, title, total, 0, 0);
112+
return start_progress_delay(title, total, 0, 0);
97113
}
98114

99-
void stop_progress(struct progress *progress)
115+
void stop_progress(struct progress **p_progress)
100116
{
117+
struct progress *progress = *p_progress;
118+
if (!progress)
119+
return;
120+
*p_progress = NULL;
101121
if (progress->last_value != -1) {
102122
/* Force the last update */
103123
progress_update = 1;
104124
display(progress, progress->last_value, 1);
105125
}
106126
clear_progress_signal();
127+
free(progress);
107128
}

progress.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
#ifndef PROGRESS_H
22
#define PROGRESS_H
33

4-
struct progress {
5-
const char *title;
6-
int last_value;
7-
unsigned total;
8-
unsigned last_percent;
9-
unsigned delay;
10-
unsigned delayed_percent_treshold;
11-
};
4+
struct progress;
125

136
int display_progress(struct progress *progress, unsigned n);
14-
void start_progress(struct progress *progress, const char *title,
15-
unsigned total);
16-
void start_progress_delay(struct progress *progress, const char *title,
17-
unsigned total, unsigned percent_treshold, unsigned delay);
18-
void stop_progress(struct progress *progress);
7+
struct progress *start_progress(const char *title, unsigned total);
8+
struct progress *start_progress_delay(const char *title, unsigned total,
9+
unsigned percent_treshold, unsigned delay);
10+
void stop_progress(struct progress **progress);
1911

2012
#endif

unpack-trees.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static void check_updates(struct cache_entry **src, int nr,
297297
{
298298
unsigned short mask = htons(CE_UPDATE);
299299
unsigned cnt = 0, total = 0;
300-
struct progress progress;
300+
struct progress *progress = NULL;
301301
char last_symlink[PATH_MAX];
302302

303303
if (o->update && o->verbose_update) {
@@ -307,8 +307,8 @@ static void check_updates(struct cache_entry **src, int nr,
307307
total++;
308308
}
309309

310-
start_progress_delay(&progress, "Checking out files",
311-
total, 50, 2);
310+
progress = start_progress_delay("Checking out files",
311+
total, 50, 2);
312312
cnt = 0;
313313
}
314314

@@ -318,7 +318,7 @@ static void check_updates(struct cache_entry **src, int nr,
318318

319319
if (total)
320320
if (!ce->ce_mode || ce->ce_flags & mask)
321-
display_progress(&progress, ++cnt);
321+
display_progress(progress, ++cnt);
322322
if (!ce->ce_mode) {
323323
if (o->update)
324324
unlink_entry(ce->name, last_symlink);
@@ -333,7 +333,7 @@ static void check_updates(struct cache_entry **src, int nr,
333333
}
334334
}
335335
if (total)
336-
stop_progress(&progress);;
336+
stop_progress(&progress);
337337
}
338338

339339
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)

0 commit comments

Comments
 (0)