submodule--helper: introduce new update-module-mode helper

This chews off a bit of the shell part of the update command in git-submodule.sh. When writing the C code, keep in mind that the submodule--helper part will go away eventually and we want to have a C function that is able to determine the submodule update strategy, it as a nicety, make determine_submodule_update_strategy accessible for arbitrary repositories. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Aug 13, 2018 at 15:42 UTC ee69b2a90c5031bffb3341c5e50653a6ecca89ac
2 files changed +62 -15
builtin/submodule--helper.c
+61
@@ -1446,6 +1446,66 @@ static int module_clone(int argc, const char **argv, const char *prefix)
1446 return 0;
1447 }
1448
1449 +static void determine_submodule_update_strategy(struct repository *r,
1450 + int just_cloned,
1451 + const char *path,
1452 + const char *update,
1453 + struct submodule_update_strategy *out)
1454 +{
1455 + const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1456 + char *key;
1457 + const char *val;
1458 +
1459 + key = xstrfmt("submodule.%s.update", sub->name);
1460 +
1461 + if (update) {
1462 + trace_printf("parsing update");
1463 + if (parse_submodule_update_strategy(update, out) < 0)
1464 + die(_("Invalid update mode '%s' for submodule path '%s'"),
1465 + update, path);
1466 + } else if (!repo_config_get_string_const(r, key, &val)) {
1467 + if (parse_submodule_update_strategy(val, out) < 0)
1468 + die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1469 + val, path);
1470 + } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1471 + trace_printf("loaded thing");
1472 + out->type = sub->update_strategy.type;
1473 + out->command = sub->update_strategy.command;
1474 + } else
1475 + out->type = SM_UPDATE_CHECKOUT;
1476 +
1477 + if (just_cloned &&
1478 + (out->type == SM_UPDATE_MERGE ||
1479 + out->type == SM_UPDATE_REBASE ||
1480 + out->type == SM_UPDATE_NONE))
1481 + out->type = SM_UPDATE_CHECKOUT;
1482 +
1483 + free(key);
1484 +}
1485 +
1486 +static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1487 +{
1488 + const char *path, *update = NULL;
1489 + int just_cloned;
1490 + struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1491 +
1492 + if (argc < 3 || argc > 4)
1493 + die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1494 +
1495 + just_cloned = git_config_int("just_cloned", argv[1]);
1496 + path = argv[2];
1497 +
1498 + if (argc == 4)
1499 + update = argv[3];
1500 +
1501 + determine_submodule_update_strategy(the_repository,
1502 + just_cloned, path, update,
1503 + &update_strategy);
1504 + fputs(submodule_strategy_to_string(&update_strategy), stdout);
1505 +
1506 + return 0;
1507 +}
1508 +
1509 struct update_clone_data {
1510 const struct submodule *sub;
1511 struct object_id oid;
@@ -2080,6 +2140,7 @@ static struct cmd_struct commands[] = {
2140 {"list", module_list, 0},
2141 {"name", module_name, 0},
2142 {"clone", module_clone, 0},
2143 + {"update-module-mode", module_update_module_mode, 0},
2144 {"update-clone", update_clone, 0},
2145 {"ensure-core-worktree", ensure_core_worktree, 0},
2146 {"relative-path", resolve_relative_path, 0},
git-submodule.sh
+1 -15
@@ -537,27 +537,13 @@ cmd_update()
537
538 git submodule--helper ensure-core-worktree "$sm_path"
539
540 - name=$(git submodule--helper name "$sm_path") || exit
541 - if ! test -z "$update"
542 - then
543 - update_module=$update
544 - else
545 - update_module=$(git config submodule."$name".update)
546 - if test -z "$update_module"
547 - then
548 - update_module="checkout"
549 - fi
550 - fi
540 + update_module=$(git submodule--helper update-module-mode $just_cloned "$sm_path" $update)
541
542 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
543
544 if test $just_cloned -eq 1
545 then
546 subsha1=
557 - case "$update_module" in
558 - merge | rebase | none)
559 - update_module=checkout ;;
560 - esac
547 else
548 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
549 git rev-parse --verify HEAD) ||