Skip to content

Commit 02ac45f

Browse files
Martin von Zweigbergkgitster
authored andcommitted
rebase: refactor reading of state
The code reading the state saved in $merge_dir or $rebase_dir is currently spread out in many places, making it harder to read and to introduce additional state. Extract this code into one method that reads the state. Only extract the code associated with the state that is written when the rebase is initiated. Leave the state that changes for each commmit, at least for now. Currently, when resuming a merge-based rebase using --continue or --skip, move_to_original_branch (via finish_rb_merge) will be called without head_name and orig_head set. These variables are then lazily read in move_to_original_branch if head_name is not set (together with onto, which is unnecessarily read again). Change this by always eagerly reading the state, for both am-based and merge-based rebase, in the --continue and --skip cases. Note that this does not change the behavior for am-based rebase, which read the state eagerly even before this commit. Reading the state eagerly means that part of the state will sometimes be read unnecessarily. One example is when the rebase is continued, but stops again at another merge conflict. Another example is when the rebase is aborted. However, since both of these cases involve user interaction, the delay is hopefully not noticeable. The call_merge/continue_merge loop is not affected. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 69a636a commit 02ac45f

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

git-rebase.sh

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ rebase_root=
5757
force_rebase=
5858
allow_rerere_autoupdate=
5959

60+
read_state () {
61+
if test -d "$merge_dir"
62+
then
63+
state_dir="$merge_dir"
64+
prev_head=$(cat "$merge_dir"/prev_head) &&
65+
end=$(cat "$merge_dir"/end) &&
66+
msgnum=$(cat "$merge_dir"/msgnum)
67+
else
68+
state_dir="$apply_dir"
69+
fi &&
70+
head_name=$(cat "$state_dir"/head-name) &&
71+
onto=$(cat "$state_dir"/onto) &&
72+
orig_head=$(cat "$state_dir"/orig-head) &&
73+
GIT_QUIET=$(cat "$state_dir"/quiet)
74+
}
75+
6076
continue_merge () {
6177
test -n "$prev_head" || die "prev_head must be defined"
6278
test -d "$merge_dir" || die "$merge_dir directory does not exist"
@@ -138,10 +154,6 @@ call_merge () {
138154
}
139155

140156
move_to_original_branch () {
141-
test -z "$head_name" &&
142-
head_name="$(cat "$merge_dir"/head-name)" &&
143-
onto="$(cat "$merge_dir"/onto)" &&
144-
orig_head="$(cat "$merge_dir"/orig-head)"
145157
case "$head_name" in
146158
refs/*)
147159
message="rebase finished: $head_name onto $onto"
@@ -220,13 +232,9 @@ do
220232
echo "mark them as resolved using git add"
221233
exit 1
222234
}
235+
read_state
223236
if test -d "$merge_dir"
224237
then
225-
prev_head=$(cat "$merge_dir/prev_head")
226-
end=$(cat "$merge_dir/end")
227-
msgnum=$(cat "$merge_dir/msgnum")
228-
onto=$(cat "$merge_dir/onto")
229-
GIT_QUIET=$(cat "$merge_dir/quiet")
230238
continue_merge
231239
while test "$msgnum" -le "$end"
232240
do
@@ -236,10 +244,6 @@ do
236244
finish_rb_merge
237245
exit
238246
fi
239-
head_name=$(cat "$apply_dir"/head-name) &&
240-
onto=$(cat "$apply_dir"/onto) &&
241-
orig_head=$(cat "$apply_dir"/orig-head) &&
242-
GIT_QUIET=$(cat "$apply_dir"/quiet)
243247
git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
244248
move_to_original_branch
245249
exit
@@ -249,15 +253,11 @@ do
249253
die "No rebase in progress?"
250254

251255
git reset --hard HEAD || exit $?
256+
read_state
252257
if test -d "$merge_dir"
253258
then
254259
git rerere clear
255-
prev_head=$(cat "$merge_dir/prev_head")
256-
end=$(cat "$merge_dir/end")
257-
msgnum=$(cat "$merge_dir/msgnum")
258260
msgnum=$(($msgnum + 1))
259-
onto=$(cat "$merge_dir/onto")
260-
GIT_QUIET=$(cat "$merge_dir/quiet")
261261
while test "$msgnum" -le "$end"
262262
do
263263
call_merge "$msgnum"
@@ -266,10 +266,6 @@ do
266266
finish_rb_merge
267267
exit
268268
fi
269-
head_name=$(cat "$apply_dir"/head-name) &&
270-
onto=$(cat "$apply_dir"/onto) &&
271-
orig_head=$(cat "$apply_dir"/orig-head) &&
272-
GIT_QUIET=$(cat "$apply_dir"/quiet)
273269
git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
274270
move_to_original_branch
275271
exit
@@ -279,18 +275,15 @@ do
279275
die "No rebase in progress?"
280276

281277
git rerere clear
282-
283-
test -d "$merge_dir" || merge_dir="$apply_dir"
284-
285-
head_name="$(cat "$merge_dir"/head-name)" &&
278+
read_state
286279
case "$head_name" in
287280
refs/*)
288281
git symbolic-ref HEAD $head_name ||
289282
die "Could not move back to $head_name"
290283
;;
291284
esac
292-
git reset --hard $(cat "$merge_dir/orig-head")
293-
rm -r "$merge_dir"
285+
git reset --hard $orig_head
286+
rm -r "$state_dir"
294287
exit
295288
;;
296289
--onto)
@@ -574,12 +567,12 @@ fi
574567
# this is rename-aware if the recursive (default) strategy is used
575568

576569
mkdir -p "$merge_dir"
577-
echo "$onto" > "$merge_dir/onto"
578570
echo "$onto_name" > "$merge_dir/onto_name"
579571
prev_head=$orig_head
580572
echo "$prev_head" > "$merge_dir/prev_head"
581-
echo "$orig_head" > "$merge_dir/orig-head"
582573
echo "$head_name" > "$merge_dir/head-name"
574+
echo "$onto" > "$merge_dir/onto"
575+
echo "$orig_head" > "$merge_dir/orig-head"
583576
echo "$GIT_QUIET" > "$merge_dir/quiet"
584577

585578
msgnum=0

0 commit comments

Comments
 (0)