Skip to content

Commit 99de064

Browse files
Martin von Zweigbergkgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc4a4b7 commit 99de064

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

git-rebase.sh

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ git_am_opt=
5656
rebase_root=
5757
force_rebase=
5858
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=
5965

6066
read_state () {
61-
if test -d "$merge_dir"
67+
if test "$type" = merge
6268
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)
6972
fi &&
7073
head_name=$(cat "$state_dir"/head-name) &&
7174
onto=$(cat "$state_dir"/onto) &&
@@ -207,6 +210,23 @@ test -f "$apply_dir"/applying &&
207210

208211
is_interactive "$@" && exec git-rebase--interactive "$@"
209212

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+
210230
while test $# != 0
211231
do
212232
case "$1" in
@@ -217,8 +237,7 @@ do
217237
OK_TO_SKIP_PRE_REBASE=
218238
;;
219239
--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?"
222241

223242
git update-index --ignore-submodules --refresh &&
224243
git diff-files --quiet --ignore-submodules || {
@@ -243,8 +262,7 @@ do
243262
exit
244263
;;
245264
--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?"
248266

249267
git reset --hard HEAD || exit $?
250268
read_state
@@ -265,8 +283,7 @@ do
265283
exit
266284
;;
267285
--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?"
270287

271288
git rerere clear
272289
read_state
@@ -374,37 +391,22 @@ do
374391
done
375392
test $# -gt 2 && usage
376393

377-
if test $# -eq 0 && test -z "$rebase_root"
394+
# Make sure no rebase is in progress
395+
if test -n "$in_progress"
378396
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"'
396404
and run me again. I am stopping in case you still have something
397405
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
406406
fi
407407

408+
test $# -eq 0 && test -z "$rebase_root" && usage
409+
408410
require_clean_work_tree "rebase" "Please commit or stash them."
409411

410412
if test -z "$rebase_root"

0 commit comments

Comments
 (0)