THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1068 lines
29KB

  1. # This shell script fragment is sourced by git-rebase to implement its
  2. # preserve-merges mode.
  3. #
  4. # Copyright (c) 2006 Johannes E. Schindelin
  5. #
  6. # The file containing rebase commands, comments, and empty lines.
  7. # This file is created by "git rebase -i" then edited by the user. As
  8. # the lines are processed, they are removed from the front of this
  9. # file and written to the tail of $done.
  10. todo="$state_dir"/git-rebase-todo
  11. # The rebase command lines that have already been processed. A line
  12. # is moved here when it is first handled, before any associated user
  13. # actions.
  14. done="$state_dir"/done
  15. # The commit message that is planned to be used for any changes that
  16. # need to be committed following a user interaction.
  17. msg="$state_dir"/message
  18. # The file into which is accumulated the suggested commit message for
  19. # squash/fixup commands. When the first of a series of squash/fixups
  20. # is seen, the file is created and the commit message from the
  21. # previous commit and from the first squash/fixup commit are written
  22. # to it. The commit message for each subsequent squash/fixup commit
  23. # is appended to the file as it is processed.
  24. #
  25. # The first line of the file is of the form
  26. # # This is a combination of $count commits.
  27. # where $count is the number of commits whose messages have been
  28. # written to the file so far (including the initial "pick" commit).
  29. # Each time that a commit message is processed, this line is read and
  30. # updated. It is deleted just before the combined commit is made.
  31. squash_msg="$state_dir"/message-squash
  32. # If the current series of squash/fixups has not yet included a squash
  33. # command, then this file exists and holds the commit message of the
  34. # original "pick" commit. (If the series ends without a "squash"
  35. # command, then this can be used as the commit message of the combined
  36. # commit without opening the editor.)
  37. fixup_msg="$state_dir"/message-fixup
  38. # $rewritten is the name of a directory containing files for each
  39. # commit that is reachable by at least one merge base of $head and
  40. # $upstream. They are not necessarily rewritten, but their children
  41. # might be. This ensures that commits on merged, but otherwise
  42. # unrelated side branches are left alone. (Think "X" in the man page's
  43. # example.)
  44. rewritten="$state_dir"/rewritten
  45. dropped="$state_dir"/dropped
  46. end="$state_dir"/end
  47. msgnum="$state_dir"/msgnum
  48. # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
  49. # GIT_AUTHOR_DATE that will be used for the commit that is currently
  50. # being rebased.
  51. author_script="$state_dir"/author-script
  52. # When an "edit" rebase command is being processed, the SHA1 of the
  53. # commit to be edited is recorded in this file. When "git rebase
  54. # --continue" is executed, if there are any staged changes then they
  55. # will be amended to the HEAD commit, but only provided the HEAD
  56. # commit is still the commit to be edited. When any other rebase
  57. # command is processed, this file is deleted.
  58. amend="$state_dir"/amend
  59. # For the post-rewrite hook, we make a list of rewritten commits and
  60. # their new sha1s. The rewritten-pending list keeps the sha1s of
  61. # commits that have been processed, but not committed yet,
  62. # e.g. because they are waiting for a 'squash' command.
  63. rewritten_list="$state_dir"/rewritten-list
  64. rewritten_pending="$state_dir"/rewritten-pending
  65. # Work around Git for Windows' Bash whose "read" does not strip CRLF
  66. # and leaves CR at the end instead.
  67. cr=$(printf "\015")
  68. resolvemsg="
  69. $(gettext 'Resolve all conflicts manually, mark them as resolved with
  70. "git add/rm <conflicted_files>", then run "git rebase --continue".
  71. You can instead skip this commit: run "git rebase --skip".
  72. To abort and get back to the state before "git rebase", run "git rebase --abort".')
  73. "
  74. write_basic_state () {
  75. echo "$head_name" > "$state_dir"/head-name &&
  76. echo "$onto" > "$state_dir"/onto &&
  77. echo "$orig_head" > "$state_dir"/orig-head &&
  78. test t = "$GIT_QUIET" && : > "$state_dir"/quiet
  79. test t = "$verbose" && : > "$state_dir"/verbose
  80. test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
  81. test -n "$strategy_opts" && echo "$strategy_opts" > \
  82. "$state_dir"/strategy_opts
  83. test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
  84. "$state_dir"/allow_rerere_autoupdate
  85. test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
  86. test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff
  87. test -n "$reschedule_failed_exec" && : > "$state_dir"/reschedule-failed-exec
  88. }
  89. apply_autostash () {
  90. if test -f "$state_dir/autostash"
  91. then
  92. stash_sha1=$(cat "$state_dir/autostash")
  93. if git stash apply $stash_sha1 >/dev/null 2>&1
  94. then
  95. echo "$(gettext 'Applied autostash.')" >&2
  96. else
  97. git stash store -m "autostash" -q $stash_sha1 ||
  98. die "$(eval_gettext "Cannot store \$stash_sha1")"
  99. gettext 'Applying autostash resulted in conflicts.
  100. Your changes are safe in the stash.
  101. You can run "git stash pop" or "git stash drop" at any time.
  102. ' >&2
  103. fi
  104. fi
  105. }
  106. output () {
  107. case "$verbose" in
  108. '')
  109. output=$("$@" 2>&1 )
  110. status=$?
  111. test $status != 0 && printf "%s\n" "$output"
  112. return $status
  113. ;;
  114. *)
  115. "$@"
  116. ;;
  117. esac
  118. }
  119. strategy_args=${strategy:+--strategy=$strategy}
  120. test -n "$strategy_opts" &&
  121. eval '
  122. for strategy_opt in '"$strategy_opts"'
  123. do
  124. strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
  125. done
  126. '
  127. GIT_CHERRY_PICK_HELP="$resolvemsg"
  128. export GIT_CHERRY_PICK_HELP
  129. comment_char=$(git config --get core.commentchar 2>/dev/null)
  130. case "$comment_char" in
  131. '' | auto)
  132. comment_char="#"
  133. ;;
  134. ?)
  135. ;;
  136. *)
  137. comment_char=$(echo "$comment_char" | cut -c1)
  138. ;;
  139. esac
  140. warn () {
  141. printf '%s\n' "$*" >&2
  142. }
  143. # Output the commit message for the specified commit.
  144. commit_message () {
  145. git cat-file commit "$1" | sed "1,/^$/d"
  146. }
  147. orig_reflog_action="$GIT_REFLOG_ACTION"
  148. comment_for_reflog () {
  149. case "$orig_reflog_action" in
  150. ''|rebase*)
  151. GIT_REFLOG_ACTION="rebase -i ($1)"
  152. export GIT_REFLOG_ACTION
  153. ;;
  154. esac
  155. }
  156. last_count=
  157. mark_action_done () {
  158. sed -e 1q < "$todo" >> "$done"
  159. sed -e 1d < "$todo" >> "$todo".new
  160. mv -f "$todo".new "$todo"
  161. new_count=$(( $(git stripspace --strip-comments <"$done" | wc -l) ))
  162. echo $new_count >"$msgnum"
  163. total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
  164. echo $total >"$end"
  165. if test "$last_count" != "$new_count"
  166. then
  167. last_count=$new_count
  168. eval_gettext "Rebasing (\$new_count/\$total)"; printf "\r"
  169. test -z "$verbose" || echo
  170. fi
  171. }
  172. # Put the last action marked done at the beginning of the todo list
  173. # again. If there has not been an action marked done yet, leave the list of
  174. # items on the todo list unchanged.
  175. reschedule_last_action () {
  176. tail -n 1 "$done" | cat - "$todo" >"$todo".new
  177. sed -e \$d <"$done" >"$done".new
  178. mv -f "$todo".new "$todo"
  179. mv -f "$done".new "$done"
  180. }
  181. append_todo_help () {
  182. gettext "
  183. Commands:
  184. p, pick <commit> = use commit
  185. r, reword <commit> = use commit, but edit the commit message
  186. e, edit <commit> = use commit, but stop for amending
  187. s, squash <commit> = use commit, but meld into previous commit
  188. f, fixup <commit> = like \"squash\", but discard this commit's log message
  189. x, exec <commit> = run command (the rest of the line) using shell
  190. d, drop <commit> = remove commit
  191. l, label <label> = label current HEAD with a name
  192. t, reset <label> = reset HEAD to a label
  193. m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  194. . create a merge commit using the original merge commit's
  195. . message (or the oneline, if no original merge commit was
  196. . specified). Use -c <commit> to reword the commit message.
  197. These lines can be re-ordered; they are executed from top to bottom.
  198. " | git stripspace --comment-lines >>"$todo"
  199. if test $(get_missing_commit_check_level) = error
  200. then
  201. gettext "
  202. Do not remove any line. Use 'drop' explicitly to remove a commit.
  203. " | git stripspace --comment-lines >>"$todo"
  204. else
  205. gettext "
  206. If you remove a line here THAT COMMIT WILL BE LOST.
  207. " | git stripspace --comment-lines >>"$todo"
  208. fi
  209. }
  210. make_patch () {
  211. sha1_and_parents="$(git rev-list --parents -1 "$1")"
  212. case "$sha1_and_parents" in
  213. ?*' '?*' '?*)
  214. git diff --cc $sha1_and_parents
  215. ;;
  216. ?*' '?*)
  217. git diff-tree -p "$1^!"
  218. ;;
  219. *)
  220. echo "Root commit"
  221. ;;
  222. esac > "$state_dir"/patch
  223. test -f "$msg" ||
  224. commit_message "$1" > "$msg"
  225. test -f "$author_script" ||
  226. get_author_ident_from_commit "$1" > "$author_script"
  227. }
  228. die_with_patch () {
  229. echo "$1" > "$state_dir"/stopped-sha
  230. git update-ref REBASE_HEAD "$1"
  231. make_patch "$1"
  232. die "$2"
  233. }
  234. exit_with_patch () {
  235. echo "$1" > "$state_dir"/stopped-sha
  236. git update-ref REBASE_HEAD "$1"
  237. make_patch $1
  238. git rev-parse --verify HEAD > "$amend"
  239. gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
  240. warn "$(eval_gettext "\
  241. You can amend the commit now, with
  242. git commit --amend \$gpg_sign_opt_quoted
  243. Once you are satisfied with your changes, run
  244. git rebase --continue")"
  245. warn
  246. exit $2
  247. }
  248. die_abort () {
  249. apply_autostash
  250. rm -rf "$state_dir"
  251. die "$1"
  252. }
  253. has_action () {
  254. test -n "$(git stripspace --strip-comments <"$1")"
  255. }
  256. is_empty_commit() {
  257. tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null) || {
  258. sha1=$1
  259. die "$(eval_gettext "\$sha1: not a commit that can be picked")"
  260. }
  261. ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null) ||
  262. ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  263. test "$tree" = "$ptree"
  264. }
  265. is_merge_commit()
  266. {
  267. git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
  268. }
  269. # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
  270. # GIT_AUTHOR_DATE exported from the current environment.
  271. do_with_author () {
  272. (
  273. export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
  274. "$@"
  275. )
  276. }
  277. git_sequence_editor () {
  278. if test -z "$GIT_SEQUENCE_EDITOR"
  279. then
  280. GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
  281. if [ -z "$GIT_SEQUENCE_EDITOR" ]
  282. then
  283. GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
  284. fi
  285. fi
  286. eval "$GIT_SEQUENCE_EDITOR" '"$@"'
  287. }
  288. pick_one () {
  289. ff=--ff
  290. case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
  291. case "$force_rebase" in '') ;; ?*) ff= ;; esac
  292. output git rev-parse --verify $sha1 || die "$(eval_gettext "Invalid commit name: \$sha1")"
  293. if is_empty_commit "$sha1"
  294. then
  295. empty_args="--allow-empty"
  296. fi
  297. pick_one_preserving_merges "$@"
  298. }
  299. pick_one_preserving_merges () {
  300. fast_forward=t
  301. case "$1" in
  302. -n)
  303. fast_forward=f
  304. sha1=$2
  305. ;;
  306. *)
  307. sha1=$1
  308. ;;
  309. esac
  310. sha1=$(git rev-parse $sha1)
  311. if test -f "$state_dir"/current-commit && test "$fast_forward" = t
  312. then
  313. while read current_commit
  314. do
  315. git rev-parse HEAD > "$rewritten"/$current_commit
  316. done <"$state_dir"/current-commit
  317. rm "$state_dir"/current-commit ||
  318. die "$(gettext "Cannot write current commit's replacement sha1")"
  319. fi
  320. echo $sha1 >> "$state_dir"/current-commit
  321. # rewrite parents; if none were rewritten, we can fast-forward.
  322. new_parents=
  323. pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
  324. if test "$pend" = " "
  325. then
  326. pend=" root"
  327. fi
  328. while [ "$pend" != "" ]
  329. do
  330. p=$(expr "$pend" : ' \([^ ]*\)')
  331. pend="${pend# $p}"
  332. if test -f "$rewritten"/$p
  333. then
  334. new_p=$(cat "$rewritten"/$p)
  335. # If the todo reordered commits, and our parent is marked for
  336. # rewriting, but hasn't been gotten to yet, assume the user meant to
  337. # drop it on top of the current HEAD
  338. if test -z "$new_p"
  339. then
  340. new_p=$(git rev-parse HEAD)
  341. fi
  342. test $p != $new_p && fast_forward=f
  343. case "$new_parents" in
  344. *$new_p*)
  345. ;; # do nothing; that parent is already there
  346. *)
  347. new_parents="$new_parents $new_p"
  348. ;;
  349. esac
  350. else
  351. if test -f "$dropped"/$p
  352. then
  353. fast_forward=f
  354. replacement="$(cat "$dropped"/$p)"
  355. test -z "$replacement" && replacement=root
  356. pend=" $replacement$pend"
  357. else
  358. new_parents="$new_parents $p"
  359. fi
  360. fi
  361. done
  362. case $fast_forward in
  363. t)
  364. output warn "$(eval_gettext "Fast-forward to \$sha1")"
  365. output git reset --hard $sha1 ||
  366. die "$(eval_gettext "Cannot fast-forward to \$sha1")"
  367. ;;
  368. f)
  369. first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
  370. if [ "$1" != "-n" ]
  371. then
  372. # detach HEAD to current parent
  373. output git checkout $first_parent 2> /dev/null ||
  374. die "$(eval_gettext "Cannot move HEAD to \$first_parent")"
  375. fi
  376. case "$new_parents" in
  377. ' '*' '*)
  378. test "a$1" = a-n && die "$(eval_gettext "Refusing to squash a merge: \$sha1")"
  379. # redo merge
  380. author_script_content=$(get_author_ident_from_commit $sha1)
  381. eval "$author_script_content"
  382. msg_content="$(commit_message $sha1)"
  383. # No point in merging the first parent, that's HEAD
  384. new_parents=${new_parents# $first_parent}
  385. merge_args="--no-log --no-ff"
  386. if ! do_with_author output eval \
  387. git merge ${gpg_sign_opt:+$(git rev-parse \
  388. --sq-quote "$gpg_sign_opt")} \
  389. $allow_rerere_autoupdate "$merge_args" \
  390. "$strategy_args" \
  391. -m "$(git rev-parse --sq-quote "$msg_content")" \
  392. "$new_parents"
  393. then
  394. printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
  395. die_with_patch $sha1 "$(eval_gettext "Error redoing merge \$sha1")"
  396. fi
  397. echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
  398. ;;
  399. *)
  400. output eval git cherry-pick $allow_rerere_autoupdate \
  401. $allow_empty_message \
  402. ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
  403. "$strategy_args" "$@" ||
  404. die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
  405. ;;
  406. esac
  407. ;;
  408. esac
  409. }
  410. this_nth_commit_message () {
  411. n=$1
  412. eval_gettext "This is the commit message #\${n}:"
  413. }
  414. skip_nth_commit_message () {
  415. n=$1
  416. eval_gettext "The commit message #\${n} will be skipped:"
  417. }
  418. update_squash_messages () {
  419. if test -f "$squash_msg"; then
  420. mv "$squash_msg" "$squash_msg".bak || exit
  421. count=$(($(sed -n \
  422. -e "1s/^$comment_char[^0-9]*\([0-9][0-9]*\).*/\1/p" \
  423. -e "q" < "$squash_msg".bak)+1))
  424. {
  425. printf '%s\n' "$comment_char $(eval_ngettext \
  426. "This is a combination of \$count commit." \
  427. "This is a combination of \$count commits." \
  428. $count)"
  429. sed -e 1d -e '2,/^./{
  430. /^$/d
  431. }' <"$squash_msg".bak
  432. } >"$squash_msg"
  433. else
  434. commit_message HEAD >"$fixup_msg" ||
  435. die "$(eval_gettext "Cannot write \$fixup_msg")"
  436. count=2
  437. {
  438. printf '%s\n' "$comment_char $(gettext "This is a combination of 2 commits.")"
  439. printf '%s\n' "$comment_char $(gettext "This is the 1st commit message:")"
  440. echo
  441. cat "$fixup_msg"
  442. } >"$squash_msg"
  443. fi
  444. case $1 in
  445. squash)
  446. rm -f "$fixup_msg"
  447. echo
  448. printf '%s\n' "$comment_char $(this_nth_commit_message $count)"
  449. echo
  450. commit_message $2
  451. ;;
  452. fixup)
  453. echo
  454. printf '%s\n' "$comment_char $(skip_nth_commit_message $count)"
  455. echo
  456. # Change the space after the comment character to TAB:
  457. commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
  458. ;;
  459. esac >>"$squash_msg"
  460. }
  461. peek_next_command () {
  462. git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
  463. }
  464. # A squash/fixup has failed. Prepare the long version of the squash
  465. # commit message, then die_with_patch. This code path requires the
  466. # user to edit the combined commit message for all commits that have
  467. # been squashed/fixedup so far. So also erase the old squash
  468. # messages, effectively causing the combined commit to be used as the
  469. # new basis for any further squash/fixups. Args: sha1 rest
  470. die_failed_squash() {
  471. sha1=$1
  472. rest=$2
  473. mv "$squash_msg" "$msg" || exit
  474. rm -f "$fixup_msg"
  475. cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
  476. warn
  477. warn "$(eval_gettext "Could not apply \$sha1... \$rest")"
  478. die_with_patch $sha1 ""
  479. }
  480. flush_rewritten_pending() {
  481. test -s "$rewritten_pending" || return
  482. newsha1="$(git rev-parse HEAD^0)"
  483. sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
  484. rm -f "$rewritten_pending"
  485. }
  486. record_in_rewritten() {
  487. oldsha1="$(git rev-parse $1)"
  488. echo "$oldsha1" >> "$rewritten_pending"
  489. case "$(peek_next_command)" in
  490. squash|s|fixup|f)
  491. ;;
  492. *)
  493. flush_rewritten_pending
  494. ;;
  495. esac
  496. }
  497. do_pick () {
  498. sha1=$1
  499. rest=$2
  500. if test "$(git rev-parse HEAD)" = "$squash_onto"
  501. then
  502. # Set the correct commit message and author info on the
  503. # sentinel root before cherry-picking the original changes
  504. # without committing (-n). Finally, update the sentinel again
  505. # to include these changes. If the cherry-pick results in a
  506. # conflict, this means our behaviour is similar to a standard
  507. # failed cherry-pick during rebase, with a dirty index to
  508. # resolve before manually running git commit --amend then git
  509. # rebase --continue.
  510. git commit --allow-empty --allow-empty-message --amend \
  511. --no-post-rewrite -n -q -C $sha1 $signoff &&
  512. pick_one -n $sha1 &&
  513. git commit --allow-empty --allow-empty-message \
  514. --amend --no-post-rewrite -n -q -C $sha1 $signoff \
  515. ${gpg_sign_opt:+"$gpg_sign_opt"} ||
  516. die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
  517. else
  518. pick_one $sha1 ||
  519. die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
  520. fi
  521. }
  522. do_next () {
  523. rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
  524. read -r command sha1 rest < "$todo"
  525. case "$command" in
  526. "$comment_char"*|''|noop|drop|d)
  527. mark_action_done
  528. ;;
  529. "$cr")
  530. # Work around CR left by "read" (e.g. with Git for Windows' Bash).
  531. mark_action_done
  532. ;;
  533. pick|p)
  534. comment_for_reflog pick
  535. mark_action_done
  536. do_pick $sha1 "$rest"
  537. record_in_rewritten $sha1
  538. ;;
  539. reword|r)
  540. comment_for_reflog reword
  541. mark_action_done
  542. do_pick $sha1 "$rest"
  543. git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} \
  544. $allow_empty_message || {
  545. warn "$(eval_gettext "\
  546. Could not amend commit after successfully picking \$sha1... \$rest
  547. This is most likely due to an empty commit message, or the pre-commit hook
  548. failed. If the pre-commit hook failed, you may need to resolve the issue before
  549. you are able to reword the commit.")"
  550. exit_with_patch $sha1 1
  551. }
  552. record_in_rewritten $sha1
  553. ;;
  554. edit|e)
  555. comment_for_reflog edit
  556. mark_action_done
  557. do_pick $sha1 "$rest"
  558. sha1_abbrev=$(git rev-parse --short $sha1)
  559. warn "$(eval_gettext "Stopped at \$sha1_abbrev... \$rest")"
  560. exit_with_patch $sha1 0
  561. ;;
  562. squash|s|fixup|f)
  563. case "$command" in
  564. squash|s)
  565. squash_style=squash
  566. ;;
  567. fixup|f)
  568. squash_style=fixup
  569. ;;
  570. esac
  571. comment_for_reflog $squash_style
  572. test -f "$done" && has_action "$done" ||
  573. die "$(eval_gettext "Cannot '\$squash_style' without a previous commit")"
  574. mark_action_done
  575. update_squash_messages $squash_style $sha1
  576. author_script_content=$(get_author_ident_from_commit HEAD)
  577. echo "$author_script_content" > "$author_script"
  578. eval "$author_script_content"
  579. if ! pick_one -n $sha1
  580. then
  581. git rev-parse --verify HEAD >"$amend"
  582. die_failed_squash $sha1 "$rest"
  583. fi
  584. case "$(peek_next_command)" in
  585. squash|s|fixup|f)
  586. # This is an intermediate commit; its message will only be
  587. # used in case of trouble. So use the long version:
  588. do_with_author output git commit --amend --no-verify -F "$squash_msg" \
  589. ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
  590. die_failed_squash $sha1 "$rest"
  591. ;;
  592. *)
  593. # This is the final command of this squash/fixup group
  594. if test -f "$fixup_msg"
  595. then
  596. do_with_author git commit --amend --no-verify -F "$fixup_msg" \
  597. ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
  598. die_failed_squash $sha1 "$rest"
  599. else
  600. cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
  601. rm -f "$GIT_DIR"/MERGE_MSG
  602. do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
  603. ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
  604. die_failed_squash $sha1 "$rest"
  605. fi
  606. rm -f "$squash_msg" "$fixup_msg"
  607. ;;
  608. esac
  609. record_in_rewritten $sha1
  610. ;;
  611. x|"exec")
  612. read -r command rest < "$todo"
  613. mark_action_done
  614. eval_gettextln "Executing: \$rest"
  615. "${SHELL:-@SHELL_PATH@}" -c "$rest" # Actual execution
  616. status=$?
  617. # Run in subshell because require_clean_work_tree can die.
  618. dirty=f
  619. (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
  620. if test "$status" -ne 0
  621. then
  622. warn "$(eval_gettext "Execution failed: \$rest")"
  623. test "$dirty" = f ||
  624. warn "$(gettext "and made changes to the index and/or the working tree")"
  625. warn "$(gettext "\
  626. You can fix the problem, and then run
  627. git rebase --continue")"
  628. warn
  629. if test $status -eq 127 # command not found
  630. then
  631. status=1
  632. fi
  633. exit "$status"
  634. elif test "$dirty" = t
  635. then
  636. # TRANSLATORS: after these lines is a command to be issued by the user
  637. warn "$(eval_gettext "\
  638. Execution succeeded: \$rest
  639. but left changes to the index and/or the working tree
  640. Commit or stash your changes, and then run
  641. git rebase --continue")"
  642. warn
  643. exit 1
  644. fi
  645. ;;
  646. *)
  647. warn "$(eval_gettext "Unknown command: \$command \$sha1 \$rest")"
  648. fixtodo="$(gettext "Please fix this using 'git rebase --edit-todo'.")"
  649. if git rev-parse --verify -q "$sha1" >/dev/null
  650. then
  651. die_with_patch $sha1 "$fixtodo"
  652. else
  653. die "$fixtodo"
  654. fi
  655. ;;
  656. esac
  657. test -s "$todo" && return
  658. comment_for_reflog finish &&
  659. newhead=$(git rev-parse HEAD) &&
  660. case $head_name in
  661. refs/*)
  662. message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
  663. git update-ref -m "$message" $head_name $newhead $orig_head &&
  664. git symbolic-ref \
  665. -m "$GIT_REFLOG_ACTION: returning to $head_name" \
  666. HEAD $head_name
  667. ;;
  668. esac && {
  669. test ! -f "$state_dir"/verbose ||
  670. git diff-tree --stat $orig_head..HEAD
  671. } &&
  672. {
  673. test -s "$rewritten_list" &&
  674. git notes copy --for-rewrite=rebase < "$rewritten_list" ||
  675. true # we don't care if this copying failed
  676. } &&
  677. hook="$(git rev-parse --git-path hooks/post-rewrite)"
  678. if test -x "$hook" && test -s "$rewritten_list"; then
  679. "$hook" rebase < "$rewritten_list"
  680. true # we don't care if this hook failed
  681. fi &&
  682. warn "$(eval_gettext "Successfully rebased and updated \$head_name.")"
  683. return 1 # not failure; just to break the do_rest loop
  684. }
  685. # can only return 0, when the infinite loop breaks
  686. do_rest () {
  687. while :
  688. do
  689. do_next || break
  690. done
  691. }
  692. expand_todo_ids() {
  693. git rebase--interactive --expand-ids
  694. }
  695. collapse_todo_ids() {
  696. git rebase--interactive --shorten-ids
  697. }
  698. # Switch to the branch in $into and notify it in the reflog
  699. checkout_onto () {
  700. GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
  701. output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
  702. git update-ref ORIG_HEAD $orig_head
  703. }
  704. get_missing_commit_check_level () {
  705. check_level=$(git config --get rebase.missingCommitsCheck)
  706. check_level=${check_level:-ignore}
  707. # Don't be case sensitive
  708. printf '%s' "$check_level" | tr 'A-Z' 'a-z'
  709. }
  710. # Initiate an action. If the cannot be any
  711. # further action it may exec a command
  712. # or exit and not return.
  713. #
  714. # TODO: Consider a cleaner return model so it
  715. # never exits and always return 0 if process
  716. # is complete.
  717. #
  718. # Parameter 1 is the action to initiate.
  719. #
  720. # Returns 0 if the action was able to complete
  721. # and if 1 if further processing is required.
  722. initiate_action () {
  723. case "$1" in
  724. continue)
  725. # do we have anything to commit?
  726. if git diff-index --cached --quiet HEAD --
  727. then
  728. # Nothing to commit -- skip this commit
  729. test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
  730. rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
  731. die "$(gettext "Could not remove CHERRY_PICK_HEAD")"
  732. else
  733. if ! test -f "$author_script"
  734. then
  735. gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
  736. die "$(eval_gettext "\
  737. You have staged changes in your working tree.
  738. If these changes are meant to be
  739. squashed into the previous commit, run:
  740. git commit --amend \$gpg_sign_opt_quoted
  741. If they are meant to go into a new commit, run:
  742. git commit \$gpg_sign_opt_quoted
  743. In both cases, once you're done, continue with:
  744. git rebase --continue
  745. ")"
  746. fi
  747. . "$author_script" ||
  748. die "$(gettext "Error trying to find the author identity to amend commit")"
  749. if test -f "$amend"
  750. then
  751. current_head=$(git rev-parse --verify HEAD)
  752. test "$current_head" = $(cat "$amend") ||
  753. die "$(gettext "\
  754. You have uncommitted changes in your working tree. Please commit them
  755. first and then run 'git rebase --continue' again.")"
  756. do_with_author git commit --amend --no-verify -F "$msg" -e \
  757. ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
  758. die "$(gettext "Could not commit staged changes.")"
  759. else
  760. do_with_author git commit --no-verify -F "$msg" -e \
  761. ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
  762. die "$(gettext "Could not commit staged changes.")"
  763. fi
  764. fi
  765. if test -r "$state_dir"/stopped-sha
  766. then
  767. record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
  768. fi
  769. require_clean_work_tree "rebase"
  770. do_rest
  771. return 0
  772. ;;
  773. skip)
  774. git rerere clear
  775. do_rest
  776. return 0
  777. ;;
  778. edit-todo)
  779. git stripspace --strip-comments <"$todo" >"$todo".new
  780. mv -f "$todo".new "$todo"
  781. collapse_todo_ids
  782. append_todo_help
  783. gettext "
  784. You are editing the todo file of an ongoing interactive rebase.
  785. To continue rebase after editing, run:
  786. git rebase --continue
  787. " | git stripspace --comment-lines >>"$todo"
  788. git_sequence_editor "$todo" ||
  789. die "$(gettext "Could not execute editor")"
  790. expand_todo_ids
  791. exit
  792. ;;
  793. show-current-patch)
  794. exec git show REBASE_HEAD --
  795. ;;
  796. *)
  797. return 1 # continue
  798. ;;
  799. esac
  800. }
  801. setup_reflog_action () {
  802. comment_for_reflog start
  803. if test ! -z "$switch_to"
  804. then
  805. GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
  806. output git checkout "$switch_to" -- ||
  807. die "$(eval_gettext "Could not checkout \$switch_to")"
  808. comment_for_reflog start
  809. fi
  810. }
  811. init_basic_state () {
  812. orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
  813. mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
  814. rm -f "$(git rev-parse --git-path REBASE_HEAD)"
  815. : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
  816. write_basic_state
  817. }
  818. init_revisions_and_shortrevisions () {
  819. shorthead=$(git rev-parse --short $orig_head)
  820. shortonto=$(git rev-parse --short $onto)
  821. if test -z "$rebase_root"
  822. # this is now equivalent to ! -z "$upstream"
  823. then
  824. shortupstream=$(git rev-parse --short $upstream)
  825. revisions=$upstream...$orig_head
  826. shortrevisions=$shortupstream..$shorthead
  827. else
  828. revisions=$onto...$orig_head
  829. shortrevisions=$shorthead
  830. test -z "$squash_onto" ||
  831. echo "$squash_onto" >"$state_dir"/squash-onto
  832. fi
  833. }
  834. complete_action() {
  835. test -s "$todo" || echo noop >> "$todo"
  836. test -z "$autosquash" || git rebase--interactive --rearrange-squash || exit
  837. test -n "$cmd" && git rebase--interactive --add-exec-commands --cmd "$cmd"
  838. todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
  839. todocount=${todocount##* }
  840. cat >>"$todo" <<EOF
  841. $comment_char $(eval_ngettext \
  842. "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
  843. "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
  844. "$todocount")
  845. EOF
  846. append_todo_help
  847. gettext "
  848. However, if you remove everything, the rebase will be aborted.
  849. " | git stripspace --comment-lines >>"$todo"
  850. if test -z "$keep_empty"
  851. then
  852. printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
  853. fi
  854. has_action "$todo" ||
  855. return 2
  856. cp "$todo" "$todo".backup
  857. collapse_todo_ids
  858. git_sequence_editor "$todo" ||
  859. die_abort "$(gettext "Could not execute editor")"
  860. has_action "$todo" ||
  861. return 2
  862. git rebase--interactive --check-todo-list || {
  863. ret=$?
  864. checkout_onto
  865. exit $ret
  866. }
  867. expand_todo_ids
  868. checkout_onto
  869. do_rest
  870. }
  871. git_rebase__preserve_merges () {
  872. initiate_action "$action"
  873. ret=$?
  874. if test $ret = 0; then
  875. return 0
  876. fi
  877. setup_reflog_action
  878. init_basic_state
  879. if test -z "$rebase_root"
  880. then
  881. mkdir "$rewritten" &&
  882. for c in $(git merge-base --all $orig_head $upstream)
  883. do
  884. echo $onto > "$rewritten"/$c ||
  885. die "$(gettext "Could not init rewritten commits")"
  886. done
  887. else
  888. mkdir "$rewritten" &&
  889. echo $onto > "$rewritten"/root ||
  890. die "$(gettext "Could not init rewritten commits")"
  891. fi
  892. init_revisions_and_shortrevisions
  893. format=$(git config --get rebase.instructionFormat)
  894. # the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
  895. git rev-list --format="%m%H ${format:-%s}" \
  896. --reverse --left-right --topo-order \
  897. $revisions ${restrict_revision+^$restrict_revision} | \
  898. sed -n "s/^>//p" |
  899. while read -r sha1 rest
  900. do
  901. if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
  902. then
  903. comment_out="$comment_char "
  904. else
  905. comment_out=
  906. fi
  907. if test -z "$rebase_root"
  908. then
  909. preserve=t
  910. for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
  911. do
  912. if test -f "$rewritten"/$p
  913. then
  914. preserve=f
  915. fi
  916. done
  917. else
  918. preserve=f
  919. fi
  920. if test f = "$preserve"
  921. then
  922. touch "$rewritten"/$sha1
  923. printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
  924. fi
  925. done
  926. # Watch for commits that been dropped by --cherry-pick
  927. mkdir "$dropped"
  928. # Save all non-cherry-picked changes
  929. git rev-list $revisions --left-right --cherry-pick | \
  930. sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
  931. # Now all commits and note which ones are missing in
  932. # not-cherry-picks and hence being dropped
  933. git rev-list $revisions |
  934. while read rev
  935. do
  936. if test -f "$rewritten"/$rev &&
  937. ! sane_grep "$rev" "$state_dir"/not-cherry-picks >/dev/null
  938. then
  939. # Use -f2 because if rev-list is telling us this commit is
  940. # not worthwhile, we don't want to track its multiple heads,
  941. # just the history of its first-parent for others that will
  942. # be rebasing on top of it
  943. git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
  944. sha1=$(git rev-list -1 $rev)
  945. sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
  946. rm "$rewritten"/$rev
  947. fi
  948. done
  949. complete_action
  950. }