Skip to content

Commit 6624e07

Browse files
committed
Merge branch 'sg/rebase-progress'
Use "Erase in Line" CSI sequence that is already used in the editor support to clear cruft in the progress output. * sg/rebase-progress: progress: use term_clear_line() rebase: fix garbled progress display with '-x' pager: add a helper function to clear the last line in the terminal t3404: make the 'rebase.missingCommitsCheck=ignore' test more focused t3404: modernize here doc style
2 parents 88176b7 + 5b12e31 commit 6624e07

File tree

8 files changed

+104
-98
lines changed

8 files changed

+104
-98
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ void setup_pager(void);
17351735
int pager_in_use(void);
17361736
extern int pager_use_color;
17371737
int term_columns(void);
1738+
void term_clear_line(void);
17381739
int decimal_width(uintmax_t);
17391740
int check_pager_config(const char *cmd);
17401741
void prepare_pager_args(struct child_process *, const char *pager);

editor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ static int launch_specified_editor(const char *editor, const char *path,
9696

9797
if (print_waiting_for_editor && !is_terminal_dumb())
9898
/*
99-
* Go back to the beginning and erase the entire line to
100-
* avoid wasting the vertical space.
99+
* Erase the entire line to avoid wasting the
100+
* vertical space.
101101
*/
102-
fputs("\r\033[K", stderr);
102+
term_clear_line();
103103
}
104104

105105
if (!buffer)

pager.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ int term_columns(void)
177177
return term_columns_at_startup;
178178
}
179179

180+
/*
181+
* Clear the entire line, leave cursor in first column.
182+
*/
183+
void term_clear_line(void)
184+
{
185+
if (is_terminal_dumb())
186+
/*
187+
* Fall back to print a terminal width worth of space
188+
* characters (hoping that the terminal is still as wide
189+
* as it was upon the first call to term_columns()).
190+
*/
191+
fprintf(stderr, "\r%*s\r", term_columns(), "");
192+
else
193+
/*
194+
* On non-dumb terminals use an escape sequence to clear
195+
* the whole line, no matter how wide the terminal.
196+
*/
197+
fputs("\r\033[K", stderr);
198+
}
199+
180200
/*
181201
* How many columns do we need to show this number in decimal?
182202
*/

progress.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ static void display(struct progress *progress, uint64_t n, const char *done)
8888
const char *tp;
8989
struct strbuf *counters_sb = &progress->counters_sb;
9090
int show_update = 0;
91-
int last_count_len = counters_sb->len;
9291

9392
if (progress->delay && (!progress_update || --progress->delay))
9493
return;
@@ -116,26 +115,21 @@ static void display(struct progress *progress, uint64_t n, const char *done)
116115
if (show_update) {
117116
if (is_foreground_fd(fileno(stderr)) || done) {
118117
const char *eol = done ? done : "\r";
119-
size_t clear_len = counters_sb->len < last_count_len ?
120-
last_count_len - counters_sb->len + 1 :
121-
0;
122-
size_t progress_line_len = progress->title_len +
123-
counters_sb->len + 2;
124-
int cols = term_columns();
125118

119+
term_clear_line();
126120
if (progress->split) {
127-
fprintf(stderr, " %s%*s", counters_sb->buf,
128-
(int) clear_len, eol);
129-
} else if (!done && cols < progress_line_len) {
130-
clear_len = progress->title_len + 1 < cols ?
131-
cols - progress->title_len - 1 : 0;
132-
fprintf(stderr, "%s:%*s\n %s%s",
133-
progress->title, (int) clear_len, "",
134-
counters_sb->buf, eol);
121+
fprintf(stderr, " %s%s", counters_sb->buf,
122+
eol);
123+
} else if (!done &&
124+
/* The "+ 2" accounts for the ": ". */
125+
term_columns() < progress->title_len +
126+
counters_sb->len + 2) {
127+
fprintf(stderr, "%s:\n %s%s",
128+
progress->title, counters_sb->buf, eol);
135129
progress->split = 1;
136130
} else {
137-
fprintf(stderr, "%s: %s%*s", progress->title,
138-
counters_sb->buf, (int) clear_len, eol);
131+
fprintf(stderr, "%s: %s%s", progress->title,
132+
counters_sb->buf, eol);
139133
}
140134
fflush(stderr);
141135
}

sequencer.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3741,8 +3741,11 @@ static int pick_commits(struct repository *r,
37413741
unlink(git_path_merge_head(the_repository));
37423742
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
37433743

3744-
if (item->command == TODO_BREAK)
3744+
if (item->command == TODO_BREAK) {
3745+
if (!opts->verbose)
3746+
term_clear_line();
37453747
return stopped_at_head(r);
3748+
}
37463749
}
37473750
if (item->command <= TODO_SQUASH) {
37483751
if (is_rebase_i(opts))
@@ -3764,11 +3767,14 @@ static int pick_commits(struct repository *r,
37643767
}
37653768
if (item->command == TODO_EDIT) {
37663769
struct commit *commit = item->commit;
3767-
if (!res)
3770+
if (!res) {
3771+
if (!opts->verbose)
3772+
term_clear_line();
37683773
fprintf(stderr,
37693774
_("Stopped at %s... %.*s\n"),
37703775
short_commit_name(commit),
37713776
item->arg_len, arg);
3777+
}
37723778
return error_with_patch(r, commit,
37733779
arg, item->arg_len, opts, res, !res);
37743780
}
@@ -3806,6 +3812,8 @@ static int pick_commits(struct repository *r,
38063812
int saved = *end_of_arg;
38073813
struct stat st;
38083814

3815+
if (!opts->verbose)
3816+
term_clear_line();
38093817
*end_of_arg = '\0';
38103818
res = do_exec(r, arg);
38113819
*end_of_arg = saved;
@@ -3964,10 +3972,13 @@ static int pick_commits(struct repository *r,
39643972
}
39653973
apply_autostash(opts);
39663974

3967-
if (!opts->quiet)
3975+
if (!opts->quiet) {
3976+
if (!opts->verbose)
3977+
term_clear_line();
39683978
fprintf(stderr,
39693979
"Successfully rebased and updated %s.\n",
39703980
head_ref.buf);
3981+
}
39713982

39723983
strbuf_release(&buf);
39733984
strbuf_release(&head_ref);

t/t3404-rebase-interactive.sh

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ test_expect_success 'rebase --keep-empty' '
7575
test_line_count = 6 actual
7676
'
7777

78-
cat > expect <<EOF
79-
error: nothing to do
80-
EOF
81-
8278
test_expect_success 'rebase -i with empty HEAD' '
79+
cat >expect <<-\EOF &&
80+
error: nothing to do
81+
EOF
8382
set_fake_editor &&
8483
test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 &&
8584
test_i18ncmp expect actual
@@ -237,25 +236,23 @@ test_expect_success 'exchange two commits' '
237236
test G = $(git cat-file commit HEAD | sed -ne \$p)
238237
'
239238

240-
cat > expect << EOF
241-
diff --git a/file1 b/file1
242-
index f70f10e..fd79235 100644
243-
--- a/file1
244-
+++ b/file1
245-
@@ -1 +1 @@
246-
-A
247-
+G
248-
EOF
249-
250-
cat > expect2 << EOF
251-
<<<<<<< HEAD
252-
D
253-
=======
254-
G
255-
>>>>>>> 5d18e54... G
256-
EOF
257-
258239
test_expect_success 'stop on conflicting pick' '
240+
cat >expect <<-\EOF &&
241+
diff --git a/file1 b/file1
242+
index f70f10e..fd79235 100644
243+
--- a/file1
244+
+++ b/file1
245+
@@ -1 +1 @@
246+
-A
247+
+G
248+
EOF
249+
cat >expect2 <<-\EOF &&
250+
<<<<<<< HEAD
251+
D
252+
=======
253+
G
254+
>>>>>>> 5d18e54... G
255+
EOF
259256
git tag new-branch1 &&
260257
set_fake_editor &&
261258
test_must_fail git rebase -i master &&
@@ -495,15 +492,14 @@ test_expect_success 'commit message retained after conflict' '
495492
git branch -D conflict-squash
496493
'
497494

498-
cat > expect-squash-fixup << EOF
499-
B
500-
501-
D
495+
test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
496+
cat >expect-squash-fixup <<-\EOF &&
497+
B
502498
503-
ONCE
504-
EOF
499+
D
505500
506-
test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
501+
ONCE
502+
EOF
507503
git checkout -b squash-fixup E &&
508504
base=$(git rev-parse HEAD~4) &&
509505
set_fake_editor &&
@@ -799,13 +795,12 @@ test_expect_success 'rebase -i can copy notes' '
799795
test "a note" = "$(git notes show HEAD)"
800796
'
801797

802-
cat >expect <<EOF
803-
an earlier note
804-
805-
a note
806-
EOF
807-
808798
test_expect_success 'rebase -i can copy notes over a fixup' '
799+
cat >expect <<-\EOF &&
800+
an earlier note
801+
802+
a note
803+
EOF
809804
git reset --hard n3 &&
810805
git notes add -m"an earlier note" n2 &&
811806
set_fake_editor &&
@@ -1304,52 +1299,37 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
13041299
actual
13051300
'
13061301

1307-
cat >expect <<EOF
1308-
Warning: some commits may have been dropped accidentally.
1309-
Dropped commits (newer to older):
1310-
- $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1311-
To avoid this message, use "drop" to explicitly remove a commit.
1312-
1313-
Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1314-
The possible behaviours are: ignore, warn, error.
1315-
1316-
Rebasing (1/4)
1317-
Rebasing (2/4)
1318-
Rebasing (3/4)
1319-
Rebasing (4/4)
1320-
Successfully rebased and updated refs/heads/missing-commit.
1321-
EOF
1322-
1323-
cr_to_nl () {
1324-
tr '\015' '\012'
1325-
}
1326-
13271302
test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1303+
cat >expect <<-EOF &&
1304+
Warning: some commits may have been dropped accidentally.
1305+
Dropped commits (newer to older):
1306+
- $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1307+
To avoid this message, use "drop" to explicitly remove a commit.
1308+
EOF
13281309
test_config rebase.missingCommitsCheck warn &&
13291310
rebase_setup_and_clean missing-commit &&
13301311
set_fake_editor &&
13311312
FAKE_LINES="1 2 3 4" \
13321313
git rebase -i --root 2>actual.2 &&
1333-
cr_to_nl <actual.2 >actual &&
1314+
head -n4 actual.2 >actual &&
13341315
test_i18ncmp expect actual &&
13351316
test D = $(git cat-file commit HEAD | sed -ne \$p)
13361317
'
13371318

1338-
cat >expect <<EOF
1339-
Warning: some commits may have been dropped accidentally.
1340-
Dropped commits (newer to older):
1341-
- $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1342-
- $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1343-
To avoid this message, use "drop" to explicitly remove a commit.
1344-
1345-
Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1346-
The possible behaviours are: ignore, warn, error.
1347-
1348-
You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1349-
Or you can abort the rebase with 'git rebase --abort'.
1350-
EOF
1351-
13521319
test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1320+
cat >expect <<-EOF &&
1321+
Warning: some commits may have been dropped accidentally.
1322+
Dropped commits (newer to older):
1323+
- $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1324+
- $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1325+
To avoid this message, use "drop" to explicitly remove a commit.
1326+
1327+
Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1328+
The possible behaviours are: ignore, warn, error.
1329+
1330+
You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1331+
Or you can abort the rebase with '\''git rebase --abort'\''.
1332+
EOF
13531333
test_config rebase.missingCommitsCheck error &&
13541334
rebase_setup_and_clean missing-commit &&
13551335
set_fake_editor &&

t/t3420-rebase-autostash.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ create_expected_success_interactive () {
4949
$(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
5050
HEAD is now at $(git rev-parse --short feature-branch) third commit
5151
Rebasing (1/2)QRebasing (2/2)QApplied autostash.
52-
Successfully rebased and updated refs/heads/rebased-feature-branch.
52+
Q QSuccessfully rebased and updated refs/heads/rebased-feature-branch.
5353
EOF
5454
}
5555

@@ -73,7 +73,7 @@ create_expected_failure_interactive () {
7373
Rebasing (1/2)QRebasing (2/2)QApplying autostash resulted in conflicts.
7474
Your changes are safe in the stash.
7575
You can run "git stash pop" or "git stash drop" at any time.
76-
Successfully rebased and updated refs/heads/rebased-feature-branch.
76+
Q QSuccessfully rebased and updated refs/heads/rebased-feature-branch.
7777
EOF
7878
}
7979

t/t5541-http-push-smart.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ test_expect_success TTY 'push shows progress when stderr is a tty' '
213213
cd "$ROOT_PATH"/test_repo_clone &&
214214
test_commit noisy &&
215215
test_terminal git push >output 2>&1 &&
216-
test_i18ngrep "^Writing objects" output
216+
test_i18ngrep "Writing objects" output
217217
'
218218

219219
test_expect_success TTY 'push --quiet silences status and progress' '
@@ -228,15 +228,15 @@ test_expect_success TTY 'push --no-progress silences progress but not status' '
228228
test_commit no-progress &&
229229
test_terminal git push --no-progress >output 2>&1 &&
230230
test_i18ngrep "^To http" output &&
231-
test_i18ngrep ! "^Writing objects" output
231+
test_i18ngrep ! "Writing objects" output
232232
'
233233

234234
test_expect_success 'push --progress shows progress to non-tty' '
235235
cd "$ROOT_PATH"/test_repo_clone &&
236236
test_commit progress &&
237237
git push --progress >output 2>&1 &&
238238
test_i18ngrep "^To http" output &&
239-
test_i18ngrep "^Writing objects" output
239+
test_i18ngrep "Writing objects" output
240240
'
241241

242242
test_expect_success 'http push gives sane defaults to reflog' '

0 commit comments

Comments
 (0)