submodule--helper run-update-procedure: learn --remote

Teach run-update-procedure to handle --remote instead of parsing --remote in git-submodule.sh. As a result, "git submodule--helper [print-default-remote|remote-branch]" have no more callers, so remove them. Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 4, 2022 at 16:13 UTC 1012a5cbc3fe4bcfae278a8103e60054d2674784
2 files changed +23 -63
builtin/submodule--helper.c
+22 -34
@@ -72,21 +72,6 @@ static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository);
73 }
74
75 -static int print_default_remote(int argc, const char **argv, const char *prefix)
76 -{
77 - char *remote;
78 -
79 - if (argc != 1)
80 - die(_("submodule--helper print-default-remote takes no arguments"));
81 -
82 - remote = get_default_remote();
83 - if (remote)
84 - printf("%s\n", remote);
85 -
86 - free(remote);
87 - return 0;
88 -}
89 -
75 static int starts_with_dot_slash(const char *str)
76 {
77 return str[0] == '.' && is_dir_sep(str[1]);
@@ -2027,6 +2012,7 @@ struct update_data {
2012 unsigned int quiet;
2013 unsigned int nofetch;
2014 unsigned int just_cloned;
2015 + unsigned int remote;
2016 };
2017 #define UPDATE_DATA_INIT { .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT }
2018
@@ -2603,6 +2589,8 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
2589 OPT_CALLBACK_F(0, "oid", &update_data.oid, N_("sha1"),
2590 N_("SHA1 expected by superproject"), PARSE_OPT_NONEG,
2591 parse_opt_object_id),
2592 + OPT_BOOL(0, "remote", &update_data.remote,
2593 + N_("use SHA-1 of submodule's remote tracking branch")),
2594 OPT_END()
2595 };
2596
@@ -2682,23 +2670,6 @@ static const char *remote_submodule_branch(const char *path)
2670 return branch;
2671 }
2672
2685 -static int resolve_remote_submodule_branch(int argc, const char **argv,
2686 - const char *prefix)
2687 -{
2688 - const char *ret;
2689 - struct strbuf sb = STRBUF_INIT;
2690 - if (argc != 2)
2691 - die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
2692 -
2693 - ret = remote_submodule_branch(argv[1]);
2694 - if (!ret)
2695 - die("submodule %s doesn't exist", argv[1]);
2696 -
2697 - printf("%s", ret);
2698 - strbuf_release(&sb);
2699 - return 0;
2700 -}
2701 -
2673 static int push_check(int argc, const char **argv, const char *prefix)
2674 {
2675 struct remote *remote;
@@ -3040,6 +3011,25 @@ static int update_submodule2(struct update_data *update_data)
3011 die(_("Unable to find current revision in submodule path '%s'"),
3012 update_data->displaypath);
3013
3014 + if (update_data->remote) {
3015 + char *remote_name = get_default_remote_submodule(update_data->sm_path);
3016 + const char *branch = remote_submodule_branch(update_data->sm_path);
3017 + char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
3018 +
3019 + if (!update_data->nofetch) {
3020 + if (fetch_in_submodule(update_data->sm_path, update_data->depth,
3021 + 0, NULL))
3022 + die(_("Unable to fetch in submodule path '%s'"),
3023 + update_data->sm_path);
3024 + }
3025 +
3026 + if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
3027 + die(_("Unable to find %s revision in submodule path '%s'"),
3028 + remote_ref, update_data->sm_path);
3029 +
3030 + free(remote_ref);
3031 + }
3032 +
3033 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
3034 return do_run_update_procedure(update_data);
3035
@@ -3439,11 +3429,9 @@ static struct cmd_struct commands[] = {
3429 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3430 {"init", module_init, SUPPORT_SUPER_PREFIX},
3431 {"status", module_status, SUPPORT_SUPER_PREFIX},
3442 - {"print-default-remote", print_default_remote, 0},
3432 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3433 {"deinit", module_deinit, 0},
3434 {"summary", module_summary, SUPPORT_SUPER_PREFIX},
3446 - {"remote-branch", resolve_remote_submodule_branch, 0},
3435 {"push-check", push_check, 0},
3436 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3437 {"is-active", is_active, 0},
git-submodule.sh
+1 -29
@@ -247,20 +247,6 @@ cmd_deinit()
247 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
248 }
249
250 -# usage: fetch_in_submodule <module_path> [<depth>] [<sha1>]
251 -# Because arguments are positional, use an empty string to omit <depth>
252 -# but include <sha1>.
253 -fetch_in_submodule () (
254 - sanitize_submodule_env &&
255 - cd "$1" &&
256 - if test $# -eq 3
257 - then
258 - echo "$3" | git fetch ${GIT_QUIET:+--quiet} --stdin ${2:+"$2"}
259 - else
260 - git fetch ${GIT_QUIET:+--quiet} ${2:+"$2"}
261 - fi
262 -)
263 -
250 #
251 # Update each submodule path to correct revision, using clone and checkout as needed
252 #
@@ -411,21 +397,6 @@ cmd_update()
397 just_cloned=
398 fi
399
414 - if test -n "$remote"
415 - then
416 - branch=$(git submodule--helper remote-branch "$sm_path")
417 - if test -z "$nofetch"
418 - then
419 - # Fetch remote before determining tracking $sha1
420 - fetch_in_submodule "$sm_path" $depth ||
421 - die "fatal: $(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
422 - fi
423 - remote_name=$(sanitize_submodule_env; cd "$sm_path" && git submodule--helper print-default-remote)
424 - sha1=$(sanitize_submodule_env; cd "$sm_path" &&
425 - git rev-parse --verify "${remote_name}/${branch}") ||
426 - die "fatal: $(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
427 - fi
428 -
400 out=$(git submodule--helper run-update-procedure \
401 ${wt_prefix:+--prefix "$wt_prefix"} \
402 ${GIT_QUIET:+--quiet} \
@@ -436,6 +407,7 @@ cmd_update()
407 ${update:+--update "$update"} \
408 ${prefix:+--recursive-prefix "$prefix"} \
409 ${sha1:+--oid "$sha1"} \
410 + ${remote:+--remote} \
411 "--" \
412 "$sm_path")
413