]>
Commit | Line | Data |
---|---|---|
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 | ||
7970aaf0 SB |
13 | OPTIONS_KEEPDASHDASH= |
14 | OPTIONS_SPEC="\ | |
15 | git-rebase [-i] [options] [--] <upstream> [<branch>] | |
16 | git-rebase [-i] (--continue | --abort | --skip) | |
17 | -- | |
18 | Available options are | |
19 | v,verbose display a diffstat of what changed upstream | |
20 | onto= rebase onto given branch instead of upstream | |
21 | p,preserve-merges try to recreate merges instead of ignoring them | |
22 | s,strategy= use the given merge strategy | |
23 | m,merge always used (no-op) | |
24 | i,interactive always used (no-op) | |
25 | Actions: | |
26 | continue continue rebasing process | |
27 | abort abort rebasing process and restore original branch | |
28 | skip skip current patch and continue rebasing process | |
c4427656 | 29 | no-verify override pre-rebase hook from stopping the operation |
d911d146 | 30 | root rebase all reachable commmits up to the root(s) |
f59baa50 | 31 | autosquash move commits that begin with squash!/fixup! under -i |
7970aaf0 | 32 | " |
1b1dce4b JS |
33 | |
34 | . git-sh-setup | |
35 | require_work_tree | |
36 | ||
28ed6e7b | 37 | DOTEST="$GIT_DIR/rebase-merge" |
80883bb3 MH |
38 | |
39 | # The file containing rebase commands, comments, and empty lines. | |
40 | # This file is created by "git rebase -i" then edited by the user. As | |
41 | # the lines are processed, they are removed from the front of this | |
42 | # file and written to the tail of $DONE. | |
c22486c9 | 43 | TODO="$DOTEST"/git-rebase-todo |
80883bb3 MH |
44 | |
45 | # The rebase command lines that have already been processed. A line | |
46 | # is moved here when it is first handled, before any associated user | |
47 | # actions. | |
1b1dce4b | 48 | DONE="$DOTEST"/done |
80883bb3 MH |
49 | |
50 | # The commit message that is planned to be used for any changes that | |
51 | # need to be committed following a user interaction. | |
6368f3f8 | 52 | MSG="$DOTEST"/message |
80883bb3 MH |
53 | |
54 | # The file into which is accumulated the suggested commit message for | |
55 | # squash/fixup commands. When the first of a series of squash/fixups | |
56 | # is seen, the file is created and the commit message from the | |
57 | # previous commit and from the first squash/fixup commit are written | |
58 | # to it. The commit message for each subsequent squash/fixup commit | |
59 | # is appended to the file as it is processed. | |
60 | # | |
61 | # The first line of the file is of the form | |
62 | # # This is a combination of $COUNT commits. | |
63 | # where $COUNT is the number of commits whose messages have been | |
64 | # written to the file so far (including the initial "pick" commit). | |
65 | # Each time that a commit message is processed, this line is read and | |
66 | # updated. It is deleted just before the combined commit is made. | |
6368f3f8 | 67 | SQUASH_MSG="$DOTEST"/message-squash |
80883bb3 | 68 | |
a25eb139 MH |
69 | # If the current series of squash/fixups has not yet included a squash |
70 | # command, then this file exists and holds the commit message of the | |
71 | # original "pick" commit. (If the series ends without a "squash" | |
72 | # command, then this can be used as the commit message of the combined | |
73 | # commit without opening the editor.) | |
74 | FIXUP_MSG="$DOTEST"/message-fixup | |
75 | ||
80883bb3 MH |
76 | # $REWRITTEN is the name of a directory containing files for each |
77 | # commit that is reachable by at least one merge base of $HEAD and | |
78 | # $UPSTREAM. They are not necessarily rewritten, but their children | |
79 | # might be. This ensures that commits on merged, but otherwise | |
80 | # unrelated side branches are left alone. (Think "X" in the man page's | |
81 | # example.) | |
f09c9b8c | 82 | REWRITTEN="$DOTEST"/rewritten |
80883bb3 | 83 | |
faae853c | 84 | DROPPED="$DOTEST"/dropped |
0aac0de4 MH |
85 | |
86 | # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
87 | # GIT_AUTHOR_DATE that will be used for the commit that is currently | |
88 | # being rebased. | |
89 | AUTHOR_SCRIPT="$DOTEST"/author-script | |
90 | ||
a4049ae7 MH |
91 | # When an "edit" rebase command is being processed, the SHA1 of the |
92 | # commit to be edited is recorded in this file. When "git rebase | |
93 | # --continue" is executed, if there are any staged changes then they | |
94 | # will be amended to the HEAD commit, but only provided the HEAD | |
95 | # commit is still the commit to be edited. When any other rebase | |
96 | # command is processed, this file is deleted. | |
97 | AMEND="$DOTEST"/amend | |
98 | ||
b079feed TR |
99 | # For the post-rewrite hook, we make a list of rewritten commits and |
100 | # their new sha1s. The rewritten-pending list keeps the sha1s of | |
101 | # commits that have been processed, but not committed yet, | |
102 | # e.g. because they are waiting for a 'squash' command. | |
103 | REWRITTEN_LIST="$DOTEST"/rewritten-list | |
104 | REWRITTEN_PENDING="$DOTEST"/rewritten-pending | |
105 | ||
f09c9b8c | 106 | PRESERVE_MERGES= |
1b1dce4b | 107 | STRATEGY= |
7970aaf0 | 108 | ONTO= |
1b1dce4b | 109 | VERBOSE= |
c4427656 | 110 | OK_TO_SKIP_PRE_REBASE= |
d911d146 | 111 | REBASE_ROOT= |
f59baa50 | 112 | AUTOSQUASH= |
1b1dce4b | 113 | |
804c7174 WC |
114 | GIT_CHERRY_PICK_HELP=" After resolving the conflicts, |
115 | mark the corrected paths with 'git add <paths>', and | |
116 | run 'git rebase --continue'" | |
117 | export GIT_CHERRY_PICK_HELP | |
118 | ||
1b1dce4b JS |
119 | warn () { |
120 | echo "$*" >&2 | |
121 | } | |
122 | ||
dfa49f33 JS |
123 | output () { |
124 | case "$VERBOSE" in | |
125 | '') | |
5166810b | 126 | output=$("$@" 2>&1 ) |
dfa49f33 | 127 | status=$? |
5166810b | 128 | test $status != 0 && printf "%s\n" "$output" |
dfa49f33 | 129 | return $status |
376ccb8c | 130 | ;; |
dfa49f33 JS |
131 | *) |
132 | "$@" | |
376ccb8c | 133 | ;; |
dfa49f33 JS |
134 | esac |
135 | } | |
136 | ||
ee0a4afb MH |
137 | # Output the commit message for the specified commit. |
138 | commit_message () { | |
139 | git cat-file commit "$1" | sed "1,/^$/d" | |
140 | } | |
141 | ||
d70b4a8f | 142 | run_pre_rebase_hook () { |
c4427656 NS |
143 | if test -z "$OK_TO_SKIP_PRE_REBASE" && |
144 | test -x "$GIT_DIR/hooks/pre-rebase" | |
d70b4a8f NS |
145 | then |
146 | "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || { | |
147 | echo >&2 "The pre-rebase hook refused to rebase." | |
148 | exit 1 | |
149 | } | |
150 | fi | |
151 | } | |
152 | ||
1b1dce4b JS |
153 | require_clean_work_tree () { |
154 | # test if working tree is dirty | |
155 | git rev-parse --verify HEAD > /dev/null && | |
6848d58c JS |
156 | git update-index --ignore-submodules --refresh && |
157 | git diff-files --quiet --ignore-submodules && | |
158 | git diff-index --cached --quiet HEAD --ignore-submodules -- || | |
1b1dce4b JS |
159 | die "Working tree is dirty" |
160 | } | |
161 | ||
162 | ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION" | |
163 | ||
164 | comment_for_reflog () { | |
165 | case "$ORIG_REFLOG_ACTION" in | |
166 | ''|rebase*) | |
167 | GIT_REFLOG_ACTION="rebase -i ($1)" | |
168 | export GIT_REFLOG_ACTION | |
376ccb8c | 169 | ;; |
1b1dce4b JS |
170 | esac |
171 | } | |
172 | ||
4e673877 | 173 | last_count= |
1b1dce4b JS |
174 | mark_action_done () { |
175 | sed -e 1q < "$TODO" >> "$DONE" | |
176 | sed -e 1d < "$TODO" >> "$TODO".new | |
177 | mv -f "$TODO".new "$TODO" | |
e1622bfc JH |
178 | count=$(sane_grep -c '^[^#]' < "$DONE") |
179 | total=$(($count+$(sane_grep -c '^[^#]' < "$TODO"))) | |
4e673877 JH |
180 | if test "$last_count" != "$count" |
181 | then | |
182 | last_count=$count | |
183 | printf "Rebasing (%d/%d)\r" $count $total | |
184 | test -z "$VERBOSE" || echo | |
185 | fi | |
1b1dce4b JS |
186 | } |
187 | ||
188 | make_patch () { | |
4fb1a19d JS |
189 | sha1_and_parents="$(git rev-list --parents -1 "$1")" |
190 | case "$sha1_and_parents" in | |
191 | ?*' '?*' '?*) | |
192 | git diff --cc $sha1_and_parents | |
193 | ;; | |
194 | ?*' '?*) | |
195 | git diff-tree -p "$1^!" | |
196 | ;; | |
197 | *) | |
198 | echo "Root commit" | |
199 | ;; | |
200 | esac > "$DOTEST"/patch | |
bdb011ad | 201 | test -f "$MSG" || |
ee0a4afb | 202 | commit_message "$1" > "$MSG" |
0aac0de4 MH |
203 | test -f "$AUTHOR_SCRIPT" || |
204 | get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT" | |
1b1dce4b JS |
205 | } |
206 | ||
207 | die_with_patch () { | |
b079feed | 208 | echo "$1" > "$DOTEST"/stopped-sha |
1b1dce4b | 209 | make_patch "$1" |
ecfe72ff | 210 | git rerere |
1b1dce4b JS |
211 | die "$2" |
212 | } | |
213 | ||
c54b7817 JS |
214 | die_abort () { |
215 | rm -rf "$DOTEST" | |
216 | die "$1" | |
217 | } | |
218 | ||
376ccb8c | 219 | has_action () { |
e1622bfc | 220 | sane_grep '^[^#]' "$1" >/dev/null |
376ccb8c JS |
221 | } |
222 | ||
7756ecff MH |
223 | # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
224 | # GIT_AUTHOR_DATE exported from the current environment. | |
225 | do_with_author () { | |
76c9c0db JH |
226 | ( |
227 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE | |
228 | "$@" | |
229 | ) | |
7756ecff MH |
230 | } |
231 | ||
1b1dce4b | 232 | pick_one () { |
1d25c8cf JS |
233 | no_ff= |
234 | case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac | |
dfa49f33 | 235 | output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1" |
f09c9b8c JS |
236 | test -d "$REWRITTEN" && |
237 | pick_one_preserving_merges "$@" && return | |
699f13ca | 238 | if test -n "$REBASE_ROOT" |
d911d146 TR |
239 | then |
240 | output git cherry-pick "$@" | |
241 | return | |
242 | fi | |
376ccb8c JS |
243 | parent_sha1=$(git rev-parse --verify $sha1^) || |
244 | die "Could not get the parent of $sha1" | |
1b1dce4b | 245 | current_sha1=$(git rev-parse --verify HEAD) |
8cddaeec | 246 | if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1" |
1d621fea | 247 | then |
dfa49f33 | 248 | output git reset --hard $sha1 |
aa7eaff8 | 249 | output warn Fast-forward to $(git rev-parse --short $sha1) |
1b1dce4b | 250 | else |
2a9c53e0 | 251 | output git cherry-pick "$@" |
1b1dce4b JS |
252 | fi |
253 | } | |
254 | ||
f09c9b8c | 255 | pick_one_preserving_merges () { |
71d9451e TR |
256 | fast_forward=t |
257 | case "$1" in | |
258 | -n) | |
259 | fast_forward=f | |
260 | sha1=$2 | |
261 | ;; | |
262 | *) | |
263 | sha1=$1 | |
264 | ;; | |
265 | esac | |
f09c9b8c JS |
266 | sha1=$(git rev-parse $sha1) |
267 | ||
3b38ec16 | 268 | if test -f "$DOTEST"/current-commit |
f09c9b8c | 269 | then |
4c1360f4 | 270 | if test "$fast_forward" = t |
bb645071 SH |
271 | then |
272 | cat "$DOTEST"/current-commit | while read current_commit | |
273 | do | |
274 | git rev-parse HEAD > "$REWRITTEN"/$current_commit | |
275 | done | |
276 | rm "$DOTEST"/current-commit || | |
277 | die "Cannot write current commit's replacement sha1" | |
278 | fi | |
f09c9b8c JS |
279 | fi |
280 | ||
bb645071 | 281 | echo $sha1 >> "$DOTEST"/current-commit |
a96dc01e | 282 | |
f09c9b8c | 283 | # rewrite parents; if none were rewritten, we can fast-forward. |
f09c9b8c | 284 | new_parents= |
d911d146 TR |
285 | pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)" |
286 | if test "$pend" = " " | |
287 | then | |
288 | pend=" root" | |
289 | fi | |
faae853c | 290 | while [ "$pend" != "" ] |
f09c9b8c | 291 | do |
faae853c SH |
292 | p=$(expr "$pend" : ' \([^ ]*\)') |
293 | pend="${pend# $p}" | |
294 | ||
3b38ec16 | 295 | if test -f "$REWRITTEN"/$p |
f09c9b8c | 296 | then |
f09c9b8c | 297 | new_p=$(cat "$REWRITTEN"/$p) |
80fe82e4 SH |
298 | |
299 | # If the todo reordered commits, and our parent is marked for | |
300 | # rewriting, but hasn't been gotten to yet, assume the user meant to | |
301 | # drop it on top of the current HEAD | |
302 | if test -z "$new_p" | |
303 | then | |
304 | new_p=$(git rev-parse HEAD) | |
305 | fi | |
306 | ||
f09c9b8c JS |
307 | test $p != $new_p && fast_forward=f |
308 | case "$new_parents" in | |
309 | *$new_p*) | |
310 | ;; # do nothing; that parent is already there | |
311 | *) | |
312 | new_parents="$new_parents $new_p" | |
376ccb8c | 313 | ;; |
f09c9b8c | 314 | esac |
1c5fa0a1 | 315 | else |
faae853c SH |
316 | if test -f "$DROPPED"/$p |
317 | then | |
318 | fast_forward=f | |
d911d146 TR |
319 | replacement="$(cat "$DROPPED"/$p)" |
320 | test -z "$replacement" && replacement=root | |
321 | pend=" $replacement$pend" | |
faae853c SH |
322 | else |
323 | new_parents="$new_parents $p" | |
324 | fi | |
f09c9b8c JS |
325 | fi |
326 | done | |
327 | case $fast_forward in | |
328 | t) | |
a75d7b54 | 329 | output warn "Fast-forward to $sha1" |
71d9451e | 330 | output git reset --hard $sha1 || |
a75d7b54 | 331 | die "Cannot fast-forward to $sha1" |
f09c9b8c JS |
332 | ;; |
333 | f) | |
376ccb8c | 334 | first_parent=$(expr "$new_parents" : ' \([^ ]*\)') |
a4f25e36 SH |
335 | |
336 | if [ "$1" != "-n" ] | |
337 | then | |
338 | # detach HEAD to current parent | |
339 | output git checkout $first_parent 2> /dev/null || | |
340 | die "Cannot move HEAD to $first_parent" | |
341 | fi | |
f09c9b8c | 342 | |
f09c9b8c | 343 | case "$new_parents" in |
376ccb8c | 344 | ' '*' '*) |
a4f25e36 SH |
345 | test "a$1" = a-n && die "Refusing to squash a merge: $sha1" |
346 | ||
f09c9b8c JS |
347 | # redo merge |
348 | author_script=$(get_author_ident_from_commit $sha1) | |
349 | eval "$author_script" | |
ee0a4afb | 350 | msg="$(commit_message $sha1)" |
f91333d6 BS |
351 | # No point in merging the first parent, that's HEAD |
352 | new_parents=${new_parents# $first_parent} | |
7756ecff MH |
353 | if ! do_with_author output \ |
354 | git merge $STRATEGY -m "$msg" $new_parents | |
f09c9b8c | 355 | then |
376ccb8c | 356 | printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG |
f5b49ea6 | 357 | die_with_patch $sha1 "Error redoing merge $sha1" |
f09c9b8c | 358 | fi |
b079feed | 359 | echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST" |
f09c9b8c JS |
360 | ;; |
361 | *) | |
2a9c53e0 | 362 | output git cherry-pick "$@" || |
f09c9b8c | 363 | die_with_patch $sha1 "Could not pick $sha1" |
376ccb8c | 364 | ;; |
f09c9b8c | 365 | esac |
376ccb8c | 366 | ;; |
f09c9b8c JS |
367 | esac |
368 | } | |
369 | ||
6368f3f8 JS |
370 | nth_string () { |
371 | case "$1" in | |
372 | *1[0-9]|*[04-9]) echo "$1"th;; | |
373 | *1) echo "$1"st;; | |
374 | *2) echo "$1"nd;; | |
375 | *3) echo "$1"rd;; | |
376 | esac | |
377 | } | |
378 | ||
a25eb139 | 379 | update_squash_messages () { |
3b38ec16 | 380 | if test -f "$SQUASH_MSG"; then |
bde1a686 | 381 | mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit |
f99e269c MH |
382 | COUNT=$(($(sed -n \ |
383 | -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \ | |
bde1a686 MH |
384 | -e "q" < "$SQUASH_MSG".bak)+1)) |
385 | { | |
386 | echo "# This is a combination of $COUNT commits." | |
387 | sed -e 1d -e '2,/^./{ | |
388 | /^$/d | |
389 | }' <"$SQUASH_MSG".bak | |
3fa7c3da | 390 | } >"$SQUASH_MSG" |
6368f3f8 | 391 | else |
a25eb139 | 392 | commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG" |
6368f3f8 | 393 | COUNT=2 |
bde1a686 MH |
394 | { |
395 | echo "# This is a combination of 2 commits." | |
396 | echo "# The first commit's message is:" | |
397 | echo | |
a25eb139 | 398 | cat "$FIXUP_MSG" |
3fa7c3da | 399 | } >"$SQUASH_MSG" |
6368f3f8 | 400 | fi |
0205e72f MH |
401 | case $1 in |
402 | squash) | |
a25eb139 | 403 | rm -f "$FIXUP_MSG" |
0205e72f MH |
404 | echo |
405 | echo "# This is the $(nth_string $COUNT) commit message:" | |
406 | echo | |
ee0a4afb | 407 | commit_message $2 |
0205e72f MH |
408 | ;; |
409 | fixup) | |
410 | echo | |
411 | echo "# The $(nth_string $COUNT) commit message will be skipped:" | |
412 | echo | |
ee0a4afb | 413 | commit_message $2 | sed -e 's/^/# /' |
0205e72f | 414 | ;; |
3fa7c3da | 415 | esac >>"$SQUASH_MSG" |
6368f3f8 JS |
416 | } |
417 | ||
418 | peek_next_command () { | |
9524cf29 | 419 | sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO" |
6368f3f8 JS |
420 | } |
421 | ||
6bdcd0d2 MH |
422 | # A squash/fixup has failed. Prepare the long version of the squash |
423 | # commit message, then die_with_patch. This code path requires the | |
424 | # user to edit the combined commit message for all commits that have | |
425 | # been squashed/fixedup so far. So also erase the old squash | |
426 | # messages, effectively causing the combined commit to be used as the | |
427 | # new basis for any further squash/fixups. Args: sha1 rest | |
428 | die_failed_squash() { | |
429 | mv "$SQUASH_MSG" "$MSG" || exit | |
430 | rm -f "$FIXUP_MSG" | |
431 | cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit | |
432 | warn | |
433 | warn "Could not apply $1... $2" | |
434 | die_with_patch $1 "" | |
435 | } | |
436 | ||
b079feed TR |
437 | flush_rewritten_pending() { |
438 | test -s "$REWRITTEN_PENDING" || return | |
439 | newsha1="$(git rev-parse HEAD^0)" | |
440 | sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST" | |
441 | rm -f "$REWRITTEN_PENDING" | |
442 | } | |
443 | ||
444 | record_in_rewritten() { | |
445 | oldsha1="$(git rev-parse $1)" | |
446 | echo "$oldsha1" >> "$REWRITTEN_PENDING" | |
447 | ||
448 | case "$(peek_next_command)" in | |
449 | squash|s|fixup|f) | |
450 | ;; | |
451 | *) | |
452 | flush_rewritten_pending | |
453 | ;; | |
454 | esac | |
455 | } | |
456 | ||
1b1dce4b | 457 | do_next () { |
a4049ae7 | 458 | rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit |
1b1dce4b JS |
459 | read command sha1 rest < "$TODO" |
460 | case "$command" in | |
ff74126c | 461 | '#'*|''|noop) |
1b1dce4b | 462 | mark_action_done |
1b1dce4b | 463 | ;; |
f8babc4d | 464 | pick|p) |
1b1dce4b JS |
465 | comment_for_reflog pick |
466 | ||
467 | mark_action_done | |
468 | pick_one $sha1 || | |
469 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
b079feed | 470 | record_in_rewritten $sha1 |
1b1dce4b | 471 | ;; |
6741aa6c BG |
472 | reword|r) |
473 | comment_for_reflog reword | |
474 | ||
475 | mark_action_done | |
476 | pick_one $sha1 || | |
477 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
6f6bee3b | 478 | git commit --amend --no-post-rewrite |
b079feed | 479 | record_in_rewritten $sha1 |
6741aa6c | 480 | ;; |
f8babc4d | 481 | edit|e) |
1b1dce4b JS |
482 | comment_for_reflog edit |
483 | ||
484 | mark_action_done | |
485 | pick_one $sha1 || | |
486 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
b079feed | 487 | echo "$1" > "$DOTEST"/stopped-sha |
1b1dce4b | 488 | make_patch $sha1 |
a4049ae7 | 489 | git rev-parse --verify HEAD > "$AMEND" |
a8ccc204 | 490 | warn "Stopped at $sha1... $rest" |
1b1dce4b JS |
491 | warn "You can amend the commit now, with" |
492 | warn | |
493 | warn " git commit --amend" | |
494 | warn | |
0460fb44 JS |
495 | warn "Once you are satisfied with your changes, run" |
496 | warn | |
497 | warn " git rebase --continue" | |
498 | warn | |
1b1dce4b JS |
499 | exit 0 |
500 | ;; | |
0205e72f MH |
501 | squash|s|fixup|f) |
502 | case "$command" in | |
503 | squash|s) | |
504 | squash_style=squash | |
505 | ;; | |
506 | fixup|f) | |
507 | squash_style=fixup | |
508 | ;; | |
509 | esac | |
510 | comment_for_reflog $squash_style | |
1b1dce4b | 511 | |
12dd1112 | 512 | test -f "$DONE" && has_action "$DONE" || |
0205e72f | 513 | die "Cannot '$squash_style' without a previous commit" |
1b1dce4b JS |
514 | |
515 | mark_action_done | |
a25eb139 | 516 | update_squash_messages $squash_style $sha1 |
7c418836 | 517 | author_script=$(get_author_ident_from_commit HEAD) |
5c5d059a MH |
518 | echo "$author_script" > "$AUTHOR_SCRIPT" |
519 | eval "$author_script" | |
7c418836 | 520 | output git reset --soft HEAD^ |
6bdcd0d2 | 521 | pick_one -n $sha1 || die_failed_squash $sha1 "$rest" |
6368f3f8 | 522 | case "$(peek_next_command)" in |
0205e72f | 523 | squash|s|fixup|f) |
a25eb139 MH |
524 | # This is an intermediate commit; its message will only be |
525 | # used in case of trouble. So use the long version: | |
6bdcd0d2 MH |
526 | do_with_author output git commit --no-verify -F "$SQUASH_MSG" || |
527 | die_failed_squash $sha1 "$rest" | |
376ccb8c | 528 | ;; |
6368f3f8 | 529 | *) |
a25eb139 | 530 | # This is the final command of this squash/fixup group |
6bdcd0d2 | 531 | if test -f "$FIXUP_MSG" |
a25eb139 | 532 | then |
6bdcd0d2 MH |
533 | do_with_author git commit --no-verify -F "$FIXUP_MSG" || |
534 | die_failed_squash $sha1 "$rest" | |
535 | else | |
536 | cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit | |
537 | rm -f "$GIT_DIR"/MERGE_MSG | |
538 | do_with_author git commit --no-verify -e || | |
539 | die_failed_squash $sha1 "$rest" | |
a25eb139 | 540 | fi |
6bdcd0d2 | 541 | rm -f "$SQUASH_MSG" "$FIXUP_MSG" |
376ccb8c | 542 | ;; |
6368f3f8 | 543 | esac |
b079feed | 544 | record_in_rewritten $sha1 |
1b1dce4b JS |
545 | ;; |
546 | *) | |
547 | warn "Unknown command: $command $sha1 $rest" | |
f1be316a JK |
548 | if git rev-parse --verify -q "$sha1" >/dev/null |
549 | then | |
550 | die_with_patch $sha1 "Please fix this in the file $TODO." | |
551 | else | |
552 | die "Please fix this in the file $TODO." | |
553 | fi | |
376ccb8c | 554 | ;; |
1b1dce4b JS |
555 | esac |
556 | test -s "$TODO" && return | |
557 | ||
68a163c9 JS |
558 | comment_for_reflog finish && |
559 | HEADNAME=$(cat "$DOTEST"/head-name) && | |
560 | OLDHEAD=$(cat "$DOTEST"/head) && | |
561 | SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) && | |
72583e6c | 562 | NEWHEAD=$(git rev-parse HEAD) && |
73697a0b JS |
563 | case $HEADNAME in |
564 | refs/*) | |
a0c0447b | 565 | message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" && |
73697a0b JS |
566 | git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD && |
567 | git symbolic-ref HEAD $HEADNAME | |
568 | ;; | |
569 | esac && { | |
3df0a859 | 570 | test ! -f "$DOTEST"/verbose || |
f3d5e463 | 571 | git diff-tree --stat $(cat "$DOTEST"/head)..HEAD |
3df0a859 | 572 | } && |
eb2151bb TR |
573 | { |
574 | git notes copy --for-rewrite=rebase < "$REWRITTEN_LIST" || | |
575 | true # we don't care if this copying failed | |
576 | } && | |
b079feed TR |
577 | if test -x "$GIT_DIR"/hooks/post-rewrite && |
578 | test -s "$REWRITTEN_LIST"; then | |
579 | "$GIT_DIR"/hooks/post-rewrite rebase < "$REWRITTEN_LIST" | |
580 | true # we don't care if this hook failed | |
581 | fi && | |
1b1dce4b | 582 | rm -rf "$DOTEST" && |
73697a0b | 583 | git gc --auto && |
1b1dce4b JS |
584 | warn "Successfully rebased and updated $HEADNAME." |
585 | ||
586 | exit | |
587 | } | |
588 | ||
589 | do_rest () { | |
590 | while : | |
591 | do | |
592 | do_next | |
593 | done | |
1b1dce4b JS |
594 | } |
595 | ||
0e757e30 JS |
596 | # skip picking commits whose parents are unchanged |
597 | skip_unnecessary_picks () { | |
598 | fd=3 | |
599 | while read command sha1 rest | |
600 | do | |
601 | # fd=3 means we skip the command | |
602 | case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in | |
603 | 3,pick,"$ONTO"*|3,p,"$ONTO"*) | |
604 | # pick a commit whose parent is current $ONTO -> skip | |
605 | ONTO=$sha1 | |
606 | ;; | |
607 | 3,#*|3,,*) | |
608 | # copy comments | |
609 | ;; | |
610 | *) | |
611 | fd=1 | |
612 | ;; | |
613 | esac | |
614 | echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd | |
615 | done <"$TODO" >"$TODO.new" 3>>"$DONE" && | |
b079feed TR |
616 | mv -f "$TODO".new "$TODO" && |
617 | case "$(peek_next_command)" in | |
618 | squash|s|fixup|f) | |
619 | record_in_rewritten "$ONTO" | |
620 | ;; | |
621 | esac || | |
0e757e30 JS |
622 | die "Could not skip unnecessary pick commands" |
623 | } | |
624 | ||
7970aaf0 SB |
625 | # check if no other options are set |
626 | is_standalone () { | |
627 | test $# -eq 2 -a "$2" = '--' && | |
628 | test -z "$ONTO" && | |
629 | test -z "$PRESERVE_MERGES" && | |
630 | test -z "$STRATEGY" && | |
631 | test -z "$VERBOSE" | |
632 | } | |
633 | ||
634 | get_saved_options () { | |
635 | test -d "$REWRITTEN" && PRESERVE_MERGES=t | |
636 | test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)" | |
637 | test -f "$DOTEST"/verbose && VERBOSE=t | |
a6c7a276 | 638 | test -f "$DOTEST"/rebase-root && REBASE_ROOT=t |
7970aaf0 SB |
639 | } |
640 | ||
f59baa50 NS |
641 | # Rearrange the todo list that has both "pick sha1 msg" and |
642 | # "pick sha1 fixup!/squash! msg" appears in it so that the latter | |
643 | # comes immediately after the former, and change "pick" to | |
644 | # "fixup"/"squash". | |
645 | rearrange_squash () { | |
646 | sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \ | |
647 | -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \ | |
648 | "$1" >"$1.sq" | |
649 | test -s "$1.sq" || return | |
650 | ||
651 | used= | |
652 | while read pick sha1 message | |
653 | do | |
654 | case " $used" in | |
655 | *" $sha1 "*) continue ;; | |
656 | esac | |
657 | echo "$pick $sha1 $message" | |
658 | while read squash action msg | |
659 | do | |
660 | case "$message" in | |
661 | "$msg"*) | |
662 | echo "$action $squash $action! $msg" | |
663 | used="$used$squash " | |
664 | ;; | |
665 | esac | |
666 | done <"$1.sq" | |
667 | done >"$1.rearranged" <"$1" | |
668 | cat "$1.rearranged" >"$1" | |
669 | rm -f "$1.sq" "$1.rearranged" | |
670 | } | |
671 | ||
230a4566 NS |
672 | LF=' |
673 | ' | |
674 | parse_onto () { | |
675 | case "$1" in | |
676 | *...*) | |
677 | if left=${1%...*} right=${1#*...} && | |
678 | onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD}) | |
679 | then | |
680 | case "$onto" in | |
681 | ?*"$LF"?* | '') | |
682 | exit 1 ;; | |
683 | esac | |
684 | echo "$onto" | |
685 | exit 0 | |
686 | fi | |
687 | esac | |
688 | git rev-parse --verify "$1^0" | |
689 | } | |
690 | ||
822f7c73 | 691 | while test $# != 0 |
1b1dce4b JS |
692 | do |
693 | case "$1" in | |
c4427656 NS |
694 | --no-verify) |
695 | OK_TO_SKIP_PRE_REBASE=yes | |
696 | ;; | |
697 | --verify) | |
698 | ;; | |
1b1dce4b | 699 | --continue) |
7970aaf0 SB |
700 | is_standalone "$@" || usage |
701 | get_saved_options | |
1b1dce4b JS |
702 | comment_for_reflog continue |
703 | ||
704 | test -d "$DOTEST" || die "No interactive rebase running" | |
705 | ||
ab119032 JH |
706 | # Sanity check |
707 | git rev-parse --verify HEAD >/dev/null || | |
708 | die "Cannot read HEAD" | |
6848d58c JS |
709 | git update-index --ignore-submodules --refresh && |
710 | git diff-files --quiet --ignore-submodules || | |
ab119032 JH |
711 | die "Working tree is dirty" |
712 | ||
713 | # do we have anything to commit? | |
6848d58c | 714 | if git diff-index --cached --quiet --ignore-submodules HEAD -- |
03270628 | 715 | then |
ab119032 JH |
716 | : Nothing to commit -- skip this |
717 | else | |
0aac0de4 | 718 | . "$AUTHOR_SCRIPT" || |
ab119032 | 719 | die "Cannot find the author identity" |
8beb1f33 | 720 | amend= |
a4049ae7 | 721 | if test -f "$AMEND" |
ab119032 | 722 | then |
8beb1f33 | 723 | amend=$(git rev-parse --verify HEAD) |
a4049ae7 | 724 | test "$amend" = $(cat "$AMEND") || |
c14c3c82 DP |
725 | die "\ |
726 | You have uncommitted changes in your working tree. Please, commit them | |
727 | first and then run 'git rebase --continue' again." | |
ab119032 JH |
728 | git reset --soft HEAD^ || |
729 | die "Cannot rewind the HEAD" | |
730 | fi | |
7756ecff | 731 | do_with_author git commit --no-verify -F "$MSG" -e || { |
8beb1f33 DP |
732 | test -n "$amend" && git reset --soft $amend |
733 | die "Could not commit staged changes." | |
734 | } | |
b079feed | 735 | record_in_rewritten "$(cat "$DOTEST"/stopped-sha)" |
03270628 | 736 | fi |
18640d99 | 737 | |
1b1dce4b JS |
738 | require_clean_work_tree |
739 | do_rest | |
740 | ;; | |
741 | --abort) | |
7970aaf0 SB |
742 | is_standalone "$@" || usage |
743 | get_saved_options | |
1b1dce4b JS |
744 | comment_for_reflog abort |
745 | ||
ecfe72ff | 746 | git rerere clear |
1b1dce4b JS |
747 | test -d "$DOTEST" || die "No interactive rebase running" |
748 | ||
749 | HEADNAME=$(cat "$DOTEST"/head-name) | |
750 | HEAD=$(cat "$DOTEST"/head) | |
73697a0b JS |
751 | case $HEADNAME in |
752 | refs/*) | |
753 | git symbolic-ref HEAD $HEADNAME | |
754 | ;; | |
755 | esac && | |
dfa49f33 | 756 | output git reset --hard $HEAD && |
1b1dce4b JS |
757 | rm -rf "$DOTEST" |
758 | exit | |
759 | ;; | |
760 | --skip) | |
7970aaf0 SB |
761 | is_standalone "$@" || usage |
762 | get_saved_options | |
1b1dce4b JS |
763 | comment_for_reflog skip |
764 | ||
ecfe72ff | 765 | git rerere clear |
1b1dce4b JS |
766 | test -d "$DOTEST" || die "No interactive rebase running" |
767 | ||
dfa49f33 | 768 | output git reset --hard && do_rest |
1b1dce4b | 769 | ;; |
7970aaf0 | 770 | -s) |
1b1dce4b JS |
771 | case "$#,$1" in |
772 | *,*=*) | |
b5e960b1 | 773 | STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;; |
1b1dce4b JS |
774 | 1,*) |
775 | usage ;; | |
776 | *) | |
777 | STRATEGY="-s $2" | |
778 | shift ;; | |
779 | esac | |
780 | ;; | |
7970aaf0 | 781 | -m) |
1b1dce4b JS |
782 | # we use merge anyway |
783 | ;; | |
7970aaf0 | 784 | -v) |
1b1dce4b JS |
785 | VERBOSE=t |
786 | ;; | |
7970aaf0 | 787 | -p) |
f09c9b8c JS |
788 | PRESERVE_MERGES=t |
789 | ;; | |
7970aaf0 | 790 | -i) |
1b1dce4b JS |
791 | # yeah, we know |
792 | ;; | |
d911d146 TR |
793 | --root) |
794 | REBASE_ROOT=t | |
795 | ;; | |
f59baa50 NS |
796 | --autosquash) |
797 | AUTOSQUASH=t | |
798 | ;; | |
7970aaf0 SB |
799 | --onto) |
800 | shift | |
230a4566 | 801 | ONTO=$(parse_onto "$1") || |
7970aaf0 | 802 | die "Does not point to a valid commit: $1" |
1b1dce4b | 803 | ;; |
7970aaf0 SB |
804 | --) |
805 | shift | |
277d7e91 JS |
806 | test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 || |
807 | test ! -z "$REBASE_ROOT" -a $# -le 1 || usage | |
1b1dce4b JS |
808 | test -d "$DOTEST" && |
809 | die "Interactive rebase already started" | |
810 | ||
811 | git var GIT_COMMITTER_IDENT >/dev/null || | |
812 | die "You need to set your committer info first" | |
813 | ||
d911d146 TR |
814 | if test -z "$REBASE_ROOT" |
815 | then | |
816 | UPSTREAM_ARG="$1" | |
817 | UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base" | |
818 | test -z "$ONTO" && ONTO=$UPSTREAM | |
819 | shift | |
820 | else | |
a6c7a276 | 821 | UPSTREAM= |
d911d146 TR |
822 | UPSTREAM_ARG=--root |
823 | test -z "$ONTO" && | |
824 | die "You must specify --onto when using --root" | |
825 | fi | |
826 | run_pre_rebase_hook "$UPSTREAM_ARG" "$@" | |
d8fab023 | 827 | |
1b1dce4b JS |
828 | comment_for_reflog start |
829 | ||
1b1dce4b JS |
830 | require_clean_work_tree |
831 | ||
d911d146 | 832 | if test ! -z "$1" |
1b1dce4b | 833 | then |
d911d146 TR |
834 | output git show-ref --verify --quiet "refs/heads/$1" || |
835 | die "Invalid branchname: $1" | |
836 | output git checkout "$1" || | |
837 | die "Could not checkout $1" | |
1b1dce4b JS |
838 | fi |
839 | ||
840 | HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?" | |
5166810b MK |
841 | mkdir "$DOTEST" || die "Could not create temporary $DOTEST" |
842 | ||
1b1dce4b | 843 | : > "$DOTEST"/interactive || die "Could not mark as interactive" |
73697a0b JS |
844 | git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null || |
845 | echo "detached HEAD" > "$DOTEST"/head-name | |
1b1dce4b JS |
846 | |
847 | echo $HEAD > "$DOTEST"/head | |
a6c7a276 JH |
848 | case "$REBASE_ROOT" in |
849 | '') | |
850 | rm -f "$DOTEST"/rebase-root ;; | |
851 | *) | |
852 | : >"$DOTEST"/rebase-root ;; | |
853 | esac | |
1b1dce4b | 854 | echo $ONTO > "$DOTEST"/onto |
8e4a91bd | 855 | test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy |
1b1dce4b | 856 | test t = "$VERBOSE" && : > "$DOTEST"/verbose |
3b38ec16 | 857 | if test t = "$PRESERVE_MERGES" |
f09c9b8c | 858 | then |
d911d146 TR |
859 | if test -z "$REBASE_ROOT" |
860 | then | |
861 | mkdir "$REWRITTEN" && | |
862 | for c in $(git merge-base --all $HEAD $UPSTREAM) | |
863 | do | |
864 | echo $ONTO > "$REWRITTEN"/$c || | |
865 | die "Could not init rewritten commits" | |
866 | done | |
867 | else | |
868 | mkdir "$REWRITTEN" && | |
869 | echo $ONTO > "$REWRITTEN"/root || | |
f09c9b8c | 870 | die "Could not init rewritten commits" |
d911d146 | 871 | fi |
acc8559a SH |
872 | # No cherry-pick because our first pass is to determine |
873 | # parents to rewrite and skipping dropped commits would | |
874 | # prematurely end our probe | |
f09c9b8c | 875 | MERGES_OPTION= |
d80d6bc1 | 876 | first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)" |
f09c9b8c | 877 | else |
acc8559a | 878 | MERGES_OPTION="--no-merges --cherry-pick" |
f09c9b8c | 879 | fi |
1b1dce4b | 880 | |
c54b7817 JS |
881 | SHORTHEAD=$(git rev-parse --short $HEAD) |
882 | SHORTONTO=$(git rev-parse --short $ONTO) | |
d911d146 TR |
883 | if test -z "$REBASE_ROOT" |
884 | # this is now equivalent to ! -z "$UPSTREAM" | |
885 | then | |
886 | SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM) | |
887 | REVISIONS=$UPSTREAM...$HEAD | |
888 | SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD | |
889 | else | |
890 | REVISIONS=$ONTO...$HEAD | |
891 | SHORTREVISIONS=$SHORTHEAD | |
892 | fi | |
6047a234 | 893 | git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \ |
acc8559a | 894 | --abbrev=7 --reverse --left-right --topo-order \ |
d911d146 | 895 | $REVISIONS | \ |
acc8559a SH |
896 | sed -n "s/^>//p" | while read shortsha1 rest |
897 | do | |
898 | if test t != "$PRESERVE_MERGES" | |
899 | then | |
900 | echo "pick $shortsha1 $rest" >> "$TODO" | |
901 | else | |
902 | sha1=$(git rev-parse $shortsha1) | |
d911d146 TR |
903 | if test -z "$REBASE_ROOT" |
904 | then | |
905 | preserve=t | |
906 | for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-) | |
907 | do | |
1830d9cb | 908 | if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \) |
d911d146 TR |
909 | then |
910 | preserve=f | |
911 | fi | |
912 | done | |
913 | else | |
914 | preserve=f | |
915 | fi | |
acc8559a SH |
916 | if test f = "$preserve" |
917 | then | |
918 | touch "$REWRITTEN"/$sha1 | |
919 | echo "pick $shortsha1 $rest" >> "$TODO" | |
920 | fi | |
921 | fi | |
922 | done | |
923 | ||
924 | # Watch for commits that been dropped by --cherry-pick | |
925 | if test t = "$PRESERVE_MERGES" | |
926 | then | |
927 | mkdir "$DROPPED" | |
928 | # Save all non-cherry-picked changes | |
d911d146 | 929 | git rev-list $REVISIONS --left-right --cherry-pick | \ |
acc8559a SH |
930 | sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks |
931 | # Now all commits and note which ones are missing in | |
932 | # not-cherry-picks and hence being dropped | |
d911d146 | 933 | git rev-list $REVISIONS | |
e249044c | 934 | while read rev |
acc8559a | 935 | do |
e1622bfc | 936 | if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = "" |
acc8559a SH |
937 | then |
938 | # Use -f2 because if rev-list is telling us this commit is | |
939 | # not worthwhile, we don't want to track its multiple heads, | |
940 | # just the history of its first-parent for others that will | |
941 | # be rebasing on top of it | |
d911d146 | 942 | git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev |
e249044c | 943 | short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev) |
e1622bfc | 944 | sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO" |
acc8559a SH |
945 | rm "$REWRITTEN"/$rev |
946 | fi | |
947 | done | |
948 | fi | |
d911d146 | 949 | |
ff74126c | 950 | test -s "$TODO" || echo noop >> "$TODO" |
f59baa50 | 951 | test -n "$AUTOSQUASH" && rearrange_squash "$TODO" |
6047a234 JS |
952 | cat >> "$TODO" << EOF |
953 | ||
d911d146 | 954 | # Rebase $SHORTREVISIONS onto $SHORTONTO |
1b1dce4b JS |
955 | # |
956 | # Commands: | |
88b1f0b8 | 957 | # p, pick = use commit |
6741aa6c | 958 | # r, reword = use commit, but edit the commit message |
88b1f0b8 MV |
959 | # e, edit = use commit, but stop for amending |
960 | # s, squash = use commit, but meld into previous commit | |
0205e72f | 961 | # f, fixup = like "squash", but discard this commit's log message |
82576ddb JS |
962 | # |
963 | # If you remove a line here THAT COMMIT WILL BE LOST. | |
6047a234 | 964 | # However, if you remove everything, the rebase will be aborted. |
82576ddb | 965 | # |
1b1dce4b | 966 | EOF |
1b1dce4b | 967 | |
376ccb8c | 968 | has_action "$TODO" || |
c54b7817 | 969 | die_abort "Nothing to do" |
1b1dce4b JS |
970 | |
971 | cp "$TODO" "$TODO".backup | |
ef0c2abf | 972 | git_editor "$TODO" || |
e49ca974 | 973 | die_abort "Could not execute editor" |
1b1dce4b | 974 | |
376ccb8c | 975 | has_action "$TODO" || |
c54b7817 JS |
976 | die_abort "Nothing to do" |
977 | ||
0e757e30 JS |
978 | test -d "$REWRITTEN" || skip_unnecessary_picks |
979 | ||
22e40795 | 980 | git update-ref ORIG_HEAD $HEAD |
dfa49f33 | 981 | output git checkout $ONTO && do_rest |
376ccb8c | 982 | ;; |
1b1dce4b JS |
983 | esac |
984 | shift | |
985 | done |