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 <nhorman@tuxdriver.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Neil Horman committed Apr 20, 2012 at 10:36 UTC 90e1818f9a06015159712e204dd90868e0a6c421
4 files changed +55 -8
Documentation/git-rebase.txt
+4
@@ -238,6 +238,10 @@ leave out at most one of A and B, in which case it defaults to HEAD.
238 will be reset to where it was when the rebase operation was
239 started.
240
241 +--keep-empty::
242 + Keep the commits that do not change anything from its
243 + parents in the result.
244 +
245 --skip::
246 Restart the rebasing process by skipping the current patch.
247
git-rebase--am.sh
+14 -5
@@ -20,11 +20,20 @@ esac
20
21 test -n "$rebase_root" && root_flag=--root
22
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 +
37 ret=$?
38 test 0 != $ret -a -d "$state_dir" && write_basic_state
39 exit $ret
git-rebase--interactive.sh
+32 -3
@@ -167,6 +167,14 @@ has_action () {
167 sane_grep '^[^#]' "$1" >/dev/null
168 }
169
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 +
178 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
179 # GIT_AUTHOR_DATE exported from the current environment.
180 do_with_author () {
@@ -191,12 +199,19 @@ git_sequence_editor () {
199
200 pick_one () {
201 ff=--ff
202 +
203 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
204 case "$force_rebase" in '') ;; ?*) ff= ;; esac
205 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 +
212 test -d "$rewritten" &&
213 pick_one_preserving_merges "$@" && return
199 - output git cherry-pick $ff "$@"
214 + output git cherry-pick $empty_args $ff "$@"
215 }
216
217 pick_one_preserving_merges () {
@@ -780,9 +795,17 @@ git rev-list $merges_option --pretty=oneline --abbrev-commit \
795 sed -n "s/^>//p" |
796 while read -r shortsha1 rest
797 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 +
806 if test t != "$preserve_merges"
807 then
785 - printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
808 + printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
809 else
810 sha1=$(git rev-parse $shortsha1)
811 if test -z "$rebase_root"
@@ -801,7 +824,7 @@ do
824 if test f = "$preserve"
825 then
826 touch "$rewritten"/$sha1
804 - printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
827 + printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
828 fi
829 fi
830 done
@@ -851,6 +874,12 @@ cat >> "$todo" << EOF
874 #
875 EOF
876
877 +if test -z "$keep_empty"
878 +then
879 + echo "# Note that empty commits are commented out" >>"$todo"
880 +fi
881 +
882 +
883 has_action "$todo" ||
884 die_abort "Nothing to do"
885
git-rebase.sh
+5
@@ -43,6 +43,7 @@ s,strategy=! use the given merge strategy
43 no-ff! cherry-pick all commits, even if unchanged
44 m,merge! use merging strategies to rebase
45 i,interactive! let the user edit the list of commits to rebase
46 +k,keep-empty preserve empty commits during rebase
47 f,force-rebase! force rebase even if branch is up to date
48 X,strategy-option=! pass the argument through to the merge strategy
49 stat! display a diffstat of what changed upstream
@@ -97,6 +98,7 @@ state_dir=
98 action=
99 preserve_merges=
100 autosquash=
101 +keep_empty=
102 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
103
104 read_basic_state () {
@@ -220,6 +222,9 @@ do
222 -i)
223 interactive_rebase=explicit
224 ;;
225 + -k)
226 + keep_empty=yes
227 + ;;
228 -p)
229 preserve_merges=t
230 test -z "$interactive_rebase" && interactive_rebase=implied