scalar: show progress if stderr refers to a terminal

Sometimes when users use scalar to download a monorepo with a long commit history, they want to check the progress bar to know how long they still need to wait during the fetch process, but scalar suppresses this output by default. So let's check whether scalar stderr refer to a terminal, if so, show progress, otherwise disable it. Signed-off-by: ZheNing Hu <adlternative@gmail.com> Acked-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed Jan 11, 2023 at 13:14 UTC 4433bd24e4acd015af9d05832c65bd6e3b097df2
2 files changed +33 -3
scalar.c
+7 -3
@@ -404,7 +404,7 @@ void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
404 static int cmd_clone(int argc, const char **argv)
405 {
406 const char *branch = NULL;
407 - int full_clone = 0, single_branch = 0;
407 + int full_clone = 0, single_branch = 0, show_progress = isatty(2);
408 struct option clone_options[] = {
409 OPT_STRING('b', "branch", &branch, N_("<branch>"),
410 N_("branch to checkout after clone")),
@@ -499,7 +499,9 @@ static int cmd_clone(int argc, const char **argv)
499 if (set_recommended_config(0))
500 return error(_("could not configure '%s'"), dir);
501
502 - if ((res = run_git("fetch", "--quiet", "origin", NULL))) {
502 + if ((res = run_git("fetch", "--quiet",
503 + show_progress ? "--progress" : "--no-progress",
504 + "origin", NULL))) {
505 warning(_("partial clone failed; attempting full clone"));
506
507 if (set_config("remote.origin.promisor") ||
@@ -508,7 +510,9 @@ static int cmd_clone(int argc, const char **argv)
510 goto cleanup;
511 }
512
511 - if ((res = run_git("fetch", "--quiet", "origin", NULL)))
513 + if ((res = run_git("fetch", "--quiet",
514 + show_progress ? "--progress" : "--no-progress",
515 + "origin", NULL)))
516 goto cleanup;
517 }
518
t/t9211-scalar-clone.sh
+26
@@ -3,6 +3,7 @@
3 test_description='test the `scalar clone` subcommand'
4
5 . ./test-lib.sh
6 +. "${TEST_DIRECTORY}/lib-terminal.sh"
7
8 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt,launchctl:true,schtasks:true"
9 export GIT_TEST_MAINT_SCHEDULER
@@ -148,4 +149,29 @@ test_expect_success '--no-single-branch clones all branches' '
149 cleanup_clone $enlistment
150 '
151
152 +test_expect_success TTY 'progress with tty' '
153 + enlistment=progress1 &&
154 +
155 + test_config -C to-clone uploadpack.allowfilter true &&
156 + test_config -C to-clone uploadpack.allowanysha1inwant true &&
157 +
158 + test_terminal env GIT_PROGRESS_DELAY=0 \
159 + scalar clone "file://$(pwd)/to-clone" "$enlistment" 2>stderr &&
160 + grep "Enumerating objects" stderr >actual &&
161 + test_line_count = 2 actual &&
162 + cleanup_clone $enlistment
163 +'
164 +
165 +test_expect_success 'progress without tty' '
166 + enlistment=progress2 &&
167 +
168 + test_config -C to-clone uploadpack.allowfilter true &&
169 + test_config -C to-clone uploadpack.allowanysha1inwant true &&
170 +
171 + GIT_PROGRESS_DELAY=0 scalar clone "file://$(pwd)/to-clone" "$enlistment" 2>stderr &&
172 + ! grep "Enumerating objects" stderr &&
173 + ! grep "Updating files" stderr &&
174 + cleanup_clone $enlistment
175 +'
176 +
177 test_done