Skip to content

Commit 1692579

Browse files
committed
Merge branch 'nh/empty-rebase'
"git rebase" learned to optionally keep commits that do not introduce any change in the original history. By Neil Horman * nh/empty-rebase: git-rebase: add keep_empty flag git-cherry-pick: Add test to validate new options git-cherry-pick: Add keep-redundant-commits option git-cherry-pick: add allow-empty option
2 parents 563b352 + 90e1818 commit 1692579

File tree

9 files changed

+193
-19
lines changed

9 files changed

+193
-19
lines changed

Documentation/git-cherry-pick.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ effect to your index in a row.
103103
cherry-pick'ed commit, then a fast forward to this commit will
104104
be performed.
105105

106+
--allow-empty::
107+
By default, cherry-picking an empty commit will fail,
108+
indicating that an explicit invocation of `git commit
109+
--allow-empty` is required. This option overrides that
110+
behavior, allowing empty commits to be preserved automatically
111+
in a cherry-pick. Note that when "--ff" is in effect, empty
112+
commits that meet the "fast-forward" requirement will be kept
113+
even without this option. Note also, that use of this option only
114+
keeps commits that were initially empty (i.e. the commit recorded the
115+
same tree as its parent). Commits which are made empty due to a
116+
previous commit are dropped. To force the inclusion of those commits
117+
use `--keep-redundant-commits`.
118+
119+
--keep-redundant-commits::
120+
If a commit being cherry picked duplicates a commit already in the
121+
current history, it will become empty. By default these
122+
redundant commits are ignored. This option overrides that behavior and
123+
creates an empty commit object. Implies `--allow-empty`.
124+
106125
--strategy=<strategy>::
107126
Use the given merge strategy. Should only be used once.
108127
See the MERGE STRATEGIES section in linkgit:git-merge[1]

Documentation/git-rebase.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ leave out at most one of A and B, in which case it defaults to HEAD.
238238
will be reset to where it was when the rebase operation was
239239
started.
240240

241+
--keep-empty::
242+
Keep the commits that do not change anything from its
243+
parents in the result.
244+
241245
--skip::
242246
Restart the rebasing process by skipping the current patch.
243247

builtin/revert.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,16 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
115115
OPT_END(),
116116
OPT_END(),
117117
OPT_END(),
118+
OPT_END(),
119+
OPT_END(),
118120
};
119121

120122
if (opts->action == REPLAY_PICK) {
121123
struct option cp_extra[] = {
122124
OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"),
123125
OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"),
126+
OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, "preserve initially empty commits"),
127+
OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, "keep redundant, empty commits"),
124128
OPT_END(),
125129
};
126130
if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
@@ -138,6 +142,10 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
138142
"--abort", rollback,
139143
NULL);
140144

145+
/* implies allow_empty */
146+
if (opts->keep_redundant_commits)
147+
opts->allow_empty = 1;
148+
141149
/* Set the subcommand */
142150
if (remove_state)
143151
opts->subcommand = REPLAY_REMOVE_STATE;

git-rebase--am.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ esac
2020

2121
test -n "$rebase_root" && root_flag=--root
2222

23-
git format-patch -k --stdout --full-index --ignore-if-in-upstream \
24-
--src-prefix=a/ --dst-prefix=b/ \
25-
--no-renames $root_flag "$revisions" |
26-
git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" &&
27-
move_to_original_branch
23+
if test -n "$keep_empty"
24+
then
25+
# we have to do this the hard way. git format-patch completely squashes
26+
# empty commits and even if it didn't the format doesn't really lend
27+
# itself well to recording empty patches. fortunately, cherry-pick
28+
# makes this easy
29+
git cherry-pick --allow-empty "$revisions"
30+
else
31+
git format-patch -k --stdout --full-index --ignore-if-in-upstream \
32+
--src-prefix=a/ --dst-prefix=b/ \
33+
--no-renames $root_flag "$revisions" |
34+
git am $git_am_opt --rebasing --resolvemsg="$resolvemsg"
35+
fi && move_to_original_branch
36+
2837
ret=$?
2938
test 0 != $ret -a -d "$state_dir" && write_basic_state
3039
exit $ret

git-rebase--interactive.sh

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ has_action () {
167167
sane_grep '^[^#]' "$1" >/dev/null
168168
}
169169

170+
is_empty_commit() {
171+
tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
172+
die "$1: not a commit that can be picked")
173+
ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
174+
ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
175+
test "$tree" = "$ptree"
176+
}
177+
170178
# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
171179
# GIT_AUTHOR_DATE exported from the current environment.
172180
do_with_author () {
@@ -191,12 +199,19 @@ git_sequence_editor () {
191199

192200
pick_one () {
193201
ff=--ff
202+
194203
case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
195204
case "$force_rebase" in '') ;; ?*) ff= ;; esac
196205
output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
206+
207+
if is_empty_commit "$sha1"
208+
then
209+
empty_args="--allow-empty"
210+
fi
211+
197212
test -d "$rewritten" &&
198213
pick_one_preserving_merges "$@" && return
199-
output git cherry-pick $ff "$@"
214+
output git cherry-pick $empty_args $ff "$@"
200215
}
201216

202217
pick_one_preserving_merges () {
@@ -780,9 +795,17 @@ git rev-list $merges_option --pretty=oneline --abbrev-commit \
780795
sed -n "s/^>//p" |
781796
while read -r shortsha1 rest
782797
do
798+
799+
if test -z "$keep_empty" && is_empty_commit $shortsha1
800+
then
801+
comment_out="# "
802+
else
803+
comment_out=
804+
fi
805+
783806
if test t != "$preserve_merges"
784807
then
785-
printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
808+
printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
786809
else
787810
sha1=$(git rev-parse $shortsha1)
788811
if test -z "$rebase_root"
@@ -801,7 +824,7 @@ do
801824
if test f = "$preserve"
802825
then
803826
touch "$rewritten"/$sha1
804-
printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
827+
printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
805828
fi
806829
fi
807830
done
@@ -853,6 +876,12 @@ cat >> "$todo" << EOF
853876
#
854877
EOF
855878

879+
if test -z "$keep_empty"
880+
then
881+
echo "# Note that empty commits are commented out" >>"$todo"
882+
fi
883+
884+
856885
has_action "$todo" ||
857886
die_abort "Nothing to do"
858887

git-rebase.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ s,strategy=! use the given merge strategy
4343
no-ff! cherry-pick all commits, even if unchanged
4444
m,merge! use merging strategies to rebase
4545
i,interactive! let the user edit the list of commits to rebase
46+
k,keep-empty preserve empty commits during rebase
4647
f,force-rebase! force rebase even if branch is up to date
4748
X,strategy-option=! pass the argument through to the merge strategy
4849
stat! display a diffstat of what changed upstream
@@ -97,6 +98,7 @@ state_dir=
9798
action=
9899
preserve_merges=
99100
autosquash=
101+
keep_empty=
100102
test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
101103

102104
read_basic_state () {
@@ -220,6 +222,9 @@ do
220222
-i)
221223
interactive_rebase=explicit
222224
;;
225+
-k)
226+
keep_empty=yes
227+
;;
223228
-p)
224229
preserve_merges=t
225230
test -z "$interactive_rebase" && interactive_rebase=implied

sequencer.c

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "rerere.h"
1414
#include "merge-recursive.h"
1515
#include "refs.h"
16+
#include "argv-array.h"
1617

1718
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
1819

@@ -251,6 +252,30 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
251252
return !clean;
252253
}
253254

255+
static int is_index_unchanged(void)
256+
{
257+
unsigned char head_sha1[20];
258+
struct commit *head_commit;
259+
260+
if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))
261+
return error(_("Could not resolve HEAD commit\n"));
262+
263+
head_commit = lookup_commit(head_sha1);
264+
if (!head_commit || parse_commit(head_commit))
265+
return error(_("could not parse commit %s\n"),
266+
sha1_to_hex(head_commit->object.sha1));
267+
268+
if (!active_cache_tree)
269+
active_cache_tree = cache_tree();
270+
271+
if (!cache_tree_fully_valid(active_cache_tree))
272+
if (cache_tree_update(active_cache_tree, active_cache,
273+
active_nr, 0))
274+
return error(_("Unable to update cache tree\n"));
275+
276+
return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.sha1);
277+
}
278+
254279
/*
255280
* If we are cherry-pick, and if the merge did not result in
256281
* hand-editing, we will hit this commit and inherit the original
@@ -260,21 +285,46 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
260285
*/
261286
static int run_git_commit(const char *defmsg, struct replay_opts *opts)
262287
{
263-
/* 6 is max possible length of our args array including NULL */
264-
const char *args[6];
265-
int i = 0;
288+
struct argv_array array;
289+
int rc;
290+
291+
argv_array_init(&array);
292+
argv_array_push(&array, "commit");
293+
argv_array_push(&array, "-n");
266294

267-
args[i++] = "commit";
268-
args[i++] = "-n";
269295
if (opts->signoff)
270-
args[i++] = "-s";
296+
argv_array_push(&array, "-s");
271297
if (!opts->edit) {
272-
args[i++] = "-F";
273-
args[i++] = defmsg;
298+
argv_array_push(&array, "-F");
299+
argv_array_push(&array, defmsg);
300+
}
301+
302+
if (opts->allow_empty)
303+
argv_array_push(&array, "--allow-empty");
304+
305+
rc = run_command_v_opt(array.argv, RUN_GIT_CMD);
306+
argv_array_clear(&array);
307+
return rc;
308+
}
309+
310+
static int is_original_commit_empty(struct commit *commit)
311+
{
312+
const unsigned char *ptree_sha1;
313+
314+
if (parse_commit(commit))
315+
return error(_("Could not parse commit %s\n"),
316+
sha1_to_hex(commit->object.sha1));
317+
if (commit->parents) {
318+
struct commit *parent = commit->parents->item;
319+
if (parse_commit(parent))
320+
return error(_("Could not parse parent commit %s\n"),
321+
sha1_to_hex(parent->object.sha1));
322+
ptree_sha1 = parent->tree->object.sha1;
323+
} else {
324+
ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */
274325
}
275-
args[i] = NULL;
276326

277-
return run_command_v_opt(args, RUN_GIT_CMD);
327+
return !hashcmp(ptree_sha1, commit->tree->object.sha1);
278328
}
279329

280330
static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
@@ -286,6 +336,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
286336
char *defmsg = NULL;
287337
struct strbuf msgbuf = STRBUF_INIT;
288338
int res;
339+
int empty_commit;
340+
int index_unchanged;
289341

290342
if (opts->no_commit) {
291343
/*
@@ -411,6 +463,10 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
411463
free_commit_list(remotes);
412464
}
413465

466+
empty_commit = is_original_commit_empty(commit);
467+
if (empty_commit < 0)
468+
return empty_commit;
469+
414470
/*
415471
* If the merge was clean or if it failed due to conflict, we write
416472
* CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
@@ -431,6 +487,25 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
431487
print_advice(res == 1, opts);
432488
rerere(opts->allow_rerere_auto);
433489
} else {
490+
index_unchanged = is_index_unchanged();
491+
/*
492+
* If index_unchanged is less than 0, that indicates we either
493+
* couldn't parse HEAD or the index, so error out here.
494+
*/
495+
if (index_unchanged < 0)
496+
return index_unchanged;
497+
498+
if (!empty_commit && !opts->keep_redundant_commits && index_unchanged)
499+
/*
500+
* The head tree and the index match
501+
* meaning the commit is empty. Since it wasn't created
502+
* empty (based on the previous test), we can conclude
503+
* the commit has been made redundant. Since we don't
504+
* want to keep redundant commits, we can just return
505+
* here, skipping this commit
506+
*/
507+
return 0;
508+
434509
if (!opts->no_commit)
435510
res = run_git_commit(defmsg, opts);
436511
}

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct replay_opts {
2929
int signoff;
3030
int allow_ff;
3131
int allow_rerere_auto;
32+
int allow_empty;
33+
int keep_redundant_commits;
3234

3335
int mainline;
3436

t/t3505-cherry-pick-empty.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ test_expect_success setup '
1818
echo third >> file1 &&
1919
git add file1 &&
2020
test_tick &&
21-
git commit --allow-empty-message -m ""
21+
git commit --allow-empty-message -m "" &&
22+
23+
git checkout master &&
24+
git checkout -b empty-branch2 &&
25+
test_tick &&
26+
git commit --allow-empty -m "empty"
2227
2328
'
2429

@@ -48,4 +53,22 @@ test_expect_success 'index lockfile was removed' '
4853
4954
'
5055

56+
test_expect_success 'cherry pick an empty non-ff commit without --allow-empty' '
57+
git checkout master &&
58+
echo fourth >>file2 &&
59+
git add file2 &&
60+
git commit -m "fourth" &&
61+
test_must_fail git cherry-pick empty-branch2
62+
'
63+
64+
test_expect_success 'cherry pick an empty non-ff commit with --allow-empty' '
65+
git checkout master &&
66+
git cherry-pick --allow-empty empty-branch2
67+
'
68+
69+
test_expect_success 'cherry pick with --keep-redundant-commits' '
70+
git checkout master &&
71+
git cherry-pick --keep-redundant-commits HEAD^
72+
'
73+
5174
test_done

0 commit comments

Comments
 (0)