@matheusbsbs / matheus-bagliano / commits / 99de0640f2
Fork 0

rebase: improve detection of rebase in progress

Detect early on if a rebase is in progress and what type of rebase it is (interactive, merge-based or am-based). This prepares for further refactoring where am-based rebase will be dispatched to git-rebase--am.sh and merge-based rebase will be dispatched to git-rebase--merge.sh. The idea is to use the same variables whether the type of rebase was detected from rebase-apply/ or rebase-merge/ directories or from the command line options. This will make the code more readable and will later also make it easier to dispatch to the type-specific scripts. Also show a consistent error message independent of the type of rebase that was in progress and remove the obsolete wording about being in the middle of a 'patch application', since that (an existing "$GIT_DIR"/rebase-apply/applying) aborts 'git rebase' at an earlier stage. 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 99de0640f2837d31f84d2889a770d936ae4394be
1 file changed +41 -39
git-rebase.sh
+41 -39
@@ -56,16 +56,19 @@ git_am_opt=
56 rebase_root=
57 force_rebase=
58 allow_rerere_autoupdate=
59 +# Non-empty if a rebase was in progress when 'git rebase' was invoked
60 +in_progress=
61 +# One of {am, merge, interactive}
62 +type=
63 +# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
64 +state_dir=
65
66 read_state () {
61 - if test -d "$merge_dir"
67 + if test "$type" = merge
68 then
63 - state_dir="$merge_dir"
64 - onto_name=$(cat "$merge_dir"/onto_name) &&
65 - end=$(cat "$merge_dir"/end) &&
66 - msgnum=$(cat "$merge_dir"/msgnum)
67 - else
68 - state_dir="$apply_dir"
69 + onto_name=$(cat "$state_dir"/onto_name) &&
70 + end=$(cat "$state_dir"/end) &&
71 + msgnum=$(cat "$state_dir"/msgnum)
72 fi &&
73 head_name=$(cat "$state_dir"/head-name) &&
74 onto=$(cat "$state_dir"/onto) &&
@@ -207,6 +210,23 @@ test -f "$apply_dir"/applying &&
210
211 is_interactive "$@" && exec git-rebase--interactive "$@"
212
213 +if test -d "$apply_dir"
214 +then
215 + type=am
216 + state_dir="$apply_dir"
217 +elif test -d "$merge_dir"
218 +then
219 + if test -f "$merge_dir"/interactive
220 + then
221 + type=interactive
222 + interactive_rebase=explicit
223 + else
224 + type=merge
225 + fi
226 + state_dir="$merge_dir"
227 +fi
228 +test -n "$type" && in_progress=t
229 +
230 while test $# != 0
231 do
232 case "$1" in
@@ -217,8 +237,7 @@ do
237 OK_TO_SKIP_PRE_REBASE=
238 ;;
239 --continue)
220 - test -d "$merge_dir" -o -d "$apply_dir" ||
221 - die "No rebase in progress?"
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 || {
@@ -243,8 +262,7 @@ do
262 exit
263 ;;
264 --skip)
246 - test -d "$merge_dir" -o -d "$apply_dir" ||
247 - die "No rebase in progress?"
265 + test -z "$in_progress" && die "No rebase in progress?"
266
267 git reset --hard HEAD || exit $?
268 read_state
@@ -265,8 +283,7 @@ do
283 exit
284 ;;
285 --abort)
268 - test -d "$merge_dir" -o -d "$apply_dir" ||
269 - die "No rebase in progress?"
286 + test -z "$in_progress" && die "No rebase in progress?"
287
288 git rerere clear
289 read_state
@@ -374,37 +391,22 @@ do
391 done
392 test $# -gt 2 && usage
393
377 -if test $# -eq 0 && test -z "$rebase_root"
394 +# Make sure no rebase is in progress
395 +if test -n "$in_progress"
396 then
379 - test -d "$merge_dir" -o -d "$apply_dir" || usage
380 - test -d "$merge_dir" -o -f "$apply_dir"/rebasing &&
381 - die 'A rebase is in progress, try --continue, --skip or --abort.'
382 -fi
383 -
384 -# Make sure we do not have $apply_dir or $merge_dir
385 -if test -z "$do_merge"
386 -then
387 - if mkdir "$apply_dir" 2>/dev/null
388 - then
389 - rmdir "$apply_dir"
390 - else
391 - echo >&2 '
392 -It seems that I cannot create a rebase-apply directory, and
393 -I wonder if you are in the middle of patch application or another
394 -rebase. If that is not the case, please
395 - rm -fr '"$apply_dir"'
397 + die '
398 +It seems that there is already a '"${state_dir##*/}"' directory, and
399 +I wonder if you are in the middle of another rebase. If that is the
400 +case, please try
401 + git rebase (--continue | --abort | --skip)
402 +If that is not the case, please
403 + rm -fr '"$state_dir"'
404 and run me again. I am stopping in case you still have something
405 valuable there.'
398 - exit 1
399 - fi
400 -else
401 - if test -d "$merge_dir"
402 - then
403 - die "previous rebase directory $merge_dir still exists." \
404 - 'Try git rebase (--continue | --abort | --skip)'
405 - fi
406 fi
407
408 +test $# -eq 0 && test -z "$rebase_root" && usage
409 +
410 require_clean_work_tree "rebase" "Please commit or stash them."
411
412 if test -z "$rebase_root"