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