Skip to content

Commit 85cb890

Browse files
lukemgitster
authored andcommitted
progress: no progress in background
Disable the display of the progress if stderr is not the current foreground process. Still display the final result when done. Signed-off-by: Luke Mewburn <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e6d207 commit 85cb890

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

progress.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ static void clear_progress_signal(void)
7272
progress_update = 0;
7373
}
7474

75+
static int is_foreground_fd(int fd)
76+
{
77+
return getpgid(0) == tcgetpgrp(fd);
78+
}
79+
7580
static int display(struct progress *progress, unsigned n, const char *done)
7681
{
7782
const char *eol, *tp;
@@ -98,16 +103,21 @@ static int display(struct progress *progress, unsigned n, const char *done)
98103
unsigned percent = n * 100 / progress->total;
99104
if (percent != progress->last_percent || progress_update) {
100105
progress->last_percent = percent;
101-
fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
102-
progress->title, percent, n,
103-
progress->total, tp, eol);
104-
fflush(stderr);
106+
if (is_foreground_fd(fileno(stderr)) || done) {
107+
fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
108+
progress->title, percent, n,
109+
progress->total, tp, eol);
110+
fflush(stderr);
111+
}
105112
progress_update = 0;
106113
return 1;
107114
}
108115
} else if (progress_update) {
109-
fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
110-
fflush(stderr);
116+
if (is_foreground_fd(fileno(stderr)) || done) {
117+
fprintf(stderr, "%s: %u%s%s",
118+
progress->title, n, tp, eol);
119+
fflush(stderr);
120+
}
111121
progress_update = 0;
112122
return 1;
113123
}

0 commit comments

Comments
 (0)