@matheusbsbs / matheus-bagliano / commits / fa99c1e1e1
Fork 0

rebase: extract merge code to new source file

Extract the code for merge-based rebase to git-rebase--merge.sh. Suggested-by: Johannes Sixt <j6t@kdbg.org> 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 fa99c1e1e16d347a354c817d96c5abbd5a08fcd8
4 files changed +167 -151
.gitignore
+1
@@ -103,6 +103,7 @@
103 /git-read-tree
104 /git-rebase
105 /git-rebase--interactive
106 +/git-rebase--merge
107 /git-receive-pack
108 /git-reflog
109 /git-relink
Makefile
+1
@@ -370,6 +370,7 @@ SCRIPT_SH += git-mergetool.sh
370 SCRIPT_SH += git-pull.sh
371 SCRIPT_SH += git-quiltimport.sh
372 SCRIPT_SH += git-rebase--interactive.sh
373 +SCRIPT_SH += git-rebase--merge.sh
374 SCRIPT_SH += git-rebase.sh
375 SCRIPT_SH += git-repack.sh
376 SCRIPT_SH += git-request-pull.sh
git-rebase--merge.sh new
+154
@@ -0,0 +1,154 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2010 Junio C Hamano.
4 +#
5 +
6 +. git-sh-setup
7 +
8 +prec=4
9 +
10 +read_state () {
11 + onto_name=$(cat "$state_dir"/onto_name) &&
12 + end=$(cat "$state_dir"/end) &&
13 + msgnum=$(cat "$state_dir"/msgnum)
14 +}
15 +
16 +continue_merge () {
17 + test -d "$state_dir" || die "$state_dir directory does not exist"
18 +
19 + unmerged=$(git ls-files -u)
20 + if test -n "$unmerged"
21 + then
22 + echo "You still have unmerged paths in your index"
23 + echo "did you forget to use git add?"
24 + die "$resolvemsg"
25 + fi
26 +
27 + cmt=`cat "$state_dir/current"`
28 + if ! git diff-index --quiet --ignore-submodules HEAD --
29 + then
30 + if ! git commit --no-verify -C "$cmt"
31 + then
32 + echo "Commit failed, please do not call \"git commit\""
33 + echo "directly, but instead do one of the following: "
34 + die "$resolvemsg"
35 + fi
36 + if test -z "$GIT_QUIET"
37 + then
38 + printf "Committed: %0${prec}d " $msgnum
39 + fi
40 + echo "$cmt $(git rev-parse HEAD^0)" >> "$state_dir/rewritten"
41 + else
42 + if test -z "$GIT_QUIET"
43 + then
44 + printf "Already applied: %0${prec}d " $msgnum
45 + fi
46 + fi
47 + test -z "$GIT_QUIET" &&
48 + GIT_PAGER='' git log --format=%s -1 "$cmt"
49 +
50 + # onto the next patch:
51 + msgnum=$(($msgnum + 1))
52 + echo "$msgnum" >"$state_dir/msgnum"
53 +}
54 +
55 +call_merge () {
56 + cmt="$(cat "$state_dir/cmt.$1")"
57 + echo "$cmt" > "$state_dir/current"
58 + hd=$(git rev-parse --verify HEAD)
59 + cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
60 + msgnum=$(cat "$state_dir/msgnum")
61 + eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
62 + eval GITHEAD_$hd='$onto_name'
63 + export GITHEAD_$cmt GITHEAD_$hd
64 + if test -n "$GIT_QUIET"
65 + then
66 + GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
67 + fi
68 + test -z "$strategy" && strategy=recursive
69 + eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
70 + rv=$?
71 + case "$rv" in
72 + 0)
73 + unset GITHEAD_$cmt GITHEAD_$hd
74 + return
75 + ;;
76 + 1)
77 + git rerere $allow_rerere_autoupdate
78 + die "$resolvemsg"
79 + ;;
80 + 2)
81 + echo "Strategy: $rv $strategy failed, try another" 1>&2
82 + die "$resolvemsg"
83 + ;;
84 + *)
85 + die "Unknown exit code ($rv) from command:" \
86 + "git-merge-$strategy $cmt^ -- HEAD $cmt"
87 + ;;
88 + esac
89 +}
90 +
91 +finish_rb_merge () {
92 + move_to_original_branch
93 + git notes copy --for-rewrite=rebase < "$state_dir"/rewritten
94 + if test -x "$GIT_DIR"/hooks/post-rewrite &&
95 + test -s "$state_dir"/rewritten; then
96 + "$GIT_DIR"/hooks/post-rewrite rebase < "$state_dir"/rewritten
97 + fi
98 + rm -r "$state_dir"
99 + say All done.
100 +}
101 +
102 +case "$action" in
103 +continue)
104 + read_state
105 + continue_merge
106 + while test "$msgnum" -le "$end"
107 + do
108 + call_merge "$msgnum"
109 + continue_merge
110 + done
111 + finish_rb_merge
112 + exit
113 + ;;
114 +skip)
115 + read_state
116 + git rerere clear
117 + msgnum=$(($msgnum + 1))
118 + while test "$msgnum" -le "$end"
119 + do
120 + call_merge "$msgnum"
121 + continue_merge
122 + done
123 + finish_rb_merge
124 + exit
125 + ;;
126 +esac
127 +
128 +mkdir -p "$state_dir"
129 +echo "$onto_name" > "$state_dir/onto_name"
130 +echo "$head_name" > "$state_dir/head-name"
131 +echo "$onto" > "$state_dir/onto"
132 +echo "$orig_head" > "$state_dir/orig-head"
133 +echo "$GIT_QUIET" > "$state_dir/quiet"
134 +
135 +msgnum=0
136 +for cmt in `git rev-list --reverse --no-merges "$revisions"`
137 +do
138 + msgnum=$(($msgnum + 1))
139 + echo "$cmt" > "$state_dir/cmt.$msgnum"
140 +done
141 +
142 +echo 1 >"$state_dir/msgnum"
143 +echo $msgnum >"$state_dir/end"
144 +
145 +end=$msgnum
146 +msgnum=1
147 +
148 +while test "$msgnum" -le "$end"
149 +do
150 + call_merge "$msgnum"
151 + continue_merge
152 +done
153 +
154 +finish_rb_merge
git-rebase.sh
+11 -151
@@ -48,7 +48,6 @@ strategy_opts=
48 do_merge=
49 merge_dir="$GIT_DIR"/rebase-merge
50 apply_dir="$GIT_DIR"/rebase-apply
51 -prec=4
51 verbose=
52 diffstat=
53 test "$(git config --bool rebase.stat)" = true && diffstat=t
@@ -68,94 +67,13 @@ preserve_merges=
67 autosquash=
68 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
69
71 -read_state () {
72 - if test "$type" = merge
73 - then
74 - onto_name=$(cat "$state_dir"/onto_name) &&
75 - end=$(cat "$state_dir"/end) &&
76 - msgnum=$(cat "$state_dir"/msgnum)
77 - fi &&
70 +read_basic_state () {
71 head_name=$(cat "$state_dir"/head-name) &&
72 onto=$(cat "$state_dir"/onto) &&
73 orig_head=$(cat "$state_dir"/orig-head) &&
74 GIT_QUIET=$(cat "$state_dir"/quiet)
75 }
76
84 -continue_merge () {
85 - test -d "$merge_dir" || die "$merge_dir directory does not exist"
86 -
87 - unmerged=$(git ls-files -u)
88 - if test -n "$unmerged"
89 - then
90 - echo "You still have unmerged paths in your index"
91 - echo "did you forget to use git add?"
92 - die "$resolvemsg"
93 - fi
94 -
95 - cmt=`cat "$merge_dir/current"`
96 - if ! git diff-index --quiet --ignore-submodules HEAD --
97 - then
98 - if ! git commit --no-verify -C "$cmt"
99 - then
100 - echo "Commit failed, please do not call \"git commit\""
101 - echo "directly, but instead do one of the following: "
102 - die "$resolvemsg"
103 - fi
104 - if test -z "$GIT_QUIET"
105 - then
106 - printf "Committed: %0${prec}d " $msgnum
107 - fi
108 - echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
109 - else
110 - if test -z "$GIT_QUIET"
111 - then
112 - printf "Already applied: %0${prec}d " $msgnum
113 - fi
114 - fi
115 - test -z "$GIT_QUIET" &&
116 - GIT_PAGER='' git log --format=%s -1 "$cmt"
117 -
118 - # onto the next patch:
119 - msgnum=$(($msgnum + 1))
120 - echo "$msgnum" >"$merge_dir/msgnum"
121 -}
122 -
123 -call_merge () {
124 - cmt="$(cat "$merge_dir/cmt.$1")"
125 - echo "$cmt" > "$merge_dir/current"
126 - hd=$(git rev-parse --verify HEAD)
127 - cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
128 - msgnum=$(cat "$merge_dir/msgnum")
129 - eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
130 - eval GITHEAD_$hd='$onto_name'
131 - export GITHEAD_$cmt GITHEAD_$hd
132 - if test -n "$GIT_QUIET"
133 - then
134 - GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
135 - fi
136 - test -z "$strategy" && strategy=recursive
137 - eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
138 - rv=$?
139 - case "$rv" in
140 - 0)
141 - unset GITHEAD_$cmt GITHEAD_$hd
142 - return
143 - ;;
144 - 1)
145 - git rerere $allow_rerere_autoupdate
146 - die "$resolvemsg"
147 - ;;
148 - 2)
149 - echo "Strategy: $rv $strategy failed, try another" 1>&2
150 - die "$resolvemsg"
151 - ;;
152 - *)
153 - die "Unknown exit code ($rv) from command:" \
154 - "git-merge-$strategy $cmt^ -- HEAD $cmt"
155 - ;;
156 - esac
157 -}
158 -
77 move_to_original_branch () {
78 case "$head_name" in
79 refs/*)
@@ -168,23 +86,12 @@ move_to_original_branch () {
86 esac
87 }
88
171 -finish_rb_merge () {
172 - move_to_original_branch
173 - git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
174 - if test -x "$GIT_DIR"/hooks/post-rewrite &&
175 - test -s "$merge_dir"/rewritten; then
176 - "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
177 - fi
178 - rm -r "$merge_dir"
179 - say All done.
180 -}
181 -
182 -run_interactive_rebase () {
89 +run_specific_rebase () {
90 if [ "$interactive_rebase" = implied ]; then
91 GIT_EDITOR=:
92 export GIT_EDITOR
93 fi
187 - . git-rebase--interactive
94 + test "$type" != am && . git-rebase--$type
95 }
96
97 run_pre_rebase_hook () {
@@ -341,7 +248,7 @@ test $# -gt 2 && usage
248 if test -n "$action"
249 then
250 test -z "$in_progress" && die "No rebase in progress?"
344 - test "$type" = interactive && run_interactive_rebase
251 + test "$type" = interactive && run_specific_rebase
252 fi
253
254 case "$action" in
@@ -352,44 +259,23 @@ continue)
259 echo "mark them as resolved using git add"
260 exit 1
261 }
355 - read_state
356 - if test -d "$merge_dir"
357 - then
358 - continue_merge
359 - while test "$msgnum" -le "$end"
360 - do
361 - call_merge "$msgnum"
362 - continue_merge
363 - done
364 - finish_rb_merge
365 - exit
366 - fi
262 + read_basic_state
263 + run_specific_rebase
264 git am --resolved --3way --resolvemsg="$resolvemsg" &&
265 move_to_original_branch
266 exit
267 ;;
268 skip)
269 git reset --hard HEAD || exit $?
373 - read_state
374 - if test -d "$merge_dir"
375 - then
376 - git rerere clear
377 - msgnum=$(($msgnum + 1))
378 - while test "$msgnum" -le "$end"
379 - do
380 - call_merge "$msgnum"
381 - continue_merge
382 - done
383 - finish_rb_merge
384 - exit
385 - fi
270 + read_basic_state
271 + run_specific_rebase
272 git am -3 --skip --resolvemsg="$resolvemsg" &&
273 move_to_original_branch
274 exit
275 ;;
276 abort)
277 git rerere clear
392 - read_state
278 + read_basic_state
279 case "$head_name" in
280 refs/*)
281 git symbolic-ref HEAD $head_name ||
@@ -548,7 +434,7 @@ then
434 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
435 fi
436
551 -test "$type" = interactive && run_interactive_rebase
437 +test "$type" = interactive && run_specific_rebase
438
439 # Detach HEAD and reset the tree
440 say "First, rewinding head to replay your work on top of it..."
@@ -590,30 +476,4 @@ fi
476 # start doing a rebase with git-merge
477 # this is rename-aware if the recursive (default) strategy is used
478
593 -mkdir -p "$merge_dir"
594 -echo "$onto_name" > "$merge_dir/onto_name"
595 -echo "$head_name" > "$merge_dir/head-name"
596 -echo "$onto" > "$merge_dir/onto"
597 -echo "$orig_head" > "$merge_dir/orig-head"
598 -echo "$GIT_QUIET" > "$merge_dir/quiet"
599 -
600 -msgnum=0
601 -for cmt in `git rev-list --reverse --no-merges "$revisions"`
602 -do
603 - msgnum=$(($msgnum + 1))
604 - echo "$cmt" > "$merge_dir/cmt.$msgnum"
605 -done
606 -
607 -echo 1 >"$merge_dir/msgnum"
608 -echo $msgnum >"$merge_dir/end"
609 -
610 -end=$msgnum
611 -msgnum=1
612 -
613 -while test "$msgnum" -le "$end"
614 -do
615 - call_merge "$msgnum"
616 - continue_merge
617 -done
618 -
619 -finish_rb_merge
479 +run_specific_rebase