builtin rebase: handle the pre-rebase hook and --no-verify

This commit converts the equivalent part of the shell script `git-legacy-rebase.sh` to run the pre-rebase hook (unless disabled), and to interrupt the rebase with error if the hook fails. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pratik Karki committed Sep 4, 2018 at 14:27 UTC 06e4775a8cfb5ccc5afdfbf43b1a270625888adc
1 file changed +11
builtin/rebase.c
+11
@@ -70,6 +70,7 @@ struct rebase_options {
70 const char *state_dir;
71 struct commit *upstream;
72 const char *upstream_name;
73 + const char *upstream_arg;
74 char *head_name;
75 struct object_id orig_head;
76 struct commit *onto;
@@ -310,6 +311,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
311 };
312 const char *branch_name;
313 int ret, flags;
314 + int ok_to_skip_pre_rebase = 0;
315 struct strbuf msg = STRBUF_INIT;
316 struct strbuf revisions = STRBUF_INIT;
317 struct object_id merge_base;
@@ -317,6 +319,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
319 OPT_STRING(0, "onto", &options.onto_name,
320 N_("revision"),
321 N_("rebase onto given branch instead of upstream")),
322 + OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
323 + N_("allow pre-rebase hook to run")),
324 OPT_END(),
325 };
326
@@ -382,6 +386,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
386 options.upstream = peel_committish(options.upstream_name);
387 if (!options.upstream)
388 die(_("invalid upstream '%s'"), options.upstream_name);
389 + options.upstream_arg = options.upstream_name;
390 } else
391 die("TODO: upstream for --root");
392
@@ -430,6 +435,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
435 die(_("Could not resolve HEAD to a revision"));
436 }
437
438 + /* If a hook exists, give it a chance to interrupt*/
439 + if (!ok_to_skip_pre_rebase &&
440 + run_hook_le(NULL, "pre-rebase", options.upstream_arg,
441 + argc ? argv[0] : NULL, NULL))
442 + die(_("The pre-rebase hook refused to rebase."));
443 +
444 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
445 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
446 die(_("Could not detach HEAD"));