Skip to content

Commit 03edb0a

Browse files
committed
Merge branch 'np/progress'
* np/progress: nicer display of thin pack completion make display of total transferred fully accurate remove dead code from the csum-file interface git-fetch: be even quieter. make display of total transferred more accurate sideband.c: ESC is spelled '\033' not '\e' for portability. fix display overlap between remote and local progress
2 parents dcb83ec + a984a06 commit 03edb0a

File tree

7 files changed

+134
-93
lines changed

7 files changed

+134
-93
lines changed

builtin-fetch.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static int s_update_ref(const char *action,
152152
}
153153

154154
#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
155+
#define REFCOL_WIDTH 10
155156

156157
static int update_local_ref(struct ref *ref,
157158
const char *remote,
@@ -181,8 +182,9 @@ static int update_local_ref(struct ref *ref,
181182

182183
if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
183184
if (verbose)
184-
sprintf(display, "= %-*s %s -> %s", SUMMARY_WIDTH,
185-
"[up to date]", remote, pretty_ref);
185+
sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
186+
"[up to date]", REFCOL_WIDTH, remote,
187+
pretty_ref);
186188
return 0;
187189
}
188190

@@ -194,15 +196,17 @@ static int update_local_ref(struct ref *ref,
194196
* If this is the head, and it's not okay to update
195197
* the head, and the old value of the head isn't empty...
196198
*/
197-
sprintf(display, "! %-*s %s -> %s (can't fetch in current branch)",
198-
SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
199+
sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
200+
SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
201+
pretty_ref);
199202
return 1;
200203
}
201204

202205
if (!is_null_sha1(ref->old_sha1) &&
203206
!prefixcmp(ref->name, "refs/tags/")) {
204-
sprintf(display, "- %-*s %s -> %s",
205-
SUMMARY_WIDTH, "[tag update]", remote, pretty_ref);
207+
sprintf(display, "- %-*s %-*s -> %s",
208+
SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
209+
pretty_ref);
206210
return s_update_ref("updating tag", ref, 0);
207211
}
208212

@@ -220,8 +224,8 @@ static int update_local_ref(struct ref *ref,
220224
what = "[new branch]";
221225
}
222226

223-
sprintf(display, "* %-*s %s -> %s",
224-
SUMMARY_WIDTH, what, remote, pretty_ref);
227+
sprintf(display, "* %-*s %-*s -> %s", SUMMARY_WIDTH, what,
228+
REFCOL_WIDTH, remote, pretty_ref);
225229
return s_update_ref(msg, ref, 0);
226230
}
227231

@@ -230,20 +234,21 @@ static int update_local_ref(struct ref *ref,
230234
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
231235
strcat(quickref, "..");
232236
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
233-
sprintf(display, " %-*s %s -> %s (fast forward)",
234-
SUMMARY_WIDTH, quickref, remote, pretty_ref);
237+
sprintf(display, " %-*s %-*s -> %s", SUMMARY_WIDTH, quickref,
238+
REFCOL_WIDTH, remote, pretty_ref);
235239
return s_update_ref("fast forward", ref, 1);
236240
} else if (force || ref->force) {
237241
char quickref[84];
238242
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
239243
strcat(quickref, "...");
240244
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
241-
sprintf(display, "+ %-*s %s -> %s (forced update)",
242-
SUMMARY_WIDTH, quickref, remote, pretty_ref);
245+
sprintf(display, "+ %-*s %-*s -> %s (forced update)",
246+
SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, pretty_ref);
243247
return s_update_ref("forced-update", ref, 1);
244248
} else {
245-
sprintf(display, "! %-*s %s -> %s (non fast forward)",
246-
SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
249+
sprintf(display, "! %-*s %-*s -> %s (non fast forward)",
250+
SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
251+
pretty_ref);
247252
return 1;
248253
}
249254
}

csum-file.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ static void sha1flush(struct sha1file *f, unsigned int count)
1818
for (;;) {
1919
int ret = xwrite(f->fd, buf, count);
2020
if (ret > 0) {
21-
display_throughput(f->tp, ret);
21+
f->total += ret;
22+
display_throughput(f->tp, f->total);
2223
buf = (char *) buf + ret;
2324
count -= ret;
2425
if (count)
@@ -87,21 +88,12 @@ struct sha1file *sha1fd(int fd, const char *name)
8788

8889
struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
8990
{
90-
struct sha1file *f;
91-
unsigned len;
92-
93-
f = xmalloc(sizeof(*f));
94-
95-
len = strlen(name);
96-
if (len >= PATH_MAX)
97-
die("you wascally wabbit, you");
98-
f->namelen = len;
99-
memcpy(f->name, name, len+1);
100-
91+
struct sha1file *f = xmalloc(sizeof(*f));
10192
f->fd = fd;
102-
f->error = 0;
10393
f->offset = 0;
94+
f->total = 0;
10495
f->tp = tp;
96+
f->name = name;
10597
f->do_crc = 0;
10698
SHA1_Init(&f->ctx);
10799
return f;

csum-file.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ struct progress;
55

66
/* A SHA1-protected file */
77
struct sha1file {
8-
int fd, error;
9-
unsigned int offset, namelen;
8+
int fd;
9+
unsigned int offset;
1010
SHA_CTX ctx;
11+
off_t total;
1112
struct progress *tp;
12-
char name[PATH_MAX];
13+
const char *name;
1314
int do_crc;
1415
uint32_t crc32;
1516
unsigned char buffer[8192];

index-pack.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ static void *fill(int min)
8787
die("early EOF");
8888
die("read error on input: %s", strerror(errno));
8989
}
90-
if (from_stdin)
91-
display_throughput(progress, ret);
9290
input_len += ret;
91+
if (from_stdin)
92+
display_throughput(progress, consumed_bytes + input_len);
9393
} while (input_len < min);
9494
return input_buffer;
9595
}
@@ -792,6 +792,7 @@ int main(int argc, char **argv)
792792
flush();
793793
} else {
794794
if (fix_thin_pack) {
795+
char msg[48];
795796
int nr_unresolved = nr_deltas - nr_resolved_deltas;
796797
int nr_objects_initial = nr_objects;
797798
if (nr_unresolved <= 0)
@@ -800,12 +801,11 @@ int main(int argc, char **argv)
800801
(nr_objects + nr_unresolved + 1)
801802
* sizeof(*objects));
802803
fix_unresolved_deltas(nr_unresolved);
803-
stop_progress(&progress);
804-
if (verbose)
805-
fprintf(stderr, "%d objects were added to complete this thin pack.\n",
806-
nr_objects - nr_objects_initial);
804+
sprintf(msg, "completed with %d local objects",
805+
nr_objects - nr_objects_initial);
806+
stop_progress_msg(&progress, msg);
807807
fixup_pack_header_footer(output_fd, sha1,
808-
curr_pack, nr_objects);
808+
curr_pack, nr_objects);
809809
}
810810
if (nr_deltas != nr_resolved_deltas)
811811
die("pack has %d unresolved deltas",

progress.c

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#define TP_IDX_MAX 8
1515

1616
struct throughput {
17+
off_t curr_total;
18+
off_t prev_total;
1719
struct timeval prev_tv;
18-
off_t total;
19-
unsigned long count;
20-
unsigned long avg_bytes;
21-
unsigned long last_bytes[TP_IDX_MAX];
20+
unsigned int avg_bytes;
2221
unsigned int avg_misecs;
22+
unsigned int last_bytes[TP_IDX_MAX];
2323
unsigned int last_misecs[TP_IDX_MAX];
2424
unsigned int idx;
2525
char display[32];
@@ -69,9 +69,9 @@ static void clear_progress_signal(void)
6969
progress_update = 0;
7070
}
7171

72-
static int display(struct progress *progress, unsigned n, int done)
72+
static int display(struct progress *progress, unsigned n, const char *done)
7373
{
74-
char *eol, *tp;
74+
const char *eol, *tp;
7575

7676
if (progress->delay) {
7777
if (!progress_update || --progress->delay)
@@ -90,7 +90,7 @@ static int display(struct progress *progress, unsigned n, int done)
9090

9191
progress->last_value = n;
9292
tp = (progress->throughput) ? progress->throughput->display : "";
93-
eol = done ? ", done. \n" : " \r";
93+
eol = done ? done : " \r";
9494
if (progress->total) {
9595
unsigned percent = n * 100 / progress->total;
9696
if (percent != progress->last_percent || progress_update) {
@@ -110,7 +110,31 @@ static int display(struct progress *progress, unsigned n, int done)
110110
return 0;
111111
}
112112

113-
void display_throughput(struct progress *progress, unsigned long n)
113+
static void throughput_string(struct throughput *tp, off_t total,
114+
unsigned int rate)
115+
{
116+
int l = sizeof(tp->display);
117+
if (total > 1 << 30) {
118+
l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
119+
(int)(total >> 30),
120+
(int)(total & ((1 << 30) - 1)) / 10737419);
121+
} else if (total > 1 << 20) {
122+
l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
123+
(int)(total >> 20),
124+
((int)(total & ((1 << 20) - 1)) * 100) >> 20);
125+
} else if (total > 1 << 10) {
126+
l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
127+
(int)(total >> 10),
128+
((int)(total & ((1 << 10) - 1)) * 100) >> 10);
129+
} else {
130+
l -= snprintf(tp->display, l, ", %u bytes", (int)total);
131+
}
132+
if (rate)
133+
snprintf(tp->display + sizeof(tp->display) - l, l,
134+
" | %u KiB/s", rate);
135+
}
136+
137+
void display_throughput(struct progress *progress, off_t total)
114138
{
115139
struct throughput *tp;
116140
struct timeval tv;
@@ -124,13 +148,13 @@ void display_throughput(struct progress *progress, unsigned long n)
124148

125149
if (!tp) {
126150
progress->throughput = tp = calloc(1, sizeof(*tp));
127-
if (tp)
151+
if (tp) {
152+
tp->prev_total = tp->curr_total = total;
128153
tp->prev_tv = tv;
154+
}
129155
return;
130156
}
131-
132-
tp->total += n;
133-
tp->count += n;
157+
tp->curr_total = total;
134158

135159
/*
136160
* We have x = bytes and y = microsecs. We want z = KiB/s:
@@ -151,47 +175,29 @@ void display_throughput(struct progress *progress, unsigned long n)
151175
misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
152176

153177
if (misecs > 512) {
154-
int l = sizeof(tp->display);
178+
unsigned int count, rate;
179+
180+
count = total - tp->prev_total;
181+
tp->prev_total = total;
155182
tp->prev_tv = tv;
156-
tp->avg_bytes += tp->count;
183+
tp->avg_bytes += count;
157184
tp->avg_misecs += misecs;
158-
159-
if (tp->total > 1 << 30) {
160-
l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
161-
(int)(tp->total >> 30),
162-
(int)(tp->total & ((1 << 30) - 1)) / 10737419);
163-
} else if (tp->total > 1 << 20) {
164-
l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
165-
(int)(tp->total >> 20),
166-
((int)(tp->total & ((1 << 20) - 1))
167-
* 100) >> 20);
168-
} else if (tp->total > 1 << 10) {
169-
l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
170-
(int)(tp->total >> 10),
171-
((int)(tp->total & ((1 << 10) - 1))
172-
* 100) >> 10);
173-
} else {
174-
l -= snprintf(tp->display, l, ", %u bytes",
175-
(int)tp->total);
176-
}
177-
snprintf(tp->display + sizeof(tp->display) - l, l,
178-
" | %lu KiB/s", tp->avg_bytes / tp->avg_misecs);
179-
185+
rate = tp->avg_bytes / tp->avg_misecs;
180186
tp->avg_bytes -= tp->last_bytes[tp->idx];
181187
tp->avg_misecs -= tp->last_misecs[tp->idx];
182-
tp->last_bytes[tp->idx] = tp->count;
188+
tp->last_bytes[tp->idx] = count;
183189
tp->last_misecs[tp->idx] = misecs;
184190
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
185-
tp->count = 0;
186191

192+
throughput_string(tp, total, rate);
187193
if (progress->last_value != -1 && progress_update)
188-
display(progress, progress->last_value, 0);
194+
display(progress, progress->last_value, NULL);
189195
}
190196
}
191197

192198
int display_progress(struct progress *progress, unsigned n)
193199
{
194-
return progress ? display(progress, n, 0) : 0;
200+
return progress ? display(progress, n, NULL) : 0;
195201
}
196202

197203
struct progress *start_progress_delay(const char *title, unsigned total,
@@ -220,15 +226,28 @@ struct progress *start_progress(const char *title, unsigned total)
220226
}
221227

222228
void stop_progress(struct progress **p_progress)
229+
{
230+
stop_progress_msg(p_progress, "done");
231+
}
232+
233+
void stop_progress_msg(struct progress **p_progress, const char *msg)
223234
{
224235
struct progress *progress = *p_progress;
225236
if (!progress)
226237
return;
227238
*p_progress = NULL;
228239
if (progress->last_value != -1) {
229240
/* Force the last update */
241+
char buf[strlen(msg) + 5];
242+
struct throughput *tp = progress->throughput;
243+
if (tp) {
244+
unsigned int rate = !tp->avg_misecs ? 0 :
245+
tp->avg_bytes / tp->avg_misecs;
246+
throughput_string(tp, tp->curr_total, rate);
247+
}
230248
progress_update = 1;
231-
display(progress, progress->last_value, 1);
249+
sprintf(buf, ", %s.\n", msg);
250+
display(progress, progress->last_value, buf);
232251
}
233252
clear_progress_signal();
234253
free(progress->throughput);

progress.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
struct progress;
55

6-
void display_throughput(struct progress *progress, unsigned long n);
6+
void display_throughput(struct progress *progress, off_t total);
77
int display_progress(struct progress *progress, unsigned n);
88
struct progress *start_progress(const char *title, unsigned total);
99
struct progress *start_progress_delay(const char *title, unsigned total,
1010
unsigned percent_treshold, unsigned delay);
1111
void stop_progress(struct progress **progress);
12+
void stop_progress_msg(struct progress **progress, const char *msg);
1213

1314
#endif

0 commit comments

Comments
 (0)