@matheusbsbs / matheus-bagliano / commits / 02ac45fa62
Fork 0

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 <gitster@pobox.com> 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 02ac45fa628ef7c529e63bb99fe198704606e7f8
1 file changed +23 -30
git-rebase.sh
+23 -30
@@ -57,6 +57,22 @@ rebase_root=
57 force_rebase=
58 allow_rerere_autoupdate=
59
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 +
76 continue_merge () {
77 test -n "$prev_head" || die "prev_head must be defined"
78 test -d "$merge_dir" || die "$merge_dir directory does not exist"
@@ -138,10 +154,6 @@ call_merge () {
154 }
155
156 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)"
157 case "$head_name" in
158 refs/*)
159 message="rebase finished: $head_name onto $onto"
@@ -220,13 +232,9 @@ do
232 echo "mark them as resolved using git add"
233 exit 1
234 }
235 + read_state
236 if test -d "$merge_dir"
237 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")
238 continue_merge
239 while test "$msgnum" -le "$end"
240 do
@@ -236,10 +244,6 @@ do
244 finish_rb_merge
245 exit
246 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)
247 git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
248 move_to_original_branch
249 exit
@@ -249,15 +253,11 @@ do
253 die "No rebase in progress?"
254
255 git reset --hard HEAD || exit $?
256 + read_state
257 if test -d "$merge_dir"
258 then
259 git rerere clear
255 - prev_head=$(cat "$merge_dir/prev_head")
256 - end=$(cat "$merge_dir/end")
257 - msgnum=$(cat "$merge_dir/msgnum")
260 msgnum=$(($msgnum + 1))
259 - onto=$(cat "$merge_dir/onto")
260 - GIT_QUIET=$(cat "$merge_dir/quiet")
261 while test "$msgnum" -le "$end"
262 do
263 call_merge "$msgnum"
@@ -266,10 +266,6 @@ do
266 finish_rb_merge
267 exit
268 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)
269 git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
270 move_to_original_branch
271 exit
@@ -279,18 +275,15 @@ do
275 die "No rebase in progress?"
276
277 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
279 case "$head_name" in
280 refs/*)
281 git symbolic-ref HEAD $head_name ||
282 die "Could not move back to $head_name"
283 ;;
284 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"
287 exit
288 ;;
289 --onto)
@@ -574,12 +567,12 @@ fi
567 # this is rename-aware if the recursive (default) strategy is used
568
569 mkdir -p "$merge_dir"
577 -echo "$onto" > "$merge_dir/onto"
570 echo "$onto_name" > "$merge_dir/onto_name"
571 prev_head=$orig_head
572 echo "$prev_head" > "$merge_dir/prev_head"
581 -echo "$orig_head" > "$merge_dir/orig-head"
573 echo "$head_name" > "$merge_dir/head-name"
574 +echo "$onto" > "$merge_dir/onto"
575 +echo "$orig_head" > "$merge_dir/orig-head"
576 echo "$GIT_QUIET" > "$merge_dir/quiet"
577
578 msgnum=0