]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
git-mailsplit: with maildirs not only process cur/, but also new/
[thirdparty/git.git] / git-rebase--interactive.sh
CommitLineData
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
f09c9b8c
JS
13USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
1b1dce4b
JS
15
16. git-sh-setup
17require_work_tree
18
19DOTEST="$GIT_DIR/.dotest-merge"
c22486c9 20TODO="$DOTEST"/git-rebase-todo
1b1dce4b 21DONE="$DOTEST"/done
6368f3f8
JS
22MSG="$DOTEST"/message
23SQUASH_MSG="$DOTEST"/message-squash
f09c9b8c
JS
24REWRITTEN="$DOTEST"/rewritten
25PRESERVE_MERGES=
1b1dce4b
JS
26STRATEGY=
27VERBOSE=
8e4a91bd
JS
28test -d "$REWRITTEN" && PRESERVE_MERGES=t
29test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
30test -f "$DOTEST"/verbose && VERBOSE=t
1b1dce4b
JS
31
32warn () {
33 echo "$*" >&2
34}
35
dfa49f33
JS
36output () {
37 case "$VERBOSE" in
38 '')
39 "$@" > "$DOTEST"/output 2>&1
40 status=$?
41 test $status != 0 &&
42 cat "$DOTEST"/output
43 return $status
44 ;;
45 *)
46 "$@"
47 esac
48}
49
1b1dce4b
JS
50require_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"
57}
58
59ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
60
61comment_for_reflog () {
62 case "$ORIG_REFLOG_ACTION" in
63 ''|rebase*)
64 GIT_REFLOG_ACTION="rebase -i ($1)"
65 export GIT_REFLOG_ACTION
66 esac
67}
68
69mark_action_done () {
70 sed -e 1q < "$TODO" >> "$DONE"
71 sed -e 1d < "$TODO" >> "$TODO".new
72 mv -f "$TODO".new "$TODO"
dfa49f33
JS
73 count=$(($(wc -l < "$DONE")))
74 total=$(($count+$(wc -l < "$TODO")))
75 printf "Rebasing (%d/%d)\r" $count $total
76 test -z "$VERBOSE" || echo
1b1dce4b
JS
77}
78
79make_patch () {
80 parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
f3d5e463 81 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
1b1dce4b
JS
82}
83
84die_with_patch () {
18640d99
JS
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
1b1dce4b
JS
89 make_patch "$1"
90 die "$2"
91}
92
c54b7817
JS
93die_abort () {
94 rm -rf "$DOTEST"
95 die "$1"
96}
97
1b1dce4b 98pick_one () {
1d25c8cf
JS
99 no_ff=
100 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
dfa49f33 101 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
f09c9b8c
JS
102 test -d "$REWRITTEN" &&
103 pick_one_preserving_merges "$@" && return
1b1dce4b
JS
104 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
105 current_sha1=$(git rev-parse --verify HEAD)
1d25c8cf 106 if test $no_ff$current_sha1 = $parent_sha1; then
dfa49f33
JS
107 output git reset --hard $sha1
108 test "a$1" = a-n && output git reset --soft $current_sha1
1b1dce4b 109 sha1=$(git rev-parse --short $sha1)
dfa49f33 110 output warn Fast forward to $sha1
1b1dce4b 111 else
2a9c53e0 112 output git cherry-pick "$@"
1b1dce4b
JS
113 fi
114}
115
f09c9b8c
JS
116pick_one_preserving_merges () {
117 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
118 sha1=$(git rev-parse $sha1)
119
3b38ec16 120 if test -f "$DOTEST"/current-commit
f09c9b8c
JS
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"
126 fi
127
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-)
133 do
3b38ec16 134 if test -f "$REWRITTEN"/$p
f09c9b8c
JS
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
142 *)
143 new_parents="$new_parents $new_p"
144 esac
145 fi
146 done
147 case $fast_forward in
148 t)
dfa49f33 149 output warn "Fast forward to $sha1"
f09c9b8c
JS
150 test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
151 ;;
152 f)
153 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
154
155 first_parent=$(expr "$new_parents" : " \([^ ]*\)")
156 # detach HEAD to current parent
dfa49f33 157 output git checkout $first_parent 2> /dev/null ||
f09c9b8c
JS
158 die "Cannot move HEAD to $first_parent"
159
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")"
f91333d6
BS
168 # No point in merging the first parent, that's HEAD
169 new_parents=${new_parents# $first_parent}
f09c9b8c 170 # NEEDSWORK: give rerere a chance
dfa49f33 171 if ! output git merge $STRATEGY -m "$msg" $new_parents
f09c9b8c
JS
172 then
173 echo "$msg" > "$GIT_DIR"/MERGE_MSG
18640d99 174 die Error redoing merge $sha1
f09c9b8c
JS
175 fi
176 ;;
177 *)
2a9c53e0 178 output git cherry-pick "$@" ||
f09c9b8c
JS
179 die_with_patch $sha1 "Could not pick $sha1"
180 esac
181 esac
182}
183
6368f3f8
JS
184nth_string () {
185 case "$1" in
186 *1[0-9]|*[04-9]) echo "$1"th;;
187 *1) echo "$1"st;;
188 *2) echo "$1"nd;;
189 *3) echo "$1"rd;;
190 esac
191}
192
193make_squash_message () {
3b38ec16 194 if test -f "$SQUASH_MSG"; then
c7965afd 195 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
6368f3f8
JS
196 < "$SQUASH_MSG" | tail -n 1)+1))
197 echo "# This is a combination of $COUNT commits."
198 sed -n "2,\$p" < "$SQUASH_MSG"
199 else
200 COUNT=2
201 echo "# This is a combination of two commits."
202 echo "# The first commit's message is:"
203 echo
204 git cat-file commit HEAD | sed -e '1,/^$/d'
205 echo
206 fi
207 echo "# This is the $(nth_string $COUNT) commit message:"
208 echo
209 git cat-file commit $1 | sed -e '1,/^$/d'
210}
211
212peek_next_command () {
213 sed -n "1s/ .*$//p" < "$TODO"
214}
215
1b1dce4b 216do_next () {
18640d99
JS
217 test -f "$DOTEST"/message && rm "$DOTEST"/message
218 test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
1b1dce4b
JS
219 read command sha1 rest < "$TODO"
220 case "$command" in
221 \#|'')
222 mark_action_done
1b1dce4b
JS
223 ;;
224 pick)
225 comment_for_reflog pick
226
227 mark_action_done
228 pick_one $sha1 ||
229 die_with_patch $sha1 "Could not apply $sha1... $rest"
230 ;;
231 edit)
232 comment_for_reflog edit
233
234 mark_action_done
235 pick_one $sha1 ||
236 die_with_patch $sha1 "Could not apply $sha1... $rest"
237 make_patch $sha1
238 warn
239 warn "You can amend the commit now, with"
240 warn
241 warn " git commit --amend"
242 warn
243 exit 0
244 ;;
245 squash)
246 comment_for_reflog squash
247
248 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
249 die "Cannot 'squash' without a previous commit"
250
251 mark_action_done
6368f3f8
JS
252 make_squash_message $sha1 > "$MSG"
253 case "$(peek_next_command)" in
254 squash)
255 EDIT_COMMIT=
91e1ee77 256 USE_OUTPUT=output
6368f3f8
JS
257 cp "$MSG" "$SQUASH_MSG"
258 ;;
259 *)
260 EDIT_COMMIT=-e
91e1ee77 261 USE_OUTPUT=
6368f3f8
JS
262 test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
263 esac
264
793ad041 265 failed=f
81ab1cb4 266 author_script=$(get_author_ident_from_commit HEAD)
dfa49f33 267 output git reset --soft HEAD^
fb47cfbd 268 pick_one -n $sha1 || failed=t
18640d99 269 echo "$author_script" > "$DOTEST"/author-script
1b1dce4b
JS
270 case $failed in
271 f)
272 # This is like --amend, but with a different message
273 eval "$author_script"
274 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
91e1ee77 275 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
1b1dce4b
JS
276 ;;
277 t)
278 cp "$MSG" "$GIT_DIR"/MERGE_MSG
279 warn
280 warn "Could not apply $sha1... $rest"
1b1dce4b
JS
281 die_with_patch $sha1 ""
282 esac
283 ;;
284 *)
285 warn "Unknown command: $command $sha1 $rest"
286 die_with_patch $sha1 "Please fix this in the file $TODO."
287 esac
288 test -s "$TODO" && return
289
68a163c9
JS
290 comment_for_reflog finish &&
291 HEADNAME=$(cat "$DOTEST"/head-name) &&
292 OLDHEAD=$(cat "$DOTEST"/head) &&
293 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
3b38ec16 294 if test -d "$REWRITTEN"
f09c9b8c
JS
295 then
296 test -f "$DOTEST"/current-commit &&
297 current_commit=$(cat "$DOTEST"/current-commit) &&
298 git rev-parse HEAD > "$REWRITTEN"/$current_commit
299 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
300 else
301 NEWHEAD=$(git rev-parse HEAD)
302 fi &&
68a163c9
JS
303 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
304 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
3df0a859
JS
305 git symbolic-ref HEAD $HEADNAME && {
306 test ! -f "$DOTEST"/verbose ||
f3d5e463 307 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
3df0a859 308 } &&
1b1dce4b
JS
309 rm -rf "$DOTEST" &&
310 warn "Successfully rebased and updated $HEADNAME."
311
312 exit
313}
314
315do_rest () {
316 while :
317 do
318 do_next
319 done
1b1dce4b
JS
320}
321
822f7c73 322while test $# != 0
1b1dce4b
JS
323do
324 case "$1" in
325 --continue)
326 comment_for_reflog continue
327
328 test -d "$DOTEST" || die "No interactive rebase running"
329
18640d99
JS
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
338
1b1dce4b
JS
339 require_clean_work_tree
340 do_rest
341 ;;
342 --abort)
343 comment_for_reflog abort
344
345 test -d "$DOTEST" || die "No interactive rebase running"
346
347 HEADNAME=$(cat "$DOTEST"/head-name)
348 HEAD=$(cat "$DOTEST"/head)
349 git symbolic-ref HEAD $HEADNAME &&
dfa49f33 350 output git reset --hard $HEAD &&
1b1dce4b
JS
351 rm -rf "$DOTEST"
352 exit
353 ;;
354 --skip)
355 comment_for_reflog skip
356
357 test -d "$DOTEST" || die "No interactive rebase running"
358
dfa49f33 359 output git reset --hard && do_rest
1b1dce4b
JS
360 ;;
361 -s|--strategy)
1b1dce4b
JS
362 case "$#,$1" in
363 *,*=*)
364 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
365 1,*)
366 usage ;;
367 *)
368 STRATEGY="-s $2"
369 shift ;;
370 esac
371 ;;
372 --merge)
373 # we use merge anyway
374 ;;
375 -C*)
376 die "Interactive rebase uses merge, so $1 does not make sense"
377 ;;
c54b7817 378 -v|--verbose)
1b1dce4b
JS
379 VERBOSE=t
380 ;;
f09c9b8c
JS
381 -p|--preserve-merges)
382 PRESERVE_MERGES=t
383 ;;
1b1dce4b
JS
384 -i|--interactive)
385 # yeah, we know
386 ;;
387 ''|-h)
388 usage
389 ;;
390 *)
391 test -d "$DOTEST" &&
392 die "Interactive rebase already started"
393
394 git var GIT_COMMITTER_IDENT >/dev/null ||
395 die "You need to set your committer info first"
396
397 comment_for_reflog start
398
399 ONTO=
400 case "$1" in
401 --onto)
402 ONTO=$(git rev-parse --verify "$2") ||
403 die "Does not point to a valid commit: $2"
404 shift; shift
405 ;;
406 esac
407
408 require_clean_work_tree
409
c9e65892 410 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
3b38ec16 411 if test ! -z "$2"
1b1dce4b 412 then
dfa49f33 413 output git show-ref --verify --quiet "refs/heads/$2" ||
1b1dce4b 414 die "Invalid branchname: $2"
dfa49f33 415 output git checkout "$2" ||
1b1dce4b
JS
416 die "Could not checkout $2"
417 fi
418
419 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
420 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
421
422 test -z "$ONTO" && ONTO=$UPSTREAM
423
1b1dce4b
JS
424 : > "$DOTEST"/interactive || die "Could not mark as interactive"
425 git symbolic-ref HEAD > "$DOTEST"/head-name ||
426 die "Could not get HEAD"
427
428 echo $HEAD > "$DOTEST"/head
429 echo $UPSTREAM > "$DOTEST"/upstream
430 echo $ONTO > "$DOTEST"/onto
8e4a91bd 431 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4b 432 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec16 433 if test t = "$PRESERVE_MERGES"
f09c9b8c
JS
434 then
435 # $REWRITTEN contains files for each commit that is
436 # reachable by at least one merge base of $HEAD and
437 # $UPSTREAM. They are not necessarily rewritten, but
438 # their children might be.
439 # This ensures that commits on merged, but otherwise
440 # unrelated side branches are left alone. (Think "X"
441 # in the man page's example.)
442 mkdir "$REWRITTEN" &&
443 for c in $(git merge-base --all $HEAD $UPSTREAM)
444 do
445 echo $ONTO > "$REWRITTEN"/$c ||
446 die "Could not init rewritten commits"
447 done
448 MERGES_OPTION=
449 else
450 MERGES_OPTION=--no-merges
451 fi
1b1dce4b 452
c54b7817
JS
453 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
454 SHORTHEAD=$(git rev-parse --short $HEAD)
455 SHORTONTO=$(git rev-parse --short $ONTO)
1b1dce4b 456 cat > "$TODO" << EOF
c54b7817 457# Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
1b1dce4b
JS
458#
459# Commands:
460# pick = use commit
461# edit = use commit, but stop for amending
462# squash = use commit, but meld into previous commit
82576ddb
JS
463#
464# If you remove a line here THAT COMMIT WILL BE LOST.
465#
1b1dce4b 466EOF
f09c9b8c 467 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
96ffe892
JS
468 --abbrev=7 --reverse --left-right --cherry-pick \
469 $UPSTREAM...$HEAD | \
470 sed -n "s/^>/pick /p" >> "$TODO"
1b1dce4b
JS
471
472 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
c54b7817 473 die_abort "Nothing to do"
1b1dce4b
JS
474
475 cp "$TODO" "$TODO".backup
ef0c2abf 476 git_editor "$TODO" ||
1b1dce4b
JS
477 die "Could not execute editor"
478
c54b7817
JS
479 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
480 die_abort "Nothing to do"
481
dfa49f33 482 output git checkout $ONTO && do_rest
1b1dce4b
JS
483 esac
484 shift
485done