submodule--helper: remove forward declaration

Rearrange functions so that submodule_update() no longer needs to be forward declared. Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 15, 2022 at 14:09 UTC f3875ab11528e605a0482940155e137537d238df
1 file changed +196 -197
builtin/submodule--helper.c
+196 -197
@@ -2452,7 +2452,202 @@ static int run_update_procedure(struct update_data *ud)
2452 return run_update_command(ud, subforce);
2453 }
2454
2455 -static int update_submodule(struct update_data *update_data);
2455 +static const char *remote_submodule_branch(const char *path)
2456 +{
2457 + const struct submodule *sub;
2458 + const char *branch = NULL;
2459 + char *key;
2460 +
2461 + sub = submodule_from_path(the_repository, null_oid(), path);
2462 + if (!sub)
2463 + return NULL;
2464 +
2465 + key = xstrfmt("submodule.%s.branch", sub->name);
2466 + if (repo_config_get_string_tmp(the_repository, key, &branch))
2467 + branch = sub->branch;
2468 + free(key);
2469 +
2470 + if (!branch)
2471 + return "HEAD";
2472 +
2473 + if (!strcmp(branch, ".")) {
2474 + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2475 +
2476 + if (!refname)
2477 + die(_("No such ref: %s"), "HEAD");
2478 +
2479 + /* detached HEAD */
2480 + if (!strcmp(refname, "HEAD"))
2481 + die(_("Submodule (%s) branch configured to inherit "
2482 + "branch from superproject, but the superproject "
2483 + "is not on any branch"), sub->name);
2484 +
2485 + if (!skip_prefix(refname, "refs/heads/", &refname))
2486 + die(_("Expecting a full ref name, got %s"), refname);
2487 + return refname;
2488 + }
2489 +
2490 + return branch;
2491 +}
2492 +
2493 +static void ensure_core_worktree(const char *path)
2494 +{
2495 + const char *cw;
2496 + struct repository subrepo;
2497 +
2498 + if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2499 + die(_("could not get a repository handle for submodule '%s'"), path);
2500 +
2501 + if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2502 + char *cfg_file, *abs_path;
2503 + const char *rel_path;
2504 + struct strbuf sb = STRBUF_INIT;
2505 +
2506 + cfg_file = repo_git_path(&subrepo, "config");
2507 +
2508 + abs_path = absolute_pathdup(path);
2509 + rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2510 +
2511 + git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2512 +
2513 + free(cfg_file);
2514 + free(abs_path);
2515 + strbuf_release(&sb);
2516 + }
2517 +}
2518 +
2519 +static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2520 +{
2521 + strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2522 + strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2523 + if (update_data->recursive_prefix)
2524 + strvec_pushl(args, "--recursive-prefix",
2525 + update_data->recursive_prefix, NULL);
2526 + if (update_data->quiet)
2527 + strvec_push(args, "--quiet");
2528 + if (update_data->force)
2529 + strvec_push(args, "--force");
2530 + if (update_data->init)
2531 + strvec_push(args, "--init");
2532 + if (update_data->remote)
2533 + strvec_push(args, "--remote");
2534 + if (update_data->nofetch)
2535 + strvec_push(args, "--no-fetch");
2536 + if (update_data->dissociate)
2537 + strvec_push(args, "--dissociate");
2538 + if (update_data->progress)
2539 + strvec_push(args, "--progress");
2540 + if (update_data->require_init)
2541 + strvec_push(args, "--require-init");
2542 + if (update_data->depth)
2543 + strvec_pushf(args, "--depth=%d", update_data->depth);
2544 + if (update_data->update_default)
2545 + strvec_pushl(args, "--update", update_data->update_default, NULL);
2546 + if (update_data->references.nr) {
2547 + struct string_list_item *item;
2548 + for_each_string_list_item(item, &update_data->references)
2549 + strvec_pushl(args, "--reference", item->string, NULL);
2550 + }
2551 + if (update_data->filter_options && update_data->filter_options->choice)
2552 + strvec_pushf(args, "--filter=%s",
2553 + expand_list_objects_filter_spec(
2554 + update_data->filter_options));
2555 + if (update_data->recommend_shallow == 0)
2556 + strvec_push(args, "--no-recommend-shallow");
2557 + else if (update_data->recommend_shallow == 1)
2558 + strvec_push(args, "--recommend-shallow");
2559 + if (update_data->single_branch >= 0)
2560 + strvec_push(args, update_data->single_branch ?
2561 + "--single-branch" :
2562 + "--no-single-branch");
2563 +}
2564 +
2565 +static int update_submodule(struct update_data *update_data)
2566 +{
2567 + char *prefixed_path;
2568 +
2569 + ensure_core_worktree(update_data->sm_path);
2570 +
2571 + if (update_data->recursive_prefix)
2572 + prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix,
2573 + update_data->sm_path);
2574 + else
2575 + prefixed_path = xstrdup(update_data->sm_path);
2576 +
2577 + update_data->displaypath = get_submodule_displaypath(prefixed_path,
2578 + update_data->prefix);
2579 + free(prefixed_path);
2580 +
2581 + determine_submodule_update_strategy(the_repository, update_data->just_cloned,
2582 + update_data->sm_path, update_data->update_default,
2583 + &update_data->update_strategy);
2584 +
2585 + if (update_data->just_cloned)
2586 + oidcpy(&update_data->suboid, null_oid());
2587 + else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2588 + die(_("Unable to find current revision in submodule path '%s'"),
2589 + update_data->displaypath);
2590 +
2591 + if (update_data->remote) {
2592 + char *remote_name = get_default_remote_submodule(update_data->sm_path);
2593 + const char *branch = remote_submodule_branch(update_data->sm_path);
2594 + char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2595 +
2596 + if (!update_data->nofetch) {
2597 + if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2598 + 0, NULL))
2599 + die(_("Unable to fetch in submodule path '%s'"),
2600 + update_data->sm_path);
2601 + }
2602 +
2603 + if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2604 + die(_("Unable to find %s revision in submodule path '%s'"),
2605 + remote_ref, update_data->sm_path);
2606 +
2607 + free(remote_ref);
2608 + }
2609 +
2610 + if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
2611 + if (run_update_procedure(update_data))
2612 + return 1;
2613 +
2614 + if (update_data->recursive) {
2615 + struct child_process cp = CHILD_PROCESS_INIT;
2616 + struct update_data next = *update_data;
2617 + int res;
2618 +
2619 + if (update_data->recursive_prefix)
2620 + prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix,
2621 + update_data->sm_path);
2622 + else
2623 + prefixed_path = xstrfmt("%s/", update_data->sm_path);
2624 +
2625 + next.recursive_prefix = get_submodule_displaypath(prefixed_path,
2626 + update_data->prefix);
2627 + next.prefix = NULL;
2628 + oidcpy(&next.oid, null_oid());
2629 + oidcpy(&next.suboid, null_oid());
2630 +
2631 + cp.dir = update_data->sm_path;
2632 + cp.git_cmd = 1;
2633 + prepare_submodule_repo_env(&cp.env_array);
2634 + update_data_to_args(&next, &cp.args);
2635 +
2636 + /* die() if child process die()'d */
2637 + res = run_command(&cp);
2638 + if (!res)
2639 + return 0;
2640 + die_message(_("Failed to recurse into submodule path '%s'"),
2641 + update_data->displaypath);
2642 + if (res == 128)
2643 + exit(res);
2644 + else if (res)
2645 + return 1;
2646 + }
2647 +
2648 + return 0;
2649 +}
2650 +
2651 static int update_submodules(struct update_data *update_data)
2652 {
2653 int i, res = 0;
@@ -2607,44 +2802,6 @@ static int module_update(int argc, const char **argv, const char *prefix)
2802 return ret;
2803 }
2804
2610 -static const char *remote_submodule_branch(const char *path)
2611 -{
2612 - const struct submodule *sub;
2613 - const char *branch = NULL;
2614 - char *key;
2615 -
2616 - sub = submodule_from_path(the_repository, null_oid(), path);
2617 - if (!sub)
2618 - return NULL;
2619 -
2620 - key = xstrfmt("submodule.%s.branch", sub->name);
2621 - if (repo_config_get_string_tmp(the_repository, key, &branch))
2622 - branch = sub->branch;
2623 - free(key);
2624 -
2625 - if (!branch)
2626 - return "HEAD";
2627 -
2628 - if (!strcmp(branch, ".")) {
2629 - const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2630 -
2631 - if (!refname)
2632 - die(_("No such ref: %s"), "HEAD");
2633 -
2634 - /* detached HEAD */
2635 - if (!strcmp(refname, "HEAD"))
2636 - die(_("Submodule (%s) branch configured to inherit "
2637 - "branch from superproject, but the superproject "
2638 - "is not on any branch"), sub->name);
2639 -
2640 - if (!skip_prefix(refname, "refs/heads/", &refname))
2641 - die(_("Expecting a full ref name, got %s"), refname);
2642 - return refname;
2643 - }
2644 -
2645 - return branch;
2646 -}
2647 -
2805 static int push_check(int argc, const char **argv, const char *prefix)
2806 {
2807 struct remote *remote;
@@ -2722,32 +2879,6 @@ static int push_check(int argc, const char **argv, const char *prefix)
2879 return 0;
2880 }
2881
2725 -static void ensure_core_worktree(const char *path)
2726 -{
2727 - const char *cw;
2728 - struct repository subrepo;
2729 -
2730 - if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2731 - die(_("could not get a repository handle for submodule '%s'"), path);
2732 -
2733 - if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2734 - char *cfg_file, *abs_path;
2735 - const char *rel_path;
2736 - struct strbuf sb = STRBUF_INIT;
2737 -
2738 - cfg_file = repo_git_path(&subrepo, "config");
2739 -
2740 - abs_path = absolute_pathdup(path);
2741 - rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2742 -
2743 - git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2744 -
2745 - free(cfg_file);
2746 - free(abs_path);
2747 - strbuf_release(&sb);
2748 - }
2749 -}
2750 -
2882 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2883 {
2884 int i;
@@ -2969,138 +3100,6 @@ static int module_create_branch(int argc, const char **argv, const char *prefix)
3100 return 0;
3101 }
3102
2972 -static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2973 -{
2974 - strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2975 - strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2976 - if (update_data->recursive_prefix)
2977 - strvec_pushl(args, "--recursive-prefix",
2978 - update_data->recursive_prefix, NULL);
2979 - if (update_data->quiet)
2980 - strvec_push(args, "--quiet");
2981 - if (update_data->force)
2982 - strvec_push(args, "--force");
2983 - if (update_data->init)
2984 - strvec_push(args, "--init");
2985 - if (update_data->remote)
2986 - strvec_push(args, "--remote");
2987 - if (update_data->nofetch)
2988 - strvec_push(args, "--no-fetch");
2989 - if (update_data->dissociate)
2990 - strvec_push(args, "--dissociate");
2991 - if (update_data->progress)
2992 - strvec_push(args, "--progress");
2993 - if (update_data->require_init)
2994 - strvec_push(args, "--require-init");
2995 - if (update_data->depth)
2996 - strvec_pushf(args, "--depth=%d", update_data->depth);
2997 - if (update_data->update_default)
2998 - strvec_pushl(args, "--update", update_data->update_default, NULL);
2999 - if (update_data->references.nr) {
3000 - struct string_list_item *item;
3001 - for_each_string_list_item(item, &update_data->references)
3002 - strvec_pushl(args, "--reference", item->string, NULL);
3003 - }
3004 - if (update_data->filter_options && update_data->filter_options->choice)
3005 - strvec_pushf(args, "--filter=%s",
3006 - expand_list_objects_filter_spec(
3007 - update_data->filter_options));
3008 - if (update_data->recommend_shallow == 0)
3009 - strvec_push(args, "--no-recommend-shallow");
3010 - else if (update_data->recommend_shallow == 1)
3011 - strvec_push(args, "--recommend-shallow");
3012 - if (update_data->single_branch >= 0)
3013 - strvec_push(args, update_data->single_branch ?
3014 - "--single-branch" :
3015 - "--no-single-branch");
3016 -}
3017 -
3018 -static int update_submodule(struct update_data *update_data)
3019 -{
3020 - char *prefixed_path;
3021 -
3022 - ensure_core_worktree(update_data->sm_path);
3023 -
3024 - if (update_data->recursive_prefix)
3025 - prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix,
3026 - update_data->sm_path);
3027 - else
3028 - prefixed_path = xstrdup(update_data->sm_path);
3029 -
3030 - update_data->displaypath = get_submodule_displaypath(prefixed_path,
3031 - update_data->prefix);
3032 - free(prefixed_path);
3033 -
3034 - determine_submodule_update_strategy(the_repository, update_data->just_cloned,
3035 - update_data->sm_path, update_data->update_default,
3036 - &update_data->update_strategy);
3037 -
3038 - if (update_data->just_cloned)
3039 - oidcpy(&update_data->suboid, null_oid());
3040 - else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
3041 - die(_("Unable to find current revision in submodule path '%s'"),
3042 - update_data->displaypath);
3043 -
3044 - if (update_data->remote) {
3045 - char *remote_name = get_default_remote_submodule(update_data->sm_path);
3046 - const char *branch = remote_submodule_branch(update_data->sm_path);
3047 - char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
3048 -
3049 - if (!update_data->nofetch) {
3050 - if (fetch_in_submodule(update_data->sm_path, update_data->depth,
3051 - 0, NULL))
3052 - die(_("Unable to fetch in submodule path '%s'"),
3053 - update_data->sm_path);
3054 - }
3055 -
3056 - if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
3057 - die(_("Unable to find %s revision in submodule path '%s'"),
3058 - remote_ref, update_data->sm_path);
3059 -
3060 - free(remote_ref);
3061 - }
3062 -
3063 - if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
3064 - if (run_update_procedure(update_data))
3065 - return 1;
3066 -
3067 - if (update_data->recursive) {
3068 - struct child_process cp = CHILD_PROCESS_INIT;
3069 - struct update_data next = *update_data;
3070 - int res;
3071 -
3072 - if (update_data->recursive_prefix)
3073 - prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix,
3074 - update_data->sm_path);
3075 - else
3076 - prefixed_path = xstrfmt("%s/", update_data->sm_path);
3077 -
3078 - next.recursive_prefix = get_submodule_displaypath(prefixed_path,
3079 - update_data->prefix);
3080 - next.prefix = NULL;
3081 - oidcpy(&next.oid, null_oid());
3082 - oidcpy(&next.suboid, null_oid());
3083 -
3084 - cp.dir = update_data->sm_path;
3085 - cp.git_cmd = 1;
3086 - prepare_submodule_repo_env(&cp.env_array);
3087 - update_data_to_args(&next, &cp.args);
3088 -
3089 - /* die() if child process die()'d */
3090 - res = run_command(&cp);
3091 - if (!res)
3092 - return 0;
3093 - die_message(_("Failed to recurse into submodule path '%s'"),
3094 - update_data->displaypath);
3095 - if (res == 128)
3096 - exit(res);
3097 - else if (res)
3098 - return 1;
3099 - }
3100 -
3101 - return 0;
3102 -}
3103 -
3103 struct add_data {
3104 const char *prefix;
3105 const char *branch;