rebase: extract functions out of git_rebase__interactive
The extracted functions are: - initiate_action - setup_reflog_action - init_basic_state - init_revisions_and_shortrevisions - complete_action Used by git_rebase__interactive Signed-off-by: Wink Saville <wink@saville.com> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Wink Saville committed
Mar 23, 2018 at 14:25 UTC
27c499bf33bf6c03cd8c9cfeaa0985f8f7dafc25
1 file changed
+111
-71
git-rebase--interactive.sh
+111
-71
@@ -740,8 +740,20 @@ get_missing_commit_check_level () {
740
printf '%s' "$check_level" | tr 'A-Z' 'a-z'
741
}
742
743
-git_rebase__interactive () {
744
- case "$action" in
743
+# Initiate an action. If the cannot be any
744
+# further action it may exec a command
745
+# or exit and not return.
746
+#
747
+# TODO: Consider a cleaner return model so it
748
+# never exits and always return 0 if process
749
+# is complete.
750
+#
751
+# Parameter 1 is the action to initiate.
752
+#
753
+# Returns 0 if the action was able to complete
754
+# and if 1 if further processing is required.
755
+initiate_action () {
756
+ case "$1" in
757
continue)
758
if test ! -d "$rewritten"
759
then
@@ -836,8 +848,13 @@ To continue rebase after editing, run:
848
show-current-patch)
849
exec git show REBASE_HEAD --
850
;;
851
+ *)
852
+ return 1 # continue
853
+ ;;
854
esac
855
+}
856
857
+setup_reflog_action () {
858
comment_for_reflog start
859
860
if test ! -z "$switch_to"
@@ -848,13 +865,102 @@ To continue rebase after editing, run:
865
866
comment_for_reflog start
867
fi
868
+}
869
870
+init_basic_state () {
871
orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
872
mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
873
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
874
875
: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
876
write_basic_state
877
+}
878
+
879
+init_revisions_and_shortrevisions () {
880
+ shorthead=$(git rev-parse --short $orig_head)
881
+ shortonto=$(git rev-parse --short $onto)
882
+ if test -z "$rebase_root"
883
+ # this is now equivalent to ! -z "$upstream"
884
+ then
885
+ shortupstream=$(git rev-parse --short $upstream)
886
+ revisions=$upstream...$orig_head
887
+ shortrevisions=$shortupstream..$shorthead
888
+ else
889
+ revisions=$onto...$orig_head
890
+ shortrevisions=$shorthead
891
+ fi
892
+}
893
+
894
+complete_action() {
895
+ test -s "$todo" || echo noop >> "$todo"
896
+ test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
897
+ test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
898
+
899
+ todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
900
+ todocount=${todocount##* }
901
+
902
+cat >>"$todo" <<EOF
903
+
904
+$comment_char $(eval_ngettext \
905
+ "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
906
+ "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
907
+ "$todocount")
908
+EOF
909
+ append_todo_help
910
+ gettext "
911
+ However, if you remove everything, the rebase will be aborted.
912
+
913
+ " | git stripspace --comment-lines >>"$todo"
914
+
915
+ if test -z "$keep_empty"
916
+ then
917
+ printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
918
+ fi
919
+
920
+
921
+ has_action "$todo" ||
922
+ return 2
923
+
924
+ cp "$todo" "$todo".backup
925
+ collapse_todo_ids
926
+ git_sequence_editor "$todo" ||
927
+ die_abort "$(gettext "Could not execute editor")"
928
+
929
+ has_action "$todo" ||
930
+ return 2
931
+
932
+ git rebase--helper --check-todo-list || {
933
+ ret=$?
934
+ checkout_onto
935
+ exit $ret
936
+ }
937
+
938
+ expand_todo_ids
939
+
940
+ test -d "$rewritten" || test -n "$force_rebase" ||
941
+ onto="$(git rebase--helper --skip-unnecessary-picks)" ||
942
+ die "Could not skip unnecessary pick commands"
943
+
944
+ checkout_onto
945
+ if test -z "$rebase_root" && test ! -d "$rewritten"
946
+ then
947
+ require_clean_work_tree "rebase"
948
+ exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
949
+ --continue
950
+ fi
951
+ do_rest
952
+}
953
+
954
+git_rebase__interactive () {
955
+ initiate_action "$action"
956
+ ret=$?
957
+ if test $ret = 0; then
958
+ return 0
959
+ fi
960
+
961
+ setup_reflog_action
962
+ init_basic_state
963
+
964
if test t = "$preserve_merges"
965
then
966
if test -z "$rebase_root"
@@ -878,18 +984,8 @@ To continue rebase after editing, run:
984
merges_option="--no-merges --cherry-pick"
985
fi
986
881
- shorthead=$(git rev-parse --short $orig_head)
882
- shortonto=$(git rev-parse --short $onto)
883
- if test -z "$rebase_root"
884
- # this is now equivalent to ! -z "$upstream"
885
- then
886
- shortupstream=$(git rev-parse --short $upstream)
887
- revisions=$upstream...$orig_head
888
- shortrevisions=$shortupstream..$shorthead
889
- else
890
- revisions=$onto...$orig_head
891
- shortrevisions=$shorthead
892
- fi
987
+ init_revisions_and_shortrevisions
988
+
989
if test t != "$preserve_merges"
990
then
991
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
@@ -960,61 +1056,5 @@ To continue rebase after editing, run:
1056
done
1057
fi
1058
963
- test -s "$todo" || echo noop >> "$todo"
964
- test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
965
- test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
966
-
967
- todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
968
- todocount=${todocount##* }
969
-
970
-cat >>"$todo" <<EOF
971
-
972
-$comment_char $(eval_ngettext \
973
- "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
974
- "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
975
- "$todocount")
976
-EOF
977
- append_todo_help
978
- gettext "
979
- However, if you remove everything, the rebase will be aborted.
980
-
981
- " | git stripspace --comment-lines >>"$todo"
982
-
983
- if test -z "$keep_empty"
984
- then
985
- printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
986
- fi
987
-
988
-
989
- has_action "$todo" ||
990
- return 2
991
-
992
- cp "$todo" "$todo".backup
993
- collapse_todo_ids
994
- git_sequence_editor "$todo" ||
995
- die_abort "$(gettext "Could not execute editor")"
996
-
997
- has_action "$todo" ||
998
- return 2
999
-
1000
- git rebase--helper --check-todo-list || {
1001
- ret=$?
1002
- checkout_onto
1003
- exit $ret
1004
- }
1005
-
1006
- expand_todo_ids
1007
-
1008
- test -d "$rewritten" || test -n "$force_rebase" ||
1009
- onto="$(git rebase--helper --skip-unnecessary-picks)" ||
1010
- die "Could not skip unnecessary pick commands"
1011
-
1012
- checkout_onto
1013
- if test -z "$rebase_root" && test ! -d "$rewritten"
1014
- then
1015
- require_clean_work_tree "rebase"
1016
- exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
1017
- --continue
1018
- fi
1019
- do_rest
1059
+ complete_action
1060
}