]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
Do not remove distributed configure script
[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)
81 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
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
dfa49f33 112 output git cherry-pick $STRATEGY "$@"
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")"
168 # NEEDSWORK: give rerere a chance
dfa49f33 169 if ! output git merge $STRATEGY -m "$msg" $new_parents
f09c9b8c
JS
170 then
171 echo "$msg" > "$GIT_DIR"/MERGE_MSG
18640d99 172 die Error redoing merge $sha1
f09c9b8c
JS
173 fi
174 ;;
175 *)
dfa49f33 176 output git cherry-pick $STRATEGY "$@" ||
f09c9b8c
JS
177 die_with_patch $sha1 "Could not pick $sha1"
178 esac
179 esac
180}
181
6368f3f8
JS
182nth_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
189}
190
191make_squash_message () {
3b38ec16 192 if test -f "$SQUASH_MSG"; then
c7965afd 193 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
6368f3f8
JS
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
204 fi
205 echo "# This is the $(nth_string $COUNT) commit message:"
206 echo
207 git cat-file commit $1 | sed -e '1,/^$/d'
208}
209
210peek_next_command () {
211 sed -n "1s/ .*$//p" < "$TODO"
212}
213
1b1dce4b 214do_next () {
18640d99
JS
215 test -f "$DOTEST"/message && rm "$DOTEST"/message
216 test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
1b1dce4b
JS
217 read command sha1 rest < "$TODO"
218 case "$command" in
219 \#|'')
220 mark_action_done
1b1dce4b
JS
221 ;;
222 pick)
223 comment_for_reflog pick
224
225 mark_action_done
226 pick_one $sha1 ||
227 die_with_patch $sha1 "Could not apply $sha1... $rest"
228 ;;
229 edit)
230 comment_for_reflog edit
231
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
242 ;;
243 squash)
244 comment_for_reflog squash
245
246 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
247 die "Cannot 'squash' without a previous commit"
248
249 mark_action_done
6368f3f8
JS
250 make_squash_message $sha1 > "$MSG"
251 case "$(peek_next_command)" in
252 squash)
253 EDIT_COMMIT=
91e1ee77 254 USE_OUTPUT=output
6368f3f8
JS
255 cp "$MSG" "$SQUASH_MSG"
256 ;;
257 *)
258 EDIT_COMMIT=-e
91e1ee77 259 USE_OUTPUT=
6368f3f8
JS
260 test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
261 esac
262
793ad041 263 failed=f
81ab1cb4 264 author_script=$(get_author_ident_from_commit HEAD)
dfa49f33 265 output git reset --soft HEAD^
fb47cfbd 266 pick_one -n $sha1 || failed=t
18640d99 267 echo "$author_script" > "$DOTEST"/author-script
1b1dce4b
JS
268 case $failed in
269 f)
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
91e1ee77 273 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
1b1dce4b
JS
274 ;;
275 t)
276 cp "$MSG" "$GIT_DIR"/MERGE_MSG
277 warn
278 warn "Could not apply $sha1... $rest"
1b1dce4b
JS
279 die_with_patch $sha1 ""
280 esac
281 ;;
282 *)
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
287
68a163c9
JS
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)) &&
3b38ec16 292 if test -d "$REWRITTEN"
f09c9b8c
JS
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 &&
68a163c9
JS
301 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
302 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
3df0a859
JS
303 git symbolic-ref HEAD $HEADNAME && {
304 test ! -f "$DOTEST"/verbose ||
305 git diff --stat $(cat "$DOTEST"/head)..HEAD
306 } &&
1b1dce4b
JS
307 rm -rf "$DOTEST" &&
308 warn "Successfully rebased and updated $HEADNAME."
309
310 exit
311}
312
313do_rest () {
314 while :
315 do
316 do_next
317 done
1b1dce4b
JS
318}
319
822f7c73 320while test $# != 0
1b1dce4b
JS
321do
322 case "$1" in
323 --continue)
324 comment_for_reflog continue
325
326 test -d "$DOTEST" || die "No interactive rebase running"
327
18640d99
JS
328 # commit if necessary
329 git rev-parse --verify HEAD > /dev/null &&
330 git update-index --refresh &&
331 git diff-files --quiet &&
332 ! git diff-index --cached --quiet HEAD &&
333 . "$DOTEST"/author-script &&
334 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
335 git commit -F "$DOTEST"/message -e
336
1b1dce4b
JS
337 require_clean_work_tree
338 do_rest
339 ;;
340 --abort)
341 comment_for_reflog abort
342
343 test -d "$DOTEST" || die "No interactive rebase running"
344
345 HEADNAME=$(cat "$DOTEST"/head-name)
346 HEAD=$(cat "$DOTEST"/head)
347 git symbolic-ref HEAD $HEADNAME &&
dfa49f33 348 output git reset --hard $HEAD &&
1b1dce4b
JS
349 rm -rf "$DOTEST"
350 exit
351 ;;
352 --skip)
353 comment_for_reflog skip
354
355 test -d "$DOTEST" || die "No interactive rebase running"
356
dfa49f33 357 output git reset --hard && do_rest
1b1dce4b
JS
358 ;;
359 -s|--strategy)
360 shift
361 case "$#,$1" in
362 *,*=*)
363 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
364 1,*)
365 usage ;;
366 *)
367 STRATEGY="-s $2"
368 shift ;;
369 esac
370 ;;
371 --merge)
372 # we use merge anyway
373 ;;
374 -C*)
375 die "Interactive rebase uses merge, so $1 does not make sense"
376 ;;
c54b7817 377 -v|--verbose)
1b1dce4b
JS
378 VERBOSE=t
379 ;;
f09c9b8c
JS
380 -p|--preserve-merges)
381 PRESERVE_MERGES=t
382 ;;
1b1dce4b
JS
383 -i|--interactive)
384 # yeah, we know
385 ;;
386 ''|-h)
387 usage
388 ;;
389 *)
390 test -d "$DOTEST" &&
391 die "Interactive rebase already started"
392
393 git var GIT_COMMITTER_IDENT >/dev/null ||
394 die "You need to set your committer info first"
395
396 comment_for_reflog start
397
398 ONTO=
399 case "$1" in
400 --onto)
401 ONTO=$(git rev-parse --verify "$2") ||
402 die "Does not point to a valid commit: $2"
403 shift; shift
404 ;;
405 esac
406
407 require_clean_work_tree
408
c9e65892 409 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
3b38ec16 410 if test ! -z "$2"
1b1dce4b 411 then
dfa49f33 412 output git show-ref --verify --quiet "refs/heads/$2" ||
1b1dce4b 413 die "Invalid branchname: $2"
dfa49f33 414 output git checkout "$2" ||
1b1dce4b
JS
415 die "Could not checkout $2"
416 fi
417
418 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
419 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
420
421 test -z "$ONTO" && ONTO=$UPSTREAM
422
1b1dce4b
JS
423 : > "$DOTEST"/interactive || die "Could not mark as interactive"
424 git symbolic-ref HEAD > "$DOTEST"/head-name ||
425 die "Could not get HEAD"
426
427 echo $HEAD > "$DOTEST"/head
428 echo $UPSTREAM > "$DOTEST"/upstream
429 echo $ONTO > "$DOTEST"/onto
8e4a91bd 430 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4b 431 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec16 432 if test t = "$PRESERVE_MERGES"
f09c9b8c
JS
433 then
434 # $REWRITTEN contains files for each commit that is
435 # reachable by at least one merge base of $HEAD and
436 # $UPSTREAM. They are not necessarily rewritten, but
437 # their children might be.
438 # This ensures that commits on merged, but otherwise
439 # unrelated side branches are left alone. (Think "X"
440 # in the man page's example.)
441 mkdir "$REWRITTEN" &&
442 for c in $(git merge-base --all $HEAD $UPSTREAM)
443 do
444 echo $ONTO > "$REWRITTEN"/$c ||
445 die "Could not init rewritten commits"
446 done
447 MERGES_OPTION=
448 else
449 MERGES_OPTION=--no-merges
450 fi
1b1dce4b 451
c54b7817
JS
452 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
453 SHORTHEAD=$(git rev-parse --short $HEAD)
454 SHORTONTO=$(git rev-parse --short $ONTO)
1b1dce4b 455 cat > "$TODO" << EOF
c54b7817 456# Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
1b1dce4b
JS
457#
458# Commands:
459# pick = use commit
460# edit = use commit, but stop for amending
461# squash = use commit, but meld into previous commit
82576ddb
JS
462#
463# If you remove a line here THAT COMMIT WILL BE LOST.
464#
1b1dce4b 465EOF
f09c9b8c 466 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
96ffe892
JS
467 --abbrev=7 --reverse --left-right --cherry-pick \
468 $UPSTREAM...$HEAD | \
469 sed -n "s/^>/pick /p" >> "$TODO"
1b1dce4b
JS
470
471 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
c54b7817 472 die_abort "Nothing to do"
1b1dce4b
JS
473
474 cp "$TODO" "$TODO".backup
ef0c2abf 475 git_editor "$TODO" ||
1b1dce4b
JS
476 die "Could not execute editor"
477
c54b7817
JS
478 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
479 die_abort "Nothing to do"
480
dfa49f33 481 output git checkout $ONTO && do_rest
1b1dce4b
JS
482 esac
483 shift
484done