diff options
Diffstat (limited to 'sequencer.c')
| -rw-r--r-- | sequencer.c | 135 |
1 files changed, 58 insertions, 77 deletions
diff --git a/sequencer.c b/sequencer.c index ea96837cde..950733af2a 100644 --- a/sequencer.c +++ b/sequencer.c @@ -919,7 +919,7 @@ static char *get_author(const char *message) return NULL; } -static const char *author_date_from_env_array(const struct strvec *env) +static const char *author_date_from_env(const struct strvec *env) { int i; const char *date; @@ -1000,7 +1000,7 @@ static int run_git_commit(const char *defmsg, if (is_rebase_i(opts) && ((opts->committer_date_is_author_date && !opts->ignore_date) || !(!defmsg && (flags & AMEND_MSG))) && - read_env_script(&cmd.env_array)) { + read_env_script(&cmd.env)) { const char *gpg_opt = gpg_sign_opt_quoted(opts); return error(_(staged_changes_advice), @@ -1008,12 +1008,12 @@ static int run_git_commit(const char *defmsg, } if (opts->committer_date_is_author_date) - strvec_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s", + strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", opts->ignore_date ? "" : - author_date_from_env_array(&cmd.env_array)); + author_date_from_env(&cmd.env)); if (opts->ignore_date) - strvec_push(&cmd.env_array, "GIT_AUTHOR_DATE="); + strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); strvec_push(&cmd.args, "commit"); @@ -1164,18 +1164,14 @@ static int run_rewrite_hook(const struct object_id *oldoid, const struct object_id *newoid) { struct child_process proc = CHILD_PROCESS_INIT; - const char *argv[3]; int code; struct strbuf sb = STRBUF_INIT; + const char *hook_path = find_hook("post-rewrite"); - argv[0] = find_hook("post-rewrite"); - if (!argv[0]) + if (!hook_path) return 0; - argv[1] = "amend"; - argv[2] = NULL; - - proc.argv = argv; + strvec_pushl(&proc.args, hook_path, "amend", NULL); proc.in = -1; proc.stdout_to_stderr = 1; proc.trace2_hook_name = "post-rewrite"; @@ -1224,7 +1220,7 @@ static int run_prepare_commit_msg_hook(struct repository *r, } else { arg1 = "message"; } - if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name, + if (run_commit_hook(0, r->index_file, NULL, "prepare-commit-msg", name, arg1, arg2, NULL)) ret = error(_("'prepare-commit-msg' hook failed")); @@ -1284,6 +1280,7 @@ void print_commit_summary(struct repository *r, struct pretty_print_context pctx = {0}; struct strbuf author_ident = STRBUF_INIT; struct strbuf committer_ident = STRBUF_INIT; + struct ref_store *refs; commit = lookup_commit(r, oid); if (!commit) @@ -1330,12 +1327,12 @@ void print_commit_summary(struct repository *r, get_commit_format(format.buf, &rev); rev.always_show_header = 0; rev.diffopt.detect_rename = DIFF_DETECT_RENAME; - rev.diffopt.break_opt = 0; diff_setup_done(&rev.diffopt); - head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); + refs = get_main_ref_store(the_repository); + head = refs_resolve_ref_unsafe(refs, "HEAD", 0, NULL, NULL); if (!head) - die_errno(_("unable to resolve HEAD after creating commit")); + die(_("unable to resolve HEAD after creating commit")); if (!strcmp(head, "HEAD")) head = _("detached HEAD"); else @@ -1349,6 +1346,7 @@ void print_commit_summary(struct repository *r, log_tree_commit(&rev, commit); } + release_revisions(&rev); strbuf_release(&format); } @@ -1554,7 +1552,7 @@ static int try_to_commit(struct repository *r, goto out; } - run_commit_hook(0, r->index_file, "post-commit", NULL); + run_commit_hook(0, r->index_file, NULL, "post-commit", NULL); if (flags & AMEND_MSG) commit_post_rewrite(r, current_head, oid); @@ -2804,7 +2802,7 @@ static int populate_opts_cb(const char *key, const char *value, void *data) return error(_("invalid key: %s"), key); if (!error_flag) - return error(_("invalid value for %s: %s"), key, value); + return error(_("invalid value for '%s': '%s'"), key, value); return 0; } @@ -3417,6 +3415,7 @@ static int make_patch(struct repository *r, unuse_commit_buffer(commit, commit_buffer); } strbuf_release(&buf); + release_revisions(&log_tree_opt); return res; } @@ -3495,17 +3494,12 @@ static int error_failed_squash(struct repository *r, static int do_exec(struct repository *r, const char *command_line) { - struct strvec child_env = STRVEC_INIT; const char *child_argv[] = { NULL, NULL }; int dirty, status; fprintf(stderr, _("Executing: %s\n"), command_line); child_argv[0] = command_line; - strvec_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); - strvec_pushf(&child_env, "GIT_WORK_TREE=%s", - absolute_path(get_git_work_tree())); - status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, - child_env.v); + status = run_command_v_opt(child_argv, RUN_USING_SHELL); /* force re-reading of the cache */ if (discard_index(r->index) < 0 || repo_read_index(r) < 0) @@ -3535,8 +3529,6 @@ static int do_exec(struct repository *r, const char *command_line) status = 1; } - strvec_clear(&child_env); - return status; } @@ -3758,7 +3750,7 @@ static int do_merge(struct repository *r, int run_commit_flags = 0; struct strbuf ref_name = STRBUF_INIT; struct commit *head_commit, *merge_commit, *i; - struct commit_list *bases, *j, *reversed = NULL; + struct commit_list *bases, *j; struct commit_list *to_merge = NULL, **tail = &to_merge; const char *strategy = !opts->xopts_nr && (!opts->strategy || @@ -3921,7 +3913,7 @@ static int do_merge(struct repository *r, /* Octopus merge */ struct child_process cmd = CHILD_PROCESS_INIT; - if (read_env_script(&cmd.env_array)) { + if (read_env_script(&cmd.env)) { const char *gpg_opt = gpg_sign_opt_quoted(opts); ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); @@ -3929,12 +3921,12 @@ static int do_merge(struct repository *r, } if (opts->committer_date_is_author_date) - strvec_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s", + strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s", opts->ignore_date ? "" : - author_date_from_env_array(&cmd.env_array)); + author_date_from_env(&cmd.env)); if (opts->ignore_date) - strvec_push(&cmd.env_array, "GIT_AUTHOR_DATE="); + strvec_push(&cmd.env, "GIT_AUTHOR_DATE="); cmd.git_cmd = 1; strvec_push(&cmd.args, "merge"); @@ -3993,9 +3985,7 @@ static int do_merge(struct repository *r, git_path_merge_head(r), 0); write_message("no-ff", 5, git_path_merge_mode(r), 0); - for (j = bases; j; j = j->next) - commit_list_insert(j->item, &reversed); - free_commit_list(bases); + bases = reverse_commit_list(bases); repo_read_index(r); init_merge_options(&o, r); @@ -4011,10 +4001,10 @@ static int do_merge(struct repository *r, * update the index and working copy immediately. */ ret = merge_ort_recursive(&o, - head_commit, merge_commit, reversed, + head_commit, merge_commit, bases, &i); } else { - ret = merge_recursive(&o, head_commit, merge_commit, reversed, + ret = merge_recursive(&o, head_commit, merge_commit, bases, &i); } if (ret <= 0) @@ -4094,8 +4084,7 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset) return -1; } -void create_autostash(struct repository *r, const char *path, - const char *default_reflog_action) +void create_autostash(struct repository *r, const char *path) { struct strbuf buf = STRBUF_INIT; struct lock_file lock_file = LOCK_INIT; @@ -4110,6 +4099,7 @@ void create_autostash(struct repository *r, const char *path, if (has_unstaged_changes(r, 1) || has_uncommitted_changes(r, 1)) { struct child_process stash = CHILD_PROCESS_INIT; + struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD }; struct object_id oid; strvec_pushl(&stash.args, @@ -4131,11 +4121,8 @@ void create_autostash(struct repository *r, const char *path, path); write_file(path, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); - if (reset_head(r, NULL, "reset --hard", - NULL, RESET_HEAD_HARD, NULL, NULL, - default_reflog_action) < 0) + if (reset_head(r, &ropts) < 0) die(_("could not reset --hard")); - if (discard_index(r->index) < 0 || repo_read_index(r) < 0) die(_("could not read index")); @@ -4220,42 +4207,26 @@ int apply_autostash_oid(const char *stash_oid) return apply_save_autostash_oid(stash_oid, 1); } -static int run_git_checkout(struct repository *r, struct replay_opts *opts, - const char *commit, const char *action) -{ - struct child_process cmd = CHILD_PROCESS_INIT; - int ret; - - cmd.git_cmd = 1; - - strvec_push(&cmd.args, "checkout"); - strvec_push(&cmd.args, commit); - strvec_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action); - - if (opts->verbose) - ret = run_command(&cmd); - else - ret = run_command_silent_on_success(&cmd); - - if (!ret) - discard_index(r->index); - - return ret; -} - static int checkout_onto(struct repository *r, struct replay_opts *opts, const char *onto_name, const struct object_id *onto, const struct object_id *orig_head) { - const char *action = reflog_message(opts, "start", "checkout %s", onto_name); - - if (run_git_checkout(r, opts, oid_to_hex(onto), action)) { + struct reset_head_opts ropts = { + .oid = onto, + .orig_head = orig_head, + .flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | + RESET_HEAD_RUN_POST_CHECKOUT_HOOK, + .head_msg = reflog_message(opts, "start", "checkout %s", + onto_name), + .default_reflog_action = "rebase" + }; + if (reset_head(r, &ropts)) { apply_autostash(rebase_path_autostash()); sequencer_remove_state(opts); return error(_("could not detach HEAD")); } - return update_ref(NULL, "ORIG_HEAD", orig_head, NULL, 0, UPDATE_REFS_MSG_ON_ERR); + return 0; } static int stopped_at_head(struct repository *r) @@ -4555,6 +4526,7 @@ cleanup_head_ref: &log_tree_opt.diffopt); log_tree_diff_flush(&log_tree_opt); } + release_revisions(&log_tree_opt); } flush_rewritten_pending(); if (!stat(rebase_path_rewritten_list(), &st) && @@ -5381,6 +5353,7 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, int rebase_merges = flags & TODO_LIST_REBASE_MERGES; int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS; int skipped_commit = 0; + int ret = 0; repo_init_revisions(r, &revs, NULL); revs.verbose_header = 1; @@ -5404,14 +5377,20 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, pp.fmt = revs.commit_format; pp.output_encoding = get_log_output_encoding(); - if (setup_revisions(argc, argv, &revs, NULL) > 1) - return error(_("make_script: unhandled options")); + if (setup_revisions(argc, argv, &revs, NULL) > 1) { + ret = error(_("make_script: unhandled options")); + goto cleanup; + } - if (prepare_revision_walk(&revs) < 0) - return error(_("make_script: error preparing revisions")); + if (prepare_revision_walk(&revs) < 0) { + ret = error(_("make_script: error preparing revisions")); + goto cleanup; + } - if (rebase_merges) - return make_script_with_merges(&pp, &revs, out, flags); + if (rebase_merges) { + ret = make_script_with_merges(&pp, &revs, out, flags); + goto cleanup; + } while ((commit = get_revision(&revs))) { int is_empty = is_original_commit_empty(commit); @@ -5435,7 +5414,9 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, if (skipped_commit) advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS, _("use --reapply-cherry-picks to include skipped commits")); - return 0; +cleanup: + release_revisions(&revs); + return ret; } /* @@ -5496,7 +5477,7 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list, } /* insert or append final <commands> */ - if (insert || nr == todo_list->nr) { + if (insert) { ALLOC_GROW(items, nr + commands->nr, alloc); COPY_ARRAY(items + nr, base_items, commands->nr); nr += commands->nr; |
