Re: [PATCHv2 3/8] submodule config: remove name_and_item_from_var

2015-10-29 Thread Eric Sunshine
On Wed, Oct 28, 2015 at 7:21 PM, Stefan Beller  wrote:
> submodule config: remove name_and_item_from_var
>
> By inlining `name_and_item_from_var` it is easy to add later options
> which are not required to have a submodule name.

I guess you're trying to say that name_and_item_from_var() didn't
provide a proper abstraction, thus wasn't as useful as expected.
Perhaps that commit message could make this shortcoming clearer.

> Signed-off-by: Stefan Beller 
> ---
> diff --git a/submodule-config.c b/submodule-config.c
> index 8b8c7d1..4d0563c 100644
> --- a/submodule-config.c
> +++ b/submodule-config.c
> @@ -251,18 +235,25 @@ static int parse_config(const char *var, const char 
> *value, void *data)
>  {
> struct parse_config_parameter *me = data;
> struct submodule *submodule;
> -   struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
> -   int ret = 0;
> +   int subsection_len, ret = 0;
> +   const char *subsection, *key;
> +   char *name;
>
> -   /* this also ensures that we only parse submodule entries */
> -   if (!name_and_item_from_var(var, , ))
> +   if (parse_config_key(var, "submodule", ,
> +_len, ) < 0)
> return 0;
>
> +   if (!subsection_len)
> +   return 0;

Alternately:

if (parse_config_key(var, "submodule", ,
_len, ) < 0 || !subsection_len)
return 0;

> +
> +   /* subsection is not null terminated */
> +   name = xmemdupz(subsection, subsection_len);
> submodule = lookup_or_create_by_name(me->cache,
>  me->gitmodules_sha1,
> -name.buf);
> +name);
> +   free(name);

Since this is all private to submodule-config.c, I wonder if it would
be cleaner to change lookup_or_create_by_name() to accept a
name_length argument?

> -   if (!strcmp(item.buf, "path")) {
> +   if (!strcmp(key, "path")) {
> if (!value)
> ret = config_error_nonbool(var);
> else if (!me->overwrite && submodule->path != NULL)
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCHv2 3/8] submodule config: remove name_and_item_from_var

2015-10-28 Thread Stefan Beller
By inlining `name_and_item_from_var` it is easy to add later options
which are not required to have a submodule name.

Signed-off-by: Stefan Beller 
---
 submodule-config.c | 46 +-
 1 file changed, 17 insertions(+), 29 deletions(-)

diff --git a/submodule-config.c b/submodule-config.c
index 8b8c7d1..4d0563c 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -161,22 +161,6 @@ static struct submodule *cache_lookup_name(struct 
submodule_cache *cache,
return NULL;
 }
 
-static int name_and_item_from_var(const char *var, struct strbuf *name,
- struct strbuf *item)
-{
-   const char *subsection, *key;
-   int subsection_len, parse;
-   parse = parse_config_key(var, "submodule", ,
-   _len, );
-   if (parse < 0 || !subsection)
-   return 0;
-
-   strbuf_add(name, subsection, subsection_len);
-   strbuf_addstr(item, key);
-
-   return 1;
-}
-
 static struct submodule *lookup_or_create_by_name(struct submodule_cache 
*cache,
const unsigned char *gitmodules_sha1, const char *name)
 {
@@ -251,18 +235,25 @@ static int parse_config(const char *var, const char 
*value, void *data)
 {
struct parse_config_parameter *me = data;
struct submodule *submodule;
-   struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
-   int ret = 0;
+   int subsection_len, ret = 0;
+   const char *subsection, *key;
+   char *name;
 
-   /* this also ensures that we only parse submodule entries */
-   if (!name_and_item_from_var(var, , ))
+   if (parse_config_key(var, "submodule", ,
+_len, ) < 0)
return 0;
 
+   if (!subsection_len)
+   return 0;
+
+   /* subsection is not null terminated */
+   name = xmemdupz(subsection, subsection_len);
submodule = lookup_or_create_by_name(me->cache,
 me->gitmodules_sha1,
-name.buf);
+name);
+   free(name);
 
-   if (!strcmp(item.buf, "path")) {
+   if (!strcmp(key, "path")) {
if (!value)
ret = config_error_nonbool(var);
else if (!me->overwrite && submodule->path != NULL)
@@ -275,7 +266,7 @@ static int parse_config(const char *var, const char *value, 
void *data)
submodule->path = xstrdup(value);
cache_put_path(me->cache, submodule);
}
-   } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
+   } else if (!strcmp(key, "fetchrecursesubmodules")) {
/* when parsing worktree configurations we can die early */
int die_on_error = is_null_sha1(me->gitmodules_sha1);
if (!me->overwrite &&
@@ -286,7 +277,7 @@ static int parse_config(const char *var, const char *value, 
void *data)
submodule->fetch_recurse = parse_fetch_recurse(
var, value,
die_on_error);
-   } else if (!strcmp(item.buf, "ignore")) {
+   } else if (!strcmp(key, "ignore")) {
if (!value)
ret = config_error_nonbool(var);
else if (!me->overwrite && submodule->ignore != NULL)
@@ -302,7 +293,7 @@ static int parse_config(const char *var, const char *value, 
void *data)
free((void *) submodule->ignore);
submodule->ignore = xstrdup(value);
}
-   } else if (!strcmp(item.buf, "url")) {
+   } else if (!strcmp(key, "url")) {
if (!value) {
ret = config_error_nonbool(var);
} else if (!me->overwrite && submodule->url != NULL) {
@@ -312,7 +303,7 @@ static int parse_config(const char *var, const char *value, 
void *data)
free((void *) submodule->url);
submodule->url = xstrdup(value);
}
-   } else if (!strcmp(item.buf, "update")) {
+   } else if (!strcmp(key, "update")) {
if (!value)
ret = config_error_nonbool(var);
else if (!me->overwrite && submodule->update != NULL)
@@ -324,9 +315,6 @@ static int parse_config(const char *var, const char *value, 
void *data)
}
}
 
-   strbuf_release();
-   strbuf_release();
-
return ret;
 }
 
-- 
2.5.0.281.g4ed9cdb

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html