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