]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
relax usage of the progress API
[thirdparty/git.git] / progress.c
CommitLineData
96a02f8f
NP
1#include "git-compat-util.h"
2#include "progress.h"
3
dc6a0757
NP
4struct 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
96a02f8f
NP
13static volatile sig_atomic_t progress_update;
14
15static void progress_interval(int signum)
16{
17 progress_update = 1;
18}
19
20static void set_progress_signal(void)
21{
22 struct sigaction sa;
23 struct itimerval v;
24
180a9f22
NP
25 progress_update = 0;
26
96a02f8f
NP
27 memset(&sa, 0, sizeof(sa));
28 sa.sa_handler = progress_interval;
29 sigemptyset(&sa.sa_mask);
30 sa.sa_flags = SA_RESTART;
31 sigaction(SIGALRM, &sa, NULL);
32
33 v.it_interval.tv_sec = 1;
34 v.it_interval.tv_usec = 0;
35 v.it_value = v.it_interval;
36 setitimer(ITIMER_REAL, &v, NULL);
37}
38
39static void clear_progress_signal(void)
40{
41 struct itimerval v = {{0,},};
42 setitimer(ITIMER_REAL, &v, NULL);
43 signal(SIGALRM, SIG_IGN);
44 progress_update = 0;
45}
46
42e18fbf 47static int display(struct progress *progress, unsigned n, int done)
96a02f8f 48{
42e18fbf
NP
49 char *eol;
50
180a9f22 51 if (progress->delay) {
180a9f22
NP
52 if (!progress_update || --progress->delay)
53 return 0;
54 if (progress->total) {
55 unsigned percent = n * 100 / progress->total;
56 if (percent > progress->delayed_percent_treshold) {
57 /* inhibit this progress report entirely */
58 clear_progress_signal();
59 progress->delay = -1;
60 progress->total = 0;
61 return 0;
62 }
63 }
180a9f22 64 }
42e18fbf
NP
65
66 progress->last_value = n;
67 eol = done ? ", done. \n" : " \r";
96a02f8f
NP
68 if (progress->total) {
69 unsigned percent = n * 100 / progress->total;
70 if (percent != progress->last_percent || progress_update) {
71 progress->last_percent = percent;
42e18fbf
NP
72 fprintf(stderr, "%s: %3u%% (%u/%u)%s", progress->title,
73 percent, n, progress->total, eol);
96a02f8f
NP
74 progress_update = 0;
75 return 1;
76 }
77 } else if (progress_update) {
42e18fbf 78 fprintf(stderr, "%s: %u%s", progress->title, n, eol);
96a02f8f
NP
79 progress_update = 0;
80 return 1;
81 }
42e18fbf 82
96a02f8f
NP
83 return 0;
84}
85
42e18fbf 86int display_progress(struct progress *progress, unsigned n)
96a02f8f 87{
dc6a0757 88 return progress ? display(progress, n, 0) : 0;
96a02f8f
NP
89}
90
dc6a0757
NP
91struct progress *start_progress_delay(const char *title, unsigned total,
92 unsigned percent_treshold, unsigned delay)
180a9f22 93{
dc6a0757
NP
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 }
42e18fbf 100 progress->title = title;
180a9f22 101 progress->total = total;
42e18fbf 102 progress->last_value = -1;
180a9f22
NP
103 progress->last_percent = -1;
104 progress->delayed_percent_treshold = percent_treshold;
180a9f22
NP
105 progress->delay = delay;
106 set_progress_signal();
dc6a0757 107 return progress;
180a9f22
NP
108}
109
dc6a0757 110struct progress *start_progress(const char *title, unsigned total)
42e18fbf 111{
dc6a0757 112 return start_progress_delay(title, total, 0, 0);
42e18fbf
NP
113}
114
dc6a0757 115void stop_progress(struct progress **p_progress)
96a02f8f 116{
dc6a0757
NP
117 struct progress *progress = *p_progress;
118 if (!progress)
119 return;
120 *p_progress = NULL;
42e18fbf
NP
121 if (progress->last_value != -1) {
122 /* Force the last update */
123 progress_update = 1;
124 display(progress, progress->last_value, 1);
125 }
96a02f8f 126 clear_progress_signal();
dc6a0757 127 free(progress);
96a02f8f 128}