Merge branch 'jc/autogc'
[git/haiku.git] / git-rebase--interactive.sh
blob96d8b6f8db15ae34a435c9330bbc6d00d7073817
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 output=$("$@" 2>&1 )
40 status=$?
41 test $status != 0 && printf "%s\n" "$output"
42 return $status
45 "$@"
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
67 esac
70 mark_action_done () {
71 sed -e 1q < "$TODO" >> "$DONE"
72 sed -e 1d < "$TODO" >> "$TODO".new
73 mv -f "$TODO".new "$TODO"
74 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
75 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
76 printf "Rebasing (%d/%d)\r" $count $total
77 test -z "$VERBOSE" || echo
80 make_patch () {
81 parent_sha1=$(git rev-parse --verify "$1"^) ||
82 die "Cannot get patch for $1^"
83 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
84 test -f "$DOTEST"/message ||
85 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
86 test -f "$DOTEST"/author-script ||
87 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
90 die_with_patch () {
91 make_patch "$1"
92 die "$2"
95 die_abort () {
96 rm -rf "$DOTEST"
97 die "$1"
100 has_action () {
101 grep -vqe '^$' -e '^#' "$1"
104 pick_one () {
105 no_ff=
106 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
107 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
108 test -d "$REWRITTEN" &&
109 pick_one_preserving_merges "$@" && return
110 parent_sha1=$(git rev-parse --verify $sha1^) ||
111 die "Could not get the parent of $sha1"
112 current_sha1=$(git rev-parse --verify HEAD)
113 if test $no_ff$current_sha1 = $parent_sha1; then
114 output git reset --hard $sha1
115 test "a$1" = a-n && output git reset --soft $current_sha1
116 sha1=$(git rev-parse --short $sha1)
117 output warn Fast forward to $sha1
118 else
119 output git cherry-pick $STRATEGY "$@"
123 pick_one_preserving_merges () {
124 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
125 sha1=$(git rev-parse $sha1)
127 if test -f "$DOTEST"/current-commit
128 then
129 current_commit=$(cat "$DOTEST"/current-commit) &&
130 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
131 rm "$DOTEST"/current-commit ||
132 die "Cannot write current commit's replacement sha1"
135 # rewrite parents; if none were rewritten, we can fast-forward.
136 fast_forward=t
137 preserve=t
138 new_parents=
139 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
141 if test -f "$REWRITTEN"/$p
142 then
143 preserve=f
144 new_p=$(cat "$REWRITTEN"/$p)
145 test $p != $new_p && fast_forward=f
146 case "$new_parents" in
147 *$new_p*)
148 ;; # do nothing; that parent is already there
150 new_parents="$new_parents $new_p"
152 esac
154 done
155 case $fast_forward in
157 output warn "Fast forward to $sha1"
158 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
161 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
163 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
164 # detach HEAD to current parent
165 output git checkout $first_parent 2> /dev/null ||
166 die "Cannot move HEAD to $first_parent"
168 echo $sha1 > "$DOTEST"/current-commit
169 case "$new_parents" in
170 ' '*' '*)
171 # redo merge
172 author_script=$(get_author_ident_from_commit $sha1)
173 eval "$author_script"
174 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
175 # NEEDSWORK: give rerere a chance
176 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
177 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
178 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
179 output git merge $STRATEGY -m "$msg" \
180 $new_parents
181 then
182 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
183 die Error redoing merge $sha1
187 output git cherry-pick $STRATEGY "$@" ||
188 die_with_patch $sha1 "Could not pick $sha1"
190 esac
192 esac
195 nth_string () {
196 case "$1" in
197 *1[0-9]|*[04-9]) echo "$1"th;;
198 *1) echo "$1"st;;
199 *2) echo "$1"nd;;
200 *3) echo "$1"rd;;
201 esac
204 make_squash_message () {
205 if test -f "$SQUASH_MSG"; then
206 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
207 < "$SQUASH_MSG" | tail -n 1)+1))
208 echo "# This is a combination of $COUNT commits."
209 sed -n "2,\$p" < "$SQUASH_MSG"
210 else
211 COUNT=2
212 echo "# This is a combination of two commits."
213 echo "# The first commit's message is:"
214 echo
215 git cat-file commit HEAD | sed -e '1,/^$/d'
216 echo
218 echo "# This is the $(nth_string $COUNT) commit message:"
219 echo
220 git cat-file commit $1 | sed -e '1,/^$/d'
223 peek_next_command () {
224 sed -n "1s/ .*$//p" < "$TODO"
227 do_next () {
228 rm -f "$DOTEST"/message "$DOTEST"/author-script \
229 "$DOTEST"/amend || exit
230 read command sha1 rest < "$TODO"
231 case "$command" in
232 '#'*|'')
233 mark_action_done
235 pick|p)
236 comment_for_reflog pick
238 mark_action_done
239 pick_one $sha1 ||
240 die_with_patch $sha1 "Could not apply $sha1... $rest"
242 edit|e)
243 comment_for_reflog edit
245 mark_action_done
246 pick_one $sha1 ||
247 die_with_patch $sha1 "Could not apply $sha1... $rest"
248 make_patch $sha1
249 : > "$DOTEST"/amend
250 warn
251 warn "You can amend the commit now, with"
252 warn
253 warn " git commit --amend"
254 warn
255 exit 0
257 squash|s)
258 comment_for_reflog squash
260 has_action "$DONE" ||
261 die "Cannot 'squash' without a previous commit"
263 mark_action_done
264 make_squash_message $sha1 > "$MSG"
265 case "$(peek_next_command)" in
266 squash|s)
267 EDIT_COMMIT=
268 USE_OUTPUT=output
269 cp "$MSG" "$SQUASH_MSG"
272 EDIT_COMMIT=-e
273 USE_OUTPUT=
274 rm -f "$SQUASH_MSG" || exit
276 esac
278 failed=f
279 author_script=$(get_author_ident_from_commit HEAD)
280 output git reset --soft HEAD^
281 pick_one -n $sha1 || failed=t
282 echo "$author_script" > "$DOTEST"/author-script
283 case $failed in
285 # This is like --amend, but with a different message
286 eval "$author_script"
287 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
288 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
289 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
290 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
293 cp "$MSG" "$GIT_DIR"/MERGE_MSG
294 warn
295 warn "Could not apply $sha1... $rest"
296 die_with_patch $sha1 ""
298 esac
301 warn "Unknown command: $command $sha1 $rest"
302 die_with_patch $sha1 "Please fix this in the file $TODO."
304 esac
305 test -s "$TODO" && return
307 comment_for_reflog finish &&
308 HEADNAME=$(cat "$DOTEST"/head-name) &&
309 OLDHEAD=$(cat "$DOTEST"/head) &&
310 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
311 if test -d "$REWRITTEN"
312 then
313 test -f "$DOTEST"/current-commit &&
314 current_commit=$(cat "$DOTEST"/current-commit) &&
315 git rev-parse HEAD > "$REWRITTEN"/$current_commit
316 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
317 else
318 NEWHEAD=$(git rev-parse HEAD)
319 fi &&
320 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
321 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
322 git symbolic-ref HEAD $HEADNAME && {
323 test ! -f "$DOTEST"/verbose ||
324 git diff --stat $(cat "$DOTEST"/head)..HEAD
325 } &&
326 rm -rf "$DOTEST" &&
327 warn "Successfully rebased and updated $HEADNAME."
329 git gc --auto
331 exit
334 do_rest () {
335 while :
337 do_next
338 done
341 while test $# != 0
343 case "$1" in
344 --continue)
345 comment_for_reflog continue
347 test -d "$DOTEST" || die "No interactive rebase running"
349 # commit if necessary
350 git rev-parse --verify HEAD > /dev/null &&
351 git update-index --refresh &&
352 git diff-files --quiet &&
353 ! git diff-index --cached --quiet HEAD &&
354 . "$DOTEST"/author-script && {
355 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
356 } &&
357 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
358 git commit -F "$DOTEST"/message -e
360 require_clean_work_tree
361 do_rest
363 --abort)
364 comment_for_reflog abort
366 test -d "$DOTEST" || die "No interactive rebase running"
368 HEADNAME=$(cat "$DOTEST"/head-name)
369 HEAD=$(cat "$DOTEST"/head)
370 git symbolic-ref HEAD $HEADNAME &&
371 output git reset --hard $HEAD &&
372 rm -rf "$DOTEST"
373 exit
375 --skip)
376 comment_for_reflog skip
378 test -d "$DOTEST" || die "No interactive rebase running"
380 output git reset --hard && do_rest
382 -s|--strategy)
383 shift
384 case "$#,$1" in
385 *,*=*)
386 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
387 1,*)
388 usage ;;
390 STRATEGY="-s $2"
391 shift ;;
392 esac
394 --merge)
395 # we use merge anyway
397 -C*)
398 die "Interactive rebase uses merge, so $1 does not make sense"
400 -v|--verbose)
401 VERBOSE=t
403 -p|--preserve-merges)
404 PRESERVE_MERGES=t
406 -i|--interactive)
407 # yeah, we know
409 ''|-h)
410 usage
413 test -d "$DOTEST" &&
414 die "Interactive rebase already started"
416 git var GIT_COMMITTER_IDENT >/dev/null ||
417 die "You need to set your committer info first"
419 comment_for_reflog start
421 ONTO=
422 case "$1" in
423 --onto)
424 ONTO=$(git rev-parse --verify "$2") ||
425 die "Does not point to a valid commit: $2"
426 shift; shift
428 esac
430 require_clean_work_tree
432 if test ! -z "$2"
433 then
434 output git show-ref --verify --quiet "refs/heads/$2" ||
435 die "Invalid branchname: $2"
436 output git checkout "$2" ||
437 die "Could not checkout $2"
440 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
441 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
443 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
445 test -z "$ONTO" && ONTO=$UPSTREAM
447 : > "$DOTEST"/interactive || die "Could not mark as interactive"
448 git symbolic-ref HEAD > "$DOTEST"/head-name ||
449 die "Could not get HEAD"
451 echo $HEAD > "$DOTEST"/head
452 echo $UPSTREAM > "$DOTEST"/upstream
453 echo $ONTO > "$DOTEST"/onto
454 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
455 test t = "$VERBOSE" && : > "$DOTEST"/verbose
456 if test t = "$PRESERVE_MERGES"
457 then
458 # $REWRITTEN contains files for each commit that is
459 # reachable by at least one merge base of $HEAD and
460 # $UPSTREAM. They are not necessarily rewritten, but
461 # their children might be.
462 # This ensures that commits on merged, but otherwise
463 # unrelated side branches are left alone. (Think "X"
464 # in the man page's example.)
465 mkdir "$REWRITTEN" &&
466 for c in $(git merge-base --all $HEAD $UPSTREAM)
468 echo $ONTO > "$REWRITTEN"/$c ||
469 die "Could not init rewritten commits"
470 done
471 MERGES_OPTION=
472 else
473 MERGES_OPTION=--no-merges
476 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
477 SHORTHEAD=$(git rev-parse --short $HEAD)
478 SHORTONTO=$(git rev-parse --short $ONTO)
479 cat > "$TODO" << EOF
480 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
482 # Commands:
483 # pick = use commit
484 # edit = use commit, but stop for amending
485 # squash = use commit, but meld into previous commit
487 # If you remove a line here THAT COMMIT WILL BE LOST.
490 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
491 --abbrev=7 --reverse --left-right --cherry-pick \
492 $UPSTREAM...$HEAD | \
493 sed -n "s/^>/pick /p" >> "$TODO"
495 has_action "$TODO" ||
496 die_abort "Nothing to do"
498 cp "$TODO" "$TODO".backup
499 git_editor "$TODO" ||
500 die "Could not execute editor"
502 has_action "$TODO" ||
503 die_abort "Nothing to do"
505 output git checkout $ONTO && do_rest
507 esac
508 shift
509 done