Skip to contentSkip to conversation

submodule config lookup API

PATCH In review Heiko Voigt 6 participants 16 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover submodule config lookup API

There have been no code changes in since the last iteration. I changed the title for the first patch since I realized that the cache is just an implementation detail and what we are really doing is to provide a new API for reading values from .gitmodules. I also added an extra paragraph in the commit message explaining that fact.

The last iteration can be found here:

http://article.gmane.org/gmane.comp.version-control.git/270545

There is no interdiff since no code changed.

Heiko Voigt (4):
implement submodule config API for lookup of .gitmodules values extract functions for submodule config set and lookup use new config API for worktree configurations of submodules do not die on error of parsing fetchrecursesubmodules option

 .gitignore                                       |   1 +
 Documentation/technical/api-submodule-config.txt |  63 +++
 Makefile                                         |   2 +
 builtin/checkout.c                               |   1 +
 builtin/fetch.c                                  |   1 +
 diff.c                                           |   1 +
 submodule-config.c                               | 484 +++++++++++++++++++++++
 submodule-config.h                               |  29 ++
 submodule.c                                      | 122 ++----
 submodule.h                                      |   4 +-
 t/t7411-submodule-config.sh                      | 153 +++++++
 test-submodule-config.c                          |  76 ++++
 12 files changed, 839 insertions(+), 98 deletions(-)
 create mode 100644 Documentation/technical/api-submodule-config.txt
 create mode 100644 submodule-config.c
 create mode 100644 submodule-config.h
 create mode 100755 t/t7411-submodule-config.sh
 create mode 100644 test-submodule-config.c
Signature · 1 line
2.4.2.391.g2979c89
#1 lore raw

PATCH v5 1/4 implement submodule 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.

---
 .gitignore                                       |   1 +
 Documentation/technical/api-submodule-config.txt |  46 +++
 Makefile                                         |   2 +
 submodule-config.c                               | 445 +++++++++++++++++++++++
 submodule-config.h                               |  27 ++
 submodule.c                                      |   1 +
 submodule.h                                      |   1 +
 t/t7411-submodule-config.sh                      |  85 +++++
 test-submodule-config.c                          |  66 ++++
 9 files changed, 674 insertions(+)
 create mode 100644 Documentation/technical/api-submodule-config.txt
 create mode 100644 submodule-config.c
 create mode 100644 submodule-config.h
 create mode 100755 t/t7411-submodule-config.sh
 create mode 100644 test-submodule-config.c
.gitignore +1 -0
index 422c538..337c121 100644
@@ -204,6 +204,7 @@
 /test-sha1-array
 /test-sigchain
 /test-string-list
+/test-submodule-config
 /test-subprocess
 /test-svn-fe
 /test-urlmatch-normalization
Documentation/technical/api-submodule-config.txt new file +46 -0

46 lines not on this page · raw message

Makefile +2 -0
index 54ec511..5d9a63f 100644
@@ -594,6 +594,7 @@ TEST_PROGRAMS_NEED_X += test-sha1
 TEST_PROGRAMS_NEED_X += test-sha1-array
 TEST_PROGRAMS_NEED_X += test-sigchain
 TEST_PROGRAMS_NEED_X += test-string-list
+TEST_PROGRAMS_NEED_X += test-submodule-config
 TEST_PROGRAMS_NEED_X += test-subprocess
 TEST_PROGRAMS_NEED_X += test-svn-fe
 TEST_PROGRAMS_NEED_X += test-urlmatch-normalization
@@ -784,6 +785,7 @@ LIB_OBJS += strbuf.o
 LIB_OBJS += streaming.o
 LIB_OBJS += string-list.o
 LIB_OBJS += submodule.o
+LIB_OBJS += submodule-config.o
 LIB_OBJS += symlinks.o
 LIB_OBJS += tag.o
 LIB_OBJS += trace.o
submodule-config.c new file +445 -0

445 lines not on this page · raw message

submodule-config.h new file +27 -0
new file mode 100644
index 0000000..cd68030
@@ -0,0 +1,27 @@
+#ifndef SUBMODULE_CONFIG_CACHE_H
+#define SUBMODULE_CONFIG_CACHE_H
+
+#include "hashmap.h"
+#include "strbuf.h"
+
+/*
+ * Submodule entry containing the information about a certain submodule
+ * in a certain revision.
+ */
+struct submodule {
+	const char *path;
+	const char *name;
+	const char *url;
+	int fetch_recurse;
+	const char *ignore;
+	/* the sha1 blob id of the responsible .gitmodules file */
+	unsigned char gitmodules_sha1[20];
+};
+
+const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
+		const char *name);
+const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
+		const char *path);
+void submodule_free(void);
+
+#endif /* SUBMODULE_CONFIG_H */
submodule.c +1 -0
index b8747f5..7822dc5 100644
@@ -355,6 +355,7 @@ int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 	default:
 		if (!strcmp(arg, "on-demand"))
 			return RECURSE_SUBMODULES_ON_DEMAND;
+		/* TODO: remove the die for history parsing here */
 		die("bad %s argument: %s", opt, arg);
 	}
 }
submodule.h +1 -0
index 7beec48..920fef3 100644
@@ -5,6 +5,7 @@ struct diff_options;
 struct argv_array;
 
 enum {
+	RECURSE_SUBMODULES_NONE = -2,
 	RECURSE_SUBMODULES_ON_DEMAND = -1,
 	RECURSE_SUBMODULES_OFF = 0,
 	RECURSE_SUBMODULES_DEFAULT = 1,
t/t7411-submodule-config.sh new file +85 -0

85 lines not on this page · raw message

test-submodule-config.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.4.2.391.g2979c89
#10 lore raw

PATCH v5 2/4 extract functions for submodule config set and lookup

In reply to Heiko Voigt

This is one step towards using the new configuration API. We just extract these functions to make replacing the actual code easier.

---
 submodule.c | 142 +++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 97 insertions(+), 45 deletions(-)
submodule.c +97 -45
index 7822dc5..c3b5f44 100644
@@ -41,6 +41,76 @@ static int gitmodules_is_unmerged;
  */
 static int gitmodules_is_modified;
 
+static const char *get_name_for_path(const char *path)
+{
+	struct string_list_item *path_option;
+	if (path == NULL) {
+		if (config_name_for_path.nr > 0)
+			return config_name_for_path.items[0].util;
+		else
+			return NULL;
+	}
+	path_option = unsorted_string_list_lookup(&config_name_for_path, path);
+	if (!path_option)
+		return NULL;
+	return path_option->util;
+}
+
+static void set_name_for_path(const char *path, const char *name, int namelen)
+{
+	struct string_list_item *config;
+	config = unsorted_string_list_lookup(&config_name_for_path, path);
+	if (config)
+		free(config->util);
+	else
+		config = string_list_append(&config_name_for_path, xstrdup(path));
+	config->util = xmemdupz(name, namelen);
+}
+
+static const char *get_ignore_for_name(const char *name)
+{
+	struct string_list_item *ignore_option;
+	ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name);
+	if (!ignore_option)
+		return NULL;
+
+	return ignore_option->util;
+}
+
+static void set_ignore_for_name(const char *name, int namelen, const char *ignore)
+{
+	struct string_list_item *config;
+	char *name_cstr = xmemdupz(name, namelen);
+	config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
+	if (config) {
+		free(config->util);
+		free(name_cstr);
+	} else
+		config = string_list_append(&config_ignore_for_name, name_cstr);
+	config->util = xstrdup(ignore);
+}
+
+static int get_fetch_recurse_for_name(const char *name)
+{
+	struct string_list_item *fetch_recurse;
+	fetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
+	if (!fetch_recurse)
+		return RECURSE_SUBMODULES_NONE;
+
+	return (intptr_t) fetch_recurse->util;
+}
+
+static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse)
+{
+	struct string_list_item *config;
+	char *name_cstr = xmemdupz(name, namelen);
+	config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
+	if (!config)
+		config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
+	else
+		free(name_cstr);
+	config->util = (void *)(intptr_t) fetch_recurse;
+}
 
 int is_staging_gitmodules_ok(void)
 {
@@ -55,7 +125,7 @@ int is_staging_gitmodules_ok(void)
 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 {
 	struct strbuf entry = STRBUF_INIT;
-	struct string_list_item *path_option;
+	const char *path;
 
 	if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
 		return -1;
@@ -63,13 +133,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 	if (gitmodules_is_unmerged)
 		die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
 
-	path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
-	if (!path_option) {
+	path = get_name_for_path(oldpath);
+	if (!path) {
 		warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
 		return -1;
 	}
 	strbuf_addstr(&entry, "submodule.");
-	strbuf_addstr(&entry, path_option->util);
+	strbuf_addstr(&entry, path);
 	strbuf_addstr(&entry, ".path");
 	if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
 		/* Maybe the user already did that, don't error out here */
@@ -89,7 +159,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 int remove_path_from_gitmodules(const char *path)
 {
 	struct strbuf sect = STRBUF_INIT;
-	struct string_list_item *path_option;
+	const char *path_option;
 
 	if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
 		return -1;
@@ -97,13 +167,13 @@ int remove_path_from_gitmodules(const char *path)
 	if (gitmodules_is_unmerged)
 		die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
 
-	path_option = unsorted_string_list_lookup(&config_name_for_path, path);
+	path_option = get_name_for_path(path);
 	if (!path_option) {
 		warning(_("Could not find section in .gitmodules where path=%s"), path);
 		return -1;
 	}
 	strbuf_addstr(&sect, "submodule.");
-	strbuf_addstr(&sect, path_option->util);
+	strbuf_addstr(&sect, path_option);
 	if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
 		/* Maybe the user already did that, don't error out here */
 		warning(_("Could not remove .gitmodules entry for %s"), path);
@@ -165,12 +235,11 @@ done:
 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
 					     const char *path)
 {
-	struct string_list_item *path_option, *ignore_option;
-	path_option = unsorted_string_list_lookup(&config_name_for_path, path);
-	if (path_option) {
-		ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
-		if (ignore_option)
-			handle_ignore_submodules_arg(diffopt, ignore_option->util);
+	const char *name = get_name_for_path(path);
+	if (name) {
+		const char *ignore = get_ignore_for_name(name);
+		if (ignore)
+			handle_ignore_submodules_arg(diffopt, ignore);
 		else if (gitmodules_is_unmerged)
 			DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
 	}
@@ -221,7 +290,6 @@ void gitmodules_config(void)
 
 int parse_submodule_config_option(const char *var, const char *value)
 {
-	struct string_list_item *config;
 	const char *name, *key;
 	int namelen;
 
@@ -232,22 +300,14 @@ int parse_submodule_config_option(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 
-		config = unsorted_string_list_lookup(&config_name_for_path, value);
-		if (config)
-			free(config->util);
-		else
-			config = string_list_append(&config_name_for_path, xstrdup(value));
-		config->util = xmemdupz(name, namelen);
+		set_name_for_path(value, name, namelen);
+
 	} else if (!strcmp(key, "fetchrecursesubmodules")) {
-		char *name_cstr = xmemdupz(name, namelen);
-		config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
-		if (!config)
-			config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
-		else
-			free(name_cstr);
-		config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
+		int fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);
+
+		set_fetch_recurse_for_name(name, namelen, fetch_recurse);
+
 	} else if (!strcmp(key, "ignore")) {
-		char *name_cstr;
 
 		if (!value)
 			return config_error_nonbool(var);
@@ -258,14 +318,7 @@ int parse_submodule_config_option(const char *var, const char *value)
 			return 0;
 		}
 
-		name_cstr = xmemdupz(name, namelen);
-		config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
-		if (config) {
-			free(config->util);
-			free(name_cstr);
-		} else
-			config = string_list_append(&config_ignore_for_name, name_cstr);
-		config->util = xstrdup(value);
+		set_ignore_for_name(name, namelen, value);
 		return 0;
 	}
 	return 0;
@@ -646,7 +699,7 @@ static void calculate_changed_submodule_paths(void)
 	struct argv_array argv = ARGV_ARRAY_INIT;
 
 	/* No need to check if there are no submodules configured */
-	if (!config_name_for_path.nr)
+	if (!get_name_for_path(NULL))
 		return;
 
 	init_revisions(&rev, NULL);
@@ -693,7 +746,7 @@ int fetch_populated_submodules(const struct argv_array *options,
 	int i, result = 0;
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct argv_array argv = ARGV_ARRAY_INIT;
-	struct string_list_item *name_for_path;
+	const char *name_for_path;
 	const char *work_tree = get_git_work_tree();
 	if (!work_tree)
 		goto out;
@@ -724,18 +777,17 @@ int fetch_populated_submodules(const struct argv_array *options,
 			continue;
 
 		name = ce->name;
-		name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
+		name_for_path = get_name_for_path(ce->name);
 		if (name_for_path)
-			name = name_for_path->util;
+			name = name_for_path;
 
 		default_argv = "yes";
 		if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
-			struct string_list_item *fetch_recurse_submodules_option;
-			fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
-			if (fetch_recurse_submodules_option) {
-				if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
+			int fetch_recurse_option = get_fetch_recurse_for_name(name);
+			if (fetch_recurse_option != RECURSE_SUBMODULES_NONE) {
+				if (fetch_recurse_option == RECURSE_SUBMODULES_OFF)
 					continue;
-				if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
+				if (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) {
 					if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 						continue;
 					default_argv = "on-demand";
Signature · 1 line
2.4.2.391.g2979c89
#11 lore raw

PATCH v5 3/4 use new config API for worktree configurations of submodules

In reply to Heiko Voigt

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.

---
 Documentation/technical/api-submodule-config.txt |  19 ++-
 builtin/checkout.c                               |   1 +
 diff.c                                           |   1 +
 submodule-config.c                               |  12 ++
 submodule-config.h                               |   1 +
 submodule.c                                      | 160 ++++-------------------
 submodule.h                                      |   1 -
 t/t7411-submodule-config.sh                      |  37 +++++-
 test-submodule-config.c                          |  10 ++
 9 files changed, 104 insertions(+), 138 deletions(-)
Documentation/technical/api-submodule-config.txt +18 -1
index 2ff4907..2ea520a 100644
@@ -10,10 +10,18 @@ submodule path or name.
 Usage
 -----
 
+To initialize the cache with configurations from the worktree the caller
+typically first calls `gitmodules_config()` to read values from the
+worktree .gitmodules and then to overlay the local git config values
+`parse_submodule_config_option()` from the config parsing
+infrastructure.
+
 The caller can look up information about submodules by using the
 `submodule_from_path()` or `submodule_from_name()` functions. They return
 a `struct submodule` which contains the values. The API automatically
-initializes and allocates the needed infrastructure on-demand.
+initializes and allocates the needed infrastructure on-demand. If the
+caller does only want to lookup values from revisions the initialization
+can be skipped.
 
 If the internal cache might grow too big or when the caller is done with
 the API, all internally cached values can be freed with submodule_free().
@@ -34,6 +42,11 @@ Functions
 
 	Use these to free the internally cached values.
 
+`int parse_submodule_config_option(const char *var, const char *value)`::
+
+	Can be passed to the config parsing infrastructure to parse
+	local (worktree) submodule configurations.
+
 `const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`::
 
 	Lookup values for one submodule by its commit_sha1 and path or
@@ -43,4 +56,8 @@ Functions
 
 	The same as above but lookup by name.
 
+If given the null_sha1 as commit_sha1 the local configuration of a
+submodule will be returned (e.g. consolidated values from local git
+configuration and the .gitmodules file in the worktree).
+
 For an example usage see test-submodule-config.c.
builtin/checkout.c +1 -0
index 2f92328..f1f168d 100644
@@ -18,6 +18,7 @@
 #include "xdiff-interface.h"
 #include "ll-merge.h"
 #include "resolve-undo.h"
+#include "submodule-config.h"
 #include "submodule.h"
 #include "argv-array.h"
 #include "sigchain.h"
diff.c +1 -0
index 7500c55..d0be279 100644
@@ -13,6 +13,7 @@
 #include "utf8.h"
 #include "userdiff.h"
 #include "sigchain.h"
+#include "submodule-config.h"
 #include "submodule.h"
 #include "ll-merge.h"
 #include "string-list.h"
submodule-config.c +12 -0
index 97f4a04..fc7bf40 100644
@@ -424,6 +424,18 @@ static void ensure_cache_init(void)
 	is_cache_init = 1;
 }
 
+int parse_submodule_config_option(const char *var, const char *value)
+{
+	struct parse_config_parameter parameter;
+	parameter.cache = &cache;
+	parameter.commit_sha1 = NULL;
+	parameter.gitmodules_sha1 = null_sha1;
+	parameter.overwrite = 1;
+
+	ensure_cache_init();
+	return parse_config(var, value, &parameter);
+}
+
 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
 		const char *name)
 {
submodule-config.h +1 -0
index cd68030..5fe44ce 100644
@@ -18,6 +18,7 @@ struct submodule {
 	unsigned char gitmodules_sha1[20];
 };
 
+int parse_submodule_config_option(const char *var, const char *value);
 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
 		const char *name);
 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
submodule.c +26 -134

246 lines not on this page · raw message

submodule.h +0 -1
index 920fef3..547219d 100644
@@ -20,7 +20,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
 		const char *path);
 int submodule_config(const char *var, const char *value, void *cb);
 void gitmodules_config(void);
-int parse_submodule_config_option(const char *var, const char *value);
 void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 void show_submodule_summary(FILE *f, const char *path,
t/t7411-submodule-config.sh +35 -2
index 2602bc5..7229978 100755
@@ -5,8 +5,8 @@
 
 test_description='Test submodules config cache infrastructure
 
-This test verifies that parsing .gitmodules configuration directly
-from the database works.
+This test verifies that parsing .gitmodules configurations directly
+from the database and from the worktree works.
 '
 
 TEST_NO_CREATE_REPO=1
@@ -82,4 +82,37 @@ test_expect_success 'error in one submodule config lets continue' '
 	)
 '
 
+cat >super/expect_url <<EOF
+Submodule url: '[email protected]:a.git' for path 'b'
+Submodule url: '[email protected]:submodule.git' for path 'submodule'
+EOF
+
+cat >super/expect_local_path <<EOF
+Submodule name: 'a' for path 'c'
+Submodule name: 'submodule' for path 'submodule'
+EOF
+
+test_expect_success 'reading of local configuration' '
+	(cd super &&
+		old_a=$(git config submodule.a.url) &&
+		old_submodule=$(git config submodule.submodule.url) &&
+		git config submodule.a.url [email protected]:a.git &&
+		git config submodule.submodule.url [email protected]:submodule.git &&
+		test-submodule-config --url \
+			"" b \
+			"" submodule \
+				>actual &&
+		test_cmp expect_url actual &&
+		git config submodule.a.path c &&
+		test-submodule-config \
+			"" c \
+			"" submodule \
+				>actual &&
+		test_cmp expect_local_path actual &&
+		git config submodule.a.url $old_a &&
+		git config submodule.submodule.url $old_submodule &&
+		git config --unset submodule.a.path c
+	)
+'
+
 test_done
test-submodule-config.c +10 -0
index f3c3918..dab8c27 100644
@@ -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;
Signature · 1 line
2.4.2.391.g2979c89
#12 lore raw

PATCH v5 4/4 do not die on error of parsing fetchrecursesubmodules option

In reply to Heiko Voigt

We should not die when reading the submodule config cache since the user might not be able to get out of that situation when the configuration is part of the history.

We should handle this condition later when the value is about to be used.

---
 builtin/fetch.c             |  1 +
 submodule-config.c          | 29 ++++++++++++++++++++++++++++-
 submodule-config.h          |  1 +
 submodule.c                 | 15 ---------------
 submodule.h                 |  2 +-
 t/t7411-submodule-config.sh | 35 +++++++++++++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 17 deletions(-)
builtin/fetch.c +1 -0
index 7910419..faae548 100644
@@ -11,6 +11,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "sigchain.h"
+#include "submodule-config.h"
 #include "submodule.h"
 #include "connected.h"
 #include "argv-array.h"
submodule-config.c +28 -1
index fc7bf40..199692b 100644
@@ -204,6 +204,30 @@ static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
 	return submodule;
 }
 
+static int parse_fetch_recurse(const char *opt, const char *arg,
+			       int die_on_error)
+{
+	switch (git_config_maybe_bool(opt, arg)) {
+	case 1:
+		return RECURSE_SUBMODULES_ON;
+	case 0:
+		return RECURSE_SUBMODULES_OFF;
+	default:
+		if (!strcmp(arg, "on-demand"))
+			return RECURSE_SUBMODULES_ON_DEMAND;
+
+		if (die_on_error)
+			die("bad %s argument: %s", opt, arg);
+		else
+			return RECURSE_SUBMODULES_ERROR;
+	}
+}
+
+int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
+{
+	return parse_fetch_recurse(opt, arg, 1);
+}
+
 static void warn_multiple_config(const unsigned char *commit_sha1,
 				 const char *name, const char *option)
 {
@@ -255,6 +279,8 @@ static int parse_config(const char *var, const char *value, void *data)
 		submodule->path = strbuf_detach(&path, NULL);
 		cache_put_path(me->cache, submodule);
 	} else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
+		/* when parsing worktree configurations we can die early */
+		int die_on_error = is_null_sha1(me->gitmodules_sha1);
 		if (!me->overwrite &&
 		    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) {
 			warn_multiple_config(me->commit_sha1, submodule->name,
@@ -262,7 +288,8 @@ static int parse_config(const char *var, const char *value, void *data)
 			goto release_return;
 		}
 
-		submodule->fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);
+		submodule->fetch_recurse = parse_fetch_recurse(var, value,
+								die_on_error);
 	} else if (!strcmp(item.buf, "ignore")) {
 		struct strbuf ignore = STRBUF_INIT;
 		if (!me->overwrite && submodule->ignore != NULL) {
submodule-config.h +1 -0
index 5fe44ce..9061e4e 100644
@@ -18,6 +18,7 @@ struct submodule {
 	unsigned char gitmodules_sha1[20];
 };
 
+int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 int parse_submodule_config_option(const char *var, const char *value);
 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
 		const char *name);
submodule.c +0 -15
index 97355eb..4822559 100644
@@ -288,21 +288,6 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,
 	strbuf_release(&sb);
 }
 
-int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
-{
-	switch (git_config_maybe_bool(opt, arg)) {
-	case 1:
-		return RECURSE_SUBMODULES_ON;
-	case 0:
-		return RECURSE_SUBMODULES_OFF;
-	default:
-		if (!strcmp(arg, "on-demand"))
-			return RECURSE_SUBMODULES_ON_DEMAND;
-		/* TODO: remove the die for history parsing here */
-		die("bad %s argument: %s", opt, arg);
-	}
-}
-
 void show_submodule_summary(FILE *f, const char *path,
 		const char *line_prefix,
 		unsigned char one[20], unsigned char two[20],
submodule.h +1 -1
index 547219d..5507c3d 100644
@@ -5,6 +5,7 @@ struct diff_options;
 struct argv_array;
 
 enum {
+	RECURSE_SUBMODULES_ERROR = -3,
 	RECURSE_SUBMODULES_NONE = -2,
 	RECURSE_SUBMODULES_ON_DEMAND = -1,
 	RECURSE_SUBMODULES_OFF = 0,
@@ -21,7 +22,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
 int submodule_config(const char *var, const char *value, void *cb);
 void gitmodules_config(void);
 void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);
-int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 void show_submodule_summary(FILE *f, const char *path,
 		const char *line_prefix,
 		unsigned char one[20], unsigned char two[20],
t/t7411-submodule-config.sh +35 -0
index 7229978..fc97c33 100755
@@ -115,4 +115,39 @@ test_expect_success 'reading of local configuration' '
 	)
 '
 
+cat >super/expect_fetchrecurse_die.err <<EOF
+fatal: bad submodule.submodule.fetchrecursesubmodules argument: blabla
+EOF
+
+test_expect_success 'local error in fetchrecursesubmodule dies early' '
+	(cd super &&
+		git config submodule.submodule.fetchrecursesubmodules blabla &&
+		test_must_fail test-submodule-config \
+			"" b \
+			"" submodule \
+				>actual.out 2>actual.err &&
+		touch expect_fetchrecurse_die.out &&
+		test_cmp expect_fetchrecurse_die.out actual.out  &&
+		test_cmp expect_fetchrecurse_die.err actual.err  &&
+		git config --unset submodule.submodule.fetchrecursesubmodules
+	)
+'
+
+test_expect_success 'error in history in fetchrecursesubmodule lets continue' '
+	(cd super &&
+		git config -f .gitmodules \
+			submodule.submodule.fetchrecursesubmodules blabla &&
+		git add .gitmodules &&
+		git config --unset -f .gitmodules \
+			submodule.submodule.fetchrecursesubmodules &&
+		git commit -m "add error in fetchrecursesubmodules" &&
+		test-submodule-config \
+			HEAD b \
+			HEAD submodule \
+				>actual &&
+		test_cmp expect_error actual  &&
+		git reset --hard HEAD^
+	)
+'
+
 test_done
Signature · 1 line
2.4.2.391.g2979c89
#2 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

In reply to Heiko Voigt
Quoted 7 lines · Heiko Voigt

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

I just realized that we are talking too much about .gitmodules here, where it probably should be "submodule configuration values". For revisions we only read from .gitmodules files but for the worktree we actually overlay those with local configurations from .git/config and friends. Not sure how we can name this though. "submodule configuration values" is kind of long compared to .gitmodules.

Does anyone have a better name? Or is it maybe to confusing, to abstract it too much and we should just keep it .gitmodules, since everyone knows that those values can be overridden by local configuration?

Cheers Heiko

#3 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

In reply to Heiko Voigt
Quoted 39 lines · Heiko Voigt

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.

Quoted 16 lines
---
 .gitignore                                       |   1 +
 Documentation/technical/api-submodule-config.txt |  46 +++
 Makefile                                         |   2 +
 submodule-config.c                               | 445 +++++++++++++++++++++++
 submodule-config.h                               |  27 ++
 submodule.c                                      |   1 +
 submodule.h                                      |   1 +
 t/t7411-submodule-config.sh                      |  85 +++++
 test-submodule-config.c                          |  66 ++++
 9 files changed, 674 insertions(+)
 create mode 100644 Documentation/technical/api-submodule-config.txt
 create mode 100644 submodule-config.c
 create mode 100644 submodule-config.h
 create mode 100755 t/t7411-submodule-config.sh
 create mode 100644 test-submodule-config.c

Instead of test-submodule-config.c to test this new module, it could be useful to implement these as extensions to rev-parse:

    git rev-parse --submodule-name [<ref>:]<path>
    git rev-parse --submodule-path [<ref>:]<name>
    git rev-parse --submodule-url [<ref>:]<name>
    git rev-parse --submodule-ignore [<ref>:]<name>
    git rev-parse --submodule-recurse [<ref>:]<name>

Has this already been considered and rejected for some reason?

#4 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Quoted 61 lines · Phil Hord
Quoted 39 lines · Heiko Voigt

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.

Quoted 16 lines
---
 .gitignore                                       |   1 +
 Documentation/technical/api-submodule-config.txt |  46 +++
 Makefile                                         |   2 +
 submodule-config.c                               | 445 +++++++++++++++++++++++
 submodule-config.h                               |  27 ++
 submodule.c                                      |   1 +
 submodule.h                                      |   1 +
 t/t7411-submodule-config.sh                      |  85 +++++
 test-submodule-config.c                          |  66 ++++
 9 files changed, 674 insertions(+)
 create mode 100644 Documentation/technical/api-submodule-config.txt
 create mode 100644 submodule-config.c
 create mode 100644 submodule-config.h
 create mode 100755 t/t7411-submodule-config.sh
 create mode 100644 test-submodule-config.c

Instead of test-submodule-config.c to test this new module, it could be useful to implement these as extensions to rev-parse:

Quoted 7 lines
    git rev-parse --submodule-name [<ref>:]<path>
    git rev-parse --submodule-path [<ref>:]<name>
    git rev-parse --submodule-url [<ref>:]<name>
    git rev-parse --submodule-ignore [<ref>:]<name>
    git rev-parse --submodule-recurse [<ref>:]<name>

Has this already been considered and rejected for some reason?

No that has not been considered. But I am open to it if others agree that this is a sensible thing to do. We should be able to adapt the existing tests right?

Cheers Heiko

#5 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Quoted 9 lines · Heiko Voigt
Quoted 9 lines

Instead of test-submodule-config.c to test this new module, it could be useful to implement these as extensions to rev-parse:

    git rev-parse --submodule-name [<ref>:]<path>
    git rev-parse --submodule-path [<ref>:]<name>
    git rev-parse --submodule-url [<ref>:]<name>
    git rev-parse --submodule-ignore [<ref>:]<name>
    git rev-parse --submodule-recurse [<ref>:]<name>
Quoted 5 lines
Quoted 1 line

Has this already been considered and rejected for some reason?

No that has not been considered. But I am open to it if others agree that this is a sensible thing to do. We should be able to adapt the existing tests right?

How does git-submodule access this information? It looks like it just hits "git config -f .gitmodules" directly. Perhaps whatever interface is designed should be suitable for its use here (and if there really is no more interesting interface needed, then why is "git config" not good enough for other callers?).

Just my two cents as an observer who does not really work on submodules.

Also, I'm not excited to see more options go into the kitchen-sink of rev-parse, but I cannot think of a better place (I would have said "git submodule config" or something, but that is a chicken-and-egg with the suggestion I made above :) ).

-Peff

#6 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Am 09.07.2015 um 17:49 schrieb Jeff King:

Quoted 17 lines · Heiko Voigt
Quoted 10 lines
Quoted 10 lines

Instead of test-submodule-config.c to test this new module, it could be useful to implement these as extensions to rev-parse:

     git rev-parse --submodule-name [<ref>:]<path>
     git rev-parse --submodule-path [<ref>:]<name>
     git rev-parse --submodule-url [<ref>:]<name>
     git rev-parse --submodule-ignore [<ref>:]<name>
     git rev-parse --submodule-recurse [<ref>:]<name>
Quoted 5 lines
Quoted 1 line

Has this already been considered and rejected for some reason?

No that has not been considered. But I am open to it if others agree that this is a sensible thing to do. We should be able to adapt the existing tests right?

Quoted 5 lines

How does git-submodule access this information? It looks like it just hits "git config -f .gitmodules" directly. Perhaps whatever interface is designed should be suitable for its use here (and if there really is no more interesting interface needed, then why is "git config" not good enough for other callers?).

The git-submodule script doesn't need this and is fine using plain old "git config", as by the time it is run the .gitmodules file is already updated in the work tree. Heiko's series is about adding infrastructure to allow builtins like checkout and friends to access the configuration values from the .gitmodules file of the to-be-checked-out commit when run with "--recurse-submodules". And yes, if we want to expose this functionality to users or scripts some day "git config" looks like the best place to do that to me too.

#7 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Quoted 6 lines · Jens Lehmann
Quoted 5 lines

How does git-submodule access this information? It looks like it just hits "git config -f .gitmodules" directly. Perhaps whatever interface is designed should be suitable for its use here (and if there really is no more interesting interface needed, then why is "git config" not good enough for other callers?).

Quoted 8 lines

The git-submodule script doesn't need this and is fine using plain old "git config", as by the time it is run the .gitmodules file is already updated in the work tree. Heiko's series is about adding infrastructure to allow builtins like checkout and friends to access the configuration values from the .gitmodules file of the to-be-checked-out commit when run with "--recurse-submodules". And yes, if we want to expose this functionality to users or scripts some day "git config" looks like the best place to do that to me too.

Did you mean "git submodule config"?

#8 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Quoted 17 lines · Junio C Hamano
Quoted 6 lines · Jens Lehmann
Quoted 5 lines

How does git-submodule access this information? It looks like it just hits "git config -f .gitmodules" directly. Perhaps whatever interface is designed should be suitable for its use here (and if there really is no more interesting interface needed, then why is "git config" not good enough for other callers?).

Quoted 8 lines

The git-submodule script doesn't need this and is fine using plain old "git config", as by the time it is run the .gitmodules file is already updated in the work tree. Heiko's series is about adding infrastructure to allow builtins like checkout and friends to access the configuration values from the .gitmodules file of the to-be-checked-out commit when run with "--recurse-submodules". And yes, if we want to expose this functionality to users or scripts some day "git config" looks like the best place to do that to me too.

Quoted 1 line

Did you mean "git submodule config"?

I think he actually meant "git config" and that is already implemented. When I implemented the infrastructure to read configurations from blobs, Peff extended it so it will be exposed via the config command line. E.g. you can do:

	git config --blob HEAD^^^:.gitmodules <value>

to get .gitmodules configurations from the history, so that is already implemented. And for reading .gitmodules values we probably do not need more, since calling git from scripting we always have new invocations of processes anyway and that would throw away the cache I am implementing. Reading such values via config from scripts is also more flexible since it supports arbitrary values and my cache only specific values needed by the builtins that use it.

My submodule config cache infrastructure is directed for C-code wanting to query submodule values. So e.g. when "git checkout" wants to know about values but the ".gitmodules" file that is in charge, but has not been checked out yet. We also need this for fetch which will actually need values from more than one revision, since we might need to merge configurations when fetching multiple branches. Fetch also needs information about URLs for new submodules that appear in branches, when auto clone is switched on. That means to support the "I want to go on an airplane get me everything I might need" use-case.

Cheers Heiko

#9 lore raw

Re: [PATCH v5 1/4] implement submodule config API for lookup of .gitmodules values

Quoted 19 lines · Heiko Voigt
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Jens Lehmann

The git-submodule script doesn't need this and is fine using plain old "git config", as by the time it is run the .gitmodules file is already updated in the work tree. Heiko's series is about adding infrastructure to allow builtins like checkout and friends to access the configuration values from the .gitmodules file of the to-be-checked-out commit when run with "--recurse-submodules". And yes, if we want to expose this functionality to users or scripts some day "git config" looks like the best place to do that to me too.

Quoted 1 line

Did you mean "git submodule config"?

I think he actually meant "git config" and that is already implemented. When I implemented the infrastructure to read configurations from blobs, Peff extended it so it will be exposed via the config command line. E.g. you can do:

Quoted 5 lines
	git config --blob HEAD^^^:.gitmodules <value>

to get .gitmodules configurations from the history, so that is already implemented. And for reading .gitmodules values we probably do not need more,...

Ah, I see. Thanks.

#14 lore raw
In reply to Junio C Hamano
Quoted 1 line · Junio C Hamano

Thanks. Will replace and wait for comments from others.

I have reviewed the patches carefully and they look good to me.

As Git is a large project and I was active in other parts until now, I noticed that there are subtle differences in style as when compared to the refs code. One example would be the way comments are written. In d378e35d256348f (Patch 1, implement submodule config API for lookup of .gitmodules values) the comments for the data structures in submodule-config.c seem to have a non exposed "headline" and if more is needed proper sentences with capitalized starts and punctuation at the end. In the refs code there are only sentences IIRC. Most of the commits touching submodule.{c,h} do not prefix their commit message with "submodule:"

The style is no show stopper of course, just an observation from someone moving into a different area of code.

Thanks,
Stefan

#15 lore raw
Quoted 4 lines · Stefan Beller
Quoted 1 line · Junio C Hamano

Thanks. Will replace and wait for comments from others.

I have reviewed the patches carefully and they look good to me.

OK, I recall there were a few iterations with review comments before this round. Is it your impression that they have been addressed adequately?

Do you prefer it to be rebased to a more recent 'master' before you build your work on top of it (I think the topic currently builds on top of v2.5.0-rc0~56)?

Thanks.