submodule: use parse_config_key when parsing config

This makes the code a lot simpler to read by dropping a whole bunch of constant offsets. As a bonus, it means we also feed the whole config variable name to our error functions: [before] $ git -c submodule.foo.fetchrecursesubmodules=bogus checkout fatal: bad foo.fetchrecursesubmodules argument: bogus [after] $ git -c submodule.foo.fetchrecursesubmodules=bogus checkout fatal: bad submodule.foo.fetchrecursesubmodules argument: bogus Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 23, 2013 at 01:25 UTC 9edbb8b1c1fdb199b47650f50fc432b1bfcb9039
1 file changed +10 -9
submodule.c
+10 -9
@@ -126,15 +126,16 @@ void gitmodules_config(void)
126
127 int parse_submodule_config_option(const char *var, const char *value)
128 {
129 - int len;
129 struct string_list_item *config;
130 struct strbuf submodname = STRBUF_INIT;
131 + const char *name, *key;
132 + int namelen;
133
133 - var += 10; /* Skip "submodule." */
134 + if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
135 + return 0;
136
135 - len = strlen(var);
136 - if ((len > 5) && !strcmp(var + len - 5, ".path")) {
137 - strbuf_add(&submodname, var, len - 5);
137 + if (!strcmp(key, "path")) {
138 + strbuf_add(&submodname, name, namelen);
139 config = unsorted_string_list_lookup(&config_name_for_path, value);
140 if (config)
141 free(config->util);
@@ -142,22 +143,22 @@ int parse_submodule_config_option(const char *var, const char *value)
143 config = string_list_append(&config_name_for_path, xstrdup(value));
144 config->util = strbuf_detach(&submodname, NULL);
145 strbuf_release(&submodname);
145 - } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
146 - strbuf_add(&submodname, var, len - 23);
146 + } else if (!strcmp(key, "fetchrecursesubmodules")) {
147 + strbuf_add(&submodname, name, namelen);
148 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
149 if (!config)
150 config = string_list_append(&config_fetch_recurse_submodules_for_name,
151 strbuf_detach(&submodname, NULL));
152 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
153 strbuf_release(&submodname);
153 - } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
154 + } else if (!strcmp(key, "ignore")) {
155 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
156 strcmp(value, "all") && strcmp(value, "none")) {
157 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
158 return 0;
159 }
160
160 - strbuf_add(&submodname, var, len - 7);
161 + strbuf_add(&submodname, name, namelen);
162 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
163 if (config)
164 free(config->util);