Skip to content

Commit 90e1818

Browse files
nhormangitster
authored andcommitted
git-rebase: add keep_empty flag
Add a command line switch to git-rebase to allow a user the ability to specify that they want to keep any commits in a series that are empty. When git-rebase's type is am, then this option will automatically keep any commit that has a tree object identical to its parent. This patch changes the default behavior of interactive rebases as well. With this patch, git-rebase -i will produce a revision set passed to git-revision-editor, in which empty commits are commented out. Empty commits may be kept manually by uncommenting them. If the new --keep-empty option is used in an interactive rebase the empty commits will automatically all be uncommented in the editor. Signed-off-by: Neil Horman <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bedfe86 commit 90e1818

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

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

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
@@ -851,6 +874,12 @@ cat >> "$todo" << EOF
851874
#
852875
EOF
853876

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

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

0 commit comments

Comments
 (0)