Skip to content

Commit 3c156c7

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 fc4e14d commit 3c156c7

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
@@ -1444,6 +1444,66 @@ static int module_clone(int argc, const char **argv, const char *prefix)
14441444
return 0;
14451445
}
14461446

1447+
static void determine_submodule_update_strategy(struct repository *r,
1448+
int just_cloned,
1449+
const char *path,
1450+
const char *update,
1451+
struct submodule_update_strategy *out)
1452+
{
1453+
const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1454+
char *key;
1455+
const char *val;
1456+
1457+
key = xstrfmt("submodule.%s.update", sub->name);
1458+
1459+
if (update) {
1460+
trace_printf("parsing update");
1461+
if (parse_submodule_update_strategy(update, out) < 0)
1462+
die(_("Invalid update mode '%s' for submodule path '%s'"),
1463+
update, path);
1464+
} else if (!repo_config_get_string_const(r, key, &val)) {
1465+
if (parse_submodule_update_strategy(val, out) < 0)
1466+
die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1467+
val, path);
1468+
} else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1469+
trace_printf("loaded thing");
1470+
out->type = sub->update_strategy.type;
1471+
out->command = sub->update_strategy.command;
1472+
} else
1473+
out->type = SM_UPDATE_CHECKOUT;
1474+
1475+
if (just_cloned &&
1476+
(out->type == SM_UPDATE_MERGE ||
1477+
out->type == SM_UPDATE_REBASE ||
1478+
out->type == SM_UPDATE_NONE))
1479+
out->type = SM_UPDATE_CHECKOUT;
1480+
1481+
free(key);
1482+
}
1483+
1484+
static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1485+
{
1486+
const char *path, *update = NULL;
1487+
int just_cloned;
1488+
struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1489+
1490+
if (argc < 3 || argc > 4)
1491+
die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1492+
1493+
just_cloned = git_config_int("just_cloned", argv[1]);
1494+
path = argv[2];
1495+
1496+
if (argc == 4)
1497+
update = argv[3];
1498+
1499+
determine_submodule_update_strategy(the_repository,
1500+
just_cloned, path, update,
1501+
&update_strategy);
1502+
fprintf(stdout, submodule_strategy_to_string(&update_strategy));
1503+
1504+
return 0;
1505+
}
1506+
14471507
struct update_clone_data {
14481508
const struct submodule *sub;
14491509
struct object_id oid;
@@ -2039,6 +2099,7 @@ static struct cmd_struct commands[] = {
20392099
{"list", module_list, 0},
20402100
{"name", module_name, 0},
20412101
{"clone", module_clone, 0},
2102+
{"update-module-mode", module_update_module_mode, 0},
20422103
{"update-clone", update_clone, 0},
20432104
{"relative-path", resolve_relative_path, 0},
20442105
{"resolve-relative-url", resolve_relative_url, 0},

git-submodule.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -535,27 +535,13 @@ cmd_update()
535535
do
536536
die_if_unmatched "$quickabort" "$sha1"
537537

538-
name=$(git submodule--helper name "$sm_path") || exit
539-
if ! test -z "$update"
540-
then
541-
update_module=$update
542-
else
543-
update_module=$(git config submodule."$name".update)
544-
if test -z "$update_module"
545-
then
546-
update_module="checkout"
547-
fi
548-
fi
538+
update_module=$(git submodule--helper update-module-mode $just_cloned "$sm_path" $update)
549539

550540
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
551541

552542
if test $just_cloned -eq 1
553543
then
554544
subsha1=
555-
case "$update_module" in
556-
merge | rebase | none)
557-
update_module=checkout ;;
558-
esac
559545
else
560546
subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
561547
git rev-parse --verify HEAD) ||

0 commit comments

Comments
 (0)