]>
Commit | Line | Data |
---|---|---|
11d62145 JN |
1 | # This shell script fragment is sourced by git-rebase to implement |
2 | # its interactive mode. "git rebase --interactive" makes it easy | |
3 | # to fix up commits in the middle of a series and rearrange commits. | |
1b1dce4b JS |
4 | # |
5 | # Copyright (c) 2006 Johannes E. Schindelin | |
1b1dce4b JS |
6 | # |
7 | # The original idea comes from Eric W. Biederman, in | |
8 | # http://article.gmane.org/gmane.comp.version-control.git/22407 | |
572a7c52 | 9 | # |
80883bb3 MH |
10 | # The file containing rebase commands, comments, and empty lines. |
11 | # This file is created by "git rebase -i" then edited by the user. As | |
12 | # the lines are processed, they are removed from the front of this | |
6bb4e485 | 13 | # file and written to the tail of $done. |
431b7e78 | 14 | todo="$state_dir"/git-rebase-todo |
80883bb3 MH |
15 | |
16 | # The rebase command lines that have already been processed. A line | |
17 | # is moved here when it is first handled, before any associated user | |
18 | # actions. | |
431b7e78 | 19 | done="$state_dir"/done |
80883bb3 MH |
20 | |
21 | # The commit message that is planned to be used for any changes that | |
22 | # need to be committed following a user interaction. | |
431b7e78 | 23 | msg="$state_dir"/message |
80883bb3 MH |
24 | |
25 | # The file into which is accumulated the suggested commit message for | |
26 | # squash/fixup commands. When the first of a series of squash/fixups | |
27 | # is seen, the file is created and the commit message from the | |
28 | # previous commit and from the first squash/fixup commit are written | |
29 | # to it. The commit message for each subsequent squash/fixup commit | |
30 | # is appended to the file as it is processed. | |
31 | # | |
32 | # The first line of the file is of the form | |
6bb4e485 MZ |
33 | # # This is a combination of $count commits. |
34 | # where $count is the number of commits whose messages have been | |
80883bb3 MH |
35 | # written to the file so far (including the initial "pick" commit). |
36 | # Each time that a commit message is processed, this line is read and | |
37 | # updated. It is deleted just before the combined commit is made. | |
431b7e78 | 38 | squash_msg="$state_dir"/message-squash |
80883bb3 | 39 | |
a25eb139 MH |
40 | # If the current series of squash/fixups has not yet included a squash |
41 | # command, then this file exists and holds the commit message of the | |
42 | # original "pick" commit. (If the series ends without a "squash" | |
43 | # command, then this can be used as the commit message of the combined | |
44 | # commit without opening the editor.) | |
431b7e78 | 45 | fixup_msg="$state_dir"/message-fixup |
a25eb139 | 46 | |
6bb4e485 MZ |
47 | # $rewritten is the name of a directory containing files for each |
48 | # commit that is reachable by at least one merge base of $head and | |
49 | # $upstream. They are not necessarily rewritten, but their children | |
80883bb3 MH |
50 | # might be. This ensures that commits on merged, but otherwise |
51 | # unrelated side branches are left alone. (Think "X" in the man page's | |
52 | # example.) | |
431b7e78 | 53 | rewritten="$state_dir"/rewritten |
80883bb3 | 54 | |
431b7e78 | 55 | dropped="$state_dir"/dropped |
0aac0de4 | 56 | |
b71dc3e1 ZK |
57 | end="$state_dir"/end |
58 | msgnum="$state_dir"/msgnum | |
59 | ||
0aac0de4 MH |
60 | # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
61 | # GIT_AUTHOR_DATE that will be used for the commit that is currently | |
62 | # being rebased. | |
431b7e78 | 63 | author_script="$state_dir"/author-script |
0aac0de4 | 64 | |
a4049ae7 MH |
65 | # When an "edit" rebase command is being processed, the SHA1 of the |
66 | # commit to be edited is recorded in this file. When "git rebase | |
67 | # --continue" is executed, if there are any staged changes then they | |
68 | # will be amended to the HEAD commit, but only provided the HEAD | |
69 | # commit is still the commit to be edited. When any other rebase | |
70 | # command is processed, this file is deleted. | |
431b7e78 | 71 | amend="$state_dir"/amend |
a4049ae7 | 72 | |
b079feed TR |
73 | # For the post-rewrite hook, we make a list of rewritten commits and |
74 | # their new sha1s. The rewritten-pending list keeps the sha1s of | |
75 | # commits that have been processed, but not committed yet, | |
76 | # e.g. because they are waiting for a 'squash' command. | |
431b7e78 MZ |
77 | rewritten_list="$state_dir"/rewritten-list |
78 | rewritten_pending="$state_dir"/rewritten-pending | |
6bb4e485 | 79 | |
db2b3b82 AF |
80 | strategy_args= |
81 | if test -n "$do_merge" | |
82 | then | |
83 | strategy_args=${strategy:+--strategy=$strategy} | |
84 | eval ' | |
85 | for strategy_opt in '"$strategy_opts"' | |
86 | do | |
87 | strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")" | |
88 | done | |
89 | ' | |
90 | fi | |
91 | ||
89c7ae9c | 92 | GIT_CHERRY_PICK_HELP="$resolvemsg" |
804c7174 WC |
93 | export GIT_CHERRY_PICK_HELP |
94 | ||
180bad3d JK |
95 | comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1) |
96 | : ${comment_char:=#} | |
97 | ||
1b1dce4b | 98 | warn () { |
938791cd | 99 | printf '%s\n' "$*" >&2 |
1b1dce4b JS |
100 | } |
101 | ||
ee0a4afb MH |
102 | # Output the commit message for the specified commit. |
103 | commit_message () { | |
104 | git cat-file commit "$1" | sed "1,/^$/d" | |
105 | } | |
106 | ||
6bb4e485 | 107 | orig_reflog_action="$GIT_REFLOG_ACTION" |
1b1dce4b JS |
108 | |
109 | comment_for_reflog () { | |
6bb4e485 | 110 | case "$orig_reflog_action" in |
1b1dce4b JS |
111 | ''|rebase*) |
112 | GIT_REFLOG_ACTION="rebase -i ($1)" | |
113 | export GIT_REFLOG_ACTION | |
376ccb8c | 114 | ;; |
1b1dce4b JS |
115 | esac |
116 | } | |
117 | ||
4e673877 | 118 | last_count= |
1b1dce4b | 119 | mark_action_done () { |
6bb4e485 MZ |
120 | sed -e 1q < "$todo" >> "$done" |
121 | sed -e 1d < "$todo" >> "$todo".new | |
122 | mv -f "$todo".new "$todo" | |
180bad3d | 123 | new_count=$(git stripspace --strip-comments <"$done" | wc -l) |
b71dc3e1 | 124 | echo $new_count >"$msgnum" |
180bad3d | 125 | total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l))) |
b71dc3e1 | 126 | echo $total >"$end" |
6bb4e485 | 127 | if test "$last_count" != "$new_count" |
4e673877 | 128 | then |
6bb4e485 MZ |
129 | last_count=$new_count |
130 | printf "Rebasing (%d/%d)\r" $new_count $total | |
131 | test -z "$verbose" || echo | |
4e673877 | 132 | fi |
1b1dce4b JS |
133 | } |
134 | ||
8cbc57ca PH |
135 | # Put the last action marked done at the beginning of the todo list |
136 | # again. If there has not been an action marked done yet, leave the list of | |
137 | # items on the todo list unchanged. | |
138 | reschedule_last_action () { | |
139 | tail -n 1 "$done" | cat - "$todo" >"$todo".new | |
140 | sed -e \$d <"$done" >"$done".new | |
141 | mv -f "$todo".new "$todo" | |
142 | mv -f "$done".new "$done" | |
143 | } | |
144 | ||
fcc5ef1c | 145 | append_todo_help () { |
180bad3d JK |
146 | git stripspace --comment-lines >>"$todo" <<\EOF |
147 | ||
148 | Commands: | |
149 | p, pick = use commit | |
150 | r, reword = use commit, but edit the commit message | |
151 | e, edit = use commit, but stop for amending | |
152 | s, squash = use commit, but meld into previous commit | |
153 | f, fixup = like "squash", but discard this commit's log message | |
154 | x, exec = run command (the rest of the line) using shell | |
155 | ||
156 | These lines can be re-ordered; they are executed from top to bottom. | |
157 | ||
158 | If you remove a line here THAT COMMIT WILL BE LOST. | |
fcc5ef1c AW |
159 | EOF |
160 | } | |
161 | ||
1b1dce4b | 162 | make_patch () { |
4fb1a19d JS |
163 | sha1_and_parents="$(git rev-list --parents -1 "$1")" |
164 | case "$sha1_and_parents" in | |
165 | ?*' '?*' '?*) | |
166 | git diff --cc $sha1_and_parents | |
167 | ;; | |
168 | ?*' '?*) | |
169 | git diff-tree -p "$1^!" | |
170 | ;; | |
171 | *) | |
172 | echo "Root commit" | |
173 | ;; | |
431b7e78 | 174 | esac > "$state_dir"/patch |
6bb4e485 MZ |
175 | test -f "$msg" || |
176 | commit_message "$1" > "$msg" | |
177 | test -f "$author_script" || | |
178 | get_author_ident_from_commit "$1" > "$author_script" | |
1b1dce4b JS |
179 | } |
180 | ||
181 | die_with_patch () { | |
431b7e78 | 182 | echo "$1" > "$state_dir"/stopped-sha |
1b1dce4b | 183 | make_patch "$1" |
ecfe72ff | 184 | git rerere |
1b1dce4b JS |
185 | die "$2" |
186 | } | |
187 | ||
0becb3e4 AW |
188 | exit_with_patch () { |
189 | echo "$1" > "$state_dir"/stopped-sha | |
190 | make_patch $1 | |
191 | git rev-parse --verify HEAD > "$amend" | |
3ee5e540 | 192 | gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} |
0becb3e4 AW |
193 | warn "You can amend the commit now, with" |
194 | warn | |
3ee5e540 | 195 | warn " git commit --amend $gpg_sign_opt_quoted" |
0becb3e4 AW |
196 | warn |
197 | warn "Once you are satisfied with your changes, run" | |
198 | warn | |
199 | warn " git rebase --continue" | |
200 | warn | |
201 | exit $2 | |
202 | } | |
203 | ||
c54b7817 | 204 | die_abort () { |
431b7e78 | 205 | rm -rf "$state_dir" |
c54b7817 JS |
206 | die "$1" |
207 | } | |
208 | ||
376ccb8c | 209 | has_action () { |
180bad3d | 210 | test -n "$(git stripspace --strip-comments <"$1")" |
376ccb8c JS |
211 | } |
212 | ||
90e1818f NH |
213 | is_empty_commit() { |
214 | tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null || | |
215 | die "$1: not a commit that can be picked") | |
216 | ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null || | |
217 | ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904) | |
218 | test "$tree" = "$ptree" | |
219 | } | |
220 | ||
98697784 PH |
221 | is_merge_commit() |
222 | { | |
223 | git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1 | |
224 | } | |
225 | ||
7756ecff MH |
226 | # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
227 | # GIT_AUTHOR_DATE exported from the current environment. | |
228 | do_with_author () { | |
76c9c0db JH |
229 | ( |
230 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE | |
231 | "$@" | |
232 | ) | |
7756ecff MH |
233 | } |
234 | ||
821881d8 PO |
235 | git_sequence_editor () { |
236 | if test -z "$GIT_SEQUENCE_EDITOR" | |
237 | then | |
238 | GIT_SEQUENCE_EDITOR="$(git config sequence.editor)" | |
239 | if [ -z "$GIT_SEQUENCE_EDITOR" ] | |
240 | then | |
241 | GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $? | |
242 | fi | |
243 | fi | |
244 | ||
245 | eval "$GIT_SEQUENCE_EDITOR" '"$@"' | |
246 | } | |
247 | ||
1b1dce4b | 248 | pick_one () { |
8e75abfd | 249 | ff=--ff |
90e1818f | 250 | |
8e75abfd | 251 | case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac |
6bb4e485 | 252 | case "$force_rebase" in '') ;; ?*) ff= ;; esac |
dfa49f33 | 253 | output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1" |
90e1818f NH |
254 | |
255 | if is_empty_commit "$sha1" | |
256 | then | |
257 | empty_args="--allow-empty" | |
258 | fi | |
259 | ||
6bb4e485 | 260 | test -d "$rewritten" && |
f09c9b8c | 261 | pick_one_preserving_merges "$@" && return |
3ee5e540 NV |
262 | output eval git cherry-pick \ |
263 | ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \ | |
264 | "$strategy_args" $empty_args $ff "$@" | |
8cbc57ca PH |
265 | |
266 | # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule | |
267 | # previous task so this commit is not lost. | |
268 | ret=$? | |
269 | case "$ret" in [01]) ;; *) reschedule_last_action ;; esac | |
270 | return $ret | |
1b1dce4b JS |
271 | } |
272 | ||
f09c9b8c | 273 | pick_one_preserving_merges () { |
71d9451e TR |
274 | fast_forward=t |
275 | case "$1" in | |
276 | -n) | |
277 | fast_forward=f | |
278 | sha1=$2 | |
279 | ;; | |
280 | *) | |
281 | sha1=$1 | |
282 | ;; | |
283 | esac | |
f09c9b8c JS |
284 | sha1=$(git rev-parse $sha1) |
285 | ||
431b7e78 | 286 | if test -f "$state_dir"/current-commit |
f09c9b8c | 287 | then |
4c1360f4 | 288 | if test "$fast_forward" = t |
bb645071 | 289 | then |
41f556b9 | 290 | while read current_commit |
bb645071 | 291 | do |
6bb4e485 | 292 | git rev-parse HEAD > "$rewritten"/$current_commit |
431b7e78 MZ |
293 | done <"$state_dir"/current-commit |
294 | rm "$state_dir"/current-commit || | |
bb645071 SH |
295 | die "Cannot write current commit's replacement sha1" |
296 | fi | |
f09c9b8c JS |
297 | fi |
298 | ||
431b7e78 | 299 | echo $sha1 >> "$state_dir"/current-commit |
a96dc01e | 300 | |
f09c9b8c | 301 | # rewrite parents; if none were rewritten, we can fast-forward. |
f09c9b8c | 302 | new_parents= |
d911d146 TR |
303 | pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)" |
304 | if test "$pend" = " " | |
305 | then | |
306 | pend=" root" | |
307 | fi | |
faae853c | 308 | while [ "$pend" != "" ] |
f09c9b8c | 309 | do |
faae853c SH |
310 | p=$(expr "$pend" : ' \([^ ]*\)') |
311 | pend="${pend# $p}" | |
312 | ||
6bb4e485 | 313 | if test -f "$rewritten"/$p |
f09c9b8c | 314 | then |
6bb4e485 | 315 | new_p=$(cat "$rewritten"/$p) |
80fe82e4 SH |
316 | |
317 | # If the todo reordered commits, and our parent is marked for | |
318 | # rewriting, but hasn't been gotten to yet, assume the user meant to | |
319 | # drop it on top of the current HEAD | |
320 | if test -z "$new_p" | |
321 | then | |
322 | new_p=$(git rev-parse HEAD) | |
323 | fi | |
324 | ||
f09c9b8c JS |
325 | test $p != $new_p && fast_forward=f |
326 | case "$new_parents" in | |
327 | *$new_p*) | |
328 | ;; # do nothing; that parent is already there | |
329 | *) | |
330 | new_parents="$new_parents $new_p" | |
376ccb8c | 331 | ;; |
f09c9b8c | 332 | esac |
1c5fa0a1 | 333 | else |
6bb4e485 | 334 | if test -f "$dropped"/$p |
faae853c SH |
335 | then |
336 | fast_forward=f | |
6bb4e485 | 337 | replacement="$(cat "$dropped"/$p)" |
d911d146 TR |
338 | test -z "$replacement" && replacement=root |
339 | pend=" $replacement$pend" | |
faae853c SH |
340 | else |
341 | new_parents="$new_parents $p" | |
342 | fi | |
f09c9b8c JS |
343 | fi |
344 | done | |
345 | case $fast_forward in | |
346 | t) | |
a75d7b54 | 347 | output warn "Fast-forward to $sha1" |
71d9451e | 348 | output git reset --hard $sha1 || |
a75d7b54 | 349 | die "Cannot fast-forward to $sha1" |
f09c9b8c JS |
350 | ;; |
351 | f) | |
376ccb8c | 352 | first_parent=$(expr "$new_parents" : ' \([^ ]*\)') |
a4f25e36 SH |
353 | |
354 | if [ "$1" != "-n" ] | |
355 | then | |
356 | # detach HEAD to current parent | |
357 | output git checkout $first_parent 2> /dev/null || | |
358 | die "Cannot move HEAD to $first_parent" | |
359 | fi | |
f09c9b8c | 360 | |
f09c9b8c | 361 | case "$new_parents" in |
376ccb8c | 362 | ' '*' '*) |
a4f25e36 SH |
363 | test "a$1" = a-n && die "Refusing to squash a merge: $sha1" |
364 | ||
f09c9b8c | 365 | # redo merge |
6bb4e485 MZ |
366 | author_script_content=$(get_author_ident_from_commit $sha1) |
367 | eval "$author_script_content" | |
368 | msg_content="$(commit_message $sha1)" | |
f91333d6 BS |
369 | # No point in merging the first parent, that's HEAD |
370 | new_parents=${new_parents# $first_parent} | |
a9f739c1 | 371 | merge_args="--no-log --no-ff" |
db2b3b82 | 372 | if ! do_with_author output eval \ |
3ee5e540 NV |
373 | 'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \ |
374 | $merge_args $strategy_args -m "$msg_content" $new_parents' | |
f09c9b8c | 375 | then |
6bb4e485 | 376 | printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG |
f5b49ea6 | 377 | die_with_patch $sha1 "Error redoing merge $sha1" |
f09c9b8c | 378 | fi |
6bb4e485 | 379 | echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list" |
f09c9b8c JS |
380 | ;; |
381 | *) | |
3ee5e540 NV |
382 | output eval git cherry-pick \ |
383 | ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \ | |
384 | "$strategy_args" "$@" || | |
f09c9b8c | 385 | die_with_patch $sha1 "Could not pick $sha1" |
376ccb8c | 386 | ;; |
f09c9b8c | 387 | esac |
376ccb8c | 388 | ;; |
f09c9b8c JS |
389 | esac |
390 | } | |
391 | ||
6368f3f8 JS |
392 | nth_string () { |
393 | case "$1" in | |
394 | *1[0-9]|*[04-9]) echo "$1"th;; | |
395 | *1) echo "$1"st;; | |
396 | *2) echo "$1"nd;; | |
397 | *3) echo "$1"rd;; | |
398 | esac | |
399 | } | |
400 | ||
a25eb139 | 401 | update_squash_messages () { |
6bb4e485 MZ |
402 | if test -f "$squash_msg"; then |
403 | mv "$squash_msg" "$squash_msg".bak || exit | |
404 | count=$(($(sed -n \ | |
180bad3d | 405 | -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \ |
6bb4e485 | 406 | -e "q" < "$squash_msg".bak)+1)) |
bde1a686 | 407 | { |
180bad3d | 408 | printf '%s\n' "$comment_char This is a combination of $count commits." |
bde1a686 MH |
409 | sed -e 1d -e '2,/^./{ |
410 | /^$/d | |
6bb4e485 MZ |
411 | }' <"$squash_msg".bak |
412 | } >"$squash_msg" | |
6368f3f8 | 413 | else |
6bb4e485 MZ |
414 | commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg" |
415 | count=2 | |
bde1a686 | 416 | { |
180bad3d JK |
417 | printf '%s\n' "$comment_char This is a combination of 2 commits." |
418 | printf '%s\n' "$comment_char The first commit's message is:" | |
bde1a686 | 419 | echo |
6bb4e485 MZ |
420 | cat "$fixup_msg" |
421 | } >"$squash_msg" | |
6368f3f8 | 422 | fi |
0205e72f MH |
423 | case $1 in |
424 | squash) | |
6bb4e485 | 425 | rm -f "$fixup_msg" |
0205e72f | 426 | echo |
180bad3d | 427 | printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:" |
0205e72f | 428 | echo |
ee0a4afb | 429 | commit_message $2 |
0205e72f MH |
430 | ;; |
431 | fixup) | |
432 | echo | |
180bad3d | 433 | printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:" |
0205e72f | 434 | echo |
180bad3d JK |
435 | # Change the space after the comment character to TAB: |
436 | commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /' | |
0205e72f | 437 | ;; |
6bb4e485 | 438 | esac >>"$squash_msg" |
6368f3f8 JS |
439 | } |
440 | ||
441 | peek_next_command () { | |
180bad3d | 442 | git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q |
6368f3f8 JS |
443 | } |
444 | ||
6bdcd0d2 MH |
445 | # A squash/fixup has failed. Prepare the long version of the squash |
446 | # commit message, then die_with_patch. This code path requires the | |
447 | # user to edit the combined commit message for all commits that have | |
448 | # been squashed/fixedup so far. So also erase the old squash | |
449 | # messages, effectively causing the combined commit to be used as the | |
450 | # new basis for any further squash/fixups. Args: sha1 rest | |
451 | die_failed_squash() { | |
6bb4e485 MZ |
452 | mv "$squash_msg" "$msg" || exit |
453 | rm -f "$fixup_msg" | |
454 | cp "$msg" "$GIT_DIR"/MERGE_MSG || exit | |
6bdcd0d2 MH |
455 | warn |
456 | warn "Could not apply $1... $2" | |
457 | die_with_patch $1 "" | |
458 | } | |
459 | ||
b079feed | 460 | flush_rewritten_pending() { |
6bb4e485 | 461 | test -s "$rewritten_pending" || return |
b079feed | 462 | newsha1="$(git rev-parse HEAD^0)" |
6bb4e485 MZ |
463 | sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list" |
464 | rm -f "$rewritten_pending" | |
b079feed TR |
465 | } |
466 | ||
467 | record_in_rewritten() { | |
468 | oldsha1="$(git rev-parse $1)" | |
6bb4e485 | 469 | echo "$oldsha1" >> "$rewritten_pending" |
b079feed TR |
470 | |
471 | case "$(peek_next_command)" in | |
41f556b9 | 472 | squash|s|fixup|f) |
b079feed | 473 | ;; |
41f556b9 | 474 | *) |
b079feed TR |
475 | flush_rewritten_pending |
476 | ;; | |
477 | esac | |
478 | } | |
479 | ||
df5df20c CW |
480 | do_pick () { |
481 | if test "$(git rev-parse HEAD)" = "$squash_onto" | |
482 | then | |
483 | # Set the correct commit message and author info on the | |
484 | # sentinel root before cherry-picking the original changes | |
485 | # without committing (-n). Finally, update the sentinel again | |
486 | # to include these changes. If the cherry-pick results in a | |
487 | # conflict, this means our behaviour is similar to a standard | |
488 | # failed cherry-pick during rebase, with a dirty index to | |
489 | # resolve before manually running git commit --amend then git | |
490 | # rebase --continue. | |
491 | git commit --allow-empty --allow-empty-message --amend \ | |
492 | --no-post-rewrite -n -q -C $1 && | |
493 | pick_one -n $1 && | |
494 | git commit --allow-empty --allow-empty-message \ | |
3ee5e540 NV |
495 | --amend --no-post-rewrite -n -q -C $1 \ |
496 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
df5df20c CW |
497 | die_with_patch $1 "Could not apply $1... $2" |
498 | else | |
499 | pick_one $1 || | |
500 | die_with_patch $1 "Could not apply $1... $2" | |
501 | fi | |
502 | } | |
503 | ||
1b1dce4b | 504 | do_next () { |
b12d3e90 | 505 | rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit |
6bb4e485 | 506 | read -r command sha1 rest < "$todo" |
1b1dce4b | 507 | case "$command" in |
180bad3d | 508 | "$comment_char"*|''|noop) |
1b1dce4b | 509 | mark_action_done |
1b1dce4b | 510 | ;; |
f8babc4d | 511 | pick|p) |
1b1dce4b JS |
512 | comment_for_reflog pick |
513 | ||
514 | mark_action_done | |
df5df20c | 515 | do_pick $sha1 "$rest" |
b079feed | 516 | record_in_rewritten $sha1 |
1b1dce4b | 517 | ;; |
6741aa6c BG |
518 | reword|r) |
519 | comment_for_reflog reword | |
520 | ||
521 | mark_action_done | |
df5df20c | 522 | do_pick $sha1 "$rest" |
3ee5e540 | 523 | git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || { |
0becb3e4 AW |
524 | warn "Could not amend commit after successfully picking $sha1... $rest" |
525 | warn "This is most likely due to an empty commit message, or the pre-commit hook" | |
526 | warn "failed. If the pre-commit hook failed, you may need to resolve the issue before" | |
527 | warn "you are able to reword the commit." | |
528 | exit_with_patch $sha1 1 | |
529 | } | |
b079feed | 530 | record_in_rewritten $sha1 |
6741aa6c | 531 | ;; |
f8babc4d | 532 | edit|e) |
1b1dce4b JS |
533 | comment_for_reflog edit |
534 | ||
535 | mark_action_done | |
df5df20c | 536 | do_pick $sha1 "$rest" |
a8ccc204 | 537 | warn "Stopped at $sha1... $rest" |
0becb3e4 | 538 | exit_with_patch $sha1 0 |
1b1dce4b | 539 | ;; |
0205e72f MH |
540 | squash|s|fixup|f) |
541 | case "$command" in | |
542 | squash|s) | |
543 | squash_style=squash | |
544 | ;; | |
545 | fixup|f) | |
546 | squash_style=fixup | |
547 | ;; | |
548 | esac | |
549 | comment_for_reflog $squash_style | |
1b1dce4b | 550 | |
6bb4e485 | 551 | test -f "$done" && has_action "$done" || |
0205e72f | 552 | die "Cannot '$squash_style' without a previous commit" |
1b1dce4b JS |
553 | |
554 | mark_action_done | |
a25eb139 | 555 | update_squash_messages $squash_style $sha1 |
6bb4e485 MZ |
556 | author_script_content=$(get_author_ident_from_commit HEAD) |
557 | echo "$author_script_content" > "$author_script" | |
558 | eval "$author_script_content" | |
2147f844 CW |
559 | if ! pick_one -n $sha1 |
560 | then | |
561 | git rev-parse --verify HEAD >"$amend" | |
562 | die_failed_squash $sha1 "$rest" | |
563 | fi | |
6368f3f8 | 564 | case "$(peek_next_command)" in |
0205e72f | 565 | squash|s|fixup|f) |
a25eb139 MH |
566 | # This is an intermediate commit; its message will only be |
567 | # used in case of trouble. So use the long version: | |
3ee5e540 NV |
568 | do_with_author output git commit --amend --no-verify -F "$squash_msg" \ |
569 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
6bdcd0d2 | 570 | die_failed_squash $sha1 "$rest" |
376ccb8c | 571 | ;; |
6368f3f8 | 572 | *) |
a25eb139 | 573 | # This is the final command of this squash/fixup group |
6bb4e485 | 574 | if test -f "$fixup_msg" |
a25eb139 | 575 | then |
3ee5e540 NV |
576 | do_with_author git commit --amend --no-verify -F "$fixup_msg" \ |
577 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
6bdcd0d2 MH |
578 | die_failed_squash $sha1 "$rest" |
579 | else | |
6bb4e485 | 580 | cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit |
6bdcd0d2 | 581 | rm -f "$GIT_DIR"/MERGE_MSG |
3ee5e540 NV |
582 | do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \ |
583 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
6bdcd0d2 | 584 | die_failed_squash $sha1 "$rest" |
a25eb139 | 585 | fi |
6bb4e485 | 586 | rm -f "$squash_msg" "$fixup_msg" |
376ccb8c | 587 | ;; |
6368f3f8 | 588 | esac |
b079feed | 589 | record_in_rewritten $sha1 |
1b1dce4b | 590 | ;; |
cd035b1c | 591 | x|"exec") |
6bb4e485 | 592 | read -r command rest < "$todo" |
cd035b1c MM |
593 | mark_action_done |
594 | printf 'Executing: %s\n' "$rest" | |
cd035b1c MM |
595 | ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution |
596 | status=$? | |
1686519a JH |
597 | # Run in subshell because require_clean_work_tree can die. |
598 | dirty=f | |
599 | (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t | |
cd035b1c MM |
600 | if test "$status" -ne 0 |
601 | then | |
602 | warn "Execution failed: $rest" | |
1686519a JH |
603 | test "$dirty" = f || |
604 | warn "and made changes to the index and/or the working tree" | |
605 | ||
cd035b1c MM |
606 | warn "You can fix the problem, and then run" |
607 | warn | |
608 | warn " git rebase --continue" | |
609 | warn | |
ecfe1ea9 JS |
610 | if test $status -eq 127 # command not found |
611 | then | |
612 | status=1 | |
613 | fi | |
cd035b1c | 614 | exit "$status" |
1686519a | 615 | elif test "$dirty" = t |
cd035b1c | 616 | then |
1686519a JH |
617 | warn "Execution succeeded: $rest" |
618 | warn "but left changes to the index and/or the working tree" | |
cd035b1c MM |
619 | warn "Commit or stash your changes, and then run" |
620 | warn | |
621 | warn " git rebase --continue" | |
622 | warn | |
623 | exit 1 | |
624 | fi | |
625 | ;; | |
1b1dce4b JS |
626 | *) |
627 | warn "Unknown command: $command $sha1 $rest" | |
9c8e1011 | 628 | fixtodo="Please fix this using 'git rebase --edit-todo'." |
f1be316a JK |
629 | if git rev-parse --verify -q "$sha1" >/dev/null |
630 | then | |
9c8e1011 | 631 | die_with_patch $sha1 "$fixtodo" |
f1be316a | 632 | else |
9c8e1011 | 633 | die "$fixtodo" |
f1be316a | 634 | fi |
376ccb8c | 635 | ;; |
1b1dce4b | 636 | esac |
6bb4e485 | 637 | test -s "$todo" && return |
1b1dce4b | 638 | |
68a163c9 | 639 | comment_for_reflog finish && |
6bb4e485 | 640 | newhead=$(git rev-parse HEAD) && |
431b7e78 | 641 | case $head_name in |
73697a0b | 642 | refs/*) |
1af221ef | 643 | message="$GIT_REFLOG_ACTION: $head_name onto $onto" && |
431b7e78 | 644 | git update-ref -m "$message" $head_name $newhead $orig_head && |
53f2ffa8 JK |
645 | git symbolic-ref \ |
646 | -m "$GIT_REFLOG_ACTION: returning to $head_name" \ | |
647 | HEAD $head_name | |
73697a0b JS |
648 | ;; |
649 | esac && { | |
431b7e78 | 650 | test ! -f "$state_dir"/verbose || |
2959c283 | 651 | git diff-tree --stat $orig_head..HEAD |
3df0a859 | 652 | } && |
eb2151bb | 653 | { |
6bb4e485 MZ |
654 | test -s "$rewritten_list" && |
655 | git notes copy --for-rewrite=rebase < "$rewritten_list" || | |
eb2151bb TR |
656 | true # we don't care if this copying failed |
657 | } && | |
b849b954 NTND |
658 | hook="$(git rev-parse --git-path hooks/post-rewrite)" |
659 | if test -x "$hook" && test -s "$rewritten_list"; then | |
660 | "$hook" rebase < "$rewritten_list" | |
b079feed TR |
661 | true # we don't care if this hook failed |
662 | fi && | |
431b7e78 | 663 | warn "Successfully rebased and updated $head_name." |
1b1dce4b | 664 | |
15d4bf2e | 665 | return 1 # not failure; just to break the do_rest loop |
1b1dce4b JS |
666 | } |
667 | ||
15d4bf2e | 668 | # can only return 0, when the infinite loop breaks |
1b1dce4b JS |
669 | do_rest () { |
670 | while : | |
671 | do | |
15d4bf2e | 672 | do_next || break |
1b1dce4b | 673 | done |
1b1dce4b JS |
674 | } |
675 | ||
0e757e30 JS |
676 | # skip picking commits whose parents are unchanged |
677 | skip_unnecessary_picks () { | |
678 | fd=3 | |
2d6ca6ef | 679 | while read -r command rest |
0e757e30 JS |
680 | do |
681 | # fd=3 means we skip the command | |
2d6ca6ef BC |
682 | case "$fd,$command" in |
683 | 3,pick|3,p) | |
6bb4e485 | 684 | # pick a commit whose parent is current $onto -> skip |
14d87298 | 685 | sha1=${rest%% *} |
2d6ca6ef | 686 | case "$(git rev-parse --verify --quiet "$sha1"^)" in |
6bb4e485 MZ |
687 | "$onto"*) |
688 | onto=$sha1 | |
2d6ca6ef BC |
689 | ;; |
690 | *) | |
691 | fd=1 | |
692 | ;; | |
693 | esac | |
0e757e30 | 694 | ;; |
7bca7afe | 695 | 3,"$comment_char"*|3,) |
0e757e30 JS |
696 | # copy comments |
697 | ;; | |
698 | *) | |
699 | fd=1 | |
700 | ;; | |
701 | esac | |
d1c3b10f | 702 | printf '%s\n' "$command${rest:+ }$rest" >&$fd |
6bb4e485 MZ |
703 | done <"$todo" >"$todo.new" 3>>"$done" && |
704 | mv -f "$todo".new "$todo" && | |
b079feed TR |
705 | case "$(peek_next_command)" in |
706 | squash|s|fixup|f) | |
6bb4e485 | 707 | record_in_rewritten "$onto" |
b079feed TR |
708 | ;; |
709 | esac || | |
0e757e30 JS |
710 | die "Could not skip unnecessary pick commands" |
711 | } | |
712 | ||
75c69766 JH |
713 | transform_todo_ids () { |
714 | while read -r command rest | |
715 | do | |
716 | case "$command" in | |
717 | "$comment_char"* | exec) | |
718 | # Be careful for oddball commands like 'exec' | |
719 | # that do not have a SHA-1 at the beginning of $rest. | |
720 | ;; | |
721 | *) | |
722 | sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) && | |
723 | rest="$sha1 ${rest#* }" | |
724 | ;; | |
725 | esac | |
726 | printf '%s\n' "$command${rest:+ }$rest" | |
727 | done <"$todo" >"$todo.new" && | |
728 | mv -f "$todo.new" "$todo" | |
729 | } | |
730 | ||
731 | expand_todo_ids() { | |
732 | transform_todo_ids | |
733 | } | |
734 | ||
735 | collapse_todo_ids() { | |
56895038 | 736 | transform_todo_ids --short |
75c69766 JH |
737 | } |
738 | ||
f59baa50 NS |
739 | # Rearrange the todo list that has both "pick sha1 msg" and |
740 | # "pick sha1 fixup!/squash! msg" appears in it so that the latter | |
741 | # comes immediately after the former, and change "pick" to | |
742 | # "fixup"/"squash". | |
743 | rearrange_squash () { | |
68d5d03b KB |
744 | # extract fixup!/squash! lines and resolve any referenced sha1's |
745 | while read -r pick sha1 message | |
746 | do | |
747 | case "$message" in | |
748 | "squash! "*|"fixup! "*) | |
749 | action="${message%%!*}" | |
22c5b136 AP |
750 | rest=$message |
751 | prefix= | |
752 | # skip all squash! or fixup! (but save for later) | |
753 | while : | |
754 | do | |
755 | case "$rest" in | |
756 | "squash! "*|"fixup! "*) | |
757 | prefix="$prefix${rest%%!*}," | |
758 | rest="${rest#*! }" | |
759 | ;; | |
760 | *) | |
761 | break | |
762 | ;; | |
763 | esac | |
764 | done | |
47be0660 | 765 | printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest" |
68d5d03b KB |
766 | # if it's a single word, try to resolve to a full sha1 and |
767 | # emit a second copy. This allows us to match on both message | |
768 | # and on sha1 prefix | |
769 | if test "${rest#* }" = "$rest"; then | |
770 | fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)" | |
771 | if test -n "$fullsha"; then | |
772 | # prefix the action to uniquely identify this line as | |
773 | # intended for full sha1 match | |
22c5b136 | 774 | echo "$sha1 +$action $prefix $fullsha" |
68d5d03b KB |
775 | fi |
776 | fi | |
777 | esac | |
778 | done >"$1.sq" <"$1" | |
f59baa50 NS |
779 | test -s "$1.sq" || return |
780 | ||
781 | used= | |
57f2b6b2 | 782 | while read -r pick sha1 message |
f59baa50 NS |
783 | do |
784 | case " $used" in | |
785 | *" $sha1 "*) continue ;; | |
786 | esac | |
938791cd | 787 | printf '%s\n' "$pick $sha1 $message" |
d3d7a421 | 788 | used="$used$sha1 " |
22c5b136 | 789 | while read -r squash action msg_prefix msg_content |
f59baa50 | 790 | do |
d3d7a421 KB |
791 | case " $used" in |
792 | *" $squash "*) continue ;; | |
793 | esac | |
68d5d03b KB |
794 | emit=0 |
795 | case "$action" in | |
796 | +*) | |
797 | action="${action#+}" | |
798 | # full sha1 prefix test | |
6bb4e485 | 799 | case "$msg_content" in "$sha1"*) emit=1;; esac ;; |
68d5d03b KB |
800 | *) |
801 | # message prefix test | |
6bb4e485 | 802 | case "$message" in "$msg_content"*) emit=1;; esac ;; |
68d5d03b KB |
803 | esac |
804 | if test $emit = 1; then | |
22c5b136 AP |
805 | real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g") |
806 | printf '%s\n' "$action $squash ${real_prefix}$msg_content" | |
f59baa50 | 807 | used="$used$squash " |
68d5d03b | 808 | fi |
f59baa50 NS |
809 | done <"$1.sq" |
810 | done >"$1.rearranged" <"$1" | |
811 | cat "$1.rearranged" >"$1" | |
812 | rm -f "$1.sq" "$1.rearranged" | |
813 | } | |
814 | ||
c2145384 LK |
815 | # Add commands after a pick or after a squash/fixup serie |
816 | # in the todo list. | |
817 | add_exec_commands () { | |
818 | { | |
819 | first=t | |
820 | while read -r insn rest | |
821 | do | |
822 | case $insn in | |
823 | pick) | |
824 | test -n "$first" || | |
825 | printf "%s" "$cmd" | |
826 | ;; | |
827 | esac | |
828 | printf "%s %s\n" "$insn" "$rest" | |
829 | first= | |
830 | done | |
831 | printf "%s" "$cmd" | |
832 | } <"$1" >"$1.new" && | |
833 | mv "$1.new" "$1" | |
834 | } | |
835 | ||
9f50d32b KM |
836 | # The whole contents of this file is run by dot-sourcing it from |
837 | # inside a shell function. It used to be that "return"s we see | |
838 | # below were not inside any function, and expected to return | |
839 | # to the function that dot-sourced us. | |
840 | # | |
841 | # However, FreeBSD /bin/sh misbehaves on such a construct and | |
842 | # continues to run the statements that follow such a "return". | |
843 | # As a work-around, we introduce an extra layer of a function | |
844 | # here, and immediately call it after defining it. | |
845 | git_rebase__interactive () { | |
846 | ||
cf432ca0 MZ |
847 | case "$action" in |
848 | continue) | |
cf432ca0 | 849 | # do we have anything to commit? |
a6754cda | 850 | if git diff-index --cached --quiet HEAD -- |
cf432ca0 | 851 | then |
0e0aff4b JS |
852 | # Nothing to commit -- skip this commit |
853 | ||
854 | test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD || | |
855 | rm "$GIT_DIR"/CHERRY_PICK_HEAD || | |
856 | die "Could not remove CHERRY_PICK_HEAD" | |
cf432ca0 | 857 | else |
ffaaed88 MM |
858 | if ! test -f "$author_script" |
859 | then | |
3ee5e540 | 860 | gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} |
ffaaed88 MM |
861 | die "You have staged changes in your working tree. If these changes are meant to be |
862 | squashed into the previous commit, run: | |
863 | ||
3ee5e540 | 864 | git commit --amend $gpg_sign_opt_quoted |
ffaaed88 MM |
865 | |
866 | If they are meant to go into a new commit, run: | |
867 | ||
3ee5e540 | 868 | git commit $gpg_sign_opt_quoted |
ffaaed88 MM |
869 | |
870 | In both case, once you're done, continue with: | |
871 | ||
872 | git rebase --continue | |
873 | " | |
874 | fi | |
cf432ca0 | 875 | . "$author_script" || |
ffaaed88 | 876 | die "Error trying to find the author identity to amend commit" |
cf432ca0 | 877 | if test -f "$amend" |
03270628 | 878 | then |
cf432ca0 MZ |
879 | current_head=$(git rev-parse --verify HEAD) |
880 | test "$current_head" = $(cat "$amend") || | |
881 | die "\ | |
c14c3c82 DP |
882 | You have uncommitted changes in your working tree. Please, commit them |
883 | first and then run 'git rebase --continue' again." | |
3ee5e540 NV |
884 | do_with_author git commit --amend --no-verify -F "$msg" -e \ |
885 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
2147f844 CW |
886 | die "Could not commit staged changes." |
887 | else | |
3ee5e540 NV |
888 | do_with_author git commit --no-verify -F "$msg" -e \ |
889 | ${gpg_sign_opt:+"$gpg_sign_opt"} || | |
2147f844 | 890 | die "Could not commit staged changes." |
03270628 | 891 | fi |
cf432ca0 | 892 | fi |
18640d99 | 893 | |
b12d3e90 MM |
894 | if test -r "$state_dir"/stopped-sha |
895 | then | |
896 | record_in_rewritten "$(cat "$state_dir"/stopped-sha)" | |
897 | fi | |
0acb62f2 | 898 | |
cf432ca0 MZ |
899 | require_clean_work_tree "rebase" |
900 | do_rest | |
15d4bf2e | 901 | return 0 |
cf432ca0 | 902 | ;; |
cf432ca0 | 903 | skip) |
cf432ca0 | 904 | git rerere clear |
cf432ca0 | 905 | |
2959c283 | 906 | do_rest |
15d4bf2e | 907 | return 0 |
cf432ca0 | 908 | ;; |
eb9a7cb4 | 909 | edit-todo) |
180bad3d | 910 | git stripspace --strip-comments <"$todo" >"$todo".new |
eb9a7cb4 | 911 | mv -f "$todo".new "$todo" |
75c69766 | 912 | collapse_todo_ids |
eb9a7cb4 | 913 | append_todo_help |
180bad3d JK |
914 | git stripspace --comment-lines >>"$todo" <<\EOF |
915 | ||
916 | You are editing the todo file of an ongoing interactive rebase. | |
917 | To continue rebase after editing, run: | |
918 | git rebase --continue | |
919 | ||
eb9a7cb4 AW |
920 | EOF |
921 | ||
922 | git_sequence_editor "$todo" || | |
923 | die "Could not execute editor" | |
75c69766 | 924 | expand_todo_ids |
eb9a7cb4 AW |
925 | |
926 | exit | |
927 | ;; | |
cf432ca0 | 928 | esac |
1b1dce4b | 929 | |
34262322 MZ |
930 | git var GIT_COMMITTER_IDENT >/dev/null || |
931 | die "You need to set your committer info first" | |
1b1dce4b | 932 | |
34262322 MZ |
933 | comment_for_reflog start |
934 | ||
71786f54 | 935 | if test ! -z "$switch_to" |
34262322 | 936 | then |
26cd160c | 937 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" |
71786f54 | 938 | output git checkout "$switch_to" -- || |
26cd160c RR |
939 | die "Could not checkout $switch_to" |
940 | ||
941 | comment_for_reflog start | |
34262322 MZ |
942 | fi |
943 | ||
431b7e78 | 944 | orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?" |
1224f3d0 | 945 | mkdir -p "$state_dir" || die "Could not create temporary $state_dir" |
34262322 | 946 | |
431b7e78 | 947 | : > "$state_dir"/interactive || die "Could not mark as interactive" |
84df4560 | 948 | write_basic_state |
6bb4e485 | 949 | if test t = "$preserve_merges" |
34262322 | 950 | then |
6bb4e485 | 951 | if test -z "$rebase_root" |
34262322 | 952 | then |
6bb4e485 | 953 | mkdir "$rewritten" && |
431b7e78 | 954 | for c in $(git merge-base --all $orig_head $upstream) |
acc8559a | 955 | do |
6bb4e485 | 956 | echo $onto > "$rewritten"/$c || |
34262322 | 957 | die "Could not init rewritten commits" |
acc8559a | 958 | done |
34262322 | 959 | else |
6bb4e485 MZ |
960 | mkdir "$rewritten" && |
961 | echo $onto > "$rewritten"/root || | |
34262322 MZ |
962 | die "Could not init rewritten commits" |
963 | fi | |
964 | # No cherry-pick because our first pass is to determine | |
965 | # parents to rewrite and skipping dropped commits would | |
966 | # prematurely end our probe | |
6bb4e485 | 967 | merges_option= |
34262322 | 968 | else |
6bb4e485 | 969 | merges_option="--no-merges --cherry-pick" |
34262322 MZ |
970 | fi |
971 | ||
431b7e78 | 972 | shorthead=$(git rev-parse --short $orig_head) |
6bb4e485 MZ |
973 | shortonto=$(git rev-parse --short $onto) |
974 | if test -z "$rebase_root" | |
975 | # this is now equivalent to ! -z "$upstream" | |
34262322 | 976 | then |
6bb4e485 | 977 | shortupstream=$(git rev-parse --short $upstream) |
431b7e78 | 978 | revisions=$upstream...$orig_head |
6bb4e485 | 979 | shortrevisions=$shortupstream..$shorthead |
34262322 | 980 | else |
431b7e78 | 981 | revisions=$onto...$orig_head |
6bb4e485 | 982 | shortrevisions=$shorthead |
34262322 | 983 | fi |
edb72d55 | 984 | git rev-list $merges_option --pretty=oneline --reverse --left-right --topo-order \ |
1e0dacdb | 985 | $revisions ${restrict_revision+^$restrict_revision} | \ |
34262322 | 986 | sed -n "s/^>//p" | |
edb72d55 | 987 | while read -r sha1 rest |
34262322 | 988 | do |
90e1818f | 989 | |
edb72d55 | 990 | if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1 |
90e1818f | 991 | then |
180bad3d | 992 | comment_out="$comment_char " |
90e1818f NH |
993 | else |
994 | comment_out= | |
995 | fi | |
996 | ||
6bb4e485 | 997 | if test t != "$preserve_merges" |
34262322 | 998 | then |
edb72d55 | 999 | printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo" |
34262322 | 1000 | else |
6bb4e485 | 1001 | if test -z "$rebase_root" |
acc8559a | 1002 | then |
34262322 MZ |
1003 | preserve=t |
1004 | for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-) | |
acc8559a | 1005 | do |
12bf8283 | 1006 | if test -f "$rewritten"/$p |
acc8559a | 1007 | then |
34262322 | 1008 | preserve=f |
acc8559a SH |
1009 | fi |
1010 | done | |
34262322 MZ |
1011 | else |
1012 | preserve=f | |
1013 | fi | |
1014 | if test f = "$preserve" | |
1015 | then | |
6bb4e485 | 1016 | touch "$rewritten"/$sha1 |
edb72d55 | 1017 | printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo" |
acc8559a | 1018 | fi |
34262322 MZ |
1019 | fi |
1020 | done | |
d911d146 | 1021 | |
34262322 | 1022 | # Watch for commits that been dropped by --cherry-pick |
6bb4e485 | 1023 | if test t = "$preserve_merges" |
34262322 | 1024 | then |
6bb4e485 | 1025 | mkdir "$dropped" |
34262322 | 1026 | # Save all non-cherry-picked changes |
6bb4e485 | 1027 | git rev-list $revisions --left-right --cherry-pick | \ |
431b7e78 | 1028 | sed -n "s/^>//p" > "$state_dir"/not-cherry-picks |
34262322 MZ |
1029 | # Now all commits and note which ones are missing in |
1030 | # not-cherry-picks and hence being dropped | |
6bb4e485 | 1031 | git rev-list $revisions | |
34262322 MZ |
1032 | while read rev |
1033 | do | |
e425f6ad | 1034 | if test -f "$rewritten"/$rev && test "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = "" |
34262322 MZ |
1035 | then |
1036 | # Use -f2 because if rev-list is telling us this commit is | |
1037 | # not worthwhile, we don't want to track its multiple heads, | |
1038 | # just the history of its first-parent for others that will | |
1039 | # be rebasing on top of it | |
6bb4e485 | 1040 | git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev |
edb72d55 KS |
1041 | sha1=$(git rev-list -1 $rev) |
1042 | sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo" | |
6bb4e485 | 1043 | rm "$rewritten"/$rev |
34262322 MZ |
1044 | fi |
1045 | done | |
1046 | fi | |
1047 | ||
6bb4e485 MZ |
1048 | test -s "$todo" || echo noop >> "$todo" |
1049 | test -n "$autosquash" && rearrange_squash "$todo" | |
c2145384 LK |
1050 | test -n "$cmd" && add_exec_commands "$todo" |
1051 | ||
97f05f43 | 1052 | todocount=$(git stripspace --strip-comments <"$todo" | wc -l) |
28c8cfc3 | 1053 | todocount=${todocount##* } |
97f05f43 | 1054 | |
180bad3d | 1055 | cat >>"$todo" <<EOF |
6047a234 | 1056 | |
2185d3b7 | 1057 | $comment_char Rebase $shortrevisions onto $shortonto ($todocount command(s)) |
fcc5ef1c AW |
1058 | EOF |
1059 | append_todo_help | |
180bad3d JK |
1060 | git stripspace --comment-lines >>"$todo" <<\EOF |
1061 | ||
1062 | However, if you remove everything, the rebase will be aborted. | |
1063 | ||
1b1dce4b | 1064 | EOF |
1b1dce4b | 1065 | |
90e1818f NH |
1066 | if test -z "$keep_empty" |
1067 | then | |
180bad3d | 1068 | printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo" |
90e1818f NH |
1069 | fi |
1070 | ||
1071 | ||
6bb4e485 | 1072 | has_action "$todo" || |
e4244eb3 | 1073 | return 2 |
1b1dce4b | 1074 | |
6bb4e485 | 1075 | cp "$todo" "$todo".backup |
edb72d55 | 1076 | collapse_todo_ids |
821881d8 | 1077 | git_sequence_editor "$todo" || |
34262322 | 1078 | die_abort "Could not execute editor" |
1b1dce4b | 1079 | |
6bb4e485 | 1080 | has_action "$todo" || |
e4244eb3 | 1081 | return 2 |
c54b7817 | 1082 | |
75c69766 JH |
1083 | expand_todo_ids |
1084 | ||
6bb4e485 | 1085 | test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks |
0e757e30 | 1086 | |
26cd160c | 1087 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" |
6bb4e485 | 1088 | output git checkout $onto || die_abort "could not detach HEAD" |
431b7e78 | 1089 | git update-ref ORIG_HEAD $orig_head |
34262322 | 1090 | do_rest |
9f50d32b KM |
1091 | |
1092 | } | |
1093 | # ... and then we call the whole thing. | |
1094 | git_rebase__interactive |