]>
Commit | Line | Data |
---|---|---|
1b1dce4b JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
4 | ||
5 | # SHORT DESCRIPTION | |
6 | # | |
7 | # This script makes it easy to fix up commits in the middle of a series, | |
8 | # and rearrange commits. | |
9 | # | |
10 | # The original idea comes from Eric W. Biederman, in | |
11 | # http://article.gmane.org/gmane.comp.version-control.git/22407 | |
12 | ||
7970aaf0 SB |
13 | OPTIONS_KEEPDASHDASH= |
14 | OPTIONS_SPEC="\ | |
15 | git-rebase [-i] [options] [--] <upstream> [<branch>] | |
16 | git-rebase [-i] (--continue | --abort | --skip) | |
17 | -- | |
18 | Available options are | |
19 | v,verbose display a diffstat of what changed upstream | |
20 | onto= rebase onto given branch instead of upstream | |
21 | p,preserve-merges try to recreate merges instead of ignoring them | |
22 | s,strategy= use the given merge strategy | |
23 | m,merge always used (no-op) | |
24 | i,interactive always used (no-op) | |
25 | Actions: | |
26 | continue continue rebasing process | |
27 | abort abort rebasing process and restore original branch | |
28 | skip skip current patch and continue rebasing process | |
29 | " | |
1b1dce4b JS |
30 | |
31 | . git-sh-setup | |
32 | require_work_tree | |
33 | ||
28ed6e7b | 34 | DOTEST="$GIT_DIR/rebase-merge" |
c22486c9 | 35 | TODO="$DOTEST"/git-rebase-todo |
1b1dce4b | 36 | DONE="$DOTEST"/done |
6368f3f8 JS |
37 | MSG="$DOTEST"/message |
38 | SQUASH_MSG="$DOTEST"/message-squash | |
f09c9b8c JS |
39 | REWRITTEN="$DOTEST"/rewritten |
40 | PRESERVE_MERGES= | |
1b1dce4b | 41 | STRATEGY= |
7970aaf0 | 42 | ONTO= |
1b1dce4b JS |
43 | VERBOSE= |
44 | ||
804c7174 WC |
45 | GIT_CHERRY_PICK_HELP=" After resolving the conflicts, |
46 | mark the corrected paths with 'git add <paths>', and | |
47 | run 'git rebase --continue'" | |
48 | export GIT_CHERRY_PICK_HELP | |
49 | ||
1b1dce4b JS |
50 | warn () { |
51 | echo "$*" >&2 | |
52 | } | |
53 | ||
dfa49f33 JS |
54 | output () { |
55 | case "$VERBOSE" in | |
56 | '') | |
5166810b | 57 | output=$("$@" 2>&1 ) |
dfa49f33 | 58 | status=$? |
5166810b | 59 | test $status != 0 && printf "%s\n" "$output" |
dfa49f33 | 60 | return $status |
376ccb8c | 61 | ;; |
dfa49f33 JS |
62 | *) |
63 | "$@" | |
376ccb8c | 64 | ;; |
dfa49f33 JS |
65 | esac |
66 | } | |
67 | ||
1b1dce4b JS |
68 | require_clean_work_tree () { |
69 | # test if working tree is dirty | |
70 | git rev-parse --verify HEAD > /dev/null && | |
6848d58c JS |
71 | git update-index --ignore-submodules --refresh && |
72 | git diff-files --quiet --ignore-submodules && | |
73 | git diff-index --cached --quiet HEAD --ignore-submodules -- || | |
1b1dce4b JS |
74 | die "Working tree is dirty" |
75 | } | |
76 | ||
77 | ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION" | |
78 | ||
79 | comment_for_reflog () { | |
80 | case "$ORIG_REFLOG_ACTION" in | |
81 | ''|rebase*) | |
82 | GIT_REFLOG_ACTION="rebase -i ($1)" | |
83 | export GIT_REFLOG_ACTION | |
376ccb8c | 84 | ;; |
1b1dce4b JS |
85 | esac |
86 | } | |
87 | ||
4e673877 | 88 | last_count= |
1b1dce4b JS |
89 | mark_action_done () { |
90 | sed -e 1q < "$TODO" >> "$DONE" | |
91 | sed -e 1d < "$TODO" >> "$TODO".new | |
92 | mv -f "$TODO".new "$TODO" | |
aadbe44f JK |
93 | count=$(grep -c '^[^#]' < "$DONE") |
94 | total=$(($count+$(grep -c '^[^#]' < "$TODO"))) | |
4e673877 JH |
95 | if test "$last_count" != "$count" |
96 | then | |
97 | last_count=$count | |
98 | printf "Rebasing (%d/%d)\r" $count $total | |
99 | test -z "$VERBOSE" || echo | |
100 | fi | |
1b1dce4b JS |
101 | } |
102 | ||
103 | make_patch () { | |
be6ff208 JS |
104 | parent_sha1=$(git rev-parse --verify "$1"^) || |
105 | die "Cannot get patch for $1^" | |
f3d5e463 | 106 | git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch |
be6ff208 JS |
107 | test -f "$DOTEST"/message || |
108 | git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message | |
109 | test -f "$DOTEST"/author-script || | |
110 | get_author_ident_from_commit "$1" > "$DOTEST"/author-script | |
1b1dce4b JS |
111 | } |
112 | ||
113 | die_with_patch () { | |
114 | make_patch "$1" | |
ecfe72ff | 115 | git rerere |
1b1dce4b JS |
116 | die "$2" |
117 | } | |
118 | ||
c54b7817 JS |
119 | die_abort () { |
120 | rm -rf "$DOTEST" | |
121 | die "$1" | |
122 | } | |
123 | ||
376ccb8c | 124 | has_action () { |
aadbe44f | 125 | grep '^[^#]' "$1" >/dev/null |
376ccb8c JS |
126 | } |
127 | ||
1b1dce4b | 128 | pick_one () { |
1d25c8cf JS |
129 | no_ff= |
130 | case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac | |
dfa49f33 | 131 | output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1" |
f09c9b8c JS |
132 | test -d "$REWRITTEN" && |
133 | pick_one_preserving_merges "$@" && return | |
376ccb8c JS |
134 | parent_sha1=$(git rev-parse --verify $sha1^) || |
135 | die "Could not get the parent of $sha1" | |
1b1dce4b | 136 | current_sha1=$(git rev-parse --verify HEAD) |
2858028e | 137 | if test "$no_ff$current_sha1" = "$parent_sha1"; then |
dfa49f33 JS |
138 | output git reset --hard $sha1 |
139 | test "a$1" = a-n && output git reset --soft $current_sha1 | |
1b1dce4b | 140 | sha1=$(git rev-parse --short $sha1) |
dfa49f33 | 141 | output warn Fast forward to $sha1 |
1b1dce4b | 142 | else |
2a9c53e0 | 143 | output git cherry-pick "$@" |
1b1dce4b JS |
144 | fi |
145 | } | |
146 | ||
f09c9b8c | 147 | pick_one_preserving_merges () { |
71d9451e TR |
148 | fast_forward=t |
149 | case "$1" in | |
150 | -n) | |
151 | fast_forward=f | |
152 | sha1=$2 | |
153 | ;; | |
154 | *) | |
155 | sha1=$1 | |
156 | ;; | |
157 | esac | |
f09c9b8c JS |
158 | sha1=$(git rev-parse $sha1) |
159 | ||
3b38ec16 | 160 | if test -f "$DOTEST"/current-commit |
f09c9b8c JS |
161 | then |
162 | current_commit=$(cat "$DOTEST"/current-commit) && | |
163 | git rev-parse HEAD > "$REWRITTEN"/$current_commit && | |
164 | rm "$DOTEST"/current-commit || | |
165 | die "Cannot write current commit's replacement sha1" | |
166 | fi | |
167 | ||
a96dc01e TR |
168 | echo $sha1 > "$DOTEST"/current-commit |
169 | ||
f09c9b8c | 170 | # rewrite parents; if none were rewritten, we can fast-forward. |
f09c9b8c | 171 | new_parents= |
376ccb8c | 172 | for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) |
f09c9b8c | 173 | do |
3b38ec16 | 174 | if test -f "$REWRITTEN"/$p |
f09c9b8c | 175 | then |
f09c9b8c JS |
176 | new_p=$(cat "$REWRITTEN"/$p) |
177 | test $p != $new_p && fast_forward=f | |
178 | case "$new_parents" in | |
179 | *$new_p*) | |
180 | ;; # do nothing; that parent is already there | |
181 | *) | |
182 | new_parents="$new_parents $new_p" | |
376ccb8c | 183 | ;; |
f09c9b8c | 184 | esac |
1c5fa0a1 SB |
185 | else |
186 | new_parents="$new_parents $p" | |
f09c9b8c JS |
187 | fi |
188 | done | |
189 | case $fast_forward in | |
190 | t) | |
dfa49f33 | 191 | output warn "Fast forward to $sha1" |
71d9451e TR |
192 | output git reset --hard $sha1 || |
193 | die "Cannot fast forward to $sha1" | |
f09c9b8c JS |
194 | ;; |
195 | f) | |
196 | test "a$1" = a-n && die "Refusing to squash a merge: $sha1" | |
197 | ||
376ccb8c | 198 | first_parent=$(expr "$new_parents" : ' \([^ ]*\)') |
f09c9b8c | 199 | # detach HEAD to current parent |
dfa49f33 | 200 | output git checkout $first_parent 2> /dev/null || |
f09c9b8c JS |
201 | die "Cannot move HEAD to $first_parent" |
202 | ||
f09c9b8c | 203 | case "$new_parents" in |
376ccb8c | 204 | ' '*' '*) |
f09c9b8c JS |
205 | # redo merge |
206 | author_script=$(get_author_ident_from_commit $sha1) | |
207 | eval "$author_script" | |
376ccb8c | 208 | msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')" |
f91333d6 BS |
209 | # No point in merging the first parent, that's HEAD |
210 | new_parents=${new_parents# $first_parent} | |
ae830ed7 JS |
211 | if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
212 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
213 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
214 | output git merge $STRATEGY -m "$msg" \ | |
215 | $new_parents | |
f09c9b8c | 216 | then |
ecfe72ff | 217 | git rerere |
376ccb8c | 218 | printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG |
18640d99 | 219 | die Error redoing merge $sha1 |
f09c9b8c JS |
220 | fi |
221 | ;; | |
222 | *) | |
2a9c53e0 | 223 | output git cherry-pick "$@" || |
f09c9b8c | 224 | die_with_patch $sha1 "Could not pick $sha1" |
376ccb8c | 225 | ;; |
f09c9b8c | 226 | esac |
376ccb8c | 227 | ;; |
f09c9b8c JS |
228 | esac |
229 | } | |
230 | ||
6368f3f8 JS |
231 | nth_string () { |
232 | case "$1" in | |
233 | *1[0-9]|*[04-9]) echo "$1"th;; | |
234 | *1) echo "$1"st;; | |
235 | *2) echo "$1"nd;; | |
236 | *3) echo "$1"rd;; | |
237 | esac | |
238 | } | |
239 | ||
240 | make_squash_message () { | |
3b38ec16 | 241 | if test -f "$SQUASH_MSG"; then |
c7965afd | 242 | COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \ |
b4ce54fc | 243 | < "$SQUASH_MSG" | sed -ne '$p')+1)) |
6368f3f8 | 244 | echo "# This is a combination of $COUNT commits." |
8ad1065e JH |
245 | sed -e 1d -e '2,/^./{ |
246 | /^$/d | |
247 | }' <"$SQUASH_MSG" | |
6368f3f8 JS |
248 | else |
249 | COUNT=2 | |
250 | echo "# This is a combination of two commits." | |
251 | echo "# The first commit's message is:" | |
252 | echo | |
253 | git cat-file commit HEAD | sed -e '1,/^$/d' | |
6368f3f8 | 254 | fi |
8ad1065e | 255 | echo |
6368f3f8 JS |
256 | echo "# This is the $(nth_string $COUNT) commit message:" |
257 | echo | |
258 | git cat-file commit $1 | sed -e '1,/^$/d' | |
259 | } | |
260 | ||
261 | peek_next_command () { | |
262 | sed -n "1s/ .*$//p" < "$TODO" | |
263 | } | |
264 | ||
1b1dce4b | 265 | do_next () { |
376ccb8c JS |
266 | rm -f "$DOTEST"/message "$DOTEST"/author-script \ |
267 | "$DOTEST"/amend || exit | |
1b1dce4b JS |
268 | read command sha1 rest < "$TODO" |
269 | case "$command" in | |
376ccb8c | 270 | '#'*|'') |
1b1dce4b | 271 | mark_action_done |
1b1dce4b | 272 | ;; |
f8babc4d | 273 | pick|p) |
1b1dce4b JS |
274 | comment_for_reflog pick |
275 | ||
276 | mark_action_done | |
277 | pick_one $sha1 || | |
278 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
279 | ;; | |
f8babc4d | 280 | edit|e) |
1b1dce4b JS |
281 | comment_for_reflog edit |
282 | ||
283 | mark_action_done | |
284 | pick_one $sha1 || | |
285 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
286 | make_patch $sha1 | |
be6ff208 | 287 | : > "$DOTEST"/amend |
a8ccc204 | 288 | warn "Stopped at $sha1... $rest" |
1b1dce4b JS |
289 | warn "You can amend the commit now, with" |
290 | warn | |
291 | warn " git commit --amend" | |
292 | warn | |
0460fb44 JS |
293 | warn "Once you are satisfied with your changes, run" |
294 | warn | |
295 | warn " git rebase --continue" | |
296 | warn | |
1b1dce4b JS |
297 | exit 0 |
298 | ;; | |
f8babc4d | 299 | squash|s) |
1b1dce4b JS |
300 | comment_for_reflog squash |
301 | ||
376ccb8c | 302 | has_action "$DONE" || |
1b1dce4b JS |
303 | die "Cannot 'squash' without a previous commit" |
304 | ||
305 | mark_action_done | |
6368f3f8 JS |
306 | make_squash_message $sha1 > "$MSG" |
307 | case "$(peek_next_command)" in | |
f8babc4d | 308 | squash|s) |
6368f3f8 | 309 | EDIT_COMMIT= |
91e1ee77 | 310 | USE_OUTPUT=output |
6368f3f8 | 311 | cp "$MSG" "$SQUASH_MSG" |
376ccb8c | 312 | ;; |
6368f3f8 JS |
313 | *) |
314 | EDIT_COMMIT=-e | |
91e1ee77 | 315 | USE_OUTPUT= |
376ccb8c JS |
316 | rm -f "$SQUASH_MSG" || exit |
317 | ;; | |
6368f3f8 JS |
318 | esac |
319 | ||
793ad041 | 320 | failed=f |
81ab1cb4 | 321 | author_script=$(get_author_ident_from_commit HEAD) |
dfa49f33 | 322 | output git reset --soft HEAD^ |
fb47cfbd | 323 | pick_one -n $sha1 || failed=t |
18640d99 | 324 | echo "$author_script" > "$DOTEST"/author-script |
dbedf972 SP |
325 | if test $failed = f |
326 | then | |
1b1dce4b JS |
327 | # This is like --amend, but with a different message |
328 | eval "$author_script" | |
ae830ed7 JS |
329 | GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
330 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
331 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
dbedf972 SP |
332 | $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t |
333 | fi | |
334 | if test $failed = t | |
335 | then | |
1b1dce4b JS |
336 | cp "$MSG" "$GIT_DIR"/MERGE_MSG |
337 | warn | |
338 | warn "Could not apply $sha1... $rest" | |
1b1dce4b | 339 | die_with_patch $sha1 "" |
dbedf972 | 340 | fi |
1b1dce4b JS |
341 | ;; |
342 | *) | |
343 | warn "Unknown command: $command $sha1 $rest" | |
344 | die_with_patch $sha1 "Please fix this in the file $TODO." | |
376ccb8c | 345 | ;; |
1b1dce4b JS |
346 | esac |
347 | test -s "$TODO" && return | |
348 | ||
68a163c9 JS |
349 | comment_for_reflog finish && |
350 | HEADNAME=$(cat "$DOTEST"/head-name) && | |
351 | OLDHEAD=$(cat "$DOTEST"/head) && | |
352 | SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) && | |
3b38ec16 | 353 | if test -d "$REWRITTEN" |
f09c9b8c JS |
354 | then |
355 | test -f "$DOTEST"/current-commit && | |
356 | current_commit=$(cat "$DOTEST"/current-commit) && | |
357 | git rev-parse HEAD > "$REWRITTEN"/$current_commit | |
34454e85 JS |
358 | if test -f "$REWRITTEN"/$OLDHEAD |
359 | then | |
360 | NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD) | |
361 | else | |
362 | NEWHEAD=$OLDHEAD | |
363 | fi | |
f09c9b8c JS |
364 | else |
365 | NEWHEAD=$(git rev-parse HEAD) | |
366 | fi && | |
73697a0b JS |
367 | case $HEADNAME in |
368 | refs/*) | |
369 | message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" && | |
370 | git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD && | |
371 | git symbolic-ref HEAD $HEADNAME | |
372 | ;; | |
373 | esac && { | |
3df0a859 | 374 | test ! -f "$DOTEST"/verbose || |
f3d5e463 | 375 | git diff-tree --stat $(cat "$DOTEST"/head)..HEAD |
3df0a859 | 376 | } && |
1b1dce4b | 377 | rm -rf "$DOTEST" && |
73697a0b | 378 | git gc --auto && |
1b1dce4b JS |
379 | warn "Successfully rebased and updated $HEADNAME." |
380 | ||
381 | exit | |
382 | } | |
383 | ||
384 | do_rest () { | |
385 | while : | |
386 | do | |
387 | do_next | |
388 | done | |
1b1dce4b JS |
389 | } |
390 | ||
7970aaf0 SB |
391 | # check if no other options are set |
392 | is_standalone () { | |
393 | test $# -eq 2 -a "$2" = '--' && | |
394 | test -z "$ONTO" && | |
395 | test -z "$PRESERVE_MERGES" && | |
396 | test -z "$STRATEGY" && | |
397 | test -z "$VERBOSE" | |
398 | } | |
399 | ||
400 | get_saved_options () { | |
401 | test -d "$REWRITTEN" && PRESERVE_MERGES=t | |
402 | test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)" | |
403 | test -f "$DOTEST"/verbose && VERBOSE=t | |
404 | } | |
405 | ||
822f7c73 | 406 | while test $# != 0 |
1b1dce4b JS |
407 | do |
408 | case "$1" in | |
409 | --continue) | |
7970aaf0 SB |
410 | is_standalone "$@" || usage |
411 | get_saved_options | |
1b1dce4b JS |
412 | comment_for_reflog continue |
413 | ||
414 | test -d "$DOTEST" || die "No interactive rebase running" | |
415 | ||
ab119032 JH |
416 | # Sanity check |
417 | git rev-parse --verify HEAD >/dev/null || | |
418 | die "Cannot read HEAD" | |
6848d58c JS |
419 | git update-index --ignore-submodules --refresh && |
420 | git diff-files --quiet --ignore-submodules || | |
ab119032 JH |
421 | die "Working tree is dirty" |
422 | ||
423 | # do we have anything to commit? | |
6848d58c | 424 | if git diff-index --cached --quiet --ignore-submodules HEAD -- |
03270628 | 425 | then |
ab119032 JH |
426 | : Nothing to commit -- skip this |
427 | else | |
428 | . "$DOTEST"/author-script || | |
429 | die "Cannot find the author identity" | |
8beb1f33 | 430 | amend= |
ab119032 JH |
431 | if test -f "$DOTEST"/amend |
432 | then | |
8beb1f33 | 433 | amend=$(git rev-parse --verify HEAD) |
ab119032 JH |
434 | git reset --soft HEAD^ || |
435 | die "Cannot rewind the HEAD" | |
436 | fi | |
437 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE && | |
8beb1f33 DP |
438 | git commit --no-verify -F "$DOTEST"/message -e || { |
439 | test -n "$amend" && git reset --soft $amend | |
440 | die "Could not commit staged changes." | |
441 | } | |
03270628 | 442 | fi |
18640d99 | 443 | |
1b1dce4b JS |
444 | require_clean_work_tree |
445 | do_rest | |
446 | ;; | |
447 | --abort) | |
7970aaf0 SB |
448 | is_standalone "$@" || usage |
449 | get_saved_options | |
1b1dce4b JS |
450 | comment_for_reflog abort |
451 | ||
ecfe72ff | 452 | git rerere clear |
1b1dce4b JS |
453 | test -d "$DOTEST" || die "No interactive rebase running" |
454 | ||
455 | HEADNAME=$(cat "$DOTEST"/head-name) | |
456 | HEAD=$(cat "$DOTEST"/head) | |
73697a0b JS |
457 | case $HEADNAME in |
458 | refs/*) | |
459 | git symbolic-ref HEAD $HEADNAME | |
460 | ;; | |
461 | esac && | |
dfa49f33 | 462 | output git reset --hard $HEAD && |
1b1dce4b JS |
463 | rm -rf "$DOTEST" |
464 | exit | |
465 | ;; | |
466 | --skip) | |
7970aaf0 SB |
467 | is_standalone "$@" || usage |
468 | get_saved_options | |
1b1dce4b JS |
469 | comment_for_reflog skip |
470 | ||
ecfe72ff | 471 | git rerere clear |
1b1dce4b JS |
472 | test -d "$DOTEST" || die "No interactive rebase running" |
473 | ||
dfa49f33 | 474 | output git reset --hard && do_rest |
1b1dce4b | 475 | ;; |
7970aaf0 | 476 | -s) |
1b1dce4b JS |
477 | case "$#,$1" in |
478 | *,*=*) | |
b5e960b1 | 479 | STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;; |
1b1dce4b JS |
480 | 1,*) |
481 | usage ;; | |
482 | *) | |
483 | STRATEGY="-s $2" | |
484 | shift ;; | |
485 | esac | |
486 | ;; | |
7970aaf0 | 487 | -m) |
1b1dce4b JS |
488 | # we use merge anyway |
489 | ;; | |
7970aaf0 | 490 | -v) |
1b1dce4b JS |
491 | VERBOSE=t |
492 | ;; | |
7970aaf0 | 493 | -p) |
f09c9b8c JS |
494 | PRESERVE_MERGES=t |
495 | ;; | |
7970aaf0 | 496 | -i) |
1b1dce4b JS |
497 | # yeah, we know |
498 | ;; | |
7970aaf0 SB |
499 | --onto) |
500 | shift | |
501 | ONTO=$(git rev-parse --verify "$1") || | |
502 | die "Does not point to a valid commit: $1" | |
1b1dce4b | 503 | ;; |
7970aaf0 SB |
504 | --) |
505 | shift | |
506 | test $# -eq 1 -o $# -eq 2 || usage | |
1b1dce4b JS |
507 | test -d "$DOTEST" && |
508 | die "Interactive rebase already started" | |
509 | ||
510 | git var GIT_COMMITTER_IDENT >/dev/null || | |
511 | die "You need to set your committer info first" | |
512 | ||
513 | comment_for_reflog start | |
514 | ||
1b1dce4b JS |
515 | require_clean_work_tree |
516 | ||
69e66f55 JS |
517 | UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base" |
518 | test -z "$ONTO" && ONTO=$UPSTREAM | |
519 | ||
3b38ec16 | 520 | if test ! -z "$2" |
1b1dce4b | 521 | then |
dfa49f33 | 522 | output git show-ref --verify --quiet "refs/heads/$2" || |
1b1dce4b | 523 | die "Invalid branchname: $2" |
dfa49f33 | 524 | output git checkout "$2" || |
1b1dce4b JS |
525 | die "Could not checkout $2" |
526 | fi | |
527 | ||
528 | HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?" | |
5166810b MK |
529 | mkdir "$DOTEST" || die "Could not create temporary $DOTEST" |
530 | ||
1b1dce4b | 531 | : > "$DOTEST"/interactive || die "Could not mark as interactive" |
73697a0b JS |
532 | git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null || |
533 | echo "detached HEAD" > "$DOTEST"/head-name | |
1b1dce4b JS |
534 | |
535 | echo $HEAD > "$DOTEST"/head | |
536 | echo $UPSTREAM > "$DOTEST"/upstream | |
537 | echo $ONTO > "$DOTEST"/onto | |
8e4a91bd | 538 | test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy |
1b1dce4b | 539 | test t = "$VERBOSE" && : > "$DOTEST"/verbose |
3b38ec16 | 540 | if test t = "$PRESERVE_MERGES" |
f09c9b8c JS |
541 | then |
542 | # $REWRITTEN contains files for each commit that is | |
543 | # reachable by at least one merge base of $HEAD and | |
544 | # $UPSTREAM. They are not necessarily rewritten, but | |
545 | # their children might be. | |
546 | # This ensures that commits on merged, but otherwise | |
547 | # unrelated side branches are left alone. (Think "X" | |
548 | # in the man page's example.) | |
549 | mkdir "$REWRITTEN" && | |
550 | for c in $(git merge-base --all $HEAD $UPSTREAM) | |
551 | do | |
552 | echo $ONTO > "$REWRITTEN"/$c || | |
553 | die "Could not init rewritten commits" | |
554 | done | |
555 | MERGES_OPTION= | |
556 | else | |
557 | MERGES_OPTION=--no-merges | |
558 | fi | |
1b1dce4b | 559 | |
c54b7817 JS |
560 | SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM) |
561 | SHORTHEAD=$(git rev-parse --short $HEAD) | |
562 | SHORTONTO=$(git rev-parse --short $ONTO) | |
6047a234 JS |
563 | git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \ |
564 | --abbrev=7 --reverse --left-right --cherry-pick \ | |
565 | $UPSTREAM...$HEAD | \ | |
566 | sed -n "s/^>/pick /p" > "$TODO" | |
567 | cat >> "$TODO" << EOF | |
568 | ||
569 | # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO | |
1b1dce4b JS |
570 | # |
571 | # Commands: | |
88b1f0b8 MV |
572 | # p, pick = use commit |
573 | # e, edit = use commit, but stop for amending | |
574 | # s, squash = use commit, but meld into previous commit | |
82576ddb JS |
575 | # |
576 | # If you remove a line here THAT COMMIT WILL BE LOST. | |
6047a234 | 577 | # However, if you remove everything, the rebase will be aborted. |
82576ddb | 578 | # |
1b1dce4b | 579 | EOF |
1b1dce4b | 580 | |
376ccb8c | 581 | has_action "$TODO" || |
c54b7817 | 582 | die_abort "Nothing to do" |
1b1dce4b JS |
583 | |
584 | cp "$TODO" "$TODO".backup | |
ef0c2abf | 585 | git_editor "$TODO" || |
1b1dce4b JS |
586 | die "Could not execute editor" |
587 | ||
376ccb8c | 588 | has_action "$TODO" || |
c54b7817 JS |
589 | die_abort "Nothing to do" |
590 | ||
22e40795 | 591 | git update-ref ORIG_HEAD $HEAD |
dfa49f33 | 592 | output git checkout $ONTO && do_rest |
376ccb8c | 593 | ;; |
1b1dce4b JS |
594 | esac |
595 | shift | |
596 | done |