Skip to content

Commit b9cbd29

Browse files
newrengitster
authored andcommitted
rebase: reinstate --no-keep-empty
Commit d48e5e2 ("rebase (interactive-backend): make --keep-empty the default", 2020-02-15) turned --keep-empty (for keeping commits which start empty) into the default. The logic underpinning that commit was: 1) 'git commit' errors out on the creation of empty commits without an override flag 2) Once someone determines that the override is worthwhile, it's annoying and/or harmful to required them to take extra steps in order to keep such commits around (and to repeat such steps with every rebase). While the logic on which the decision was made is sound, the result was a bit of an overcorrection. Instead of jumping to having --keep-empty being the default, it jumped to making --keep-empty the only available behavior. There was a simple workaround, though, which was thought to be good enough at the time. People could still drop commits which started empty the same way the could drop any commits: by firing up an interactive rebase and picking out the commits they didn't want from the list. However, there are cases where external tools might create enough empty commits that picking all of them out is painful. As such, having a flag to automatically remove start-empty commits may be beneficial. Provide users a way to drop commits which start empty using a flag that existed for years: --no-keep-empty. Interpret --keep-empty as countermanding any previous --no-keep-empty, but otherwise leaving --keep-empty as the default. This might lead to some slight weirdness since commands like git rebase --empty=drop --keep-empty git rebase --empty=keep --no-keep-empty look really weird despite making perfect sense (the first will drop commits which become empty, but keep commits that started empty; the second will keep commits which become empty, but drop commits which started empty). However, --no-keep-empty was named years ago and we are predominantly keeping it for backward compatibility; also we suspect it will only be used rarely since folks already have a simple way to drop commits they don't want with an interactive rebase. Reported-by: Bryan Turner <[email protected]> Reported-by: Sami Boukortt <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b5735f commit b9cbd29

File tree

6 files changed

+87
-30
lines changed

6 files changed

+87
-30
lines changed

Documentation/git-rebase.txt

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,32 @@ See also INCOMPATIBLE OPTIONS below.
277277
Other options, like --exec, will use the default of drop unless
278278
-i/--interactive is explicitly specified.
279279
+
280-
Note that commits which start empty are kept, and commits which are
281-
clean cherry-picks (as determined by `git log --cherry-mark ...`) are
282-
always dropped.
280+
Note that commits which start empty are kept (unless --no-keep-empty
281+
is specified), and commits which are clean cherry-picks (as determined
282+
by `git log --cherry-mark ...`) are always dropped.
283283
+
284284
See also INCOMPATIBLE OPTIONS below.
285285

286+
--no-keep-empty::
286287
--keep-empty::
287-
No-op. Rebasing commits that started empty (had no change
288-
relative to their parent) used to fail and this option would
289-
override that behavior, allowing commits with empty changes to
290-
be rebased. Now commits with no changes do not cause rebasing
291-
to halt.
288+
Do not keep commits that start empty before the rebase
289+
(i.e. that do not change anything from its parent) in the
290+
result. The default is to keep commits which start empty,
291+
since creating such commits requires passing the --allow-empty
292+
override flag to `git commit`, signifying that a user is very
293+
intentionally creating such a commit and thus wants to keep
294+
it.
292295
+
293-
See also BEHAVIORAL DIFFERENCES and INCOMPATIBLE OPTIONS below.
296+
Usage of this flag will probably be rare, since you can get rid of
297+
commits that start empty by just firing up an interactive rebase and
298+
removing the lines corresponding to the commits you don't want. This
299+
flag exists as a convenient shortcut, such as for cases where external
300+
tools generate many empty commits and you want them all removed.
301+
+
302+
For commits which do not start empty but become empty after rebasing,
303+
see the --empty flag.
304+
+
305+
See also INCOMPATIBLE OPTIONS below.
294306

295307
--allow-empty-message::
296308
No-op. Rebasing commits with an empty message used to fail
@@ -587,7 +599,7 @@ are incompatible with the following options:
587599
* --preserve-merges
588600
* --interactive
589601
* --exec
590-
* --keep-empty
602+
* --no-keep-empty
591603
* --empty=
592604
* --edit-todo
593605
* --root when used in combination with --onto
@@ -620,13 +632,15 @@ commits that started empty, though these are rare in practice. It
620632
also drops commits that become empty and has no option for controlling
621633
this behavior.
622634

623-
The merge backend keeps intentionally empty commits (though with -i
624-
they are marked as empty in the todo list editor). Similar to the
625-
apply backend, by default the merge backend drops commits that become
626-
empty unless -i/--interactive is specified (in which case it stops and
627-
asks the user what to do). The merge backend also has an
628-
--empty={drop,keep,ask} option for changing the behavior of handling
629-
commits that become empty.
635+
The merge backend keeps intentionally empty commits by default (though
636+
with -i they are marked as empty in the todo list editor, or they can
637+
be dropped automatically with --no-keep-empty).
638+
639+
Similar to the apply backend, by default the merge backend drops
640+
commits that become empty unless -i/--interactive is specified (in
641+
which case it stops and asks the user what to do). The merge backend
642+
also has an --empty={drop,keep,ask} option for changing the behavior
643+
of handling commits that become empty.
630644

631645
Directory rename detection
632646
~~~~~~~~~~~~~~~~~~~~~~~~~~

builtin/rebase.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct rebase_options {
8585
const char *action;
8686
int signoff;
8787
int allow_rerere_autoupdate;
88+
int keep_empty;
8889
int autosquash;
8990
char *gpg_sign_opt;
9091
int autostash;
@@ -100,6 +101,7 @@ struct rebase_options {
100101
#define REBASE_OPTIONS_INIT { \
101102
.type = REBASE_UNSPECIFIED, \
102103
.empty = EMPTY_UNSPECIFIED, \
104+
.keep_empty = 1, \
103105
.default_backend = "merge", \
104106
.flags = REBASE_NO_QUIET, \
105107
.git_am_opts = ARGV_ARRAY_INIT, \
@@ -379,6 +381,7 @@ static int run_sequencer_rebase(struct rebase_options *opts,
379381

380382
git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
381383

384+
flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
382385
flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
383386
flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
384387
flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
@@ -442,17 +445,16 @@ static int run_sequencer_rebase(struct rebase_options *opts,
442445
return ret;
443446
}
444447

448+
static void imply_merge(struct rebase_options *opts, const char *option);
445449
static int parse_opt_keep_empty(const struct option *opt, const char *arg,
446450
int unset)
447451
{
448452
struct rebase_options *opts = opt->value;
449453

450454
BUG_ON_OPT_ARG(arg);
451455

452-
/*
453-
* If we ever want to remap --keep-empty to --empty=keep, insert:
454-
* opts->empty = unset ? EMPTY_UNSPECIFIED : EMPTY_KEEP;
455-
*/
456+
imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty");
457+
opts->keep_empty = !unset;
456458
opts->type = REBASE_MERGE;
457459
return 0;
458460
}
@@ -471,7 +473,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
471473
OPT_NEGBIT(0, "ff", &opts.flags, N_("allow fast-forward"),
472474
REBASE_FORCE),
473475
{ OPTION_CALLBACK, 'k', "keep-empty", &options, NULL,
474-
N_("(DEPRECATED) keep empty commits"),
476+
N_("keep commits which start empty"),
475477
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
476478
parse_opt_keep_empty },
477479
OPT_BOOL_F(0, "allow-empty-message", &opts.allow_empty_message,
@@ -1162,6 +1164,7 @@ static int run_specific_rebase(struct rebase_options *opts, enum action action)
11621164
opts->allow_rerere_autoupdate ?
11631165
opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
11641166
"--rerere-autoupdate" : "--no-rerere-autoupdate" : "");
1167+
add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
11651168
add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
11661169
add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
11671170
add_var(&script_snippet, "cmd", opts->cmd);
@@ -1547,7 +1550,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15471550
N_("how to handle commits that become empty"),
15481551
PARSE_OPT_NONEG, parse_opt_empty),
15491552
{ OPTION_CALLBACK, 'k', "keep-empty", &options, NULL,
1550-
N_("(DEPRECATED) keep empty commits"),
1553+
N_("keep commits which start empty"),
15511554
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
15521555
parse_opt_keep_empty },
15531556
OPT_BOOL(0, "autosquash", &options.autosquash,

sequencer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4591,6 +4591,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
45914591
struct rev_info *revs, struct strbuf *out,
45924592
unsigned flags)
45934593
{
4594+
int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
45944595
int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS;
45954596
int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO;
45964597
struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT;
@@ -4645,6 +4646,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,
46454646
is_empty = is_original_commit_empty(commit);
46464647
if (!is_empty && (commit->object.flags & PATCHSAME))
46474648
continue;
4649+
if (is_empty && !keep_empty)
4650+
continue;
46484651

46494652
strbuf_reset(&oneline);
46504653
pretty_print_commit(pp, commit, &oneline);
@@ -4822,6 +4825,7 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
48224825
struct pretty_print_context pp = {0};
48234826
struct rev_info revs;
48244827
struct commit *commit;
4828+
int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
48254829
const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick";
48264830
int rebase_merges = flags & TODO_LIST_REBASE_MERGES;
48274831

@@ -4861,6 +4865,8 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
48614865

48624866
if (!is_empty && (commit->object.flags & PATCHSAME))
48634867
continue;
4868+
if (is_empty && !keep_empty)
4869+
continue;
48644870
strbuf_addf(out, "%s %s ", insn,
48654871
oid_to_hex(&commit->object.oid));
48664872
pretty_print_commit(&pp, commit, out);

sequencer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int sequencer_rollback(struct repository *repo, struct replay_opts *opts);
134134
int sequencer_skip(struct repository *repo, struct replay_opts *opts);
135135
int sequencer_remove_state(struct replay_opts *opts);
136136

137-
/* #define TODO_LIST_KEEP_EMPTY (1U << 0) */ /* No longer used */
137+
#define TODO_LIST_KEEP_EMPTY (1U << 0)
138138
#define TODO_LIST_SHORTEN_IDS (1U << 1)
139139
#define TODO_LIST_ABBREVIATE_CMDS (1U << 2)
140140
#define TODO_LIST_REBASE_MERGES (1U << 3)

t/t3421-rebase-topology-linear.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ test_have_prereq !REBASE_P || test_run_rebase failure -p
220220
test_run_rebase () {
221221
result=$1
222222
shift
223-
test_expect_$result "rebase $* --keep-empty" "
223+
test_expect_$result "rebase $* --no-keep-empty drops begin-empty commits" "
224224
reset_rebase &&
225-
git rebase $* --keep-empty c l &&
226-
test_cmp_rev c HEAD~3 &&
227-
test_linear_range 'd k l' c..
225+
git rebase $* --no-keep-empty c l &&
226+
test_cmp_rev c HEAD~2 &&
227+
test_linear_range 'd l' c..
228228
"
229229
}
230-
test_run_rebase success --apply
231230
test_run_rebase success -m
232231
test_run_rebase success -i
233232
test_have_prereq !REBASE_P || test_run_rebase success -p
@@ -242,7 +241,6 @@ test_run_rebase () {
242241
test_linear_range 'd k l' j..
243242
"
244243
}
245-
test_run_rebase success --apply
246244
test_run_rebase success -m
247245
test_run_rebase success -i
248246
test_have_prereq !REBASE_P || test_run_rebase success -p

t/t3424-rebase-empty.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,42 @@ test_expect_success 'rebase --interactive uses default of --empty=ask' '
123123
test_cmp expect actual
124124
'
125125

126+
test_expect_success 'rebase --merge --empty=drop --keep-empty' '
127+
git checkout -B testing localmods &&
128+
git rebase --merge --empty=drop --keep-empty upstream &&
129+
130+
test_write_lines D C B A >expect &&
131+
git log --format=%s >actual &&
132+
test_cmp expect actual
133+
'
134+
135+
test_expect_success 'rebase --merge --empty=drop --no-keep-empty' '
136+
git checkout -B testing localmods &&
137+
git rebase --merge --empty=drop --no-keep-empty upstream &&
138+
139+
test_write_lines C B A >expect &&
140+
git log --format=%s >actual &&
141+
test_cmp expect actual
142+
'
143+
144+
test_expect_success 'rebase --merge --empty=keep --keep-empty' '
145+
git checkout -B testing localmods &&
146+
git rebase --merge --empty=keep --keep-empty upstream &&
147+
148+
test_write_lines D C2 C B A >expect &&
149+
git log --format=%s >actual &&
150+
test_cmp expect actual
151+
'
152+
153+
test_expect_success 'rebase --merge --empty=keep --no-keep-empty' '
154+
git checkout -B testing localmods &&
155+
git rebase --merge --empty=keep --no-keep-empty upstream &&
156+
157+
test_write_lines C2 C B A >expect &&
158+
git log --format=%s >actual &&
159+
test_cmp expect actual
160+
'
161+
126162
test_expect_success 'rebase --merge does not leave state laying around' '
127163
git checkout -B testing localmods~2 &&
128164
git rebase --merge upstream &&

0 commit comments

Comments
 (0)