Skip to content

Commit cf432ca

Browse files
Martin von Zweigbergkgitster
authored andcommitted
rebase: factor out command line option processing
Factor out the command line processing in git-rebase--interactive.sh to git-rebase.sh. Store the options in variables in git-rebase.sh and then source git-rebase--interactive.sh. Suggested-by: Johannes Sixt <[email protected]> Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9765b6a commit cf432ca

File tree

2 files changed

+100
-182
lines changed

2 files changed

+100
-182
lines changed

git-rebase--interactive.sh

Lines changed: 62 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,7 @@
1010
# The original idea comes from Eric W. Biederman, in
1111
# http://article.gmane.org/gmane.comp.version-control.git/22407
1212

13-
OPTIONS_KEEPDASHDASH=
14-
OPTIONS_SPEC="\
15-
git-rebase [-i] [options] [--] <upstream> [<branch>]
16-
git-rebase [-i] (--continue | --abort | --skip)
17-
--
18-
Available options are
19-
v,verbose display a diffstat of what changed upstream
20-
onto= rebase onto given branch instead of upstream
21-
p,preserve-merges try to recreate merges instead of ignoring them
22-
s,strategy= use the given merge strategy
23-
no-ff cherry-pick all commits, even if unchanged
24-
m,merge always used (no-op)
25-
i,interactive always used (no-op)
26-
Actions:
27-
continue continue rebasing process
28-
abort abort rebasing process and restore original branch
29-
skip skip current patch and continue rebasing process
30-
no-verify override pre-rebase hook from stopping the operation
31-
verify allow pre-rebase hook to run
32-
root rebase all reachable commmits up to the root(s)
33-
autosquash move commits that begin with squash!/fixup! under -i
34-
"
35-
3613
. git-sh-setup
37-
require_work_tree
3814

3915
dotest="$GIT_DIR/rebase-merge"
4016

@@ -105,16 +81,6 @@ amend="$dotest"/amend
10581
rewritten_list="$dotest"/rewritten-list
10682
rewritten_pending="$dotest"/rewritten-pending
10783

108-
preserve_merges=
109-
strategy=
110-
onto=
111-
verbose=
112-
ok_to_skip_pre_rebase=
113-
rebase_root=
114-
autosquash=
115-
test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
116-
force_rebase=
117-
11884
GIT_CHERRY_PICK_HELP="\
11985
hint: after resolving the conflicts, mark the corrected paths
12086
hint: with 'git add <paths>' and run 'git rebase --continue'"
@@ -648,15 +614,6 @@ skip_unnecessary_picks () {
648614
die "Could not skip unnecessary pick commands"
649615
}
650616

651-
# check if no other options are set
652-
is_standalone () {
653-
test $# -eq 2 -a "$2" = '--' &&
654-
test -z "$onto" &&
655-
test -z "$preserve_merges" &&
656-
test -z "$strategy" &&
657-
test -z "$verbose"
658-
}
659-
660617
get_saved_options () {
661618
test -d "$rewritten" && preserve_merges=t
662619
test -f "$dotest"/strategy && strategy="$(cat "$dotest"/strategy)"
@@ -744,134 +701,77 @@ parse_onto () {
744701
git rev-parse --verify "$1^0"
745702
}
746703

747-
while test $# != 0
748-
do
749-
case "$1" in
750-
--no-verify)
751-
ok_to_skip_pre_rebase=yes
752-
;;
753-
--verify)
754-
ok_to_skip_pre_rebase=
755-
;;
756-
--continue)
757-
is_standalone "$@" || usage
758-
get_saved_options
759-
comment_for_reflog continue
760-
761-
test -d "$dotest" || die "No interactive rebase running"
762-
763-
# Sanity check
764-
git rev-parse --verify HEAD >/dev/null ||
765-
die "Cannot read HEAD"
766-
git update-index --ignore-submodules --refresh &&
767-
git diff-files --quiet --ignore-submodules ||
768-
die "Working tree is dirty"
769-
770-
# do we have anything to commit?
771-
if git diff-index --cached --quiet --ignore-submodules HEAD --
704+
case "$action" in
705+
continue)
706+
get_saved_options
707+
comment_for_reflog continue
708+
709+
test -d "$dotest" || die "No interactive rebase running"
710+
711+
# Sanity check
712+
git rev-parse --verify HEAD >/dev/null ||
713+
die "Cannot read HEAD"
714+
git update-index --ignore-submodules --refresh &&
715+
git diff-files --quiet --ignore-submodules ||
716+
die "Working tree is dirty"
717+
718+
# do we have anything to commit?
719+
if git diff-index --cached --quiet --ignore-submodules HEAD --
720+
then
721+
: Nothing to commit -- skip this
722+
else
723+
. "$author_script" ||
724+
die "Cannot find the author identity"
725+
current_head=
726+
if test -f "$amend"
772727
then
773-
: Nothing to commit -- skip this
774-
else
775-
. "$author_script" ||
776-
die "Cannot find the author identity"
777-
current_head=
778-
if test -f "$amend"
779-
then
780-
current_head=$(git rev-parse --verify HEAD)
781-
test "$current_head" = $(cat "$amend") ||
782-
die "\
728+
current_head=$(git rev-parse --verify HEAD)
729+
test "$current_head" = $(cat "$amend") ||
730+
die "\
783731
You have uncommitted changes in your working tree. Please, commit them
784732
first and then run 'git rebase --continue' again."
785-
git reset --soft HEAD^ ||
786-
die "Cannot rewind the HEAD"
787-
fi
788-
do_with_author git commit --no-verify -F "$msg" -e || {
789-
test -n "$current_head" && git reset --soft $current_head
790-
die "Could not commit staged changes."
791-
}
733+
git reset --soft HEAD^ ||
734+
die "Cannot rewind the HEAD"
792735
fi
736+
do_with_author git commit --no-verify -F "$msg" -e || {
737+
test -n "$current_head" && git reset --soft $current_head
738+
die "Could not commit staged changes."
739+
}
740+
fi
793741

794-
record_in_rewritten "$(cat "$dotest"/stopped-sha)"
742+
record_in_rewritten "$(cat "$dotest"/stopped-sha)"
795743

796-
require_clean_work_tree "rebase"
797-
do_rest
798-
;;
799-
--abort)
800-
is_standalone "$@" || usage
801-
get_saved_options
802-
comment_for_reflog abort
803-
804-
git rerere clear
805-
test -d "$dotest" || die "No interactive rebase running"
806-
807-
headname=$(cat "$dotest"/head-name)
808-
head=$(cat "$dotest"/head)
809-
case $headname in
810-
refs/*)
811-
git symbolic-ref HEAD $headname
812-
;;
813-
esac &&
814-
output git reset --hard $head &&
815-
rm -rf "$dotest"
816-
exit
817-
;;
818-
--skip)
819-
is_standalone "$@" || usage
820-
get_saved_options
821-
comment_for_reflog skip
744+
require_clean_work_tree "rebase"
745+
do_rest
746+
;;
747+
abort)
748+
get_saved_options
749+
comment_for_reflog abort
822750

823-
git rerere clear
824-
test -d "$dotest" || die "No interactive rebase running"
751+
git rerere clear
752+
test -d "$dotest" || die "No interactive rebase running"
825753

826-
output git reset --hard && do_rest
827-
;;
828-
-s)
829-
case "$#,$1" in
830-
*,*=*)
831-
strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
832-
1,*)
833-
usage ;;
834-
*)
835-
strategy="$2"
836-
shift ;;
837-
esac
838-
;;
839-
-m)
840-
# we use merge anyway
841-
;;
842-
-v)
843-
verbose=t
844-
;;
845-
-p)
846-
preserve_merges=t
847-
;;
848-
-i)
849-
# yeah, we know
850-
;;
851-
--no-ff)
852-
force_rebase=t
853-
;;
854-
--root)
855-
rebase_root=t
856-
;;
857-
--autosquash)
858-
autosquash=t
859-
;;
860-
--no-autosquash)
861-
autosquash=
862-
;;
863-
--onto)
864-
test 2 -le "$#" || usage
865-
onto="$2"
866-
shift
867-
;;
868-
--)
869-
shift
870-
break
754+
headname=$(cat "$dotest"/head-name)
755+
head=$(cat "$dotest"/head)
756+
case $headname in
757+
refs/*)
758+
git symbolic-ref HEAD $headname
871759
;;
872-
esac
873-
shift
874-
done
760+
esac &&
761+
output git reset --hard $head &&
762+
rm -rf "$dotest"
763+
exit
764+
;;
765+
skip)
766+
get_saved_options
767+
comment_for_reflog skip
768+
769+
git rerere clear
770+
test -d "$dotest" || die "No interactive rebase running"
771+
772+
output git reset --hard && do_rest
773+
;;
774+
esac
875775

876776
if test -n "$onto"
877777
then

git-rebase.sh

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type=
6464
state_dir=
6565
# One of {'', continue, skip, abort}, as parsed from command line
6666
action=
67+
preserve_merges=
68+
autosquash=
69+
test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
6770

6871
read_state () {
6972
if test "$type" = merge
@@ -176,27 +179,12 @@ finish_rb_merge () {
176179
say All done.
177180
}
178181

179-
is_interactive () {
180-
while test $# != 0
181-
do
182-
case "$1" in
183-
-i|--interactive)
184-
interactive_rebase=explicit
185-
break
186-
;;
187-
-p|--preserve-merges)
188-
interactive_rebase=implied
189-
;;
190-
esac
191-
shift
192-
done
193-
182+
run_interactive_rebase () {
194183
if [ "$interactive_rebase" = implied ]; then
195184
GIT_EDITOR=:
196185
export GIT_EDITOR
197186
fi
198-
199-
test -n "$interactive_rebase" || test -f "$merge_dir"/interactive
187+
. git-rebase--interactive "$@"
200188
}
201189

202190
run_pre_rebase_hook () {
@@ -211,8 +199,6 @@ run_pre_rebase_hook () {
211199
test -f "$apply_dir"/applying &&
212200
die 'It looks like git-am is in progress. Cannot rebase.'
213201

214-
is_interactive "$@" && exec git-rebase--interactive "$@"
215-
216202
if test -d "$apply_dir"
217203
then
218204
type=am
@@ -249,6 +235,19 @@ do
249235
onto="$2"
250236
shift
251237
;;
238+
-i|--interactive)
239+
interactive_rebase=explicit
240+
;;
241+
-p|--preserve-merges)
242+
preserve_merges=t
243+
test -z "$interactive_rebase" && interactive_rebase=implied
244+
;;
245+
--autosquash)
246+
autosquash=t
247+
;;
248+
--no-autosquash)
249+
autosquash=
250+
;;
252251
-M|-m|--m|--me|--mer|--merg|--merge)
253252
do_merge=t
254253
;;
@@ -339,7 +338,11 @@ do
339338
done
340339
test $# -gt 2 && usage
341340

342-
test -n "$action" && test -z "$in_progress" && die "No rebase in progress?"
341+
if test -n "$action"
342+
then
343+
test -z "$in_progress" && die "No rebase in progress?"
344+
test "$type" = interactive && run_interactive_rebase
345+
fi
343346

344347
case "$action" in
345348
continue)
@@ -415,6 +418,21 @@ fi
415418

416419
test $# -eq 0 && test -z "$rebase_root" && usage
417420

421+
if test -n "$interactive_rebase"
422+
then
423+
type=interactive
424+
state_dir="$merge_dir"
425+
elif test -n "$do_merge"
426+
then
427+
type=merge
428+
state_dir="$merge_dir"
429+
else
430+
type=am
431+
state_dir="$apply_dir"
432+
fi
433+
434+
test "$type" = interactive && run_interactive_rebase "$@"
435+
418436
require_clean_work_tree "rebase" "Please commit or stash them."
419437

420438
if test -z "$rebase_root"

0 commit comments

Comments
 (0)