@matheusbsbs / matheus-bagliano / commits / 96a02f8f6d
Fork 0

common progress display support

Instead of having this code duplicated in multiple places, let's have a common interface for progress display. If someday someone wishes to display a cheezy progress bar instead then only one file will have to be changed. Note: I left merge-recursive.c out since it has a strange notion of progress as it apparently increase the expected total number as it goes. Someone with more intimate knowledge of what that is supposed to mean might look at converting it to the common progress interface. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 18, 2007 at 14:27 UTC 96a02f8f6d2192d3686cd1c719044082c89e8391
7 files changed +154 -185
Makefile
+2 -2
@@ -283,7 +283,7 @@ LIB_H = \
283 diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
284 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
285 tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
286 - utf8.h reflog-walk.h patch-ids.h attr.h decorate.h
286 + utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h
287
288 DIFF_OBJS = \
289 diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -305,7 +305,7 @@ LIB_OBJS = \
305 write_or_die.o trace.o list-objects.o grep.o match-trees.o \
306 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
307 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
308 - convert.o attr.o decorate.o
308 + convert.o attr.o decorate.o progress.o
309
310 BUILTIN_OBJS = \
311 builtin-add.o \
builtin-pack-objects.c
+21 -62
@@ -12,6 +12,7 @@
12 #include "diff.h"
13 #include "revision.h"
14 #include "list-objects.h"
15 +#include "progress.h"
16
17 static const char pack_usage[] = "\
18 git-pack-objects [{ -q | --progress | --all-progress }] \n\
@@ -62,10 +63,10 @@ static const char *pack_tmp_name, *idx_tmp_name;
63 static char tmpname[PATH_MAX];
64 static unsigned char pack_file_sha1[20];
65 static int progress = 1;
65 -static volatile sig_atomic_t progress_update;
66 static int window = 10;
67 static int pack_to_stdout;
68 static int num_preferred_base;
69 +static struct progress progress_state;
70
71 /*
72 * The object names in objects array are hashed with this hashtable,
@@ -564,7 +565,6 @@ static off_t write_pack_file(void)
565 struct sha1file *f;
566 off_t offset, last_obj_offset = 0;
567 struct pack_header hdr;
567 - unsigned last_percent = 999;
568 int do_progress = progress;
569
570 if (pack_to_stdout) {
@@ -580,8 +580,10 @@ static off_t write_pack_file(void)
580 f = sha1fd(fd, pack_tmp_name);
581 }
582
583 - if (do_progress)
583 + if (do_progress) {
584 fprintf(stderr, "Writing %u objects.\n", nr_result);
585 + start_progress(&progress_state, "", nr_result);
586 + }
587
588 hdr.hdr_signature = htonl(PACK_SIGNATURE);
589 hdr.hdr_version = htonl(PACK_VERSION);
@@ -593,18 +595,11 @@ static off_t write_pack_file(void)
595 for (i = 0; i < nr_objects; i++) {
596 last_obj_offset = offset;
597 offset = write_one(f, objects + i, offset);
596 - if (do_progress) {
597 - unsigned percent = written * 100 / nr_result;
598 - if (progress_update || percent != last_percent) {
599 - fprintf(stderr, "%4u%% (%u/%u) done\r",
600 - percent, written, nr_result);
601 - progress_update = 0;
602 - last_percent = percent;
603 - }
604 - }
598 + if (do_progress)
599 + display_progress(&progress_state, written);
600 }
601 if (do_progress)
607 - fputc('\n', stderr);
602 + stop_progress(&progress_state);
603 done:
604 if (written != nr_result)
605 die("wrote %u objects while expecting %u", written, nr_result);
@@ -865,10 +860,8 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
860 else
861 object_ix[-1 - ix] = nr_objects;
862
868 - if (progress_update) {
869 - fprintf(stderr, "Counting objects...%u\r", nr_objects);
870 - progress_update = 0;
871 - }
863 + if (progress)
864 + display_progress(&progress_state, nr_objects);
865
866 return 1;
867 }
@@ -1390,15 +1383,16 @@ static void find_deltas(struct object_entry **list, int window, int depth)
1383 uint32_t i = nr_objects, idx = 0, processed = 0;
1384 unsigned int array_size = window * sizeof(struct unpacked);
1385 struct unpacked *array;
1393 - unsigned last_percent = 999;
1386 int max_depth;
1387
1388 if (!nr_objects)
1389 return;
1390 array = xmalloc(array_size);
1391 memset(array, 0, array_size);
1400 - if (progress)
1392 + if (progress) {
1393 fprintf(stderr, "Deltifying %u objects.\n", nr_result);
1394 + start_progress(&progress_state, "", nr_result);
1395 + }
1396
1397 do {
1398 struct object_entry *entry = list[--i];
@@ -1408,15 +1402,8 @@ static void find_deltas(struct object_entry **list, int window, int depth)
1402 if (!entry->preferred_base)
1403 processed++;
1404
1411 - if (progress) {
1412 - unsigned percent = processed * 100 / nr_result;
1413 - if (percent != last_percent || progress_update) {
1414 - fprintf(stderr, "%4u%% (%u/%u) done\r",
1415 - percent, processed, nr_result);
1416 - progress_update = 0;
1417 - last_percent = percent;
1418 - }
1419 - }
1405 + if (progress)
1406 + display_progress(&progress_state, processed);
1407
1408 if (entry->delta)
1409 /* This happens if we decided to reuse existing
@@ -1471,7 +1458,7 @@ static void find_deltas(struct object_entry **list, int window, int depth)
1458 } while (i > 0);
1459
1460 if (progress)
1474 - fputc('\n', stderr);
1461 + stop_progress(&progress_state);
1462
1463 for (i = 0; i < window; ++i) {
1464 free_delta_index(array[i].index);
@@ -1498,28 +1485,6 @@ static void prepare_pack(int window, int depth)
1485 free(delta_list);
1486 }
1487
1501 -static void progress_interval(int signum)
1502 -{
1503 - progress_update = 1;
1504 -}
1505 -
1506 -static void setup_progress_signal(void)
1507 -{
1508 - struct sigaction sa;
1509 - struct itimerval v;
1510 -
1511 - memset(&sa, 0, sizeof(sa));
1512 - sa.sa_handler = progress_interval;
1513 - sigemptyset(&sa.sa_mask);
1514 - sa.sa_flags = SA_RESTART;
1515 - sigaction(SIGALRM, &sa, NULL);
1516 -
1517 - v.it_interval.tv_sec = 1;
1518 - v.it_interval.tv_usec = 0;
1519 - v.it_value = v.it_interval;
1520 - setitimer(ITIMER_REAL, &v, NULL);
1521 -}
1522 -
1488 static int git_pack_config(const char *k, const char *v)
1489 {
1490 if(!strcmp(k, "pack.window")) {
@@ -1759,31 +1724,25 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1724
1725 if (progress) {
1726 fprintf(stderr, "Generating pack...\n");
1762 - setup_progress_signal();
1727 + start_progress(&progress_state, "Counting objects: ", 0);
1728 }
1764 -
1729 if (!use_internal_rev_list)
1730 read_object_list_from_stdin();
1731 else {
1732 rp_av[rp_ac] = NULL;
1733 get_object_list(rp_ac, rp_av);
1734 }
1771 -
1772 - if (progress)
1735 + if (progress) {
1736 + stop_progress(&progress_state);
1737 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1738 + }
1739 +
1740 if (non_empty && !nr_result)
1741 return 0;
1742 if (progress && (nr_objects != nr_result))
1743 fprintf(stderr, "Result has %u objects.\n", nr_result);
1744 if (nr_result)
1745 prepare_pack(window, depth);
1780 - if (progress == 1 && pack_to_stdout) {
1781 - /* the other end usually displays progress itself */
1782 - struct itimerval v = {{0,},};
1783 - setitimer(ITIMER_REAL, &v, NULL);
1784 - signal(SIGALRM, SIG_IGN );
1785 - progress_update = 0;
1786 - }
1746 last_obj_offset = write_pack_file();
1747 if (!pack_to_stdout) {
1748 unsigned char object_list_sha1[20];
builtin-unpack-objects.c
+17 -21
@@ -7,6 +7,7 @@
7 #include "commit.h"
8 #include "tag.h"
9 #include "tree.h"
10 +#include "progress.h"
11
12 static int dry_run, quiet, recover, has_errors;
13 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
@@ -264,7 +265,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
265 free(base);
266 }
267
267 -static void unpack_one(unsigned nr, unsigned total)
268 +static void unpack_one(unsigned nr)
269 {
270 unsigned shift;
271 unsigned char *pack, c;
@@ -286,20 +287,7 @@ static void unpack_one(unsigned nr, unsigned total)
287 size += (c & 0x7f) << shift;
288 shift += 7;
289 }
289 - if (!quiet) {
290 - static unsigned long last_sec;
291 - static unsigned last_percent;
292 - struct timeval now;
293 - unsigned percentage = ((nr+1) * 100) / total;
294 -
295 - gettimeofday(&now, NULL);
296 - if (percentage != last_percent || now.tv_sec != last_sec) {
297 - last_sec = now.tv_sec;
298 - last_percent = percentage;
299 - fprintf(stderr, "%4u%% (%u/%u) done\r",
300 - percentage, (nr+1), total);
301 - }
302 - }
290 +
291 switch (type) {
292 case OBJ_COMMIT:
293 case OBJ_TREE:
@@ -323,6 +311,7 @@ static void unpack_one(unsigned nr, unsigned total)
311 static void unpack_all(void)
312 {
313 int i;
314 + struct progress progress;
315 struct pack_header *hdr = fill(sizeof(struct pack_header));
316 unsigned nr_objects = ntohl(hdr->hdr_entries);
317
@@ -330,12 +319,21 @@ static void unpack_all(void)
319 die("bad pack file");
320 if (!pack_version_ok(hdr->hdr_version))
321 die("unknown pack file version %d", ntohl(hdr->hdr_version));
333 - fprintf(stderr, "Unpacking %d objects\n", nr_objects);
322 + use(sizeof(struct pack_header));
323
324 + if (!quiet) {
325 + fprintf(stderr, "Unpacking %d objects\n", nr_objects);
326 + start_progress(&progress, "", nr_objects);
327 + }
328 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
336 - use(sizeof(struct pack_header));
337 - for (i = 0; i < nr_objects; i++)
338 - unpack_one(i, nr_objects);
329 + for (i = 0; i < nr_objects; i++) {
330 + unpack_one(i);
331 + if (!quiet)
332 + display_progress(&progress, i + 1);
333 + }
334 + if (!quiet)
335 + stop_progress(&progress);
336 +
337 if (delta_list)
338 die("unresolved deltas left after unpacking");
339 }
@@ -404,7 +402,5 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
402 }
403
404 /* All done */
407 - if (!quiet)
408 - fprintf(stderr, "\n");
405 return has_errors;
406 }
index-pack.c
+23 -55
@@ -6,6 +6,7 @@
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
9 +#include "progress.h"
10
11 static const char index_pack_usage[] =
12 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
@@ -47,40 +48,7 @@ static int nr_resolved_deltas;
48 static int from_stdin;
49 static int verbose;
50
50 -static volatile sig_atomic_t progress_update;
51 -
52 -static void progress_interval(int signum)
53 -{
54 - progress_update = 1;
55 -}
56 -
57 -static void setup_progress_signal(void)
58 -{
59 - struct sigaction sa;
60 - struct itimerval v;
61 -
62 - memset(&sa, 0, sizeof(sa));
63 - sa.sa_handler = progress_interval;
64 - sigemptyset(&sa.sa_mask);
65 - sa.sa_flags = SA_RESTART;
66 - sigaction(SIGALRM, &sa, NULL);
67 -
68 - v.it_interval.tv_sec = 1;
69 - v.it_interval.tv_usec = 0;
70 - v.it_value = v.it_interval;
71 - setitimer(ITIMER_REAL, &v, NULL);
72 -
73 -}
74 -
75 -static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
76 -{
77 - unsigned percent = n * 100 / total;
78 - if (percent != last_pc || progress_update) {
79 - fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total);
80 - progress_update = 0;
81 - }
82 - return percent;
83 -}
51 +static struct progress progress;
52
53 /* We always read in 4kB chunks. */
54 static unsigned char input_buffer[4096];
@@ -428,7 +396,7 @@ static int compare_delta_entry(const void *a, const void *b)
396 /* Parse all objects and return the pack content SHA1 hash */
397 static void parse_pack_objects(unsigned char *sha1)
398 {
431 - int i, percent = -1;
399 + int i;
400 struct delta_entry *delta = deltas;
401 void *data;
402 struct stat st;
@@ -439,8 +407,10 @@ static void parse_pack_objects(unsigned char *sha1)
407 * - calculate SHA1 of all non-delta objects;
408 * - remember base (SHA1 or offset) for all deltas.
409 */
442 - if (verbose)
410 + if (verbose) {
411 fprintf(stderr, "Indexing %d objects.\n", nr_objects);
412 + start_progress(&progress, "", nr_objects);
413 + }
414 for (i = 0; i < nr_objects; i++) {
415 struct object_entry *obj = &objects[i];
416 data = unpack_raw_entry(obj, &delta->base);
@@ -453,11 +423,11 @@ static void parse_pack_objects(unsigned char *sha1)
423 sha1_object(data, obj->size, obj->type, obj->sha1);
424 free(data);
425 if (verbose)
456 - percent = display_progress(i+1, nr_objects, percent);
426 + display_progress(&progress, i+1);
427 }
428 objects[i].offset = consumed_bytes;
429 if (verbose)
460 - fputc('\n', stderr);
430 + stop_progress(&progress);
431
432 /* Check pack integrity */
433 flush();
@@ -488,8 +458,10 @@ static void parse_pack_objects(unsigned char *sha1)
458 * recursively checking if the resulting object is used as a base
459 * for some more deltas.
460 */
491 - if (verbose)
461 + if (verbose) {
462 fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
463 + start_progress(&progress, "", nr_deltas);
464 + }
465 for (i = 0; i < nr_objects; i++) {
466 struct object_entry *obj = &objects[i];
467 union delta_base base;
@@ -521,11 +493,8 @@ static void parse_pack_objects(unsigned char *sha1)
493 }
494 free(data);
495 if (verbose)
524 - percent = display_progress(nr_resolved_deltas,
525 - nr_deltas, percent);
496 + display_progress(&progress, nr_resolved_deltas);
497 }
527 - if (verbose && nr_resolved_deltas == nr_deltas)
528 - fputc('\n', stderr);
498 }
499
500 static int write_compressed(int fd, void *in, unsigned int size, uint32_t *obj_crc)
@@ -587,7 +556,7 @@ static int delta_pos_compare(const void *_a, const void *_b)
556 static void fix_unresolved_deltas(int nr_unresolved)
557 {
558 struct delta_entry **sorted_by_pos;
590 - int i, n = 0, percent = -1;
559 + int i, n = 0;
560
561 /*
562 * Since many unresolved deltas may well be themselves base objects
@@ -632,12 +601,9 @@ static void fix_unresolved_deltas(int nr_unresolved)
601 append_obj_to_pack(d->base.sha1, data, size, type);
602 free(data);
603 if (verbose)
635 - percent = display_progress(nr_resolved_deltas,
636 - nr_deltas, percent);
604 + display_progress(&progress, nr_resolved_deltas);
605 }
606 free(sorted_by_pos);
639 - if (verbose)
640 - fputc('\n', stderr);
607 }
608
609 static void readjust_pack_header_and_sha1(unsigned char *sha1)
@@ -980,10 +946,13 @@ int main(int argc, char **argv)
946 parse_pack_header();
947 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
948 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
983 - if (verbose)
984 - setup_progress_signal();
949 parse_pack_objects(sha1);
986 - if (nr_deltas != nr_resolved_deltas) {
950 + if (nr_deltas == nr_resolved_deltas) {
951 + if (verbose)
952 + stop_progress(&progress);
953 + /* Flush remaining pack final 20-byte SHA1. */
954 + flush();
955 + } else {
956 if (fix_thin_pack) {
957 int nr_unresolved = nr_deltas - nr_resolved_deltas;
958 int nr_objects_initial = nr_objects;
@@ -993,17 +962,16 @@ int main(int argc, char **argv)
962 (nr_objects + nr_unresolved + 1)
963 * sizeof(*objects));
964 fix_unresolved_deltas(nr_unresolved);
996 - if (verbose)
965 + if (verbose) {
966 + stop_progress(&progress);
967 fprintf(stderr, "%d objects were added to complete this thin pack.\n",
968 nr_objects - nr_objects_initial);
969 + }
970 readjust_pack_header_and_sha1(sha1);
971 }
972 if (nr_deltas != nr_resolved_deltas)
973 die("pack has %d unresolved deltas",
974 nr_deltas - nr_resolved_deltas);
1004 - } else {
1005 - /* Flush remaining pack final 20-byte SHA1. */
1006 - flush();
975 }
976 free(deltas);
977 curr_index = write_index_file(index_name, sha1);
progress.c new
+68
@@ -0,0 +1,68 @@
1 +#include "git-compat-util.h"
2 +#include "progress.h"
3 +
4 +static volatile sig_atomic_t progress_update;
5 +
6 +static void progress_interval(int signum)
7 +{
8 + progress_update = 1;
9 +}
10 +
11 +static void set_progress_signal(void)
12 +{
13 + struct sigaction sa;
14 + struct itimerval v;
15 +
16 + memset(&sa, 0, sizeof(sa));
17 + sa.sa_handler = progress_interval;
18 + sigemptyset(&sa.sa_mask);
19 + sa.sa_flags = SA_RESTART;
20 + sigaction(SIGALRM, &sa, NULL);
21 +
22 + v.it_interval.tv_sec = 1;
23 + v.it_interval.tv_usec = 0;
24 + v.it_value = v.it_interval;
25 + setitimer(ITIMER_REAL, &v, NULL);
26 +}
27 +
28 +static void clear_progress_signal(void)
29 +{
30 + struct itimerval v = {{0,},};
31 + setitimer(ITIMER_REAL, &v, NULL);
32 + signal(SIGALRM, SIG_IGN);
33 + progress_update = 0;
34 +}
35 +
36 +int display_progress(struct progress *progress, unsigned n)
37 +{
38 + if (progress->total) {
39 + unsigned percent = n * 100 / progress->total;
40 + if (percent != progress->last_percent || progress_update) {
41 + progress->last_percent = percent;
42 + fprintf(stderr, "%s%4u%% (%u/%u) done\r",
43 + progress->msg, percent, n, progress->total);
44 + progress_update = 0;
45 + return 1;
46 + }
47 + } else if (progress_update) {
48 + fprintf(stderr, "%s%u\r", progress->msg, n);
49 + progress_update = 0;
50 + return 1;
51 + }
52 + return 0;
53 +}
54 +
55 +void start_progress(struct progress *progress, const char *msg, unsigned total)
56 +{
57 + progress->msg = msg;
58 + progress->total = total;
59 + progress->last_percent = -1;
60 + set_progress_signal();
61 +}
62 +
63 +void stop_progress(struct progress *progress)
64 +{
65 + clear_progress_signal();
66 + if (progress->total)
67 + fputc('\n', stderr);
68 +}
progress.h new
+14
@@ -0,0 +1,14 @@
1 +#ifndef __progress_h__
2 +#define __progress_h__
3 +
4 +struct progress {
5 + const char *msg;
6 + unsigned total;
7 + unsigned last_percent;
8 +};
9 +
10 +int display_progress(struct progress *progress, unsigned n);
11 +void start_progress(struct progress *progress, const char *msg, unsigned total);
12 +void stop_progress(struct progress *progress);
13 +
14 +#endif
unpack-trees.c
+9 -45
@@ -4,6 +4,7 @@
4 #include "tree-walk.h"
5 #include "cache-tree.h"
6 #include "unpack-trees.h"
7 +#include "progress.h"
8
9 #define DBRT_DEBUG 1
10
@@ -288,36 +289,13 @@ static void unlink_entry(char *name)
289 }
290 }
291
291 -static volatile sig_atomic_t progress_update;
292 -
293 -static void progress_interval(int signum)
294 -{
295 - progress_update = 1;
296 -}
297 -
298 -static void setup_progress_signal(void)
299 -{
300 - struct sigaction sa;
301 - struct itimerval v;
302 -
303 - memset(&sa, 0, sizeof(sa));
304 - sa.sa_handler = progress_interval;
305 - sigemptyset(&sa.sa_mask);
306 - sa.sa_flags = SA_RESTART;
307 - sigaction(SIGALRM, &sa, NULL);
308 -
309 - v.it_interval.tv_sec = 1;
310 - v.it_interval.tv_usec = 0;
311 - v.it_value = v.it_interval;
312 - setitimer(ITIMER_REAL, &v, NULL);
313 -}
314 -
292 static struct checkout state;
293 static void check_updates(struct cache_entry **src, int nr,
294 struct unpack_trees_options *o)
295 {
296 unsigned short mask = htons(CE_UPDATE);
320 - unsigned last_percent = 200, cnt = 0, total = 0;
297 + unsigned cnt = 0, total = 0;
298 + struct progress progress;
299
300 if (o->update && o->verbose_update) {
301 for (total = cnt = 0; cnt < nr; cnt++) {
@@ -332,8 +310,7 @@ static void check_updates(struct cache_entry **src, int nr,
310
311 if (total) {
312 fprintf(stderr, "Checking files out...\n");
335 - setup_progress_signal();
336 - progress_update = 1;
313 + start_progress(&progress, "", total);
314 }
315 cnt = 0;
316 }
@@ -341,20 +318,9 @@ static void check_updates(struct cache_entry **src, int nr,
318 while (nr--) {
319 struct cache_entry *ce = *src++;
320
344 - if (total) {
345 - if (!ce->ce_mode || ce->ce_flags & mask) {
346 - unsigned percent;
347 - cnt++;
348 - percent = (cnt * 100) / total;
349 - if (percent != last_percent ||
350 - progress_update) {
351 - fprintf(stderr, "%4u%% (%u/%u) done\r",
352 - percent, cnt, total);
353 - last_percent = percent;
354 - progress_update = 0;
355 - }
356 - }
357 - }
321 + if (total)
322 + if (!ce->ce_mode || ce->ce_flags & mask)
323 + display_progress(&progress, ++cnt);
324 if (!ce->ce_mode) {
325 if (o->update)
326 unlink_entry(ce->name);
@@ -366,10 +332,8 @@ static void check_updates(struct cache_entry **src, int nr,
332 checkout_entry(ce, &state, NULL);
333 }
334 }
369 - if (total) {
370 - signal(SIGALRM, SIG_IGN);
371 - fputc('\n', stderr);
372 - }
335 + if (total)
336 + stop_progress(&progress);;
337 }
338
339 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)