Code

rebase -i: handle --continue more like non-interactive rebase
[git.git] / git-rebase--interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Johannes E. Schindelin
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
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"/todo
21 DONE="$DOTEST"/done
22 REWRITTEN="$DOTEST"/rewritten
23 PRESERVE_MERGES=
24 STRATEGY=
25 VERBOSE=
27 warn () {
28         echo "$*" >&2
29 }
31 require_clean_work_tree () {
32         # test if working tree is dirty
33         git rev-parse --verify HEAD > /dev/null &&
34         git update-index --refresh &&
35         git diff-files --quiet &&
36         git diff-index --cached --quiet HEAD ||
37         die "Working tree is dirty"
38 }
40 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
42 comment_for_reflog () {
43         case "$ORIG_REFLOG_ACTION" in
44         ''|rebase*)
45                 GIT_REFLOG_ACTION="rebase -i ($1)"
46                 export GIT_REFLOG_ACTION
47         esac
48 }
50 mark_action_done () {
51         sed -e 1q < "$TODO" >> "$DONE"
52         sed -e 1d < "$TODO" >> "$TODO".new
53         mv -f "$TODO".new "$TODO"
54 }
56 make_patch () {
57         parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
58         git diff "$parent_sha1".."$1" > "$DOTEST"/patch
59 }
61 die_with_patch () {
62         test -f "$DOTEST"/message ||
63                 git cat-file commit $sha1 | sed "1,/^$/d" > "$DOTEST"/message
64         test -f "$DOTEST"/author-script ||
65                 get_author_ident_from_commit $sha1 > "$DOTEST"/author-script
66         make_patch "$1"
67         die "$2"
68 }
70 die_abort () {
71         rm -rf "$DOTEST"
72         die "$1"
73 }
75 pick_one () {
76         case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
77         git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
78         test -d "$REWRITTEN" &&
79                 pick_one_preserving_merges "$@" && return
80         parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
81         current_sha1=$(git rev-parse --verify HEAD)
82         if [ $current_sha1 = $parent_sha1 ]; then
83                 git reset --hard $sha1
84                 sha1=$(git rev-parse --short $sha1)
85                 warn Fast forward to $sha1
86         else
87                 git cherry-pick $STRATEGY "$@"
88         fi
89 }
91 pick_one_preserving_merges () {
92         case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
93         sha1=$(git rev-parse $sha1)
95         if [ -f "$DOTEST"/current-commit ]
96         then
97                 current_commit=$(cat "$DOTEST"/current-commit) &&
98                 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
99                 rm "$DOTEST"/current-commit ||
100                 die "Cannot write current commit's replacement sha1"
101         fi
103         # rewrite parents; if none were rewritten, we can fast-forward.
104         fast_forward=t
105         preserve=t
106         new_parents=
107         for p in $(git rev-list --parents -1 $sha1 | cut -d\  -f2-)
108         do
109                 if [ -f "$REWRITTEN"/$p ]
110                 then
111                         preserve=f
112                         new_p=$(cat "$REWRITTEN"/$p)
113                         test $p != $new_p && fast_forward=f
114                         case "$new_parents" in
115                         *$new_p*)
116                                 ;; # do nothing; that parent is already there
117                         *)
118                                 new_parents="$new_parents $new_p"
119                         esac
120                 fi
121         done
122         case $fast_forward in
123         t)
124                 echo "Fast forward to $sha1"
125                 test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
126                 ;;
127         f)
128                 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
130                 first_parent=$(expr "$new_parents" : " \([^ ]*\)")
131                 # detach HEAD to current parent
132                 git checkout $first_parent 2> /dev/null ||
133                         die "Cannot move HEAD to $first_parent"
135                 echo $sha1 > "$DOTEST"/current-commit
136                 case "$new_parents" in
137                 \ *\ *)
138                         # redo merge
139                         author_script=$(get_author_ident_from_commit $sha1)
140                         eval "$author_script"
141                         msg="$(git cat-file commit $sha1 | \
142                                 sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
143                         # NEEDSWORK: give rerere a chance
144                         if ! git merge $STRATEGY -m "$msg" $new_parents
145                         then
146                                 echo "$msg" > "$GIT_DIR"/MERGE_MSG
147                                 die Error redoing merge $sha1
148                         fi
149                         ;;
150                 *)
151                         git cherry-pick $STRATEGY "$@" ||
152                                 die_with_patch $sha1 "Could not pick $sha1"
153                 esac
154         esac
157 do_next () {
158         test -f "$DOTEST"/message && rm "$DOTEST"/message
159         test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
160         read command sha1 rest < "$TODO"
161         case "$command" in
162         \#|'')
163                 mark_action_done
164                 ;;
165         pick)
166                 comment_for_reflog pick
168                 mark_action_done
169                 pick_one $sha1 ||
170                         die_with_patch $sha1 "Could not apply $sha1... $rest"
171                 ;;
172         edit)
173                 comment_for_reflog edit
175                 mark_action_done
176                 pick_one $sha1 ||
177                         die_with_patch $sha1 "Could not apply $sha1... $rest"
178                 make_patch $sha1
179                 warn
180                 warn "You can amend the commit now, with"
181                 warn
182                 warn "  git commit --amend"
183                 warn
184                 exit 0
185                 ;;
186         squash)
187                 comment_for_reflog squash
189                 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
190                         die "Cannot 'squash' without a previous commit"
192                 mark_action_done
193                 failed=f
194                 pick_one -n $sha1 || failed=t
195                 MSG="$DOTEST"/message
196                 echo "# This is a combination of two commits." > "$MSG"
197                 echo "# The first commit's message is:" >> "$MSG"
198                 echo >> "$MSG"
199                 git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
200                 echo >> "$MSG"
201                 echo "# And this is the 2nd commit message:" >> "$MSG"
202                 echo >> "$MSG"
203                 git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
204                 git reset --soft HEAD^
205                 author_script=$(get_author_ident_from_commit $sha1)
206                 echo "$author_script" > "$DOTEST"/author-script
207                 case $failed in
208                 f)
209                         # This is like --amend, but with a different message
210                         eval "$author_script"
211                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
212                         git commit -F "$MSG" -e
213                         ;;
214                 t)
215                         cp "$MSG" "$GIT_DIR"/MERGE_MSG
216                         warn
217                         warn "Could not apply $sha1... $rest"
218                         die_with_patch $sha1 ""
219                 esac
220                 ;;
221         *)
222                 warn "Unknown command: $command $sha1 $rest"
223                 die_with_patch $sha1 "Please fix this in the file $TODO."
224         esac
225         test -s "$TODO" && return
227         comment_for_reflog finish &&
228         HEADNAME=$(cat "$DOTEST"/head-name) &&
229         OLDHEAD=$(cat "$DOTEST"/head) &&
230         SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
231         if [ -d "$REWRITTEN" ]
232         then
233                 test -f "$DOTEST"/current-commit &&
234                         current_commit=$(cat "$DOTEST"/current-commit) &&
235                         git rev-parse HEAD > "$REWRITTEN"/$current_commit
236                 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
237         else
238                 NEWHEAD=$(git rev-parse HEAD)
239         fi &&
240         message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
241         git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
242         git symbolic-ref HEAD $HEADNAME &&
243         rm -rf "$DOTEST" &&
244         warn "Successfully rebased and updated $HEADNAME."
246         exit
249 do_rest () {
250         while :
251         do
252                 do_next
253         done
254         test -f "$DOTEST"/verbose &&
255                 git diff --stat $(cat "$DOTEST"/head)..HEAD
256         exit
259 while case $# in 0) break ;; esac
260 do
261         case "$1" in
262         --continue)
263                 comment_for_reflog continue
265                 test -d "$DOTEST" || die "No interactive rebase running"
267                 # commit if necessary
268                 git rev-parse --verify HEAD > /dev/null &&
269                 git update-index --refresh &&
270                 git diff-files --quiet &&
271                 ! git diff-index --cached --quiet HEAD &&
272                 . "$DOTEST"/author-script &&
273                 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
274                 git commit -F "$DOTEST"/message -e
276                 require_clean_work_tree
277                 do_rest
278                 ;;
279         --abort)
280                 comment_for_reflog abort
282                 test -d "$DOTEST" || die "No interactive rebase running"
284                 HEADNAME=$(cat "$DOTEST"/head-name)
285                 HEAD=$(cat "$DOTEST"/head)
286                 git symbolic-ref HEAD $HEADNAME &&
287                 git reset --hard $HEAD &&
288                 rm -rf "$DOTEST"
289                 exit
290                 ;;
291         --skip)
292                 comment_for_reflog skip
294                 test -d "$DOTEST" || die "No interactive rebase running"
296                 git reset --hard && do_rest
297                 ;;
298         -s|--strategy)
299                 shift
300                 case "$#,$1" in
301                 *,*=*)
302                         STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
303                 1,*)
304                         usage ;;
305                 *)
306                         STRATEGY="-s $2"
307                         shift ;;
308                 esac
309                 ;;
310         --merge)
311                 # we use merge anyway
312                 ;;
313         -C*)
314                 die "Interactive rebase uses merge, so $1 does not make sense"
315                 ;;
316         -v|--verbose)
317                 VERBOSE=t
318                 ;;
319         -p|--preserve-merges)
320                 PRESERVE_MERGES=t
321                 ;;
322         -i|--interactive)
323                 # yeah, we know
324                 ;;
325         ''|-h)
326                 usage
327                 ;;
328         *)
329                 test -d "$DOTEST" &&
330                         die "Interactive rebase already started"
332                 git var GIT_COMMITTER_IDENT >/dev/null ||
333                         die "You need to set your committer info first"
335                 comment_for_reflog start
337                 ONTO=
338                 case "$1" in
339                 --onto)
340                         ONTO=$(git rev-parse --verify "$2") ||
341                                 die "Does not point to a valid commit: $2"
342                         shift; shift
343                         ;;
344                 esac
346                 require_clean_work_tree
348                 if [ ! -z "$2"]
349                 then
350                         git show-ref --verify --quiet "refs/heads/$2" ||
351                                 die "Invalid branchname: $2"
352                         git checkout "$2" ||
353                                 die "Could not checkout $2"
354                 fi
356                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
357                 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
359                 test -z "$ONTO" && ONTO=$UPSTREAM
361                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
362                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
363                 git symbolic-ref HEAD > "$DOTEST"/head-name ||
364                         die "Could not get HEAD"
366                 echo $HEAD > "$DOTEST"/head
367                 echo $UPSTREAM > "$DOTEST"/upstream
368                 echo $ONTO > "$DOTEST"/onto
369                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
370                 if [ t = "$PRESERVE_MERGES" ]
371                 then
372                         # $REWRITTEN contains files for each commit that is
373                         # reachable by at least one merge base of $HEAD and
374                         # $UPSTREAM. They are not necessarily rewritten, but
375                         # their children might be.
376                         # This ensures that commits on merged, but otherwise
377                         # unrelated side branches are left alone. (Think "X"
378                         # in the man page's example.)
379                         mkdir "$REWRITTEN" &&
380                         for c in $(git merge-base --all $HEAD $UPSTREAM)
381                         do
382                                 echo $ONTO > "$REWRITTEN"/$c ||
383                                         die "Could not init rewritten commits"
384                         done
385                         MERGES_OPTION=
386                 else
387                         MERGES_OPTION=--no-merges
388                 fi
390                 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
391                 SHORTHEAD=$(git rev-parse --short $HEAD)
392                 SHORTONTO=$(git rev-parse --short $ONTO)
393                 cat > "$TODO" << EOF
394 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
396 # Commands:
397 #  pick = use commit
398 #  edit = use commit, but stop for amending
399 #  squash = use commit, but meld into previous commit
400 EOF
401                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
402                         --abbrev=7 --reverse $UPSTREAM..$HEAD | \
403                         sed "s/^/pick /" >> "$TODO"
405                 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
406                         die_abort "Nothing to do"
408                 cp "$TODO" "$TODO".backup
409                 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
410                         die "Could not execute editor"
412                 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
413                         die_abort "Nothing to do"
415                 git checkout $ONTO && do_rest
416         esac
417         shift
418 done