Skip to content

Commit ee69b2a

Browse files
stefanbellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74d4731 commit ee69b2a

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

builtin/submodule--helper.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,66 @@ static int module_clone(int argc, const char **argv, const char *prefix)
14461446
return 0;
14471447
}
14481448

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+
14491509
struct update_clone_data {
14501510
const struct submodule *sub;
14511511
struct object_id oid;
@@ -2080,6 +2140,7 @@ static struct cmd_struct commands[] = {
20802140
{"list", module_list, 0},
20812141
{"name", module_name, 0},
20822142
{"clone", module_clone, 0},
2143+
{"update-module-mode", module_update_module_mode, 0},
20832144
{"update-clone", update_clone, 0},
20842145
{"ensure-core-worktree", ensure_core_worktree, 0},
20852146
{"relative-path", resolve_relative_path, 0},

git-submodule.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -537,27 +537,13 @@ cmd_update()
537537

538538
git submodule--helper ensure-core-worktree "$sm_path"
539539

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)
551541

552542
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
553543

554544
if test $just_cloned -eq 1
555545
then
556546
subsha1=
557-
case "$update_module" in
558-
merge | rebase | none)
559-
update_module=checkout ;;
560-
esac
561547
else
562548
subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
563549
git rev-parse --verify HEAD) ||

0 commit comments

Comments
 (0)