add progress status to index-pack
This is more interesting to look at when performing a big fetch. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Oct 25, 2006 at 23:32 UTC
3c9af366469524920864bbc1af4dcfd72029c314
2 files changed
+75
-6
Documentation/git-index-pack.txt
+5
-2
@@ -8,8 +8,8 @@ git-index-pack - Build pack index file for an existing packed archive
8
9
SYNOPSIS
10
--------
11
-'git-index-pack' [-o <index-file>] <pack-file>
12
-'git-index-pack' --stdin [--fix-thin] [-o <index-file>] [<pack-file>]
11
+'git-index-pack' [-v] [-o <index-file>] <pack-file>
12
+'git-index-pack' --stdin [--fix-thin] [-v] [-o <index-file>] [<pack-file>]
13
14
15
DESCRIPTION
@@ -22,6 +22,9 @@ objects/pack/ directory of a git repository.
22
23
OPTIONS
24
-------
25
+-v::
26
+ Be verbose about what is going on, including progress status.
27
+
28
-o <index-file>::
29
Write the generated pack index into the specified
30
file. Without this option the name of pack index
index-pack.c
+70
-4
@@ -6,9 +6,11 @@
6
#include "commit.h"
7
#include "tag.h"
8
#include "tree.h"
9
+#include <sys/time.h>
10
+#include <signal.h>
11
12
static const char index_pack_usage[] =
11
-"git-index-pack [-o <index-file>] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
13
+"git-index-pack [-v] [-o <index-file>] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
14
15
struct object_entry
16
{
@@ -44,6 +46,42 @@ static int nr_deltas;
46
static int nr_resolved_deltas;
47
48
static int from_stdin;
49
+static int verbose;
50
+
51
+static volatile sig_atomic_t progress_update;
52
+
53
+static void progress_interval(int signum)
54
+{
55
+ progress_update = 1;
56
+}
57
+
58
+static void setup_progress_signal(void)
59
+{
60
+ struct sigaction sa;
61
+ struct itimerval v;
62
+
63
+ memset(&sa, 0, sizeof(sa));
64
+ sa.sa_handler = progress_interval;
65
+ sigemptyset(&sa.sa_mask);
66
+ sa.sa_flags = SA_RESTART;
67
+ sigaction(SIGALRM, &sa, NULL);
68
+
69
+ v.it_interval.tv_sec = 1;
70
+ v.it_interval.tv_usec = 0;
71
+ v.it_value = v.it_interval;
72
+ setitimer(ITIMER_REAL, &v, NULL);
73
+
74
+}
75
+
76
+static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
77
+{
78
+ unsigned percent = n * 100 / total;
79
+ if (percent != last_pc || progress_update) {
80
+ fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total);
81
+ progress_update = 0;
82
+ }
83
+ return percent;
84
+}
85
86
/* We always read in 4kB chunks. */
87
static unsigned char input_buffer[4096];
@@ -135,7 +173,6 @@ static void parse_pack_header(void)
173
174
nr_objects = ntohl(hdr->hdr_entries);
175
use(sizeof(struct pack_header));
138
- /*fprintf(stderr, "Indexing %d objects\n", nr_objects);*/
176
}
177
178
static void bad_object(unsigned long offset, const char *format,
@@ -383,7 +420,7 @@ static int compare_delta_entry(const void *a, const void *b)
420
/* Parse all objects and return the pack content SHA1 hash */
421
static void parse_pack_objects(unsigned char *sha1)
422
{
386
- int i;
423
+ int i, percent = -1;
424
struct delta_entry *delta = deltas;
425
void *data;
426
struct stat st;
@@ -394,6 +431,8 @@ static void parse_pack_objects(unsigned char *sha1)
431
* - calculate SHA1 of all non-delta objects;
432
* - remember base SHA1 for all deltas.
433
*/
434
+ if (verbose)
435
+ fprintf(stderr, "Indexing %d objects.\n", nr_objects);
436
for (i = 0; i < nr_objects; i++) {
437
struct object_entry *obj = &objects[i];
438
data = unpack_raw_entry(obj, &delta->base);
@@ -405,8 +444,12 @@ static void parse_pack_objects(unsigned char *sha1)
444
} else
445
sha1_object(data, obj->size, obj->type, obj->sha1);
446
free(data);
447
+ if (verbose)
448
+ percent = display_progress(i+1, nr_objects, percent);
449
}
450
objects[i].offset = consumed_bytes;
451
+ if (verbose)
452
+ fputc('\n', stderr);
453
454
/* Check pack integrity */
455
flush();
@@ -420,6 +463,9 @@ static void parse_pack_objects(unsigned char *sha1)
463
if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes + 20)
464
die("pack has junk at the end");
465
466
+ if (!nr_deltas)
467
+ return;
468
+
469
/* Sort deltas by base SHA1/offset for fast searching */
470
qsort(deltas, nr_deltas, sizeof(struct delta_entry),
471
compare_delta_entry);
@@ -432,6 +478,8 @@ static void parse_pack_objects(unsigned char *sha1)
478
* recursively checking if the resulting object is used as a base
479
* for some more deltas.
480
*/
481
+ if (verbose)
482
+ fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
483
for (i = 0; i < nr_objects; i++) {
484
struct object_entry *obj = &objects[i];
485
union delta_base base;
@@ -462,7 +510,12 @@ static void parse_pack_objects(unsigned char *sha1)
510
obj->size, obj->type);
511
}
512
free(data);
513
+ if (verbose)
514
+ percent = display_progress(nr_resolved_deltas,
515
+ nr_deltas, percent);
516
}
517
+ if (verbose && nr_resolved_deltas == nr_deltas)
518
+ fputc('\n', stderr);
519
}
520
521
static int write_compressed(int fd, void *in, unsigned int size)
@@ -521,7 +574,7 @@ static int delta_pos_compare(const void *_a, const void *_b)
574
static void fix_unresolved_deltas(int nr_unresolved)
575
{
576
struct delta_entry **sorted_by_pos;
524
- int i, n = 0;
577
+ int i, n = 0, percent = -1;
578
579
/*
580
* Since many unresolved deltas may well be themselves base objects
@@ -570,8 +623,13 @@ static void fix_unresolved_deltas(int nr_unresolved)
623
624
append_obj_to_pack(data, size, obj_type);
625
free(data);
626
+ if (verbose)
627
+ percent = display_progress(nr_resolved_deltas,
628
+ nr_deltas, percent);
629
}
630
free(sorted_by_pos);
631
+ if (verbose)
632
+ fputc('\n', stderr);
633
}
634
635
static void readjust_pack_header_and_sha1(unsigned char *sha1)
@@ -747,6 +805,8 @@ int main(int argc, char **argv)
805
from_stdin = 1;
806
} else if (!strcmp(arg, "--fix-thin")) {
807
fix_thin_pack = 1;
808
+ } else if (!strcmp(arg, "-v")) {
809
+ verbose = 1;
810
} else if (!strcmp(arg, "-o")) {
811
if (index_name || (i+1) >= argc)
812
usage(index_pack_usage);
@@ -780,16 +840,22 @@ int main(int argc, char **argv)
840
parse_pack_header();
841
objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
842
deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
843
+ if (verbose)
844
+ setup_progress_signal();
845
parse_pack_objects(sha1);
846
if (nr_deltas != nr_resolved_deltas) {
847
if (fix_thin_pack) {
848
int nr_unresolved = nr_deltas - nr_resolved_deltas;
849
+ int nr_objects_initial = nr_objects;
850
if (nr_unresolved <= 0)
851
die("confusion beyond insanity");
852
objects = xrealloc(objects,
853
(nr_objects + nr_unresolved + 1)
854
* sizeof(*objects));
855
fix_unresolved_deltas(nr_unresolved);
856
+ if (verbose)
857
+ fprintf(stderr, "%d objects were added to complete this thin pack.\n",
858
+ nr_objects - nr_objects_initial);
859
readjust_pack_header_and_sha1(sha1);
860
}
861
if (nr_deltas != nr_resolved_deltas)