2773
return run_command_v_opt(argv, RUN_GIT_CMD);
2774
}
2775
2776
-static int commit_staged_changes(struct replay_opts *opts)
2776
+static int commit_staged_changes(struct replay_opts *opts,
2777
+ struct todo_list *todo_list)
2778
{
2779
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
2780
+ unsigned int final_fixup = 0, is_clean;
2781
2782
if (has_unstaged_changes(1))
2783
return error(_("cannot rebase: You have unstaged changes."));
2782
- if (!has_uncommitted_changes(0)) {
2783
- const char *cherry_pick_head = git_path_cherry_pick_head();
2784
2785
- if (file_exists(cherry_pick_head) && unlink(cherry_pick_head))
2786
- return error(_("could not remove CHERRY_PICK_HEAD"));
2787
- return 0;
2788
- }
2785
+ is_clean = !has_uncommitted_changes(0);
2786
2787
if (file_exists(rebase_path_amend())) {
2788
struct strbuf rev = STRBUF_INIT;
2795
if (get_oid_hex(rev.buf, &to_amend))
2796
return error(_("invalid contents: '%s'"),
2797
rebase_path_amend());
2801
- if (oidcmp(&head, &to_amend))
2798
+ if (!is_clean && oidcmp(&head, &to_amend))
2799
return error(_("\nYou have uncommitted changes in your "
2800
"working tree. Please, commit them\n"
2801
"first and then run 'git rebase "
2802
"--continue' again."));
2803
+ /*
2804
+ * When skipping a failed fixup/squash, we need to edit the
2805
+ * commit message, the current fixup list and count, and if it
2806
+ * was the last fixup/squash in the chain, we need to clean up
2807
+ * the commit message and if there was a squash, let the user
2808
+ * edit it.
2809
+ */
2810
+ if (is_clean && !oidcmp(&head, &to_amend) &&
2811
+ opts->current_fixup_count > 0 &&
2812
+ file_exists(rebase_path_stopped_sha())) {
2813
+ const char *p = opts->current_fixups.buf;
2814
+ int len = opts->current_fixups.len;
2815
+
2816
+ opts->current_fixup_count--;
2817
+ if (!len)
2818
+ BUG("Incorrect current_fixups:\n%s", p);
2819
+ while (len && p[len - 1] != '\n')
2820
+ len--;
2821
+ strbuf_setlen(&opts->current_fixups, len);
2822
+ if (write_message(p, len, rebase_path_current_fixups(),
2823
+ 0) < 0)
2824
+ return error(_("could not write file: '%s'"),
2825
+ rebase_path_current_fixups());
2826
+
2827
+ /*
2828
+ * If a fixup/squash in a fixup/squash chain failed, the
2829
+ * commit message is already correct, no need to commit
2830
+ * it again.
2831
+ *
2832
+ * Only if it is the final command in the fixup/squash
2833
+ * chain, and only if the chain is longer than a single
2834
+ * fixup/squash command (which was just skipped), do we
2835
+ * actually need to re-commit with a cleaned up commit
2836
+ * message.
2837
+ */
2838
+ if (opts->current_fixup_count > 0 &&
2839
+ !is_fixup(peek_command(todo_list, 0))) {
2840
+ final_fixup = 1;
2841
+ /*
2842
+ * If there was not a single "squash" in the
2843
+ * chain, we only need to clean up the commit
2844
+ * message, no need to bother the user with
2845
+ * opening the commit message in the editor.
2846
+ */
2847
+ if (!starts_with(p, "squash ") &&
2848
+ !strstr(p, "\nsquash "))
2849
+ flags = (flags & ~EDIT_MSG) | CLEANUP_MSG;
2850
+ } else if (is_fixup(peek_command(todo_list, 0))) {
2851
+ /*
2852
+ * We need to update the squash message to skip
2853
+ * the latest commit message.
2854
+ */
2855
+ struct commit *commit;
2856
+ const char *path = rebase_path_squash_msg();
2857
+
2858
+ if (parse_head(&commit) ||
2859
+ !(p = get_commit_buffer(commit, NULL)) ||
2860
+ write_message(p, strlen(p), path, 0)) {
2861
+ unuse_commit_buffer(commit, p);
2862
+ return error(_("could not write file: "
2863
+ "'%s'"), path);
2864
+ }
2865
+ unuse_commit_buffer(commit, p);
2866
+ }
2867
+ }
2868
2869
strbuf_release(&rev);
2870
flags |= AMEND_MSG;
2871
}
2872
2811
- if (run_git_commit(rebase_path_message(), opts, flags))
2873
+ if (is_clean) {
2874
+ const char *cherry_pick_head = git_path_cherry_pick_head();
2875
+
2876
+ if (file_exists(cherry_pick_head) && unlink(cherry_pick_head))
2877
+ return error(_("could not remove CHERRY_PICK_HEAD"));
2878
+ if (!final_fixup)
2879
+ return 0;
2880
+ }
2881
+
2882
+ if (run_git_commit(final_fixup ? NULL : rebase_path_message(),
2883
+ opts, flags))
2884
return error(_("could not commit staged changes."));
2885
unlink(rebase_path_amend());
2886
+ if (final_fixup) {
2887
+ unlink(rebase_path_fixup_msg());
2888
+ unlink(rebase_path_squash_msg());
2889
+ }
2890
+ if (opts->current_fixup_count > 0) {
2891
+ /*
2892
+ * Whether final fixup or not, we just cleaned up the commit
2893
+ * message...
2894
+ */
2895
+ unlink(rebase_path_current_fixups());
2896
+ strbuf_reset(&opts->current_fixups);
2897
+ opts->current_fixup_count = 0;
2898
+ }
2899
return 0;
2900
}
2901
2907
if (read_and_refresh_cache(opts))
2908
return -1;
2909
2910
+ if (read_populate_opts(opts))
2911
+ return -1;
2912
if (is_rebase_i(opts)) {
2826
- if (commit_staged_changes(opts))
2913
+ if ((res = read_populate_todo(&todo_list, opts)))
2914
+ goto release_todo_list;
2915
+ if (commit_staged_changes(opts, &todo_list))
2916
return -1;
2917
} else if (!file_exists(get_todo_path(opts)))
2918
return continue_single_pick();
2830
- if (read_populate_opts(opts))
2831
- return -1;
2832
- if ((res = read_populate_todo(&todo_list, opts)))
2919
+ else if ((res = read_populate_todo(&todo_list, opts)))
2920
goto release_todo_list;
2921
2922
if (!is_rebase_i(opts)) {