From c9749b369da14cbac6201b1730586abf1f3974f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 24 Jun 2019 20:13:14 +0200 Subject: [PATCH 1/5] t3404: modernize here doc style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In 't3404-rebase-interactive.sh' the expected output of several tests is prepared from here documents, which are outside of 'test_expect_success' blocks and have spaces around redirection operators. Move these here documents into the corresponding 'test_expect_success' block and avoid spaces between filename and redition operators. Furthermore, quote the here docs' delimiter word to prevent parameter expansions and what not, where applicable. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t3404-rebase-interactive.sh | 123 ++++++++++++++++------------------ 1 file changed, 58 insertions(+), 65 deletions(-) diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 1723e1a858585d..9146f9d47b2696 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -75,11 +75,10 @@ test_expect_success 'rebase --keep-empty' ' test_line_count = 6 actual ' -cat > expect <expect <<-\EOF && + error: nothing to do + EOF set_fake_editor && test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 && test_i18ncmp expect actual @@ -237,25 +236,23 @@ test_expect_success 'exchange two commits' ' test G = $(git cat-file commit HEAD | sed -ne \$p) ' -cat > expect << EOF -diff --git a/file1 b/file1 -index f70f10e..fd79235 100644 ---- a/file1 -+++ b/file1 -@@ -1 +1 @@ --A -+G -EOF - -cat > expect2 << EOF -<<<<<<< HEAD -D -======= -G ->>>>>>> 5d18e54... G -EOF - test_expect_success 'stop on conflicting pick' ' + cat >expect <<-\EOF && + diff --git a/file1 b/file1 + index f70f10e..fd79235 100644 + --- a/file1 + +++ b/file1 + @@ -1 +1 @@ + -A + +G + EOF + cat >expect2 <<-\EOF && + <<<<<<< HEAD + D + ======= + G + >>>>>>> 5d18e54... G + EOF git tag new-branch1 && set_fake_editor && test_must_fail git rebase -i master && @@ -495,15 +492,14 @@ test_expect_success 'commit message retained after conflict' ' git branch -D conflict-squash ' -cat > expect-squash-fixup << EOF -B - -D +test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' ' + cat >expect-squash-fixup <<-\EOF && + B -ONCE -EOF + D -test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' ' + ONCE + EOF git checkout -b squash-fixup E && base=$(git rev-parse HEAD~4) && set_fake_editor && @@ -799,13 +795,12 @@ test_expect_success 'rebase -i can copy notes' ' test "a note" = "$(git notes show HEAD)" ' -cat >expect <expect <<-\EOF && + an earlier note + + a note + EOF git reset --hard n3 && git notes add -m"an earlier note" n2 && set_fake_editor && @@ -1304,27 +1299,26 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' ' actual ' -cat >expect <expect <<-EOF && + Warning: some commits may have been dropped accidentally. + Dropped commits (newer to older): + - $(git rev-list --pretty=oneline --abbrev-commit -1 master) + To avoid this message, use "drop" to explicitly remove a commit. + + Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings. + The possible behaviours are: ignore, warn, error. + + Rebasing (1/4) + Rebasing (2/4) + Rebasing (3/4) + Rebasing (4/4) + Successfully rebased and updated refs/heads/missing-commit. + EOF test_config rebase.missingCommitsCheck warn && rebase_setup_and_clean missing-commit && set_fake_editor && @@ -1335,21 +1329,20 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' ' test D = $(git cat-file commit HEAD | sed -ne \$p) ' -cat >expect <expect <<-EOF && + Warning: some commits may have been dropped accidentally. + Dropped commits (newer to older): + - $(git rev-list --pretty=oneline --abbrev-commit -1 master) + - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2) + To avoid this message, use "drop" to explicitly remove a commit. + + Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings. + The possible behaviours are: ignore, warn, error. + + You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''. + Or you can abort the rebase with '\''git rebase --abort'\''. + EOF test_config rebase.missingCommitsCheck error && rebase_setup_and_clean missing-commit && set_fake_editor && From 077b9798919ac2aa63600a1547f55f89b62b3b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 24 Jun 2019 20:13:15 +0200 Subject: [PATCH 2/5] t3404: make the 'rebase.missingCommitsCheck=ignore' test more focused MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test 'rebase -i respects rebase.missingCommitsCheck = warn' is mainly interested in the warning about the dropped commits, but it checks the whole output of 'git rebase', including progress lines and what not that are not at all relevant to 'rebase.missingCommitsCheck', but make it necessary to update this test whenever e.g. the way we show progress is updated (as it will happen in one of the later patches of this series). Modify the test to verify only the first four lines of 'git rebase's output that contain all the important lines, notably the line containing the "Warning:" itself and the oneline log of the dropped commit. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t3404-rebase-interactive.sh | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 9146f9d47b2696..0b8267c97cb7e2 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -1299,32 +1299,19 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' ' actual ' -cr_to_nl () { - tr '\015' '\012' -} - test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' ' cat >expect <<-EOF && Warning: some commits may have been dropped accidentally. Dropped commits (newer to older): - $(git rev-list --pretty=oneline --abbrev-commit -1 master) To avoid this message, use "drop" to explicitly remove a commit. - - Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings. - The possible behaviours are: ignore, warn, error. - - Rebasing (1/4) - Rebasing (2/4) - Rebasing (3/4) - Rebasing (4/4) - Successfully rebased and updated refs/heads/missing-commit. EOF test_config rebase.missingCommitsCheck warn && rebase_setup_and_clean missing-commit && set_fake_editor && FAKE_LINES="1 2 3 4" \ git rebase -i --root 2>actual.2 && - cr_to_nl actual && + head -n4 actual.2 >actual && test_i18ncmp expect actual && test D = $(git cat-file commit HEAD | sed -ne \$p) ' From cd1096b2820d64d5fcb5da20613d5af290e42929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 24 Jun 2019 20:13:16 +0200 Subject: [PATCH 3/5] pager: add a helper function to clear the last line in the terminal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are a couple of places where we want to clear the last line on the terminal, e.g. when a progress bar line is overwritten by a shorter line, then the end of that progress line would remain visible, unless we cover it up. In 'progress.c' we did this by always appending a fixed number of space characters to the next line (even if it was not shorter than the previous), but as it turned out that fixed number was not quite large enough, see the fix in 9f1fd84e15 (progress: clear previous progress update dynamically, 2019-04-12). From then on we've been keeping track of the length of the last displayed progress line and appending the appropriate number of space characters to the next line, if necessary, but, alas, this approach turned out to be error prone, see the fix in 1aed1a5f25 (progress: avoid empty line when breaking the progress line, 2019-05-19). The next patch in this series is about to fix a case where we don't clear the last line, and on occasion do end up with such garbage at the end of the line. It would be great if we could do that without the need to deal with that without meticulously computing the necessary number of space characters. So add a helper function to clear the last line on the terminal using an ANSI escape sequence, which has the advantage to clear the whole line no matter how wide it is, even after the terminal width changed. Such an escape sequence is not available on dumb terminals, though, so in that case fall back to simply print a whole terminal width (as reported by term_columns()) worth of space characters. In 'editor.c' launch_specified_editor() already used this ANSI escape sequence, so replace it with a call to this function. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- cache.h | 1 + editor.c | 6 +++--- pager.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cache.h b/cache.h index b4bb2e2c11adff..5b2cd32bad89b6 100644 --- a/cache.h +++ b/cache.h @@ -1759,6 +1759,7 @@ void setup_pager(void); int pager_in_use(void); extern int pager_use_color; int term_columns(void); +void term_clear_line(void); int decimal_width(uintmax_t); int check_pager_config(const char *cmd); void prepare_pager_args(struct child_process *, const char *pager); diff --git a/editor.c b/editor.c index 71547674ab4e88..f079abbf110268 100644 --- a/editor.c +++ b/editor.c @@ -96,10 +96,10 @@ static int launch_specified_editor(const char *editor, const char *path, if (print_waiting_for_editor && !is_terminal_dumb()) /* - * Go back to the beginning and erase the entire line to - * avoid wasting the vertical space. + * Erase the entire line to avoid wasting the + * vertical space. */ - fputs("\r\033[K", stderr); + term_clear_line(); } if (!buffer) diff --git a/pager.c b/pager.c index 4168460ae92ceb..41446d4f0543df 100644 --- a/pager.c +++ b/pager.c @@ -177,6 +177,26 @@ int term_columns(void) return term_columns_at_startup; } +/* + * Clear the entire line, leave cursor in first column. + */ +void term_clear_line(void) +{ + if (is_terminal_dumb()) + /* + * Fall back to print a terminal width worth of space + * characters (hoping that the terminal is still as wide + * as it was upon the first call to term_columns()). + */ + fprintf(stderr, "\r%*s\r", term_columns(), ""); + else + /* + * On non-dumb terminals use an escape sequence to clear + * the whole line, no matter how wide the terminal. + */ + fputs("\r\033[K", stderr); +} + /* * How many columns do we need to show this number in decimal? */ From d7d90885e0aeb50eb4acc221ee43301b2a43aa3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Thu, 27 Jun 2019 15:42:48 +0200 Subject: [PATCH 4/5] rebase: fix garbled progress display with '-x' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running a command with the 'exec' instruction during an interactive rebase session, or for a range of commits using 'git rebase -x', the output can be a bit garbled when the name of the command is short enough: $ git rebase -x true HEAD~5 Executing: true Executing: true Executing: true Executing: true Executing: true) Successfully rebased and updated refs/heads/master. Note the ')' at the end of the last line. It gets more garbled as the range of commits increases: $ git rebase -x true HEAD~50 Executing: true) [ repeated 3 more times ] Executing: true0) [ repeated 44 more times ] Executing: true00) Successfully rebased and updated refs/heads/master. Those extra numbers and ')' are remnants of the previously displayed "Rebasing (N/M)" progress lines that are usually completely overwritten by the "Executing: " lines, unless 'cmd' is short and the "N/M" part is long. Make sure that the previously displayed "Rebasing (N/M)" line is cleared by using the term_clear_line() helper function added in the previous patch. Do so only when not being '--verbose', because in that case these "Rebasing (N/M)" lines are not printed as progress (i.e. as lines with '\r' at the end), but as "regular" output (with '\n' at the end). A couple of other rebase commands print similar messages, e.g. "Stopped at ... " for the 'edit' or 'break' commands, or the "Successfully rebased and updated ." at the very end. These are so long that they practically always overwrite that "Rebasing (N/M)" progress line, but let's be prudent, and clear the last line before printing these, too. In 't3420-rebase-autostash.sh' two helper functions prepare the expected output of four tests that check the full output of 'git rebase' and thus are affected by this change, so adjust their expectations to account for the new line clearing. Note that this patch doesn't completely eliminate the possibility of similar garbled outputs, e.g. some error messages from rebase or the "Auto-merging " message from within the depths of the merge machinery might not be long enough to completely cover the last "Rebasing (N/M)" line. This patch doesn't do anything about them, because dealing with them individually would result in way too much churn, while having a catch-all term_clear_line() call in the common code path of pick_commits() would hide the "Rebasing (N/M)" line way too soon, and it would either flicker or be invisible. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- sequencer.c | 17 ++++++++++++++--- t/t3420-rebase-autostash.sh | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/sequencer.c b/sequencer.c index f88a97fb10a322..63b09cfceb5d3d 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3731,8 +3731,11 @@ static int pick_commits(struct repository *r, unlink(git_path_merge_head(the_repository)); delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); - if (item->command == TODO_BREAK) + if (item->command == TODO_BREAK) { + if (!opts->verbose) + term_clear_line(); return stopped_at_head(r); + } } if (item->command <= TODO_SQUASH) { if (is_rebase_i(opts)) @@ -3754,11 +3757,14 @@ static int pick_commits(struct repository *r, } if (item->command == TODO_EDIT) { struct commit *commit = item->commit; - if (!res) + if (!res) { + if (!opts->verbose) + term_clear_line(); fprintf(stderr, _("Stopped at %s... %.*s\n"), short_commit_name(commit), item->arg_len, arg); + } return error_with_patch(r, commit, arg, item->arg_len, opts, res, !res); } @@ -3796,6 +3802,8 @@ static int pick_commits(struct repository *r, int saved = *end_of_arg; struct stat st; + if (!opts->verbose) + term_clear_line(); *end_of_arg = '\0'; res = do_exec(r, arg); *end_of_arg = saved; @@ -3954,10 +3962,13 @@ static int pick_commits(struct repository *r, } apply_autostash(opts); - if (!opts->quiet) + if (!opts->quiet) { + if (!opts->verbose) + term_clear_line(); fprintf(stderr, "Successfully rebased and updated %s.\n", head_ref.buf); + } strbuf_release(&buf); strbuf_release(&head_ref); diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index 2d1094e4831a88..9186e90127712f 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -49,7 +49,7 @@ create_expected_success_interactive () { $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) HEAD is now at $(git rev-parse --short feature-branch) third commit Rebasing (1/2)QRebasing (2/2)QApplied autostash. - Successfully rebased and updated refs/heads/rebased-feature-branch. + Q QSuccessfully rebased and updated refs/heads/rebased-feature-branch. EOF } @@ -73,7 +73,7 @@ create_expected_failure_interactive () { Rebasing (1/2)QRebasing (2/2)QApplying autostash resulted in conflicts. Your changes are safe in the stash. You can run "git stash pop" or "git stash drop" at any time. - Successfully rebased and updated refs/heads/rebased-feature-branch. + Q QSuccessfully rebased and updated refs/heads/rebased-feature-branch. EOF } From 5b12e3123b7b70e3875404a4ffe571ca079364fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 24 Jun 2019 20:13:18 +0200 Subject: [PATCH 5/5] progress: use term_clear_line() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To make sure that the previously displayed progress line is completely covered up when the new line is shorter, commit 545dc345eb (progress: break too long progress bar lines, 2019-04-12) added a bunch of calculations to figure out how many characters it needs to overwrite with spaces. Use the just introduced term_clear_line() helper function to, well, clear the last line, making all these calculations unnecessary, and thus simplifying the code considerably. Three tests in 't5541-http-push-smart.sh' 'grep' for specific text shown in the progress lines at the beginning of the line, but now those lines begin either with the ANSI escape sequence or with the terminal width worth of space characters clearing the line. Relax the 'grep' patterns to match anywhere on the line. Note that only two of these three tests fail without relaxing their 'grep' pattern, but the third looks for the absence of the pattern, so it still succeeds, but without the adjustment would potentially hide future regressions. Note also that with this change we no longer need the length of the previously displayed progress line, so the strbuf added to 'struct progress' in d53ba841d4 (progress: assemble percentage and counters in a strbuf before printing, 2019-04-05) is not strictly necessary anymore. We still keep it, though, as it avoids allocating and releasing a strbuf each time the progress is updated. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- progress.c | 28 +++++++++++----------------- t/t5541-http-push-smart.sh | 6 +++--- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/progress.c b/progress.c index a2e8cf64a8d1aa..095dcd0ddff213 100644 --- a/progress.c +++ b/progress.c @@ -88,7 +88,6 @@ static void display(struct progress *progress, uint64_t n, const char *done) const char *tp; struct strbuf *counters_sb = &progress->counters_sb; int show_update = 0; - int last_count_len = counters_sb->len; if (progress->delay && (!progress_update || --progress->delay)) return; @@ -116,26 +115,21 @@ static void display(struct progress *progress, uint64_t n, const char *done) if (show_update) { if (is_foreground_fd(fileno(stderr)) || done) { const char *eol = done ? done : "\r"; - size_t clear_len = counters_sb->len < last_count_len ? - last_count_len - counters_sb->len + 1 : - 0; - size_t progress_line_len = progress->title_len + - counters_sb->len + 2; - int cols = term_columns(); + term_clear_line(); if (progress->split) { - fprintf(stderr, " %s%*s", counters_sb->buf, - (int) clear_len, eol); - } else if (!done && cols < progress_line_len) { - clear_len = progress->title_len + 1 < cols ? - cols - progress->title_len - 1 : 0; - fprintf(stderr, "%s:%*s\n %s%s", - progress->title, (int) clear_len, "", - counters_sb->buf, eol); + fprintf(stderr, " %s%s", counters_sb->buf, + eol); + } else if (!done && + /* The "+ 2" accounts for the ": ". */ + term_columns() < progress->title_len + + counters_sb->len + 2) { + fprintf(stderr, "%s:\n %s%s", + progress->title, counters_sb->buf, eol); progress->split = 1; } else { - fprintf(stderr, "%s: %s%*s", progress->title, - counters_sb->buf, (int) clear_len, eol); + fprintf(stderr, "%s: %s%s", progress->title, + counters_sb->buf, eol); } fflush(stderr); } diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh index 8ef8763e063cf3..2e4802e2063b87 100755 --- a/t/t5541-http-push-smart.sh +++ b/t/t5541-http-push-smart.sh @@ -213,7 +213,7 @@ test_expect_success TTY 'push shows progress when stderr is a tty' ' cd "$ROOT_PATH"/test_repo_clone && test_commit noisy && test_terminal git push >output 2>&1 && - test_i18ngrep "^Writing objects" output + test_i18ngrep "Writing objects" output ' test_expect_success TTY 'push --quiet silences status and progress' ' @@ -228,7 +228,7 @@ test_expect_success TTY 'push --no-progress silences progress but not status' ' test_commit no-progress && test_terminal git push --no-progress >output 2>&1 && test_i18ngrep "^To http" output && - test_i18ngrep ! "^Writing objects" output + test_i18ngrep ! "Writing objects" output ' test_expect_success 'push --progress shows progress to non-tty' ' @@ -236,7 +236,7 @@ test_expect_success 'push --progress shows progress to non-tty' ' test_commit progress && git push --progress >output 2>&1 && test_i18ngrep "^To http" output && - test_i18ngrep "^Writing objects" output + test_i18ngrep "Writing objects" output ' test_expect_success 'http push gives sane defaults to reflog' '