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