From 959b5455d07c7724eea9d323ca8ce6bb6ddde40e Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:21:57 -0700 Subject: submodule: implement a config API for lookup of .gitmodules values In a superproject some commands need to interact with submodules. They need to query values from the .gitmodules file either from the worktree of from certain revisions. At the moment this is quite hard since a caller would need to read the .gitmodules file from the history and then parse the values. We want to provide an API for this so we have one place to get values from .gitmodules from any revision (including the worktree). The API is realized as a cache which allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed. It is expected that .gitmodules files do not change often between commits. Thats why we lookup the .gitmodules sha1 from a commit and then either lookup an already parsed configuration or parse and cache an unknown one for each sha1. The cache is lazily build on demand for each requested commit. This cache can be used for all purposes which need knowledge about submodule configurations. Example use cases are: * Recursive submodule checkout needs to lookup a submodule name from its path when a submodule first appears. This needs be done before this configuration exists in the worktree. * The implementation of submodule support for 'git archive' needs to lookup the submodule name to generate the archive when given a revision that is not checked out. * 'git fetch' when given the --recurse-submodules=on-demand option (or configuration) needs to lookup submodule names by path from the database rather than reading from the worktree. For new submodule it needs to lookup the name from its path to allow cloning new submodules into the .git folder so they can be checked out without any network interaction when the user does a checkout of that revision. Signed-off-by: Heiko Voigt Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- test-submodule-config.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test-submodule-config.c (limited to 'test-submodule-config.c') diff --git a/test-submodule-config.c b/test-submodule-config.c new file mode 100644 index 0000000000..f3c391860d --- /dev/null +++ b/test-submodule-config.c @@ -0,0 +1,66 @@ +#include "cache.h" +#include "submodule-config.h" + +static void die_usage(int argc, char **argv, const char *msg) +{ + fprintf(stderr, "%s\n", msg); + fprintf(stderr, "Usage: %s [ ] ...\n", argv[0]); + exit(1); +} + +int main(int argc, char **argv) +{ + char **arg = argv; + int my_argc = argc; + int output_url = 0; + int lookup_name = 0; + + arg++; + my_argc--; + while (starts_with(arg[0], "--")) { + if (!strcmp(arg[0], "--url")) + output_url = 1; + if (!strcmp(arg[0], "--name")) + lookup_name = 1; + arg++; + my_argc--; + } + + if (my_argc % 2 != 0) + die_usage(argc, argv, "Wrong number of arguments."); + + while (*arg) { + unsigned char commit_sha1[20]; + const struct submodule *submodule; + const char *commit; + const char *path_or_name; + + commit = arg[0]; + path_or_name = arg[1]; + + if (commit[0] == '\0') + hashcpy(commit_sha1, null_sha1); + else if (get_sha1(commit, commit_sha1) < 0) + die_usage(argc, argv, "Commit not found."); + + if (lookup_name) { + submodule = submodule_from_name(commit_sha1, path_or_name); + } else + submodule = submodule_from_path(commit_sha1, path_or_name); + if (!submodule) + die_usage(argc, argv, "Submodule not found."); + + if (output_url) + printf("Submodule url: '%s' for path '%s'\n", + submodule->url, submodule->path); + else + printf("Submodule name: '%s' for path '%s'\n", + submodule->name, submodule->path); + + arg += 2; + } + + submodule_free(); + + return 0; +} -- cgit v1.2.3 From 851e18c3859ad0f9f7e91fdb4d6cce5a8272420b Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:21:59 -0700 Subject: submodule: use new config API for worktree configurations We remove the extracted functions and directly parse into and read out of the cache. This allows us to have one unified way of accessing submodule configuration values specific to single submodules. Regardless whether we need to access a configuration from history or from the worktree. Signed-off-by: Heiko Voigt Signed-off-by: Junio C Hamano Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- test-submodule-config.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test-submodule-config.c') diff --git a/test-submodule-config.c b/test-submodule-config.c index f3c391860d..dab8c27768 100644 --- a/test-submodule-config.c +++ b/test-submodule-config.c @@ -1,5 +1,6 @@ #include "cache.h" #include "submodule-config.h" +#include "submodule.h" static void die_usage(int argc, char **argv, const char *msg) { @@ -8,6 +9,11 @@ static void die_usage(int argc, char **argv, const char *msg) exit(1); } +static int git_test_config(const char *var, const char *value, void *cb) +{ + return parse_submodule_config_option(var, value); +} + int main(int argc, char **argv) { char **arg = argv; @@ -29,6 +35,10 @@ int main(int argc, char **argv) if (my_argc % 2 != 0) die_usage(argc, argv, "Wrong number of arguments."); + setup_git_directory(); + gitmodules_config(); + git_config(git_test_config, NULL); + while (*arg) { unsigned char commit_sha1[20]; const struct submodule *submodule; -- cgit v1.2.3