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 <j6t@kdbg.org> Signed-off-by: Martin von Zweigbergk <martin.von.zweigbergk@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin von Zweigbergk committed
Feb 6, 2011 at 13:43 UTC
cf432ca0512d68afa603941ecddbb45bb7ded01d
2 files changed
+100
-182
git-rebase--interactive.sh
+62
-162
@@ -10,31 +10,7 @@
10
# The original idea comes from Eric W. Biederman, in
11
# http://article.gmane.org/gmane.comp.version-control.git/22407
12
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
-
13
. git-sh-setup
37
-require_work_tree
14
15
dotest="$GIT_DIR/rebase-merge"
16
@@ -105,16 +81,6 @@ amend="$dotest"/amend
81
rewritten_list="$dotest"/rewritten-list
82
rewritten_pending="$dotest"/rewritten-pending
83
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
-
84
GIT_CHERRY_PICK_HELP="\
85
hint: after resolving the conflicts, mark the corrected paths
86
hint: with 'git add <paths>' and run 'git rebase --continue'"
@@ -648,15 +614,6 @@ skip_unnecessary_picks () {
614
die "Could not skip unnecessary pick commands"
615
}
616
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
-
617
get_saved_options () {
618
test -d "$rewritten" && preserve_merges=t
619
test -f "$dotest"/strategy && strategy="$(cat "$dotest"/strategy)"
@@ -744,134 +701,77 @@ parse_onto () {
701
git rev-parse --verify "$1^0"
702
}
703
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"
727
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 "\
731
You have uncommitted changes in your working tree. Please, commit them
732
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"
735
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
741
794
- record_in_rewritten "$(cat "$dotest"/stopped-sha)"
742
+ record_in_rewritten "$(cat "$dotest"/stopped-sha)"
743
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
750
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"
753
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
759
;;
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
775
776
if test -n "$onto"
777
then
git-rebase.sh
+38
-20
@@ -64,6 +64,9 @@ type=
64
state_dir=
65
# One of {'', continue, skip, abort}, as parsed from command line
66
action=
67
+preserve_merges=
68
+autosquash=
69
+test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
70
71
read_state () {
72
if test "$type" = merge
@@ -176,27 +179,12 @@ finish_rb_merge () {
179
say All done.
180
}
181
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 () {
183
if [ "$interactive_rebase" = implied ]; then
184
GIT_EDITOR=:
185
export GIT_EDITOR
186
fi
198
-
199
- test -n "$interactive_rebase" || test -f "$merge_dir"/interactive
187
+ . git-rebase--interactive "$@"
188
}
189
190
run_pre_rebase_hook () {
@@ -211,8 +199,6 @@ run_pre_rebase_hook () {
199
test -f "$apply_dir"/applying &&
200
die 'It looks like git-am is in progress. Cannot rebase.'
201
214
-is_interactive "$@" && exec git-rebase--interactive "$@"
215
-
202
if test -d "$apply_dir"
203
then
204
type=am
@@ -249,6 +235,19 @@ do
235
onto="$2"
236
shift
237
;;
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
+ ;;
251
-M|-m|--m|--me|--mer|--merg|--merge)
252
do_merge=t
253
;;
@@ -339,7 +338,11 @@ do
338
done
339
test $# -gt 2 && usage
340
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
346
347
case "$action" in
348
continue)
@@ -415,6 +418,21 @@ fi
418
419
test $# -eq 0 && test -z "$rebase_root" && usage
420
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
+
436
require_clean_work_tree "rebase" "Please commit or stash them."
437
438
if test -z "$rebase_root"