git-gc --auto: add documentation.
[alt-git.git] / git-rebase--interactive.sh
blob8258b7adf97d2bc4378d3b92f66e83b383669414
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
16 . git-sh-setup
17 require_work_tree
19 DOTEST="$GIT_DIR/.dotest-merge"
20 TODO="$DOTEST"/git-rebase-todo
21 DONE="$DOTEST"/done
22 MSG="$DOTEST"/message
23 SQUASH_MSG="$DOTEST"/message-squash
24 REWRITTEN="$DOTEST"/rewritten
25 PRESERVE_MERGES=
26 STRATEGY=
27 VERBOSE=
28 test -d "$REWRITTEN" && PRESERVE_MERGES=t
29 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
30 test -f "$DOTEST"/verbose && VERBOSE=t
32 warn () {
33 echo "$*" >&2
36 output () {
37 case "$VERBOSE" in
38 '')
39 "$@" > "$DOTEST"/output 2>&1
40 status=$?
41 test $status != 0 &&
42 cat "$DOTEST"/output
43 return $status
46 "$@"
47 esac
50 require_clean_work_tree () {
51 # test if working tree is dirty
52 git rev-parse --verify HEAD > /dev/null &&
53 git update-index --refresh &&
54 git diff-files --quiet &&
55 git diff-index --cached --quiet HEAD ||
56 die "Working tree is dirty"
59 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
61 comment_for_reflog () {
62 case "$ORIG_REFLOG_ACTION" in
63 ''|rebase*)
64 GIT_REFLOG_ACTION="rebase -i ($1)"
65 export GIT_REFLOG_ACTION
66 esac
69 mark_action_done () {
70 sed -e 1q < "$TODO" >> "$DONE"
71 sed -e 1d < "$TODO" >> "$TODO".new
72 mv -f "$TODO".new "$TODO"
73 count=$(($(wc -l < "$DONE")))
74 total=$(($count+$(wc -l < "$TODO")))
75 printf "Rebasing (%d/%d)\r" $count $total
76 test -z "$VERBOSE" || echo
79 make_patch () {
80 parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
81 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
84 die_with_patch () {
85 test -f "$DOTEST"/message ||
86 git cat-file commit $sha1 | sed "1,/^$/d" > "$DOTEST"/message
87 test -f "$DOTEST"/author-script ||
88 get_author_ident_from_commit $sha1 > "$DOTEST"/author-script
89 make_patch "$1"
90 die "$2"
93 die_abort () {
94 rm -rf "$DOTEST"
95 die "$1"
98 pick_one () {
99 no_ff=
100 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
101 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
102 test -d "$REWRITTEN" &&
103 pick_one_preserving_merges "$@" && return
104 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
105 current_sha1=$(git rev-parse --verify HEAD)
106 if test $no_ff$current_sha1 = $parent_sha1; then
107 output git reset --hard $sha1
108 test "a$1" = a-n && output git reset --soft $current_sha1
109 sha1=$(git rev-parse --short $sha1)
110 output warn Fast forward to $sha1
111 else
112 output git cherry-pick $STRATEGY "$@"
116 pick_one_preserving_merges () {
117 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
118 sha1=$(git rev-parse $sha1)
120 if test -f "$DOTEST"/current-commit
121 then
122 current_commit=$(cat "$DOTEST"/current-commit) &&
123 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
124 rm "$DOTEST"/current-commit ||
125 die "Cannot write current commit's replacement sha1"
128 # rewrite parents; if none were rewritten, we can fast-forward.
129 fast_forward=t
130 preserve=t
131 new_parents=
132 for p in $(git rev-list --parents -1 $sha1 | cut -d\ -f2-)
134 if test -f "$REWRITTEN"/$p
135 then
136 preserve=f
137 new_p=$(cat "$REWRITTEN"/$p)
138 test $p != $new_p && fast_forward=f
139 case "$new_parents" in
140 *$new_p*)
141 ;; # do nothing; that parent is already there
143 new_parents="$new_parents $new_p"
144 esac
146 done
147 case $fast_forward in
149 output warn "Fast forward to $sha1"
150 test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
153 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
155 first_parent=$(expr "$new_parents" : " \([^ ]*\)")
156 # detach HEAD to current parent
157 output git checkout $first_parent 2> /dev/null ||
158 die "Cannot move HEAD to $first_parent"
160 echo $sha1 > "$DOTEST"/current-commit
161 case "$new_parents" in
162 \ *\ *)
163 # redo merge
164 author_script=$(get_author_ident_from_commit $sha1)
165 eval "$author_script"
166 msg="$(git cat-file commit $sha1 | \
167 sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
168 # NEEDSWORK: give rerere a chance
169 if ! output git merge $STRATEGY -m "$msg" $new_parents
170 then
171 echo "$msg" > "$GIT_DIR"/MERGE_MSG
172 die Error redoing merge $sha1
176 output git cherry-pick $STRATEGY "$@" ||
177 die_with_patch $sha1 "Could not pick $sha1"
178 esac
179 esac
182 nth_string () {
183 case "$1" in
184 *1[0-9]|*[04-9]) echo "$1"th;;
185 *1) echo "$1"st;;
186 *2) echo "$1"nd;;
187 *3) echo "$1"rd;;
188 esac
191 make_squash_message () {
192 if test -f "$SQUASH_MSG"; then
193 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
194 < "$SQUASH_MSG" | tail -n 1)+1))
195 echo "# This is a combination of $COUNT commits."
196 sed -n "2,\$p" < "$SQUASH_MSG"
197 else
198 COUNT=2
199 echo "# This is a combination of two commits."
200 echo "# The first commit's message is:"
201 echo
202 git cat-file commit HEAD | sed -e '1,/^$/d'
203 echo
205 echo "# This is the $(nth_string $COUNT) commit message:"
206 echo
207 git cat-file commit $1 | sed -e '1,/^$/d'
210 peek_next_command () {
211 sed -n "1s/ .*$//p" < "$TODO"
214 do_next () {
215 test -f "$DOTEST"/message && rm "$DOTEST"/message
216 test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
217 read command sha1 rest < "$TODO"
218 case "$command" in
219 \#|'')
220 mark_action_done
222 pick)
223 comment_for_reflog pick
225 mark_action_done
226 pick_one $sha1 ||
227 die_with_patch $sha1 "Could not apply $sha1... $rest"
229 edit)
230 comment_for_reflog edit
232 mark_action_done
233 pick_one $sha1 ||
234 die_with_patch $sha1 "Could not apply $sha1... $rest"
235 make_patch $sha1
236 warn
237 warn "You can amend the commit now, with"
238 warn
239 warn " git commit --amend"
240 warn
241 exit 0
243 squash)
244 comment_for_reflog squash
246 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
247 die "Cannot 'squash' without a previous commit"
249 mark_action_done
250 make_squash_message $sha1 > "$MSG"
251 case "$(peek_next_command)" in
252 squash)
253 EDIT_COMMIT=
254 USE_OUTPUT=output
255 cp "$MSG" "$SQUASH_MSG"
258 EDIT_COMMIT=-e
259 USE_OUTPUT=
260 test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
261 esac
263 failed=f
264 output git reset --soft HEAD^
265 pick_one -n $sha1 || failed=t
266 author_script=$(get_author_ident_from_commit $sha1)
267 echo "$author_script" > "$DOTEST"/author-script
268 case $failed in
270 # This is like --amend, but with a different message
271 eval "$author_script"
272 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
273 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
276 cp "$MSG" "$GIT_DIR"/MERGE_MSG
277 warn
278 warn "Could not apply $sha1... $rest"
279 die_with_patch $sha1 ""
280 esac
283 warn "Unknown command: $command $sha1 $rest"
284 die_with_patch $sha1 "Please fix this in the file $TODO."
285 esac
286 test -s "$TODO" && return
288 comment_for_reflog finish &&
289 HEADNAME=$(cat "$DOTEST"/head-name) &&
290 OLDHEAD=$(cat "$DOTEST"/head) &&
291 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
292 if test -d "$REWRITTEN"
293 then
294 test -f "$DOTEST"/current-commit &&
295 current_commit=$(cat "$DOTEST"/current-commit) &&
296 git rev-parse HEAD > "$REWRITTEN"/$current_commit
297 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
298 else
299 NEWHEAD=$(git rev-parse HEAD)
300 fi &&
301 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
302 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
303 git symbolic-ref HEAD $HEADNAME && {
304 test ! -f "$DOTEST"/verbose ||
305 git diff --stat $(cat "$DOTEST"/head)..HEAD
306 } &&
307 rm -rf "$DOTEST" &&
308 warn "Successfully rebased and updated $HEADNAME."
310 git gc --auto
312 exit
315 do_rest () {
316 while :
318 do_next
319 done
322 while case $# in 0) break ;; esac
324 case "$1" in
325 --continue)
326 comment_for_reflog continue
328 test -d "$DOTEST" || die "No interactive rebase running"
330 # commit if necessary
331 git rev-parse --verify HEAD > /dev/null &&
332 git update-index --refresh &&
333 git diff-files --quiet &&
334 ! git diff-index --cached --quiet HEAD &&
335 . "$DOTEST"/author-script &&
336 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
337 git commit -F "$DOTEST"/message -e
339 require_clean_work_tree
340 do_rest
342 --abort)
343 comment_for_reflog abort
345 test -d "$DOTEST" || die "No interactive rebase running"
347 HEADNAME=$(cat "$DOTEST"/head-name)
348 HEAD=$(cat "$DOTEST"/head)
349 git symbolic-ref HEAD $HEADNAME &&
350 output git reset --hard $HEAD &&
351 rm -rf "$DOTEST"
352 exit
354 --skip)
355 comment_for_reflog skip
357 test -d "$DOTEST" || die "No interactive rebase running"
359 output git reset --hard && do_rest
361 -s|--strategy)
362 shift
363 case "$#,$1" in
364 *,*=*)
365 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
366 1,*)
367 usage ;;
369 STRATEGY="-s $2"
370 shift ;;
371 esac
373 --merge)
374 # we use merge anyway
376 -C*)
377 die "Interactive rebase uses merge, so $1 does not make sense"
379 -v|--verbose)
380 VERBOSE=t
382 -p|--preserve-merges)
383 PRESERVE_MERGES=t
385 -i|--interactive)
386 # yeah, we know
388 ''|-h)
389 usage
392 test -d "$DOTEST" &&
393 die "Interactive rebase already started"
395 git var GIT_COMMITTER_IDENT >/dev/null ||
396 die "You need to set your committer info first"
398 comment_for_reflog start
400 ONTO=
401 case "$1" in
402 --onto)
403 ONTO=$(git rev-parse --verify "$2") ||
404 die "Does not point to a valid commit: $2"
405 shift; shift
407 esac
409 require_clean_work_tree
411 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
412 if test ! -z "$2"
413 then
414 output git show-ref --verify --quiet "refs/heads/$2" ||
415 die "Invalid branchname: $2"
416 output git checkout "$2" ||
417 die "Could not checkout $2"
420 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
421 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
423 test -z "$ONTO" && ONTO=$UPSTREAM
425 : > "$DOTEST"/interactive || die "Could not mark as interactive"
426 git symbolic-ref HEAD > "$DOTEST"/head-name ||
427 die "Could not get HEAD"
429 echo $HEAD > "$DOTEST"/head
430 echo $UPSTREAM > "$DOTEST"/upstream
431 echo $ONTO > "$DOTEST"/onto
432 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
433 test t = "$VERBOSE" && : > "$DOTEST"/verbose
434 if test t = "$PRESERVE_MERGES"
435 then
436 # $REWRITTEN contains files for each commit that is
437 # reachable by at least one merge base of $HEAD and
438 # $UPSTREAM. They are not necessarily rewritten, but
439 # their children might be.
440 # This ensures that commits on merged, but otherwise
441 # unrelated side branches are left alone. (Think "X"
442 # in the man page's example.)
443 mkdir "$REWRITTEN" &&
444 for c in $(git merge-base --all $HEAD $UPSTREAM)
446 echo $ONTO > "$REWRITTEN"/$c ||
447 die "Could not init rewritten commits"
448 done
449 MERGES_OPTION=
450 else
451 MERGES_OPTION=--no-merges
454 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
455 SHORTHEAD=$(git rev-parse --short $HEAD)
456 SHORTONTO=$(git rev-parse --short $ONTO)
457 cat > "$TODO" << EOF
458 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
460 # Commands:
461 # pick = use commit
462 # edit = use commit, but stop for amending
463 # squash = use commit, but meld into previous commit
465 # If you remove a line here THAT COMMIT WILL BE LOST.
468 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
469 --abbrev=7 --reverse --left-right --cherry-pick \
470 $UPSTREAM...$HEAD | \
471 sed -n "s/^>/pick /p" >> "$TODO"
473 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
474 die_abort "Nothing to do"
476 cp "$TODO" "$TODO".backup
477 git_editor "$TODO" ||
478 die "Could not execute editor"
480 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
481 die_abort "Nothing to do"
483 output git checkout $ONTO && do_rest
484 esac
485 shift
486 done