Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/hook.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / hook.c` 865 lines ยท 24712 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "abspath.h" 3 #include "advice.h" 4 #include "config.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "hook.h" 8 #include "hook-list.h" 9 #include "parse.h" 10 #include "path.h" 11 #include "run-command.h" 12 #include "setup.h" 13 #include "strbuf.h" 14 #include "strmap.h" 15 #include "thread-utils.h" 16 17 bool is_known_hook(const char *name) 18 { 19 const char **h; 20 for (h = hook_name_list; *h; h++) 21 if (!strcmp(*h, name)) 22 return true; 23 return false; 24 } 25 26 const char *find_hook(struct repository *r, const char *name) 27 { 28 static struct strbuf path = STRBUF_INIT; 29 30 int found_hook; 31 32 if (!r || !r->gitdir) 33 return NULL; 34 35 repo_git_path_replace(r, &path, "hooks/%s", name); 36 found_hook = access(path.buf, X_OK) >= 0; 37 #ifdef STRIP_EXTENSION 38 if (!found_hook) { 39 int err = errno; 40 41 strbuf_addstr(&path, STRIP_EXTENSION); 42 found_hook = access(path.buf, X_OK) >= 0; 43 if (!found_hook) 44 errno = err; 45 } 46 #endif 47 48 if (!found_hook) { 49 if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) { 50 static struct string_list advise_given = STRING_LIST_INIT_DUP; 51 52 if (!string_list_lookup(&advise_given, name)) { 53 string_list_insert(&advise_given, name); 54 advise(_("The '%s' hook was ignored because " 55 "it's not set as executable.\n" 56 "You can disable this warning with " 57 "`git config set advice.ignoredHook false`."), 58 path.buf); 59 } 60 } 61 return NULL; 62 } 63 return path.buf; 64 } 65 66 void hook_free(void *p, const char *str UNUSED) 67 { 68 struct hook *h = p; 69 70 if (!h) 71 return; 72 73 if (h->kind == HOOK_TRADITIONAL) { 74 free((void *)h->u.traditional.path); 75 } else if (h->kind == HOOK_CONFIGURED) { 76 free((void *)h->u.configured.friendly_name); 77 free((void *)h->u.configured.command); 78 } 79 80 if (h->data_free && h->feed_pipe_cb_data) 81 h->data_free(h->feed_pipe_cb_data); 82 83 free(h); 84 } 85 86 /* Helper to detect and add default "traditional" hooks from the hookdir. */ 87 static void list_hooks_add_default(struct repository *r, const char *hookname, 88 struct string_list *hook_list, 89 struct run_hooks_opt *options) 90 { 91 const char *hook_path = find_hook(r, hookname); 92 struct hook *h; 93 94 if (!hook_path) 95 return; 96 97 CALLOC_ARRAY(h, 1); 98 99 /* 100 * If the hook is to run in a specific dir, a relative path can 101 * become invalid in that dir, so convert to an absolute path. 102 */ 103 if (options && options->dir) 104 hook_path = absolute_path(hook_path); 105 106 /* 107 * Setup per-hook internal state callback data. 108 * When provided, the alloc/free callbacks are always provided 109 * together, so use them to alloc/free the internal hook state. 110 */ 111 if (options && options->feed_pipe_cb_data_alloc) { 112 h->feed_pipe_cb_data = options->feed_pipe_cb_data_alloc(options->feed_pipe_ctx); 113 h->data_free = options->feed_pipe_cb_data_free; 114 } 115 116 h->kind = HOOK_TRADITIONAL; 117 h->u.traditional.path = xstrdup(hook_path); 118 /* list_hooks_add_configured() has loaded r->disabled_events */ 119 h->event_disabled = !!unsorted_string_list_lookup(&r->disabled_events, 120 hookname); 121 122 string_list_append(hook_list, hook_path)->util = h; 123 } 124 125 /* 126 * Cache entry stored as the .util pointer of string_list items inside the 127 * hook config cache. 128 */ 129 struct hook_config_cache_entry { 130 char *command; 131 enum config_scope scope; 132 bool disabled; 133 bool parallel; 134 }; 135 136 /* 137 * Callback struct to collect all hook.* keys in a single config pass. 138 * commands: friendly-name to command map. 139 * event_hooks: event-name to list of friendly-names map. 140 * disabled_hooks: set of all names with hook..enabled = false; after 141 * parsing, names that are not friendly-names become event-level 142 * disables stored in r->disabled_events. This collects all. 143 * parallel_hooks: friendly-name to parallel flag. 144 * event_jobs: event-name to per-event jobs count (stored as uintptr_t, NULL == unset). 145 * jobs: value of the global hook.jobs key. Defaults to 0 if unset (stored in r->hook_jobs). 146 */ 147 struct hook_all_config_cb { 148 struct strmap commands; 149 struct strmap event_hooks; 150 struct string_list disabled_hooks; 151 struct strmap parallel_hooks; 152 struct strmap event_jobs; 153 unsigned int jobs; 154 }; 155 156 /* repo_config() callback that collects all hook.* configuration in one pass. */ 157 static int hook_config_lookup_all(const char *key, const char *value, 158 const struct config_context *ctx, 159 void *cb_data) 160 { 161 struct hook_all_config_cb *data = cb_data; 162 const char *name, *subkey; 163 char *hook_name; 164 size_t name_len = 0; 165 166 if (parse_config_key(key, "hook", &name, &name_len, &subkey)) 167 return 0; 168 169 /* Handle plain hook. entries that have no hook name component. */ 170 if (!name) { 171 if (!strcmp(subkey, "jobs") && value) { 172 int v; 173 if (!git_parse_int(value, &v)) 174 warning(_("hook.jobs must be an integer, ignoring: '%s'"), value); 175 else if (v == -1) 176 data->jobs = online_cpus(); 177 else if (v > 0) 178 data->jobs = v; 179 else 180 warning(_("hook.jobs must be a positive integer" 181 " or -1, ignoring: '%s'"), 182 value); 183 } 184 return 0; 185 } 186 187 /* .enabled and .parallel are bools, where a missing value means true */ 188 if (!value && strcmp(subkey, "enabled") && strcmp(subkey, "parallel")) 189 return config_error_nonbool(key); 190 191 /* Extract name, ensuring it is null-terminated. */ 192 hook_name = xmemdupz(name, name_len); 193 194 if (!strcmp(subkey, "event")) { 195 if (!*value) { 196 /* Empty values reset previous events for this hook. */ 197 struct hashmap_iter iter; 198 struct strmap_entry *e; 199 200 strmap_for_each_entry(&data->event_hooks, &iter, e) 201 unsorted_string_list_remove(e->value, hook_name, 0); 202 } else { 203 struct string_list *hooks; 204 205 if (is_known_hook(hook_name)) 206 die(_("hook friendly-name '%s' collides with " 207 "a known event name; please choose a " 208 "different friendly-name"), 209 hook_name); 210 211 if (!strcmp(hook_name, value)) 212 warning(_("hook friendly-name '%s' is the " 213 "same as its event; this may cause " 214 "ambiguity with hook.%s.enabled"), 215 hook_name, hook_name); 216 217 hooks = strmap_get(&data->event_hooks, value); 218 219 if (!hooks) { 220 CALLOC_ARRAY(hooks, 1); 221 string_list_init_dup(hooks); 222 strmap_put(&data->event_hooks, value, hooks); 223 } 224 225 /* Re-insert if necessary to preserve last-seen order. */ 226 unsorted_string_list_remove(hooks, hook_name, 0); 227 228 if (!ctx->kvi) 229 BUG("hook config callback called without key-value info"); 230 231 /* 232 * Stash the config scope in the util pointer for 233 * later retrieval in build_hook_config_map(). This 234 * intermediate struct is transient and never leaves 235 * that function, so we pack the enum value into the 236 * pointer rather than heap-allocating a wrapper. 237 */ 238 string_list_append(hooks, hook_name)->util = 239 (void *)(uintptr_t)ctx->kvi->scope; 240 } 241 } else if (!strcmp(subkey, "command")) { 242 /* Store command overwriting the old value */ 243 char *old = strmap_put(&data->commands, hook_name, 244 xstrdup(value)); 245 free(old); 246 } else if (!strcmp(subkey, "enabled")) { 247 switch (git_parse_maybe_bool(value)) { 248 case 0: /* disabled */ 249 if (!unsorted_string_list_lookup(&data->disabled_hooks, 250 hook_name)) 251 string_list_append(&data->disabled_hooks, 252 hook_name); 253 break; 254 case 1: /* enabled: undo a prior disabled entry */ 255 unsorted_string_list_remove(&data->disabled_hooks, 256 hook_name, 0); 257 break; 258 default: 259 break; /* ignore unrecognised values */ 260 } 261 } else if (!strcmp(subkey, "parallel")) { 262 int v = git_parse_maybe_bool(value); 263 if (v >= 0) 264 strmap_put(&data->parallel_hooks, hook_name, 265 (void *)(uintptr_t)v); 266 else 267 warning(_("hook.%s.parallel must be a boolean," 268 " ignoring: '%s'"), 269 hook_name, value); 270 } else if (!strcmp(subkey, "jobs")) { 271 int v; 272 if (!git_parse_int(value, &v)) 273 warning(_("hook.%s.jobs must be an integer," 274 " ignoring: '%s'"), 275 hook_name, value); 276 else if (v == -1) 277 strmap_put(&data->event_jobs, hook_name, 278 (void *)(uintptr_t)online_cpus()); 279 else if (v > 0) 280 strmap_put(&data->event_jobs, hook_name, 281 (void *)(uintptr_t)v); 282 else 283 warning(_("hook.%s.jobs must be a positive" 284 " integer or -1, ignoring: '%s'"), 285 hook_name, value); 286 } 287 288 free(hook_name); 289 return 0; 290 } 291 292 /* 293 * The hook config cache maps each hook event name to a string_list where 294 * every item's string is the hook's friendly-name and its util pointer is 295 * the corresponding command string. Both strings are owned by the map. 296 * 297 * Disabled hooks are kept in the cache with entry->disabled set, so that 298 * "git hook list" can display them. A non-disabled hook missing a command 299 * is fatal; a disabled hook missing a command emits a warning and is kept 300 * in the cache with entry->command = NULL. 301 */ 302 void hook_cache_clear(struct strmap *cache) 303 { 304 struct hashmap_iter iter; 305 struct strmap_entry *e; 306 307 strmap_for_each_entry(cache, &iter, e) { 308 struct string_list *hooks = e->value; 309 for (size_t i = 0; i < hooks->nr; i++) { 310 struct hook_config_cache_entry *entry = hooks->items[i].util; 311 free(entry->command); 312 free(entry); 313 } 314 string_list_clear(hooks, 0); 315 free(hooks); 316 } 317 strmap_clear(cache, 0); 318 } 319 320 /* 321 * Return true if `name` is a hook friendly-name, i.e. it has at least one of 322 * .command, .event, or .parallel configured. These are the reliable clues 323 * that distinguish a friendly-name from an event name. Note: .enabled is 324 * deliberately excluded because it can appear under both namespaces. 325 */ 326 static int is_friendly_name(struct hook_all_config_cb *cb, const char *name) 327 { 328 struct hashmap_iter iter; 329 struct strmap_entry *e; 330 331 if (strmap_get(&cb->commands, name) || strmap_get(&cb->parallel_hooks, name)) 332 return 1; 333 334 strmap_for_each_entry(&cb->event_hooks, &iter, e) { 335 if (unsorted_string_list_lookup(e->value, name)) 336 return 1; 337 } 338 339 return 0; 340 } 341 342 /* Warn if any name in event_jobs is also a hook friendly-name. */ 343 static void warn_jobs_on_friendly_names(struct hook_all_config_cb *cb_data) 344 { 345 struct hashmap_iter iter; 346 struct strmap_entry *e; 347 348 strmap_for_each_entry(&cb_data->event_jobs, &iter, e) { 349 if (is_friendly_name(cb_data, e->key)) 350 warning(_("hook.%s.jobs is set but '%s' looks like a " 351 "hook friendly-name, not an event name; " 352 "hook..jobs uses the event name " 353 "(e.g. hook.post-receive.jobs), so this " 354 "setting will be ignored"), e->key, e->key); 355 } 356 } 357 358 /* Populate `cache` with the complete hook configuration */ 359 static void build_hook_config_map(struct repository *r, struct strmap *cache) 360 { 361 struct hook_all_config_cb cb_data = { 0 }; 362 struct hashmap_iter iter; 363 struct strmap_entry *e; 364 365 strmap_init(&cb_data.commands); 366 strmap_init(&cb_data.event_hooks); 367 string_list_init_dup(&cb_data.disabled_hooks); 368 strmap_init(&cb_data.parallel_hooks); 369 strmap_init(&cb_data.event_jobs); 370 371 /* Parse all configs in one run, capturing hook.* including hook.jobs. */ 372 repo_config(r, hook_config_lookup_all, &cb_data); 373 374 warn_jobs_on_friendly_names(&cb_data); 375 376 /* 377 * Populate disabled_events: names in disabled_hooks that are not 378 * friendly-names are event-level switches (hook..enabled = false). 379 * Names that are friendly-names are already handled per-hook via the 380 * hook_config_cache_entry.disabled flag below. 381 */ 382 if (r) { 383 string_list_clear(&r->disabled_events, 0); 384 string_list_init_dup(&r->disabled_events); 385 for (size_t i = 0; i < cb_data.disabled_hooks.nr; i++) { 386 const char *n = cb_data.disabled_hooks.items[i].string; 387 if (!is_friendly_name(&cb_data, n)) 388 string_list_append(&r->disabled_events, n); 389 } 390 } 391 392 /* Construct the cache from parsed configs. */ 393 strmap_for_each_entry(&cb_data.event_hooks, &iter, e) { 394 struct string_list *hook_names = e->value; 395 struct string_list *hooks; 396 397 CALLOC_ARRAY(hooks, 1); 398 string_list_init_dup(hooks); 399 400 for (size_t i = 0; i < hook_names->nr; i++) { 401 const char *hname = hook_names->items[i].string; 402 enum config_scope scope = 403 (enum config_scope)(uintptr_t)hook_names->items[i].util; 404 struct hook_config_cache_entry *entry; 405 char *command; 406 407 bool is_par = !!strmap_get(&cb_data.parallel_hooks, hname); 408 bool is_disabled = 409 !!unsorted_string_list_lookup( 410 &cb_data.disabled_hooks, hname); 411 412 command = strmap_get(&cb_data.commands, hname); 413 if (!command) { 414 if (is_disabled) 415 warning(_("disabled hook '%s' has no " 416 "command configured"), hname); 417 else 418 die(_("'hook.%s.command' must be configured or " 419 "'hook.%s.event' must be removed;" 420 " aborting."), hname, hname); 421 } 422 423 /* util stores a cache entry; owned by the cache. */ 424 CALLOC_ARRAY(entry, 1); 425 entry->command = xstrdup_or_null(command); 426 entry->scope = scope; 427 entry->disabled = is_disabled; 428 entry->parallel = is_par; 429 string_list_append(hooks, hname)->util = entry; 430 } 431 432 strmap_put(cache, e->key, hooks); 433 } 434 435 if (r) { 436 r->hook_jobs = cb_data.jobs; 437 r->event_jobs = cb_data.event_jobs; 438 } 439 440 strmap_clear(&cb_data.commands, 1); 441 strmap_clear(&cb_data.parallel_hooks, 0); /* values are uintptr_t, not heap ptrs */ 442 string_list_clear(&cb_data.disabled_hooks, 0); 443 strmap_for_each_entry(&cb_data.event_hooks, &iter, e) { 444 string_list_clear(e->value, 0); 445 free(e->value); 446 } 447 strmap_clear(&cb_data.event_hooks, 0); 448 } 449 450 /* 451 * Return the hook config map for `r`, populating it first if needed. 452 * 453 * Out-of-repo calls (r->gitdir == NULL) allocate and return a temporary 454 * cache map; the caller is responsible for freeing it with 455 * hook_cache_clear() + free(). 456 */ 457 static struct strmap *get_hook_config_cache(struct repository *r) 458 { 459 struct strmap *cache = NULL; 460 461 if (r && r->gitdir) { 462 /* 463 * For in-repo calls, the map is stored in r->hook_config_cache, 464 * so repeated invocations don't parse the configs, so allocate 465 * it just once on the first call. 466 */ 467 if (!r->hook_config_cache) { 468 CALLOC_ARRAY(r->hook_config_cache, 1); 469 strmap_init(r->hook_config_cache); 470 build_hook_config_map(r, r->hook_config_cache); 471 } 472 cache = r->hook_config_cache; 473 } else { 474 /* 475 * Out-of-repo calls (no gitdir) allocate and return a temporary 476 * cache which gets freed immediately by the caller. 477 */ 478 CALLOC_ARRAY(cache, 1); 479 strmap_init(cache); 480 build_hook_config_map(r, cache); 481 } 482 483 return cache; 484 } 485 486 static void list_hooks_add_configured(struct repository *r, 487 const char *hookname, 488 struct string_list *list, 489 struct run_hooks_opt *options) 490 { 491 struct strmap *cache = get_hook_config_cache(r); 492 struct string_list *configured_hooks = strmap_get(cache, hookname); 493 bool event_is_disabled = r ? !!unsorted_string_list_lookup(&r->disabled_events, 494 hookname) : 0; 495 496 /* Iterate through configured hooks and initialize internal states */ 497 for (size_t i = 0; configured_hooks && i < configured_hooks->nr; i++) { 498 const char *friendly_name = configured_hooks->items[i].string; 499 struct hook_config_cache_entry *entry = configured_hooks->items[i].util; 500 struct hook *hook; 501 502 CALLOC_ARRAY(hook, 1); 503 504 /* 505 * When provided, the alloc/free callbacks are always provided 506 * together, so use them to alloc/free the internal hook state. 507 */ 508 if (options && options->feed_pipe_cb_data_alloc) { 509 hook->feed_pipe_cb_data = 510 options->feed_pipe_cb_data_alloc( 511 options->feed_pipe_ctx); 512 hook->data_free = options->feed_pipe_cb_data_free; 513 } 514 515 hook->kind = HOOK_CONFIGURED; 516 hook->u.configured.friendly_name = xstrdup(friendly_name); 517 hook->u.configured.command = 518 entry->command ? xstrdup(entry->command) : NULL; 519 hook->u.configured.scope = entry->scope; 520 hook->u.configured.disabled = entry->disabled; 521 hook->event_disabled = event_is_disabled; 522 hook->parallel = entry->parallel; 523 524 string_list_append(list, friendly_name)->util = hook; 525 } 526 527 /* 528 * Cleanup temporary cache for out-of-repo calls since they can't be 529 * stored persistently. Next out-of-repo calls will have to re-parse. 530 */ 531 if (!r || !r->gitdir) { 532 hook_cache_clear(cache); 533 free(cache); 534 if (r) 535 string_list_clear(&r->disabled_events, 0); 536 } 537 } 538 539 struct string_list *list_hooks(struct repository *r, const char *hookname, 540 struct run_hooks_opt *options) 541 { 542 struct string_list *hook_head; 543 544 if (!hookname) 545 BUG("null hookname was provided to hook_list()!"); 546 547 CALLOC_ARRAY(hook_head, 1); 548 string_list_init_dup(hook_head); 549 550 /* Add hooks from the config, e.g. hook.myhook.event = pre-commit */ 551 list_hooks_add_configured(r, hookname, hook_head, options); 552 553 /* Add the default "traditional" hooks from hookdir. */ 554 list_hooks_add_default(r, hookname, hook_head, options); 555 556 return hook_head; 557 } 558 559 int hook_exists(struct repository *r, const char *name) 560 { 561 struct string_list *hooks = list_hooks(r, name, NULL); 562 int exists = 0; 563 564 for (size_t i = 0; i < hooks->nr; i++) { 565 struct hook *h = hooks->items[i].util; 566 if (!h->event_disabled && 567 (h->kind == HOOK_TRADITIONAL || 568 !h->u.configured.disabled)) { 569 exists = 1; 570 break; 571 } 572 } 573 string_list_clear_func(hooks, hook_free); 574 free(hooks); 575 return exists; 576 } 577 578 static int pick_next_hook(struct child_process *cp, 579 struct strbuf *out UNUSED, 580 void *pp_cb, 581 void **pp_task_cb) 582 { 583 struct hook_cb_data *hook_cb = pp_cb; 584 struct string_list *hook_list = hook_cb->hook_command_list; 585 struct hook *h; 586 587 do { 588 if (hook_cb->hook_to_run_index >= hook_list->nr) 589 return 0; 590 h = hook_list->items[hook_cb->hook_to_run_index++].util; 591 } while (h->event_disabled || 592 (h->kind == HOOK_CONFIGURED && h->u.configured.disabled)); 593 594 cp->no_stdin = 1; 595 strvec_pushv(&cp->env, hook_cb->options->env.v); 596 597 if (hook_cb->options->path_to_stdin && hook_cb->options->feed_pipe) 598 BUG("options path_to_stdin and feed_pipe are mutually exclusive"); 599 600 /* reopen the file for stdin; run_command closes it. */ 601 if (hook_cb->options->path_to_stdin) { 602 cp->no_stdin = 0; 603 cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY); 604 } 605 606 if (hook_cb->options->feed_pipe) { 607 cp->no_stdin = 0; 608 /* start_command() will allocate a pipe / stdin fd for us */ 609 cp->in = -1; 610 } 611 612 cp->stdout_to_stderr = hook_cb->options->stdout_to_stderr; 613 cp->trace2_hook_name = hook_cb->hook_name; 614 cp->dir = hook_cb->options->dir; 615 616 /* Add hook exec paths or commands */ 617 if (h->kind == HOOK_TRADITIONAL) { 618 strvec_push(&cp->args, h->u.traditional.path); 619 } else if (h->kind == HOOK_CONFIGURED) { 620 /* to enable oneliners, let config-specified hooks run in shell. */ 621 cp->use_shell = true; 622 if (!h->u.configured.command) 623 BUG("non-disabled HOOK_CONFIGURED hook has no command"); 624 strvec_push(&cp->args, h->u.configured.command); 625 } else { 626 BUG("unknown hook kind"); 627 } 628 629 if (!cp->args.nr) 630 BUG("hook must have at least one command or exec path"); 631 632 strvec_pushv(&cp->args, hook_cb->options->args.v); 633 634 /* 635 * Provide per-hook internal state via task_cb for easy access, so 636 * hook callbacks don't have to go through hook_cb->options. 637 */ 638 *pp_task_cb = h->feed_pipe_cb_data; 639 640 return 1; 641 } 642 643 static int notify_start_failure(struct strbuf *out UNUSED, 644 void *pp_cb, 645 void *pp_task_cp UNUSED) 646 { 647 struct hook_cb_data *hook_cb = pp_cb; 648 649 hook_cb->rc |= 1; 650 651 return 1; 652 } 653 654 static int notify_hook_finished(int result, 655 struct strbuf *out UNUSED, 656 void *pp_cb, 657 void *pp_task_cb UNUSED) 658 { 659 struct hook_cb_data *hook_cb = pp_cb; 660 struct run_hooks_opt *opt = hook_cb->options; 661 662 hook_cb->rc |= result; 663 664 if (opt->invoked_hook) 665 *opt->invoked_hook = 1; 666 667 return 0; 668 } 669 670 static void run_hooks_opt_clear(struct run_hooks_opt *options) 671 { 672 strvec_clear(&options->env); 673 strvec_clear(&options->args); 674 } 675 676 /* 677 * When running in parallel, stdout must be merged into stderr so 678 * run-command can buffer and de-interleave outputs correctly. This 679 * applies even to hooks like pre-push that normally keep stdout and 680 * stderr separate: the user has opted into parallelism, so the output 681 * stream behavior changes accordingly. 682 */ 683 static void merge_output_if_parallel(struct run_hooks_opt *options) 684 { 685 if (options->jobs > 1) 686 options->stdout_to_stderr = 1; 687 } 688 689 static void warn_non_parallel_hooks_override(unsigned int jobs, 690 struct string_list *hook_list) 691 { 692 /* Don't warn for hooks running sequentially. */ 693 if (jobs == 1) 694 return; 695 696 for (size_t i = 0; i < hook_list->nr; i++) { 697 struct hook *h = hook_list->items[i].util; 698 if (h->kind == HOOK_CONFIGURED && !h->parallel) 699 warning(_("hook '%s' is not marked as parallel=true, " 700 "running in parallel anyway due to -j%u"), 701 h->u.configured.friendly_name, jobs); 702 } 703 } 704 705 /* Resolve a hook.jobs config key, handling -1 as online_cpus(). */ 706 static void resolve_hook_config_jobs(struct repository *r, 707 const char *key, 708 unsigned int *jobs) 709 { 710 int v; 711 712 if (repo_config_get_int(r, key, &v)) 713 return; 714 715 if (v == -1) 716 *jobs = online_cpus(); 717 else if (v > 0) 718 *jobs = v; 719 else 720 warning(_("%s must be a positive integer or -1," 721 " ignoring: %d"), key, v); 722 } 723 724 /* Determine how many jobs to use for hook execution. */ 725 static unsigned int get_hook_jobs(struct repository *r, 726 struct run_hooks_opt *options, 727 const char *hook_name, 728 struct string_list *hook_list) 729 { 730 /* 731 * An explicit job count overrides everything else: this covers both 732 * FORCE_SERIAL callers (for hooks that must never run in parallel) 733 * and the -j flag from the CLI. The CLI override is intentional: users 734 * may want to serialize hooks declared parallel or to parallelize more 735 * aggressively than the default. 736 */ 737 if (options->jobs) 738 goto cleanup; 739 740 /* 741 * Use hook.jobs from the already-parsed config cache (in-repo), or 742 * fallback to a direct config lookup (out-of-repo). 743 * Default to 1 (serial execution) on failure. 744 */ 745 options->jobs = 1; 746 if (r) { 747 if (r->gitdir && r->hook_config_cache) { 748 void *event_jobs; 749 750 if (r->hook_jobs) 751 options->jobs = r->hook_jobs; 752 753 event_jobs = strmap_get(&r->event_jobs, hook_name); 754 if (event_jobs) 755 options->jobs = (unsigned int)(uintptr_t)event_jobs; 756 } else { 757 char *key; 758 759 resolve_hook_config_jobs(r, "hook.jobs", &options->jobs); 760 761 key = xstrfmt("hook.%s.jobs", hook_name); 762 resolve_hook_config_jobs(r, key, &options->jobs); 763 free(key); 764 } 765 } 766 767 /* 768 * Cap to serial any configured hook not marked as parallel = true. 769 * This enforces the parallel = false default, even for "traditional" 770 * hooks from the hookdir which cannot be marked parallel = true. 771 * The same restriction applies whether jobs came from hook.jobs or 772 * hook..jobs. 773 */ 774 for (size_t i = 0; i < hook_list->nr; i++) { 775 struct hook *h = hook_list->items[i].util; 776 if (h->kind == HOOK_CONFIGURED && !h->parallel) { 777 options->jobs = 1; 778 break; 779 } 780 } 781 782 cleanup: 783 merge_output_if_parallel(options); 784 warn_non_parallel_hooks_override(options->jobs, hook_list); 785 return options->jobs; 786 } 787 788 int run_hooks_opt(struct repository *r, const char *hook_name, 789 struct run_hooks_opt *options) 790 { 791 struct string_list *hook_list = list_hooks(r, hook_name, options); 792 struct hook_cb_data cb_data = { 793 .rc = 0, 794 .hook_name = hook_name, 795 .hook_command_list = hook_list, 796 .options = options, 797 }; 798 int ret = 0; 799 unsigned int jobs = get_hook_jobs(r, options, hook_name, hook_list); 800 const struct run_process_parallel_opts opts = { 801 .tr2_category = "hook", 802 .tr2_label = hook_name, 803 804 .processes = jobs, 805 .ungroup = jobs == 1, 806 807 .get_next_task = pick_next_hook, 808 .start_failure = notify_start_failure, 809 .feed_pipe = options->feed_pipe, 810 .task_finished = notify_hook_finished, 811 812 .data = &cb_data, 813 }; 814 815 if (!options) 816 BUG("a struct run_hooks_opt must be provided to run_hooks"); 817 818 if (options->path_to_stdin && options->feed_pipe) 819 BUG("options path_to_stdin and feed_pipe are mutually exclusive"); 820 821 /* 822 * Ensure cb_data copy and free functions are either provided together, 823 * or neither one is provided. 824 */ 825 if (!options->feed_pipe_cb_data_alloc != !options->feed_pipe_cb_data_free) 826 BUG("feed_pipe_cb_data_alloc and feed_pipe_cb_data_free must be set together"); 827 828 if (options->invoked_hook) 829 *options->invoked_hook = 0; 830 831 if (!cb_data.hook_command_list->nr) { 832 if (options->error_if_missing) 833 ret = error("cannot find a hook named %s", hook_name); 834 goto cleanup; 835 } 836 837 run_processes_parallel(&opts); 838 ret = cb_data.rc; 839 cleanup: 840 string_list_clear_func(cb_data.hook_command_list, hook_free); 841 free(cb_data.hook_command_list); 842 run_hooks_opt_clear(options); 843 return ret; 844 } 845 846 int run_hooks(struct repository *r, const char *hook_name) 847 { 848 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; 849 850 return run_hooks_opt(r, hook_name, &opt); 851 } 852 853 int run_hooks_l(struct repository *r, const char *hook_name, ...) 854 { 855 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; 856 va_list ap; 857 const char *arg; 858 859 va_start(ap, hook_name); 860 while ((arg = va_arg(ap, const char *))) 861 strvec_push(&opt.args, arg); 862 va_end(ap); 863 864 return run_hooks_opt(r, hook_name, &opt); 865 }