rebase: act on command line outside parsing loop
To later be able to use the command line processing in git-rebase.sh for both interactive and non-interactive rebases, move anything that is specific to non-interactive rebase outside of the parsing loop. Keep only parsing and validation of command line options in the loop. 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
342623224823cbadf61370a43df6941fba477b59
2 files changed
+217
-209
git-rebase--interactive.sh
+151
-149
@@ -866,152 +866,158 @@ first and then run 'git rebase --continue' again."
866
;;
867
--)
868
shift
869
- test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
870
- test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
871
- test -d "$DOTEST" &&
872
- die "Interactive rebase already started"
873
-
874
- git var GIT_COMMITTER_IDENT >/dev/null ||
875
- die "You need to set your committer info first"
876
-
877
- if test -z "$REBASE_ROOT"
878
- then
879
- UPSTREAM_ARG="$1"
880
- UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
881
- test -z "$ONTO" && ONTO=$UPSTREAM
882
- shift
883
- else
884
- UPSTREAM=
885
- UPSTREAM_ARG=--root
886
- test -z "$ONTO" &&
887
- die "You must specify --onto when using --root"
888
- fi
889
- run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
890
-
891
- comment_for_reflog start
892
-
893
- require_clean_work_tree "rebase" "Please commit or stash them."
894
-
895
- if test ! -z "$1"
896
- then
897
- output git checkout "$1" -- ||
898
- die "Could not checkout $1"
899
- fi
869
+ break
870
+ ;;
871
+ esac
872
+ shift
873
+done
874
901
- HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
902
- mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
875
+test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
876
+test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
877
+test -d "$DOTEST" &&
878
+ die "Interactive rebase already started"
879
904
- : > "$DOTEST"/interactive || die "Could not mark as interactive"
905
- git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
906
- echo "detached HEAD" > "$DOTEST"/head-name
880
+git var GIT_COMMITTER_IDENT >/dev/null ||
881
+ die "You need to set your committer info first"
882
908
- echo $HEAD > "$DOTEST"/head
909
- case "$REBASE_ROOT" in
910
- '')
911
- rm -f "$DOTEST"/rebase-root ;;
912
- *)
913
- : >"$DOTEST"/rebase-root ;;
914
- esac
915
- echo $ONTO > "$DOTEST"/onto
916
- test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
917
- test t = "$VERBOSE" && : > "$DOTEST"/verbose
918
- if test t = "$PRESERVE_MERGES"
919
- then
920
- if test -z "$REBASE_ROOT"
921
- then
922
- mkdir "$REWRITTEN" &&
923
- for c in $(git merge-base --all $HEAD $UPSTREAM)
924
- do
925
- echo $ONTO > "$REWRITTEN"/$c ||
926
- die "Could not init rewritten commits"
927
- done
928
- else
929
- mkdir "$REWRITTEN" &&
930
- echo $ONTO > "$REWRITTEN"/root ||
931
- die "Could not init rewritten commits"
932
- fi
933
- # No cherry-pick because our first pass is to determine
934
- # parents to rewrite and skipping dropped commits would
935
- # prematurely end our probe
936
- MERGES_OPTION=
937
- first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
938
- else
939
- MERGES_OPTION="--no-merges --cherry-pick"
940
- fi
941
-
942
- SHORTHEAD=$(git rev-parse --short $HEAD)
943
- SHORTONTO=$(git rev-parse --short $ONTO)
944
- if test -z "$REBASE_ROOT"
945
- # this is now equivalent to ! -z "$UPSTREAM"
946
- then
947
- SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
948
- REVISIONS=$UPSTREAM...$HEAD
949
- SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
950
- else
951
- REVISIONS=$ONTO...$HEAD
952
- SHORTREVISIONS=$SHORTHEAD
953
- fi
954
- git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
955
- --abbrev=7 --reverse --left-right --topo-order \
956
- $REVISIONS | \
957
- sed -n "s/^>//p" |
958
- while read -r shortsha1 rest
883
+if test -z "$REBASE_ROOT"
884
+then
885
+ UPSTREAM_ARG="$1"
886
+ UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
887
+ test -z "$ONTO" && ONTO=$UPSTREAM
888
+ shift
889
+else
890
+ UPSTREAM=
891
+ UPSTREAM_ARG=--root
892
+ test -z "$ONTO" &&
893
+ die "You must specify --onto when using --root"
894
+fi
895
+run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
896
+
897
+comment_for_reflog start
898
+
899
+require_clean_work_tree "rebase" "Please commit or stash them."
900
+
901
+if test ! -z "$1"
902
+then
903
+ output git checkout "$1" -- ||
904
+ die "Could not checkout $1"
905
+fi
906
+
907
+HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
908
+mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
909
+
910
+: > "$DOTEST"/interactive || die "Could not mark as interactive"
911
+git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
912
+ echo "detached HEAD" > "$DOTEST"/head-name
913
+
914
+echo $HEAD > "$DOTEST"/head
915
+case "$REBASE_ROOT" in
916
+'')
917
+ rm -f "$DOTEST"/rebase-root ;;
918
+*)
919
+ : >"$DOTEST"/rebase-root ;;
920
+esac
921
+echo $ONTO > "$DOTEST"/onto
922
+test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
923
+test t = "$VERBOSE" && : > "$DOTEST"/verbose
924
+if test t = "$PRESERVE_MERGES"
925
+then
926
+ if test -z "$REBASE_ROOT"
927
+ then
928
+ mkdir "$REWRITTEN" &&
929
+ for c in $(git merge-base --all $HEAD $UPSTREAM)
930
do
960
- if test t != "$PRESERVE_MERGES"
961
- then
962
- printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
963
- else
964
- sha1=$(git rev-parse $shortsha1)
965
- if test -z "$REBASE_ROOT"
966
- then
967
- preserve=t
968
- for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
969
- do
970
- if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
971
- then
972
- preserve=f
973
- fi
974
- done
975
- else
976
- preserve=f
977
- fi
978
- if test f = "$preserve"
979
- then
980
- touch "$REWRITTEN"/$sha1
981
- printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
982
- fi
983
- fi
931
+ echo $ONTO > "$REWRITTEN"/$c ||
932
+ die "Could not init rewritten commits"
933
done
985
-
986
- # Watch for commits that been dropped by --cherry-pick
987
- if test t = "$PRESERVE_MERGES"
934
+ else
935
+ mkdir "$REWRITTEN" &&
936
+ echo $ONTO > "$REWRITTEN"/root ||
937
+ die "Could not init rewritten commits"
938
+ fi
939
+ # No cherry-pick because our first pass is to determine
940
+ # parents to rewrite and skipping dropped commits would
941
+ # prematurely end our probe
942
+ MERGES_OPTION=
943
+ first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
944
+else
945
+ MERGES_OPTION="--no-merges --cherry-pick"
946
+fi
947
+
948
+SHORTHEAD=$(git rev-parse --short $HEAD)
949
+SHORTONTO=$(git rev-parse --short $ONTO)
950
+if test -z "$REBASE_ROOT"
951
+ # this is now equivalent to ! -z "$UPSTREAM"
952
+then
953
+ SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
954
+ REVISIONS=$UPSTREAM...$HEAD
955
+ SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
956
+else
957
+ REVISIONS=$ONTO...$HEAD
958
+ SHORTREVISIONS=$SHORTHEAD
959
+fi
960
+git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
961
+ --abbrev=7 --reverse --left-right --topo-order \
962
+ $REVISIONS | \
963
+ sed -n "s/^>//p" |
964
+while read -r shortsha1 rest
965
+do
966
+ if test t != "$PRESERVE_MERGES"
967
+ then
968
+ printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
969
+ else
970
+ sha1=$(git rev-parse $shortsha1)
971
+ if test -z "$REBASE_ROOT"
972
then
989
- mkdir "$DROPPED"
990
- # Save all non-cherry-picked changes
991
- git rev-list $REVISIONS --left-right --cherry-pick | \
992
- sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
993
- # Now all commits and note which ones are missing in
994
- # not-cherry-picks and hence being dropped
995
- git rev-list $REVISIONS |
996
- while read rev
973
+ preserve=t
974
+ for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
975
do
998
- if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
976
+ if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
977
then
1000
- # Use -f2 because if rev-list is telling us this commit is
1001
- # not worthwhile, we don't want to track its multiple heads,
1002
- # just the history of its first-parent for others that will
1003
- # be rebasing on top of it
1004
- git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
1005
- short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
1006
- sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
1007
- rm "$REWRITTEN"/$rev
978
+ preserve=f
979
fi
980
done
981
+ else
982
+ preserve=f
983
+ fi
984
+ if test f = "$preserve"
985
+ then
986
+ touch "$REWRITTEN"/$sha1
987
+ printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
988
fi
989
+ fi
990
+done
991
1012
- test -s "$TODO" || echo noop >> "$TODO"
1013
- test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1014
- cat >> "$TODO" << EOF
992
+# Watch for commits that been dropped by --cherry-pick
993
+if test t = "$PRESERVE_MERGES"
994
+then
995
+ mkdir "$DROPPED"
996
+ # Save all non-cherry-picked changes
997
+ git rev-list $REVISIONS --left-right --cherry-pick | \
998
+ sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
999
+ # Now all commits and note which ones are missing in
1000
+ # not-cherry-picks and hence being dropped
1001
+ git rev-list $REVISIONS |
1002
+ while read rev
1003
+ do
1004
+ if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
1005
+ then
1006
+ # Use -f2 because if rev-list is telling us this commit is
1007
+ # not worthwhile, we don't want to track its multiple heads,
1008
+ # just the history of its first-parent for others that will
1009
+ # be rebasing on top of it
1010
+ git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
1011
+ short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
1012
+ sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
1013
+ rm "$REWRITTEN"/$rev
1014
+ fi
1015
+ done
1016
+fi
1017
+
1018
+test -s "$TODO" || echo noop >> "$TODO"
1019
+test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1020
+cat >> "$TODO" << EOF
1021
1022
# Rebase $SHORTREVISIONS onto $SHORTONTO
1023
#
@@ -1028,22 +1034,18 @@ first and then run 'git rebase --continue' again."
1034
#
1035
EOF
1036
1031
- has_action "$TODO" ||
1032
- die_abort "Nothing to do"
1037
+has_action "$TODO" ||
1038
+ die_abort "Nothing to do"
1039
1034
- cp "$TODO" "$TODO".backup
1035
- git_editor "$TODO" ||
1036
- die_abort "Could not execute editor"
1040
+cp "$TODO" "$TODO".backup
1041
+git_editor "$TODO" ||
1042
+ die_abort "Could not execute editor"
1043
1038
- has_action "$TODO" ||
1039
- die_abort "Nothing to do"
1044
+has_action "$TODO" ||
1045
+ die_abort "Nothing to do"
1046
1041
- test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
1047
+test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
1048
1043
- output git checkout $ONTO || die_abort "could not detach HEAD"
1044
- git update-ref ORIG_HEAD $HEAD
1045
- do_rest
1046
- ;;
1047
- esac
1048
- shift
1049
-done
1049
+output git checkout $ONTO || die_abort "could not detach HEAD"
1050
+git update-ref ORIG_HEAD $HEAD
1051
+do_rest
git-rebase.sh
+66
-60
@@ -62,6 +62,8 @@ in_progress=
62
type=
63
# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
64
state_dir=
65
+# One of {'', continue, skip, abort}, as parsed from command line
66
+action=
67
68
read_state () {
69
if test "$type" = merge
@@ -236,66 +238,10 @@ do
238
--verify)
239
OK_TO_SKIP_PRE_REBASE=
240
;;
239
- --continue)
240
- test -z "$in_progress" && die "No rebase in progress?"
241
-
242
- git update-index --ignore-submodules --refresh &&
243
- git diff-files --quiet --ignore-submodules || {
244
- echo "You must edit all merge conflicts and then"
245
- echo "mark them as resolved using git add"
246
- exit 1
247
- }
248
- read_state
249
- if test -d "$merge_dir"
250
- then
251
- continue_merge
252
- while test "$msgnum" -le "$end"
253
- do
254
- call_merge "$msgnum"
255
- continue_merge
256
- done
257
- finish_rb_merge
258
- exit
259
- fi
260
- git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
261
- move_to_original_branch
262
- exit
263
- ;;
264
- --skip)
265
- test -z "$in_progress" && die "No rebase in progress?"
266
-
267
- git reset --hard HEAD || exit $?
268
- read_state
269
- if test -d "$merge_dir"
270
- then
271
- git rerere clear
272
- msgnum=$(($msgnum + 1))
273
- while test "$msgnum" -le "$end"
274
- do
275
- call_merge "$msgnum"
276
- continue_merge
277
- done
278
- finish_rb_merge
279
- exit
280
- fi
281
- git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
282
- move_to_original_branch
283
- exit
284
- ;;
285
- --abort)
286
- test -z "$in_progress" && die "No rebase in progress?"
287
-
288
- git rerere clear
289
- read_state
290
- case "$head_name" in
291
- refs/*)
292
- git symbolic-ref HEAD $head_name ||
293
- die "Could not move back to $head_name"
294
- ;;
295
- esac
296
- git reset --hard $orig_head
297
- rm -r "$state_dir"
298
- exit
241
+ --continue|--skip|--abort)
242
+ action=${1##--}
243
+ shift
244
+ break
245
;;
246
--onto)
247
test 2 -le "$#" || usage
@@ -391,6 +337,66 @@ do
337
done
338
test $# -gt 2 && usage
339
340
+test -n "$action" && test -z "$in_progress" && die "No rebase in progress?"
341
+
342
+case "$action" in
343
+continue)
344
+ git update-index --ignore-submodules --refresh &&
345
+ git diff-files --quiet --ignore-submodules || {
346
+ echo "You must edit all merge conflicts and then"
347
+ echo "mark them as resolved using git add"
348
+ exit 1
349
+ }
350
+ read_state
351
+ if test -d "$merge_dir"
352
+ then
353
+ continue_merge
354
+ while test "$msgnum" -le "$end"
355
+ do
356
+ call_merge "$msgnum"
357
+ continue_merge
358
+ done
359
+ finish_rb_merge
360
+ exit
361
+ fi
362
+ git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
363
+ move_to_original_branch
364
+ exit
365
+ ;;
366
+skip)
367
+ git reset --hard HEAD || exit $?
368
+ read_state
369
+ if test -d "$merge_dir"
370
+ then
371
+ git rerere clear
372
+ msgnum=$(($msgnum + 1))
373
+ while test "$msgnum" -le "$end"
374
+ do
375
+ call_merge "$msgnum"
376
+ continue_merge
377
+ done
378
+ finish_rb_merge
379
+ exit
380
+ fi
381
+ git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
382
+ move_to_original_branch
383
+ exit
384
+ ;;
385
+abort)
386
+ git rerere clear
387
+ read_state
388
+ case "$head_name" in
389
+ refs/*)
390
+ git symbolic-ref HEAD $head_name ||
391
+ die "Could not move back to $head_name"
392
+ ;;
393
+ esac
394
+ git reset --hard $orig_head
395
+ rm -r "$state_dir"
396
+ exit
397
+ ;;
398
+esac
399
+
400
# Make sure no rebase is in progress
401
if test -n "$in_progress"
402
then