submodule--helper: understand --checkout, --merge and --rebase synonyms

Understand --checkout, --merge and --rebase synonyms for --update={checkout,merge,rebase}, as well as the short options that 'git submodule' itself understands. This removes a difference between the CLI API of "git submodule" and "git submodule--helper", making it easier to make the latter an alias for the former. See 48308681b07 (git submodule update: have a dedicated helper for cloning, 2016-02-29) for the initial addition of --update. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 28, 2022 at 12:05 UTC 8f12108c2951cdfa181d6be66b6def28cd007bdd
2 files changed +39 -5
builtin/submodule--helper.c
+30
@@ -2404,6 +2404,23 @@ static void ensure_core_worktree(const char *path)
2404 }
2405 }
2406
2407 +static const char *submodule_update_type_to_label(enum submodule_update_type type)
2408 +{
2409 + switch (type) {
2410 + case SM_UPDATE_CHECKOUT:
2411 + return "checkout";
2412 + case SM_UPDATE_MERGE:
2413 + return "merge";
2414 + case SM_UPDATE_REBASE:
2415 + return "rebase";
2416 + case SM_UPDATE_UNSPECIFIED:
2417 + case SM_UPDATE_NONE:
2418 + case SM_UPDATE_COMMAND:
2419 + break;
2420 + }
2421 + BUG("unreachable with type %d", type);
2422 +}
2423 +
2424 static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2425 {
2426 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
@@ -2582,6 +2599,7 @@ static int module_update(int argc, const char **argv, const char *prefix)
2599 struct update_data opt = UPDATE_DATA_INIT;
2600 struct list_objects_filter_options filter_options;
2601 int ret;
2602 + enum submodule_update_type update_type = SM_UPDATE_UNSPECIFIED;
2603
2604 struct option module_update_options[] = {
2605 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
@@ -2603,6 +2621,15 @@ static int module_update(int argc, const char **argv, const char *prefix)
2621 OPT_STRING(0, "update", &opt.update_default,
2622 N_("string"),
2623 N_("rebase, merge, checkout or none")),
2624 + OPT_SET_INT(0, "checkout", &update_type,
2625 + N_("use the 'checkout' update strategy (default)"),
2626 + SM_UPDATE_CHECKOUT),
2627 + OPT_SET_INT('m', "merge", &update_type,
2628 + N_("use the 'merge' update strategy"),
2629 + SM_UPDATE_MERGE),
2630 + OPT_SET_INT('r', "rebase", &update_type,
2631 + N_("use the 'rebase' update strategy"),
2632 + SM_UPDATE_REBASE),
2633 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2634 N_("reference repository")),
2635 OPT_BOOL(0, "dissociate", &opt.dissociate,
@@ -2652,6 +2679,9 @@ static int module_update(int argc, const char **argv, const char *prefix)
2679
2680 opt.filter_options = &filter_options;
2681
2682 + if (update_type != SM_UPDATE_UNSPECIFIED)
2683 + opt.update_default = submodule_update_type_to_label(update_type);
2684 +
2685 if (opt.update_default)
2686 if (parse_submodule_update_strategy(opt.update_default,
2687 &opt.update_strategy) < 0)
git-submodule.sh
+9 -5
@@ -40,7 +40,9 @@ require_init=
40 files=
41 remote=
42 nofetch=
43 -update=
43 +rebase=
44 +merge=
45 +checkout=
46 custom_name=
47 depth=
48 progress=
@@ -260,7 +262,7 @@ cmd_update()
262 force=$1
263 ;;
264 -r|--rebase)
263 - update="rebase"
265 + rebase=1
266 ;;
267 --reference)
268 case "$2" in '') usage ;; esac
@@ -274,13 +276,13 @@ cmd_update()
276 dissociate=1
277 ;;
278 -m|--merge)
277 - update="merge"
279 + merge=1
280 ;;
281 --recursive)
282 recursive=1
283 ;;
284 --checkout)
283 - update="checkout"
285 + checkout=1
286 ;;
287 --recommend-shallow)
288 recommend_shallow="--recommend-shallow"
@@ -341,7 +343,9 @@ cmd_update()
343 ${init:+--init} \
344 ${nofetch:+--no-fetch} \
345 ${wt_prefix:+--prefix "$wt_prefix"} \
344 - ${update:+--update "$update"} \
346 + ${rebase:+--rebase} \
347 + ${merge:+--merge} \
348 + ${checkout:+--checkout} \
349 ${reference:+"$reference"} \
350 ${dissociate:+"--dissociate"} \
351 ${depth:+"$depth"} \