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 <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Oct 30, 2007 at 14:57 UTC
dc6a0757c4f966dd124bd85be2adad5a0b7b2167
7 files changed
+57
-45
builtin-pack-objects.c
+8
-8
@@ -73,7 +73,7 @@ static int depth = 50;
73
static int delta_search_threads = 1;
74
static int pack_to_stdout;
75
static int num_preferred_base;
76
-static struct progress progress_state;
76
+static struct progress *progress_state;
77
static int pack_compression_level = Z_DEFAULT_COMPRESSION;
78
static int pack_compression_seen;
79
@@ -598,7 +598,7 @@ static void write_pack_file(void)
598
uint32_t nr_remaining = nr_result;
599
600
if (do_progress)
601
- start_progress(&progress_state, "Writing objects", nr_result);
601
+ progress_state = start_progress("Writing objects", nr_result);
602
written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
603
604
do {
@@ -630,7 +630,7 @@ static void write_pack_file(void)
630
break;
631
offset = offset_one;
632
if (do_progress)
633
- display_progress(&progress_state, written);
633
+ display_progress(progress_state, written);
634
}
635
636
/*
@@ -854,7 +854,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
854
object_ix[-1 - ix] = nr_objects;
855
856
if (progress)
857
- display_progress(&progress_state, nr_objects);
857
+ display_progress(progress_state, nr_objects);
858
859
if (name && no_try_delta(name))
860
entry->no_try_delta = 1;
@@ -1518,7 +1518,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
1518
progress_lock();
1519
(*processed)++;
1520
if (progress)
1521
- display_progress(&progress_state, *processed);
1521
+ display_progress(progress_state, *processed);
1522
progress_unlock();
1523
1524
/*
@@ -1718,8 +1718,8 @@ static void prepare_pack(int window, int depth)
1718
if (nr_deltas && n > 1) {
1719
unsigned nr_done = 0;
1720
if (progress)
1721
- start_progress(&progress_state, "Compressing objects",
1722
- nr_deltas);
1721
+ progress_state = start_progress("Compressing objects",
1722
+ nr_deltas);
1723
qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
1724
ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
1725
if (progress)
@@ -2135,7 +2135,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2135
prepare_packed_git();
2136
2137
if (progress)
2138
- start_progress(&progress_state, "Counting objects", 0);
2138
+ progress_state = start_progress("Counting objects", 0);
2139
if (!use_internal_rev_list)
2140
read_object_list_from_stdin();
2141
else {
builtin-prune-packed.c
+3
-4
@@ -8,7 +8,7 @@ static const char prune_packed_usage[] =
8
#define DRY_RUN 01
9
#define VERBOSE 02
10
11
-static struct progress progress;
11
+static struct progress *progress;
12
13
static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
14
{
@@ -16,7 +16,7 @@ static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
16
char hex[40];
17
18
if (opts == VERBOSE)
19
- display_progress(&progress, i + 1);
19
+ display_progress(progress, i + 1);
20
21
sprintf(hex, "%02x", i);
22
while ((de = readdir(dir)) != NULL) {
@@ -46,8 +46,7 @@ void prune_packed_objects(int opts)
46
int len = strlen(dir);
47
48
if (opts == VERBOSE)
49
- start_progress_delay(&progress,
50
- "Removing duplicate objects",
49
+ progress = start_progress_delay("Removing duplicate objects",
50
256, 95, 2);
51
52
if (len > PATH_MAX - 42)
builtin-unpack-objects.c
+3
-3
@@ -311,7 +311,7 @@ static void unpack_one(unsigned nr)
311
static void unpack_all(void)
312
{
313
int i;
314
- struct progress progress;
314
+ struct progress *progress = NULL;
315
struct pack_header *hdr = fill(sizeof(struct pack_header));
316
unsigned nr_objects = ntohl(hdr->hdr_entries);
317
@@ -322,12 +322,12 @@ static void unpack_all(void)
322
use(sizeof(struct pack_header));
323
324
if (!quiet)
325
- start_progress(&progress, "Unpacking objects", nr_objects);
325
+ progress = start_progress("Unpacking objects", nr_objects);
326
obj_list = xmalloc(nr_objects * sizeof(*obj_list));
327
for (i = 0; i < nr_objects; i++) {
328
unpack_one(i);
329
if (!quiet)
330
- display_progress(&progress, i + 1);
330
+ display_progress(progress, i + 1);
331
}
332
if (!quiet)
333
stop_progress(&progress);
index-pack.c
+6
-6
@@ -46,7 +46,7 @@ static int nr_resolved_deltas;
46
static int from_stdin;
47
static int verbose;
48
49
-static struct progress progress;
49
+static struct progress *progress;
50
51
/* We always read in 4kB chunks. */
52
static unsigned char input_buffer[4096];
@@ -406,7 +406,7 @@ static void parse_pack_objects(unsigned char *sha1)
406
* - remember base (SHA1 or offset) for all deltas.
407
*/
408
if (verbose)
409
- start_progress(&progress, "Indexing objects", nr_objects);
409
+ progress = start_progress("Indexing objects", nr_objects);
410
for (i = 0; i < nr_objects; i++) {
411
struct object_entry *obj = &objects[i];
412
data = unpack_raw_entry(obj, &delta->base);
@@ -419,7 +419,7 @@ static void parse_pack_objects(unsigned char *sha1)
419
sha1_object(data, obj->size, obj->type, obj->idx.sha1);
420
free(data);
421
if (verbose)
422
- display_progress(&progress, i+1);
422
+ display_progress(progress, i+1);
423
}
424
objects[i].idx.offset = consumed_bytes;
425
if (verbose)
@@ -455,7 +455,7 @@ static void parse_pack_objects(unsigned char *sha1)
455
* for some more deltas.
456
*/
457
if (verbose)
458
- start_progress(&progress, "Resolving deltas", nr_deltas);
458
+ progress = start_progress("Resolving deltas", nr_deltas);
459
for (i = 0; i < nr_objects; i++) {
460
struct object_entry *obj = &objects[i];
461
union delta_base base;
@@ -487,7 +487,7 @@ static void parse_pack_objects(unsigned char *sha1)
487
}
488
free(data);
489
if (verbose)
490
- display_progress(&progress, nr_resolved_deltas);
490
+ display_progress(progress, nr_resolved_deltas);
491
}
492
}
493
@@ -595,7 +595,7 @@ static void fix_unresolved_deltas(int nr_unresolved)
595
append_obj_to_pack(d->base.sha1, data, size, type);
596
free(data);
597
if (verbose)
598
- display_progress(&progress, nr_resolved_deltas);
598
+ display_progress(progress, nr_resolved_deltas);
599
}
600
free(sorted_by_pos);
601
}
progress.c
+27
-6
@@ -1,6 +1,15 @@
1
#include "git-compat-util.h"
2
#include "progress.h"
3
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
+
13
static volatile sig_atomic_t progress_update;
14
15
static void progress_interval(int signum)
@@ -76,12 +85,18 @@ static int display(struct progress *progress, unsigned n, int done)
85
86
int display_progress(struct progress *progress, unsigned n)
87
{
79
- return display(progress, n, 0);
88
+ return progress ? display(progress, n, 0) : 0;
89
}
90
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)
93
{
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
+ }
100
progress->title = title;
101
progress->total = total;
102
progress->last_value = -1;
@@ -89,19 +104,25 @@ void start_progress_delay(struct progress *progress, const char *title,
104
progress->delayed_percent_treshold = percent_treshold;
105
progress->delay = delay;
106
set_progress_signal();
107
+ return progress;
108
}
109
94
-void start_progress(struct progress *progress, const char *title, unsigned total)
110
+struct progress *start_progress(const char *title, unsigned total)
111
{
96
- start_progress_delay(progress, title, total, 0, 0);
112
+ return start_progress_delay(title, total, 0, 0);
113
}
114
99
-void stop_progress(struct progress *progress)
115
+void stop_progress(struct progress **p_progress)
116
{
117
+ struct progress *progress = *p_progress;
118
+ if (!progress)
119
+ return;
120
+ *p_progress = NULL;
121
if (progress->last_value != -1) {
122
/* Force the last update */
123
progress_update = 1;
124
display(progress, progress->last_value, 1);
125
}
126
clear_progress_signal();
127
+ free(progress);
128
}
progress.h
+5
-13
@@ -1,20 +1,12 @@
1
#ifndef PROGRESS_H
2
#define PROGRESS_H
3
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;
5
6
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);
11
12
#endif
unpack-trees.c
+5
-5
@@ -297,7 +297,7 @@ static void check_updates(struct cache_entry **src, int nr,
297
{
298
unsigned short mask = htons(CE_UPDATE);
299
unsigned cnt = 0, total = 0;
300
- struct progress progress;
300
+ struct progress *progress = NULL;
301
char last_symlink[PATH_MAX];
302
303
if (o->update && o->verbose_update) {
@@ -307,8 +307,8 @@ static void check_updates(struct cache_entry **src, int nr,
307
total++;
308
}
309
310
- start_progress_delay(&progress, "Checking out files",
311
- total, 50, 2);
310
+ progress = start_progress_delay("Checking out files",
311
+ total, 50, 2);
312
cnt = 0;
313
}
314
@@ -318,7 +318,7 @@ static void check_updates(struct cache_entry **src, int nr,
318
319
if (total)
320
if (!ce->ce_mode || ce->ce_flags & mask)
321
- display_progress(&progress, ++cnt);
321
+ display_progress(progress, ++cnt);
322
if (!ce->ce_mode) {
323
if (o->update)
324
unlink_entry(ce->name, last_symlink);
@@ -333,7 +333,7 @@ static void check_updates(struct cache_entry **src, int nr,
333
}
334
}
335
if (total)
336
- stop_progress(&progress);;
336
+ stop_progress(&progress);
337
}
338
339
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)