Prevent case sensitive commit
[git/jr.git] / git-rebase--interactive.sh
blob402ff3782c0aaa472a2013ff3a9bef44a6cd8550
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 OPTIONS_SPEC=
17 . git-sh-setup
18 require_work_tree
20 DOTEST="$GIT_DIR/.dotest-merge"
21 TODO="$DOTEST"/git-rebase-todo
22 DONE="$DOTEST"/done
23 MSG="$DOTEST"/message
24 SQUASH_MSG="$DOTEST"/message-squash
25 REWRITTEN="$DOTEST"/rewritten
26 PRESERVE_MERGES=
27 STRATEGY=
28 VERBOSE=
29 test -d "$REWRITTEN" && PRESERVE_MERGES=t
30 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31 test -f "$DOTEST"/verbose && VERBOSE=t
33 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34 mark the corrected paths with 'git add <paths>', and
35 run 'git rebase --continue'"
36 export GIT_CHERRY_PICK_HELP
38 warn () {
39 echo "$*" >&2
42 output () {
43 case "$VERBOSE" in
44 '')
45 output=$("$@" 2>&1 )
46 status=$?
47 test $status != 0 && printf "%s\n" "$output"
48 return $status
51 "$@"
53 esac
56 require_clean_work_tree () {
57 # test if working tree is dirty
58 git rev-parse --verify HEAD > /dev/null &&
59 git update-index --refresh &&
60 git diff-files --quiet &&
61 git diff-index --cached --quiet HEAD -- ||
62 die "Working tree is dirty"
65 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
67 comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
69 ''|rebase*)
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
73 esac
76 last_count=
77 mark_action_done () {
78 sed -e 1q < "$TODO" >> "$DONE"
79 sed -e 1d < "$TODO" >> "$TODO".new
80 mv -f "$TODO".new "$TODO"
81 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
82 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
83 if test "$last_count" != "$count"
84 then
85 last_count=$count
86 printf "Rebasing (%d/%d)\r" $count $total
87 test -z "$VERBOSE" || echo
91 make_patch () {
92 parent_sha1=$(git rev-parse --verify "$1"^) ||
93 die "Cannot get patch for $1^"
94 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
95 test -f "$DOTEST"/message ||
96 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
97 test -f "$DOTEST"/author-script ||
98 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
101 die_with_patch () {
102 make_patch "$1"
103 git rerere
104 die "$2"
107 die_abort () {
108 rm -rf "$DOTEST"
109 die "$1"
112 has_action () {
113 grep -vqe '^$' -e '^#' "$1"
116 pick_one () {
117 no_ff=
118 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
119 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
120 test -d "$REWRITTEN" &&
121 pick_one_preserving_merges "$@" && return
122 parent_sha1=$(git rev-parse --verify $sha1^) ||
123 die "Could not get the parent of $sha1"
124 current_sha1=$(git rev-parse --verify HEAD)
125 if test "$no_ff$current_sha1" = "$parent_sha1"; then
126 output git reset --hard $sha1
127 test "a$1" = a-n && output git reset --soft $current_sha1
128 sha1=$(git rev-parse --short $sha1)
129 output warn Fast forward to $sha1
130 else
131 output git cherry-pick "$@"
135 pick_one_preserving_merges () {
136 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
137 sha1=$(git rev-parse $sha1)
139 if test -f "$DOTEST"/current-commit
140 then
141 current_commit=$(cat "$DOTEST"/current-commit) &&
142 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
143 rm "$DOTEST"/current-commit ||
144 die "Cannot write current commit's replacement sha1"
147 # rewrite parents; if none were rewritten, we can fast-forward.
148 fast_forward=t
149 preserve=t
150 new_parents=
151 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
153 if test -f "$REWRITTEN"/$p
154 then
155 preserve=f
156 new_p=$(cat "$REWRITTEN"/$p)
157 test $p != $new_p && fast_forward=f
158 case "$new_parents" in
159 *$new_p*)
160 ;; # do nothing; that parent is already there
162 new_parents="$new_parents $new_p"
164 esac
166 done
167 case $fast_forward in
169 output warn "Fast forward to $sha1"
170 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
173 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
175 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
176 # detach HEAD to current parent
177 output git checkout $first_parent 2> /dev/null ||
178 die "Cannot move HEAD to $first_parent"
180 echo $sha1 > "$DOTEST"/current-commit
181 case "$new_parents" in
182 ' '*' '*)
183 # redo merge
184 author_script=$(get_author_ident_from_commit $sha1)
185 eval "$author_script"
186 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
187 # No point in merging the first parent, that's HEAD
188 new_parents=${new_parents# $first_parent}
189 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
190 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
191 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
192 output git merge $STRATEGY -m "$msg" \
193 $new_parents
194 then
195 git rerere
196 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
197 die Error redoing merge $sha1
201 output git cherry-pick "$@" ||
202 die_with_patch $sha1 "Could not pick $sha1"
204 esac
206 esac
209 nth_string () {
210 case "$1" in
211 *1[0-9]|*[04-9]) echo "$1"th;;
212 *1) echo "$1"st;;
213 *2) echo "$1"nd;;
214 *3) echo "$1"rd;;
215 esac
218 make_squash_message () {
219 if test -f "$SQUASH_MSG"; then
220 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
221 < "$SQUASH_MSG" | tail -n 1)+1))
222 echo "# This is a combination of $COUNT commits."
223 sed -e 1d -e '2,/^./{
224 /^$/d
225 }' <"$SQUASH_MSG"
226 else
227 COUNT=2
228 echo "# This is a combination of two commits."
229 echo "# The first commit's message is:"
230 echo
231 git cat-file commit HEAD | sed -e '1,/^$/d'
233 echo
234 echo "# This is the $(nth_string $COUNT) commit message:"
235 echo
236 git cat-file commit $1 | sed -e '1,/^$/d'
239 peek_next_command () {
240 sed -n "1s/ .*$//p" < "$TODO"
243 do_next () {
244 rm -f "$DOTEST"/message "$DOTEST"/author-script \
245 "$DOTEST"/amend || exit
246 read command sha1 rest < "$TODO"
247 case "$command" in
248 '#'*|'')
249 mark_action_done
251 pick|p)
252 comment_for_reflog pick
254 mark_action_done
255 pick_one $sha1 ||
256 die_with_patch $sha1 "Could not apply $sha1... $rest"
258 edit|e)
259 comment_for_reflog edit
261 mark_action_done
262 pick_one $sha1 ||
263 die_with_patch $sha1 "Could not apply $sha1... $rest"
264 make_patch $sha1
265 : > "$DOTEST"/amend
266 warn
267 warn "You can amend the commit now, with"
268 warn
269 warn " git commit --amend"
270 warn
271 exit 0
273 squash|s)
274 comment_for_reflog squash
276 has_action "$DONE" ||
277 die "Cannot 'squash' without a previous commit"
279 mark_action_done
280 make_squash_message $sha1 > "$MSG"
281 case "$(peek_next_command)" in
282 squash|s)
283 EDIT_COMMIT=
284 USE_OUTPUT=output
285 cp "$MSG" "$SQUASH_MSG"
288 EDIT_COMMIT=-e
289 USE_OUTPUT=
290 rm -f "$SQUASH_MSG" || exit
292 esac
294 failed=f
295 author_script=$(get_author_ident_from_commit HEAD)
296 output git reset --soft HEAD^
297 pick_one -n $sha1 || failed=t
298 echo "$author_script" > "$DOTEST"/author-script
299 if test $failed = f
300 then
301 # This is like --amend, but with a different message
302 eval "$author_script"
303 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
304 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
305 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
306 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
308 if test $failed = t
309 then
310 cp "$MSG" "$GIT_DIR"/MERGE_MSG
311 warn
312 warn "Could not apply $sha1... $rest"
313 die_with_patch $sha1 ""
317 warn "Unknown command: $command $sha1 $rest"
318 die_with_patch $sha1 "Please fix this in the file $TODO."
320 esac
321 test -s "$TODO" && return
323 comment_for_reflog finish &&
324 HEADNAME=$(cat "$DOTEST"/head-name) &&
325 OLDHEAD=$(cat "$DOTEST"/head) &&
326 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
327 if test -d "$REWRITTEN"
328 then
329 test -f "$DOTEST"/current-commit &&
330 current_commit=$(cat "$DOTEST"/current-commit) &&
331 git rev-parse HEAD > "$REWRITTEN"/$current_commit
332 if test -f "$REWRITTEN"/$OLDHEAD
333 then
334 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
335 else
336 NEWHEAD=$OLDHEAD
338 else
339 NEWHEAD=$(git rev-parse HEAD)
340 fi &&
341 case $HEADNAME in
342 refs/*)
343 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
344 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
345 git symbolic-ref HEAD $HEADNAME
347 esac && {
348 test ! -f "$DOTEST"/verbose ||
349 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
350 } &&
351 rm -rf "$DOTEST" &&
352 git gc --auto &&
353 warn "Successfully rebased and updated $HEADNAME."
355 exit
358 do_rest () {
359 while :
361 do_next
362 done
365 while test $# != 0
367 case "$1" in
368 --continue)
369 comment_for_reflog continue
371 test -d "$DOTEST" || die "No interactive rebase running"
373 # Sanity check
374 git rev-parse --verify HEAD >/dev/null ||
375 die "Cannot read HEAD"
376 git update-index --refresh && git diff-files --quiet ||
377 die "Working tree is dirty"
379 # do we have anything to commit?
380 if git diff-index --cached --quiet HEAD --
381 then
382 : Nothing to commit -- skip this
383 else
384 . "$DOTEST"/author-script ||
385 die "Cannot find the author identity"
386 if test -f "$DOTEST"/amend
387 then
388 git reset --soft HEAD^ ||
389 die "Cannot rewind the HEAD"
391 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
392 git commit --no-verify -F "$DOTEST"/message -e ||
393 die "Could not commit staged changes."
396 require_clean_work_tree
397 do_rest
399 --abort)
400 comment_for_reflog abort
402 git rerere clear
403 test -d "$DOTEST" || die "No interactive rebase running"
405 HEADNAME=$(cat "$DOTEST"/head-name)
406 HEAD=$(cat "$DOTEST"/head)
407 case $HEADNAME in
408 refs/*)
409 git symbolic-ref HEAD $HEADNAME
411 esac &&
412 output git reset --hard $HEAD &&
413 rm -rf "$DOTEST"
414 exit
416 --skip)
417 comment_for_reflog skip
419 git rerere clear
420 test -d "$DOTEST" || die "No interactive rebase running"
422 output git reset --hard && do_rest
424 -s|--strategy)
425 case "$#,$1" in
426 *,*=*)
427 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
428 1,*)
429 usage ;;
431 STRATEGY="-s $2"
432 shift ;;
433 esac
435 --merge)
436 # we use merge anyway
438 -C*)
439 die "Interactive rebase uses merge, so $1 does not make sense"
441 -v|--verbose)
442 VERBOSE=t
444 -p|--preserve-merges)
445 PRESERVE_MERGES=t
447 -i|--interactive)
448 # yeah, we know
450 ''|-h)
451 usage
454 test -d "$DOTEST" &&
455 die "Interactive rebase already started"
457 git var GIT_COMMITTER_IDENT >/dev/null ||
458 die "You need to set your committer info first"
460 comment_for_reflog start
462 ONTO=
463 case "$1" in
464 --onto)
465 ONTO=$(git rev-parse --verify "$2") ||
466 die "Does not point to a valid commit: $2"
467 shift; shift
469 esac
471 require_clean_work_tree
473 if test ! -z "$2"
474 then
475 output git show-ref --verify --quiet "refs/heads/$2" ||
476 die "Invalid branchname: $2"
477 output git checkout "$2" ||
478 die "Could not checkout $2"
481 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
482 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
484 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
486 test -z "$ONTO" && ONTO=$UPSTREAM
488 : > "$DOTEST"/interactive || die "Could not mark as interactive"
489 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
490 echo "detached HEAD" > "$DOTEST"/head-name
492 echo $HEAD > "$DOTEST"/head
493 echo $UPSTREAM > "$DOTEST"/upstream
494 echo $ONTO > "$DOTEST"/onto
495 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
496 test t = "$VERBOSE" && : > "$DOTEST"/verbose
497 if test t = "$PRESERVE_MERGES"
498 then
499 # $REWRITTEN contains files for each commit that is
500 # reachable by at least one merge base of $HEAD and
501 # $UPSTREAM. They are not necessarily rewritten, but
502 # their children might be.
503 # This ensures that commits on merged, but otherwise
504 # unrelated side branches are left alone. (Think "X"
505 # in the man page's example.)
506 mkdir "$REWRITTEN" &&
507 for c in $(git merge-base --all $HEAD $UPSTREAM)
509 echo $ONTO > "$REWRITTEN"/$c ||
510 die "Could not init rewritten commits"
511 done
512 MERGES_OPTION=
513 else
514 MERGES_OPTION=--no-merges
517 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
518 SHORTHEAD=$(git rev-parse --short $HEAD)
519 SHORTONTO=$(git rev-parse --short $ONTO)
520 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
521 --abbrev=7 --reverse --left-right --cherry-pick \
522 $UPSTREAM...$HEAD | \
523 sed -n "s/^>/pick /p" > "$TODO"
524 cat >> "$TODO" << EOF
526 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
528 # Commands:
529 # pick = use commit
530 # edit = use commit, but stop for amending
531 # squash = use commit, but meld into previous commit
533 # If you remove a line here THAT COMMIT WILL BE LOST.
534 # However, if you remove everything, the rebase will be aborted.
538 has_action "$TODO" ||
539 die_abort "Nothing to do"
541 cp "$TODO" "$TODO".backup
542 git_editor "$TODO" ||
543 die "Could not execute editor"
545 has_action "$TODO" ||
546 die_abort "Nothing to do"
548 output git checkout $ONTO && do_rest
550 esac
551 shift
552 done