fetch moved submodules on-demand
cover fetch moved submodules on-demand
Hi,
this is a series I have been working on and off. My plan is that it might be part of a slightly bigger series also implementing on-demand clone of submodules into the .git/modules folder if a submodule is configured like that.
This is needed as preparation for the final goal of automatic checkout/deletion of submodules when they appear/disappear. My plan is to introduce a new .gitmodules variable
submodule.<name>.init
to specify whether a submodule should be initialized (and thus cloned) by default or not. If not configured it will default to off to stay closed to the current behavior. If it proves useful we can maybe change that default later to on.
That way we would get much closer to "clone/fetch and get everything you need to work on a project".
I send this series mainly to inform people what I have been working and to maybe get some early feedback about the approach. Let me know what you think.
Cheers Heiko
Heiko Voigt (3):
teach config parsing to read from strbuf
implement fetching of moved submodules
submodule: simplify decision tree whether to or not to fetch
.gitignore | 1 + Makefile | 2 + cache.h | 1 + config.c | 119 +++++++++++++++++----- submodule-config-cache.c | 96 ++++++++++++++++++ submodule-config-cache.h | 34 +++++++ submodule.c | 242 ++++++++++++++++++++++++++++++++++++-------- t/t1300-repo-config.sh | 4 + t/t5526-fetch-submodules.sh | 31 ++++++ test-config.c | 41 ++++++++ 10 files changed, 502 insertions(+), 69 deletions(-) create mode 100644 submodule-config-cache.c create mode 100644 submodule-config-cache.h create mode 100644 test-config.c
Signature · 1 line
1.8.2.rc0.25.g5062c01
PATCH 1/3 teach config parsing to read from strbuf
This can be used to read configuration values directly from gits database.
--- .gitignore | 1 + Makefile | 1 + cache.h | 1 + config.c | 119 ++++++++++++++++++++++++++++++++++++++----------- t/t1300-repo-config.sh | 4 ++ test-config.c | 41 +++++++++++++++++ 6 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 test-config.c
.gitignore +1 -0
@@ -178,6 +178,7 @@ /gitweb/static/gitweb.min.* /test-chmtime /test-ctype +/test-config /test-date /test-delta /test-dump-cache-tree
Makefile +1 -0
@@ -543,6 +543,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS)) TEST_PROGRAMS_NEED_X += test-chmtime TEST_PROGRAMS_NEED_X += test-ctype +TEST_PROGRAMS_NEED_X += test-config TEST_PROGRAMS_NEED_X += test-date TEST_PROGRAMS_NEED_X += test-delta TEST_PROGRAMS_NEED_X += test-dump-cache-tree
cache.h +1 -0
@@ -1128,6 +1128,7 @@ extern int update_server_info(int);typedef int (*config_fn_t )(const char *,const char *,void *);extern int git_default_config (const char *,const char *,void *);extern int git_config_from_file (config_fn_t fn,const char *,void *); +extern int git_config_from_strbuf (config_fn_t fn,struct strbuf *strbuf,void *data);extern void git_config_push_parameter (const char *text);extern int git_config_from_parameters (config_fn_t fn,void *data);extern int git_config (config_fn_t fn,void *);
config.c +94 -25
189 lines not on this page · raw message
t/t1300-repo-config.sh +4 -0
@@ -1087,4 +1087,8 @@ test_expect_success 'barf on incomplete string' ' grep " line 3 " error ' +test_expect_success 'reading config from strbuf' ' + test-config strbuf +' + test_done
test-config.c new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
1.8.2.rc0.25.g5062c01
Re: [RFC/WIP PATCH 1/3] teach config parsing to read from strbuf
Quoted 4 lines · Heiko Voigt
This can be used to read configuration values directly from gits database.
Signed-off-by Heiko Voigt <[email protected]>
FWIW, I implemented something quite similar as a 2-patch series here:
http://article.gmane.org/gmane.comp.version-control.git/189142
http://article.gmane.org/gmane.comp.version-control.git/189143
but they were never merged as we ended up throwing out the feature built on top (including config from blobs). I didn't look too closely at your implementation, but it looks like you touched the same spots in the same way. Which is probably a good thing, and may give another data point for Junio's "what would it look like to read another source" comment.
-Peff
Re: [RFC/WIP PATCH 1/3] teach config parsing to read from strbuf
In reply to Heiko VoigtQuoted 5 lines · Heiko Voigt
config.c +0
-0
config.c line 21
Quoted 9 lines
config.c +3
-0@@ -13,6 +13,9 @@ typedef struct config_file { struct config_file *prev; FILE *f; + int is_strbuf; + struct strbuf *strbuf_contents; + int strbuf_pos; const char *name; int linenr; int eof;
The idea to allow more kinds of sources specified for "config_file" structure is not bad per-se, but whenever you design an enhancement to something that currently supports only on thing to allow taking another kind, please consider what needs to be done by the next person who adds the third kind. That would help catch design mistakes early. For example, will the "string-list" (I am not saying use of string-list makes sense as the third kind; just as an example off the top of my head) source patch add
int is_string_list;
struct string_list *string_list_contents;fields to this structure? Sounds insane for at least two reasons:
* if both is_strbuf and is_string_list are true, what should happen?
* is there a good reason to waste storage for the three fields your patch adds when sring_list strage (or FILE * storage for that matter) is used?
The helper functions like config_fgetc() and config_ftell() sounds like you are going in the right direction but may want to do the OO-in-C in a similar way transport.c does, keeping a pointer to a structure of methods, but I didn't read the remainder of this patch very carefully enough to comment further.
Re: [RFC/WIP PATCH 1/3] teach config parsing to read from strbuf
Hi Junio,
Quoted 38 lines · Junio C Hamano
Quoted 5 lines · Heiko Voigt
config.c +0
-0config.c line 21
Quoted 9 lines
config.c +3
-0@@ -13,6 +13,9 @@ typedef struct config_file { struct config_file *prev; FILE *f; + int is_strbuf; + struct strbuf *strbuf_contents; + int strbuf_pos; const char *name; int linenr; int eof;The idea to allow more kinds of sources specified for "
config_file" structure is not bad per-se, but whenever you design an enhancement to something that currently supports only on thing to allow taking another kind, please consider what needs to be done by the next person who adds the third kind. That would help catch design mistakes early. For example, will the "string-list" (I am not saying use of string-list makes sense as the third kind; just as an example off the top of my head) source patch addint is_string_list; struct string_list *string_list_contents;fields to this structure? Sounds insane for at least two reasons:
* if both
is_strbufandis_string_listare true, what should happen?* is there a good reason to waste storage for the three fields your patch adds when
sring_liststrage (or FILE * storage for that matter) is used?
Quoted 5 lines
The helper functions like
config_fgetc()andconfig_ftell()sounds like you are going in the right direction but may want to do the OO-in-C in a similar waytransport.cdoes, keeping a pointer to a structure of methods, but I didn't read the remainder of this patch very carefully enough to comment further.
Thanks for taking a look. You suggestion sounds reasonable, I will modify my patch accordingly.
Cheers Heiko
PATCH 2/3 implement fetching of moved submodules
In reply to Heiko VoigtChange calculation of changed submodule paths to read revisions config.
We now read the submodule name for every changed submodule path in a commit. We then use the submodules names for fetching instead of the submodule paths.
We lazily read all configurations of changed submodule path into a cache which can then be used to lookup the configuration for a certain revision and path or name.
It is expected that .gitmodules files do not change often between commits. Thats why we lookup the .gitmodules sha1 and use a submodule configuration cache to lazily lookup the parsed result for a submodule stored by its name.
This cache could be reused for other purposes which need knowledge about submodule configurations. For example automatic clone on fetch of a new submodule if it is configured to be automatically initialized.
The submodule configuration in the current worktree can be stored using the null sha1.
--- Makefile | 1 + submodule-config-cache.c | 96 ++++++++++++++++++++++ submodule-config-cache.h | 34 ++++++++ submodule.c | 196 ++++++++++++++++++++++++++++++++++++++------ t/t5526-fetch-submodules.sh | 31 +++++++ 5 files changed, 335 insertions(+), 23 deletions(-) create mode 100644 submodule-config-cache.c create mode 100644 submodule-config-cache.h
Makefile +1 -0
@@ -858,6 +858,7 @@ LIB_OBJS += strbuf.o LIB_OBJS += streaming.o LIB_OBJS += string-list.o LIB_OBJS += submodule.o +LIB_OBJS += submodule-config-cache.o LIB_OBJS += symlinks.o LIB_OBJS += tag.o LIB_OBJS += trace.o
submodule-config-cache.c new file +96 -0
96 lines not on this page · raw message
submodule-config-cache.h new file +34 -0
@@ -0,0 +1,34 @@ +#ifndef SUBMODULE_CONFIG_CACHE_H +#define SUBMODULE_CONFIG_CACHE_H + +#include "hash.h" +#include "strbuf.h" + +struct submodule_config_cache { +struct hash_table for_path; +struct hash_table for_name; +}; + +/* one submodule_config_cache entry */ +struct submodule_config { +struct strbuf path; +struct strbuf name; +unsigned char gitmodule_sha1[20 ]; +int fetch_recurse_submodules; +struct submodule_config *next; +}; + +void submodule_config_cache_init (struct submodule_config_cache *cache); +void submodule_config_cache_free (struct submodule_config_cache *cache); + +void submodule_config_cache_update_path (struct submodule_config_cache *cache, +struct submodule_config *config); +void submodule_config_cache_insert (struct submodule_config_cache *cache, +struct submodule_config *config); + +struct submodule_config *submodule_config_cache_lookup_path (struct submodule_config_cache *cache, +const unsigned char *gitmodule_sha1,const char *path); +struct submodule_config *submodule_config_cache_lookup_name (struct submodule_config_cache *cache, +const unsigned char *gitmodule_sha1,const char *name); + +#endif /* SUBMODULE_CONFIG_CACHE_H */
submodule.c +173 -23
275 lines not on this page · raw message
t/t5526-fetch-submodules.sh +31 -0
@@ -450,4 +450,35 @@ test_expect_success "don't fetch submodule when newly recorded commits are alrea test_i18ncmp expect.err actual.err ' +test_expect_success "fetch new commits when submodule got renamed" ' + ( + cd submodule && + git checkout -b rename_sub && + echo a >a && + git add a && + git commit -ma && + git rev-parse HEAD >../expect + ) && + git clone . downstream2 && + ( + cd downstream2 && + git submodule update --init --recursive && + git checkout -b rename && + mv submodule submodule_renamed && + git config -f .gitmodules submodule.submodule.path submodule_renamed && + git add submodule_renamed .gitmodules && + git commit -m "rename submodule -> submodule-renamed" && + git push origin rename + ) && + ( + cd downstream && + git fetch --recurse-submodules=on-demand && + ( + cd submodule && + git rev-parse origin/rename_sub >../../actual + ) + ) && + test_cmp expect actual +' + test_done
Signature · 1 line
1.8.2.rc0.25.g5062c01
PATCH 3/3 submodule: simplify decision tree whether to or not to fetch
In reply to Heiko VoigtTo make extending this logic later easier.
--- submodule.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-)
submodule.c +27 -23
@@ -737,6 +737,23 @@ static void calculate_changed_submodule_paths(void)submodule_config_cache_free (&submodule_config_cache); } +static int get_fetch_recurse_config (const char *name,int command_line_option) +{ +if (command_line_option !=RECURSE_SUBMODULES_DEFAULT ) +return command_line_option; + +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) +/* local config overrules everything except commandline */ +return (intptr_t )fetch_recurse_submodules_option->util; + +if (gitmodules_is_unmerged) +return RECURSE_SUBMODULES_OFF ; + +return config_fetch_recurse_submodules; +} +int fetch_populated_submodules (const struct argv_array *options,const char *prefix,int command_line_option,int quiet)
@@ -781,32 +798,19 @@ int fetch_populated_submodules(const struct argv_array *options,if (name_for_path) name = name_for_path->util; - 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 ) -continue ; -if ((intptr_t )fetch_recurse_submodules_option->util ==RECURSE_SUBMODULES_ON_DEMAND ) { -if (!unsorted_string_list_lookup (&changed_submodule_names, name)) -continue ; - default_argv ="on-demand" ; - } - }else { -if ((config_fetch_recurse_submodules ==RECURSE_SUBMODULES_OFF ) || - gitmodules_is_unmerged) -continue ; -if (config_fetch_recurse_submodules ==RECURSE_SUBMODULES_ON_DEMAND ) { -if (!unsorted_string_list_lookup (&changed_submodule_names, name)) -continue ; - default_argv ="on-demand" ; - } - } - }else if (command_line_option ==RECURSE_SUBMODULES_ON_DEMAND ) { +switch (get_fetch_recurse_config (name, command_line_option)) { +default : +case RECURSE_SUBMODULES_DEFAULT : +case RECURSE_SUBMODULES_ON_DEMAND :if (!unsorted_string_list_lookup (&changed_submodule_names, name))continue ; default_argv ="on-demand" ; +break ; +case RECURSE_SUBMODULES_ON : + default_argv ="yes" ; +break ; +case RECURSE_SUBMODULES_OFF : +continue ; }strbuf_addf (&submodule_path,"%s/%s" , work_tree, ce->name);
Signature · 1 line
1.8.2.rc0.25.g5062c01
cover allow more sources for config values
In reply to Junio C HamanoHi,
Quoted 21 lines · Junio C Hamano
The idea to allow more kinds of sources specified for "
config_file" structure is not bad per-se, but whenever you design an enhancement to something that currently supports only on thing to allow taking another kind, please consider what needs to be done by the next person who adds the third kind. That would help catch design mistakes early. For example, will the "string-list" (I am not saying use of string-list makes sense as the third kind; just as an example off the top of my head) source patch addint is_string_list; struct string_list *string_list_contents;fields to this structure? Sounds insane for at least two reasons:
* if both
is_strbufandis_string_listare true, what should happen?* is there a good reason to waste storage for the three fields your patch adds when
sring_liststrage (or FILE * storage for that matter) is used?
Quoted 5 lines
The helper functions like
config_fgetc()andconfig_ftell()sounds like you are going in the right direction but may want to do the OO-in-C in a similar waytransport.cdoes, keeping a pointer to a structure of methods, but I didn't read the remainder of this patch very carefully enough to comment further.
I split up my config-from-strings patch from the "fetch moved submodules on-demand" series[1] for nicer reviewability. Since Peff almost needed it and the recursive submodule checkout will definitely[2] need it: How about making it a seperate topic and implement this infrastructure first?
Junio I incorporated your comments this seems like a result ready for inclusion.
[1] http://article.gmane.org/gmane.comp.version-control.git/217018
[2] http://article.gmane.org/gmane.comp.version-control.git/217155
Heiko Voigt (4):
config: factor out config file stack management
config: drop file pointer validity check in get_next_char()
config: make parsing stack struct independent from actual data source teach config parsing to read from strbuf
.gitignore | 1 + Makefile | 1 + cache.h | 1 + config.c | 140 ++++++++++++++++++++++++++++++++++++++----------- t/t1300-repo-config.sh | 4 ++ test-config.c | 41 +++++++++++++++ 6 files changed, 158 insertions(+), 30 deletions(-) create mode 100644 test-config.c
Signature · 1 line
1.8.2.rc0.26.gf7384c5
PATCH 1/4 config: factor out config file stack management
Because a config callback may start parsing a new file, the global context regarding the current config file is stored as a stack. Currently we only need to manage that stack from git_config_from_file. Let's factor it out to allow new sources of config data.
---
Peff, I hope you do not mind that I totally copied your commit message here. The patch takes a different approach though. If you like we can add a
here :-)
Cheers Heiko
config.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-)
config.c +24 -14
@@ -896,6 +896,28 @@ int git_default_config(const char *var, const char *value, void *dummy)return 0 ; } +static int do_config_from (struct config_file *top,config_fn_t fn,void *data) +{ +int ret; + +/* push config-file parsing state stack */ + top->prev = cf; + top->linenr =1 ; + top->eof =0 ; +strbuf_init (&top->value,1024 ); +strbuf_init (&top->var,1024 ); + cf = top; + + ret =git_parse_file (fn, data); + +/* pop config-file parsing state stack */ +strbuf_release (&top->value); +strbuf_release (&top->var); + cf = top->prev; + +return ret; +} +int git_config_from_file (config_fn_t fn,const char *filename,void *data) {int ret;
@@ -905,22 +927,10 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)if (f) {config_file top; -/* push config-file parsing state stack */ - top.prev = cf; top.f = f; top.name = filename; - top.linenr =1 ; - top.eof =0 ; -strbuf_init (&top.value,1024 ); -strbuf_init (&top.var,1024 ); - cf = ⊤ - - ret =git_parse_file (fn, data); - -/* pop config-file parsing state stack */ -strbuf_release (&top.value); -strbuf_release (&top.var); - cf = top.prev; + + ret =do_config_from (&top, fn, data);fclose (f); }
Signature · 1 line
1.8.2.rc0.26.gf7384c5
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 11 lines · Heiko Voigt
Because a config callback may start parsing a new file, the global context regarding the current config file is stored as a stack. Currently we only need to manage that stack from
git_config_from_file. Let's factor it out to allow new sources of config data.Signed-off-by Heiko Voigt <[email protected]>---
Peff, I hope you do not mind that I totally copied your commit message here.
I don't mind at all.
Quoted 5 lines
The patch takes a different approach though. If you like we can add a
Commit-Message-by Jeff King <[email protected]>here :-)
I think my name is plastered all over "git log" enough as it is.
Quoted 11 lines
config.c +11
-0+static int do_config_from (struct config_file *top,config_fn_t fn,void *data) +{ +int ret; + +/* push config-file parsing state stack */ + top->prev = cf; + top->linenr =1 ; + top->eof =0 ; +strbuf_init (&top->value,1024 ); +strbuf_init (&top->var,1024 ); + cf = top;
config.c int git_default_config(const char *var, const char *value, void *dummy) · line 919
Quoted 10 lines
config.c +10
-0+ + ret =git_parse_file (fn, data); + +/* pop config-file parsing state stack */ +strbuf_release (&top->value); +strbuf_release (&top->var); + cf = top->prev; + +return ret; +}
This function name is a bit weird. I would have thought the "from" here was going to be a file, or a string, or whatever. But the filename setup happens outside this function (and yet this function depends on it being set up, as it calls git_parse_file). But maybe it will get less confusing with the other patches on top...
-Peff
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 20 lines · Jeff King
Quoted 19 lines · Heiko Voigt
config.c +19
-0+static int do_config_from (struct config_file *top,config_fn_t fn,void *data) +{ +int ret; + +/* push config-file parsing state stack */ + top->prev = cf; + top->linenr =1 ; + top->eof =0 ; +strbuf_init (&top->value,1024 ); +strbuf_init (&top->var,1024 ); + cf = top; + + ret =git_parse_file (fn, data); + +/* pop config-file parsing state stack */ +strbuf_release (&top->value); +strbuf_release (&top->var); + cf = top->prev; +
Quoted 8 lines
config.c int git_default_config(const char *var, const char *value, void *dummy) · line 919
Quoted 2 lines
config.c +2
-0+return ret; +}This function name is a bit weird. I would have thought the "from" here was going to be a file, or a string, or whatever. But the filename setup happens outside this function (and yet this function depends on it being set up, as it calls
git_parse_file). But maybe it will get less confusing with the other patches on top...
The "do_config_from" means "parse from whatever is in 'top'". Later in the series its type changes from config_file to struct config.
The name 'git_parse_file' becomes definitely wrong after this series. Maybe I should rename it?
Cheers Heiko
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 8 lines · Heiko Voigt
Quoted 5 lines
This function name is a bit weird. I would have thought the "from" here was going to be a file, or a string, or whatever. But the filename setup happens outside this function (and yet this function depends on it being set up, as it calls
git_parse_file). But maybe it will get less confusing with the other patches on top...The "
do_config_from" means "parse from whatever is in 'top'". Later in the series its type changes fromconfig_fileto struct config.
Ah, I see. The "from" is the "struct config".
I wonder if it would be more obvious with the more usual OO-struct functions, like:
struct config_source {
...
};
void config_source_init_file(struct config_source *, const char *fn);
void config_source_init_strbuf(struct config_source *,
const struct strbuf *buf);
void config_source_clear(struct config_source *);
int config_source_parse(struct config_source *);and then the use would be something like:
struct config_source top;
int ret;
config_source_init_file(&top, "foo"); ret = config_source_parse(&top); config_source_clear(&top);
return ret;
I.e., "init" constructors, a "clear" destructor, and any methods like "parse" that you need. I haven't though too hard about it, though, so maybe there is some reason it does not fit that model (it is a little uncommon that the "init" would push itself onto a stack, but I think that's OK).
-Peff
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 18 lines · Jeff King
I wonder if it would be more obvious with the more usual OO-struct functions, like:
struct config_source { ... }; void config_source_init_file(struct config_source *, const char *fn); void config_source_init_strbuf(struct config_source *, const struct strbuf *buf); void config_source_clear(struct config_source *); int config_source_parse(struct config_source *);and then the use would be something like:
struct
config_sourcetop;
int ret;
Quoted 8 lines
config_source_init_file(&top, "foo"); ret = config_source_parse(&top); config_source_clear(&top);return ret;
I.e., "init" constructors, a "clear" destructor, and any methods like "parse" that you need.
Yup, that cocincides with my first impression I sent out for the previous RFC/PATCH round.
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 24 lines · Junio C Hamano
Quoted 22 lines · Jeff King
I wonder if it would be more obvious with the more usual OO-struct functions, like:
struct config_source { ... }; void config_source_init_file(struct config_source *, const char *fn); void config_source_init_strbuf(struct config_source *, const struct strbuf *buf); void config_source_clear(struct config_source *); int config_source_parse(struct config_source *);and then the use would be something like:
struct
config_sourcetop;
int ret;config_source_init_file(&top, "foo"); ret = config_source_parse(&top); config_source_clear(&top);
Quoted 7 lines
Quoted 4 lines
return ret;
I.e., "init" constructors, a "clear" destructor, and any methods like "parse" that you need.
Yup, that cocincides with my first impression I sent out for the previous RFC/PATCH round.
I agree that your suggestions would make the design more OO and provide us with more flexibility. However at the following costs:
Currently the push and pop are combined into one function. This design makes it impossible to forget the cleanup if someone else uses this function. The same applies to the private data handling around do_config_from().
All memory is currently handled on the stack. If we have an init/clear design at least the private data for strbuf needs to be put on the heap. We could pass in the config_strbuf but IMO that would violate the encapsulation.
Thus we also require a specialized clear which frees that private data (we need that to close the file anyway). Because of that I would probably call it destroy or free.
That means there will be six additional functions instead of one: We need init and clear for both, a common init and a common clear for the push/pop part. IMO that will make the code harder to read for the first time.
Then we would have a hybrid stack/heap solution still involving side effects (setting the global cf).
Do not get me wrong. I am not against changing it but I would like to spell out the consequences first before doing so.
Do you still think that is nicer approach?
Cheers Heiko
Re: [PATCH 1/4] config: factor out config file stack management
In reply to Heiko VoigtQuoted 2 lines · Heiko Voigt
The "
do_config_from" means "parse from whatever is in 'top'". Later in the series its type changes fromconfig_fileto struct config.
Yuck. It would be nice to have it as struct config_src or something. "struct config" sounds as if it represents the entire configuration state and you can ask it to add new ones or enumerate all known configuration variables, etc.
Re: [PATCH 1/4] config: factor out config file stack management
Quoted 9 lines · Junio C Hamano
Quoted 2 lines · Heiko Voigt
The "
do_config_from" means "parse from whatever is in 'top'". Later in the series its type changes fromconfig_fileto struct config.Yuck. It would be nice to have it as struct
config_srcor something. "struct config" sounds as if it represents the entire configuration state and you can ask it to add new ones or enumerate all known configuration variables, etc.
Will change it to struct config_source. I choose that name in lack of a better one. Since it can be considered the base for both sources I just removed the _file postfix.
Cheers Heiko
PATCH 2/4 config: drop file pointer validity check in get_next_char()
In reply to Heiko VoigtThe only location where cf is set in this file is in do_config_from(). This function has only one callsite which is config_from_file(). In config_from_file() its ensured that the f member is set to non-zero.
--- config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
config.c +2 -2
@@ -169,10 +169,10 @@ int git_config_from_parameters(config_fn_t fn, void *data)static int get_next_char (void ) {int c; -FILE *f; c ='\n' ; -if (cf && ((f = cf->f) !=NULL )) { +if (cf) { +FILE *f = cf->f; c =fgetc (f);if (c =='\r' ) {/* DOS like systems */
Signature · 1 line
1.8.2.rc0.26.gf7384c5
Re: [PATCH 2/4] config: drop file pointer validity check in get_next_char()
Quoted 3 lines · Heiko Voigt
The only location where cf is set in this file is in
do_config_from(). This function has only one callsite which isconfig_from_file(). Inconfig_from_file()its ensured that the f member is set to non-zero.
Makes sense, although...
config.c int git_config_from_parameters(config_fn_t fn, void *data) · line 175
Quoted 3 lines
config.c +2
-1-if (cf && ((f = cf->f) !=NULL )) { +if (cf) { +FILE *f = cf->f;
Couldn't we say the same thing about "cf" here (i.e., that it would never be NULL)? Can we just get rid of this conditional entirely?
-Peff
Re: [PATCH 2/4] config: drop file pointer validity check in get_next_char()
Quoted 6 lines · Jeff King
Quoted 3 lines · Heiko Voigt
The only location where cf is set in this file is in
do_config_from(). This function has only one callsite which isconfig_from_file(). Inconfig_from_file()its ensured that the f member is set to non-zero.
Quoted 8 lines
Makes sense, although...
config.c int git_config_from_parameters(config_fn_t fn, void *data) · line 175
Quoted 3 lines
config.c +2
-1-if (cf && ((f = cf->f) !=NULL )) { +if (cf) { +FILE *f = cf->f;Couldn't we say the same thing about "cf" here (i.e., that it would never be NULL)? Can we just get rid of this conditional entirely?
That might be true. I will look into it. Just wanted to get rid of an extra callback in my series.
Cheers Heiko
Re: [PATCH 2/4] config: drop file pointer validity check in get_next_char()
Quoted 16 lines · Heiko Voigt
Quoted 6 lines · Jeff King
Quoted 3 lines · Heiko Voigt
The only location where cf is set in this file is in
do_config_from(). This function has only one callsite which isconfig_from_file(). Inconfig_from_file()its ensured that the f member is set to non-zero.Quoted 8 lines
Makes sense, although...
config.c int git_config_from_parameters(config_fn_t fn, void *data) · line 175
Quoted 3 lines
config.c +2
-1-if (cf && ((f = cf->f) !=NULL )) { +if (cf) { +FILE *f = cf->f;Couldn't we say the same thing about "cf" here (i.e., that it would never be NULL)? Can we just get rid of this conditional entirely?
Quoted 2 lines
That might be true. I will look into it. Just wanted to get rid of an extra callback in my series.
I had a look and it might be true that cf will never be NULL in a code path. Nevertheless its much harder to verify by looking at the code since its a global variable. get_next_char() is called from all over the place and I would have to look at all the code paths. As far as I know static global variables are always initialized to zero so its safe to check even if has not yet been explicitly initialized.
The statement if cf is not NULL all members will be initialized is much simpler to verify since its just one place now and two places after this series.
Cheers Heiko
Re: [PATCH 2/4] config: drop file pointer validity check in get_next_char()
Quoted 27 lines · Heiko Voigt
Quoted 16 lines · Heiko Voigt
Quoted 6 lines · Jeff King
Quoted 3 lines · Heiko Voigt
The only location where cf is set in this file is in
do_config_from(). This function has only one callsite which isconfig_from_file(). Inconfig_from_file()its ensured that the f member is set to non-zero.Quoted 8 lines
Makes sense, although...
config.c int git_config_from_parameters(config_fn_t fn, void *data) · line 175
Quoted 3 lines
config.c +2
-1-if (cf && ((f = cf->f) !=NULL )) { +if (cf) { +FILE *f = cf->f;Couldn't we say the same thing about "cf" here (i.e., that it would never be NULL)? Can we just get rid of this conditional entirely?
Quoted 2 lines
That might be true. I will look into it. Just wanted to get rid of an extra callback in my series.
I had a look and it might be true that cf will never be NULL in a code path. Nevertheless its much harder to verify by looking at the code since its a global variable.
get_next_char()is called from all over the place and I would have to look at all the code paths. As far as I know static global variables are always initialized to zero so its safe to check even if has not yet been explicitly initialized.
Quoted 3 lines
The statement if cf is not NULL all members will be initialized is much simpler to verify since its just one place now and two places after this series.
To add some empirical information: I just ran the testsuite without the conditional and it still passes. To me it only make sense to start the parsing with cf initialized. But I am not familiar enough with the code to judge whether it is safe to assume this.
Cheers Heiko
PATCH 3/4 config: make parsing stack struct independent from actual data source
In reply to Heiko VoigtTo simplify adding other sources we extract all functions needed for parsing into a list of callbacks. We implement those callbacks for the current file parsing. A new source can implement its own set of callbacks.
Instead of storing the concrete FILE pointer for parsing we store a void pointer. A new source can use this to store its custom data.
--- config.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-)
config.c +40 -17
@@ -10,20 +10,42 @@#include "strbuf.h" #include "quote.h" -typedef struct config_file { -struct config_file *prev; -FILE *f; +struct config { +struct config *prev; +void *data;const char *name;int linenr;int eof;struct strbuf value;struct strbuf var; -}config_file ; -static config_file *cf; +int (*fgetc)(struct config *c); +int (*ungetc)(int c,struct config *conf); +long (*ftell)(struct config *c); +}; + +static struct config *cf;static int zlib_compression_seen; +static int config_file_fgetc (struct config *conf) +{ +FILE *f = conf->data; +return fgetc (f); +} + +static int config_file_ungetc (int c,struct config *conf) +{ +FILE *f = conf->data; +return ungetc (c, f); +} + +static long config_file_ftell (struct config *conf) +{ +FILE *f = conf->data; +return ftell (f); +} +#define MAX_INCLUDE_DEPTH 10static const char include_depth_advice[] ="exceeded maximum include depth (%d) while including\n"
@@ -172,13 +194,12 @@ static int get_next_char(void) c ='\n' ;if (cf) { -FILE *f = cf->f; - c =fgetc (f); + c = cf->fgetc (cf);if (c =='\r' ) {/* DOS like systems */ - c =fgetc (f); + c = cf->fgetc (cf);if (c !='\n' ) { -ungetc (c, f); + cf->ungetc (c, cf); c ='\r' ; } }
@@ -896,7 +917,7 @@ int git_default_config(const char *var, const char *value, void *dummy)return 0 ; } -static int do_config_from (struct config_file *top,config_fn_t fn,void *data) +static int do_config_from (struct config *top,config_fn_t fn,void *data) {int ret;
@@ -925,10 +946,13 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data) ret =-1 ;if (f) { -config_file top; +struct config top; - top.f = f; + top.data = f; top.name = filename; + top.fgetc = config_file_fgetc; + top.ungetc = config_file_ungetc; + top.ftell = config_file_ftell; ret =do_config_from (&top, fn, data);
@@ -1063,7 +1087,6 @@ static int store_aux(const char *key, const char *value, void *cb) {const char *ep;size_t section_len; -FILE *f = cf->f;switch (store.state) {case KEY_SEEN :
@@ -1075,7 +1098,7 @@ static int store_aux(const char *key, const char *value, void *cb)return 1 ; } - store.offset[store.seen] =ftell (f); + store.offset[store.seen] = cf->ftell (cf); store.seen++; }break ;
@@ -1102,19 +1125,19 @@ static int store_aux(const char *key, const char *value, void *cb)* Do not increment matches: this is no match, but we * just made sure we are in the desired section. */ - store.offset[store.seen] =ftell (f); + store.offset[store.seen] = cf->ftell (cf);/* fallthru */ case SECTION_END_SEEN :case START :if (matches (key, value)) { - store.offset[store.seen] =ftell (f); + store.offset[store.seen] = cf->ftell (cf); store.state =KEY_SEEN ; store.seen++; }else {if (strrchr (key,'.' ) - key == store.baselen && !strncmp (key, store.key, store.baselen)) { store.state =SECTION_SEEN ; - store.offset[store.seen] =ftell (f); + store.offset[store.seen] = cf->ftell (cf); } } }
Signature · 1 line
1.8.2.rc0.26.gf7384c5
PATCH 4/4 teach config parsing to read from strbuf
In reply to Heiko VoigtThis can be used to read configuration values directly from gits database.
--- .gitignore | 1 + Makefile | 1 + cache.h | 1 + config.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ t/t1300-repo-config.sh | 4 ++++ test-config.c | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 test-config.c
.gitignore +1 -0
@@ -178,6 +178,7 @@ /gitweb/static/gitweb.min.* /test-chmtime /test-ctype +/test-config /test-date /test-delta /test-dump-cache-tree
Makefile +1 -0
@@ -543,6 +543,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS)) TEST_PROGRAMS_NEED_X += test-chmtime TEST_PROGRAMS_NEED_X += test-ctype +TEST_PROGRAMS_NEED_X += test-config TEST_PROGRAMS_NEED_X += test-date TEST_PROGRAMS_NEED_X += test-delta TEST_PROGRAMS_NEED_X += test-dump-cache-tree
cache.h +1 -0
@@ -1128,6 +1128,7 @@ extern int update_server_info(int);typedef int (*config_fn_t )(const char *,const char *,void *);extern int git_default_config (const char *,const char *,void *);extern int git_config_from_file (config_fn_t fn,const char *,void *); +extern int git_config_from_strbuf (config_fn_t fn,struct strbuf *strbuf,void *data);extern void git_config_push_parameter (const char *text);extern int git_config_from_parameters (config_fn_t fn,void *data);extern int git_config (config_fn_t fn,void *);
config.c +47 -0
59 lines not on this page · raw message
t/t1300-repo-config.sh +4 -0
@@ -1087,4 +1087,8 @@ test_expect_success 'barf on incomplete string' ' grep " line 3 " error ' +test_expect_success 'reading config from strbuf' ' + test-config strbuf +' + test_done
test-config.c new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
1.8.2.rc0.26.gf7384c5
Re: [PATCH 4/4] teach config parsing to read from strbuf
Quoted 14 lines · Heiko Voigt
This can be used to read configuration values directly from gits database.
Signed-off-by Heiko Voigt <[email protected]>--- .gitignore | 1 + Makefile | 1 + cache.h | 1 + config.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ t/t1300-repo-config.sh | 4 ++++ test-config.c | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 test-config.c
[...]
Quoted 45 lines
config.c +31
-0@@ -46,6 +46,37 @@ static long config_file_ftell(struct config *conf)return ftell (f); } +struct config_strbuf { +struct strbuf *strbuf; +int pos; +}; + +static int config_strbuf_fgetc (struct config *conf) +{ +struct config_strbuf *str = conf->data; + +if (str->pos < str->strbuf->len) +return str->strbuf->buf[str->pos++]; + +return EOF ; +} + +static int config_strbuf_ungetc (int c,struct config *conf) +{ +struct config_strbuf *str = conf->data; + +if (str->pos >0 ) +return str->strbuf->buf[--str->pos]; + +return EOF ; +} + +static long config_strbuf_ftell (struct config *conf) +{ +struct config_strbuf *str = conf->data; +return str->pos; +} +#define MAX_INCLUDE_DEPTH 10static const char include_depth_advice[] ="exceeded maximum include depth (%d) while including\n" @@ -961,6 +992,22 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)return ret; }
config.c int git_config_from_file(config_fn_t fn, const char *filename, void *data) · line 1003
Quoted 10 lines
config.c +9
-0+int git_config_from_strbuf (config_fn_t fn,struct strbuf *strbuf,void *data) +{ +struct config top; +struct config_strbuf str; + + str.strbuf = strbuf; + str.pos =0 ; + + top.data = &str;
You will definitely want to initialise 'top.name' here, rather than let it take whatever value happens to be at that position on the stack. In your editor, search for 'cf->name' and contemplate each such occurrence.
Quoted 61 lines
config.c +7
-0+ top.fgetc = config_strbuf_fgetc; + top.ungetc = config_strbuf_ungetc; + top.ftell = config_strbuf_ftell; + +return do_config_from (&top, fn, data); +} +const char *git_etc_gitconfig (void ) {static const char *system_wide;t/t1300-repo-config.sh +4
-0@@ -1087,4 +1087,8 @@ test_expect_success 'barf on incomplete string' ' grep " line 3 " error ' +test_expect_success 'reading config from strbuf' ' + test-config strbuf +' + test_donetest-config.c new file +31
-0@@ -0,0 +1,41 @@ +#include "cache.h" + +static const char *config_string ="[some]\n" +" value = content\n" ; + +static int config_strbuf (const char *var,const char *value,void *data) +{ +int *success = data; +if (!strcmp (var,"some.value" ) && !strcmp (value,"content" )) + *success =0 ; + +printf ("var: %s, value: %s\n" , var, value); + +return 1 ; +} + +static void die_usage (int argc,char **argv) +{ +fprintf (stderr,"Usage: %s strbuf\n" , argv[0 ]); +exit (1 ); +} + +int main (int argc,char **argv) +{ +if (argc <2 ) +die_usage (argc, argv); + +if (!strcmp (argv[1 ],"strbuf" )) { +int success =1 ; +struct strbuf buf =STRBUF_INIT ; +
test-config.c line 41
Quoted 10 lines
test-config.c new file +10
-0+strbuf_addstr (&buf, config_string); +git_config_from_strbuf (config_strbuf, &buf, &success); + +return success; + } + +die_usage (argc, argv); + +return 1 ; +}
Does the 'include' facility work from a strbuf? Should it?
Are you happy with the error handling/reporting?
Do the above additions to the test-suite give you confidence that the code works as you intend?
ATB,
Ramsay Jones
Re: [PATCH 4/4] teach config parsing to read from strbuf
Hi,
Quoted 15 lines · Ramsay Jones
config.c int git_config_from_file(config_fn_t fn, const char *filename, void *data) · line 1003
Quoted 9 lines · Heiko Voigt
config.c +9
-0+int git_config_from_strbuf (config_fn_t fn,struct strbuf *strbuf,void *data) +{ +struct config top; +struct config_strbuf str; + + str.strbuf = strbuf; + str.pos =0 ; + + top.data = &str;You will definitely want to initialise 'top.name' here, rather than let it take whatever value happens to be at that position on the stack. In your editor, search for '
cf->name' and contemplate each such occurrence.
Good catch, thanks. The initialization seems to got lost during refactoring. In the codepaths we call with the new strbuf function it is only used for error reporting so I think we need to get the name from the user of this function so the error messages are useful.
I have extended the test to demonstrate how I imagine this name could look like.
Quoted 1 line
Does the 'include' facility work from a strbuf? Should it?
No the 'include' facility does not work here. A special handling would need to be implemented by the caller. For us 'include' values have no special meaning and are parsed like normal values.
AFAICS when a config file is given to git config we do not currently respect includes. So we can just do the same behavior here for the moment. There is no roadblock against adding it later.
Quoted 1 line
Are you happy with the error handling/reporting?
Good point, while adding the name for strbuf I noticed that we currently die on some parsing errors. We should probably make these errors handleable for strbufs. Currently the main usecase is to read submodule configurations from the database and in case someone commits a broken configuration we should be able to continue with that. Otherwise the repository might render into an unuseable state. I will look into that.
Quoted 2 lines
Do the above additions to the test-suite give you confidence that the code works as you intend?
Well, I am reusing most of the existing infrastructure which I assume is tested using config files. So what I want to test here is the integration of this new function. As you mentioned the name variable I have now added a test for the parsing error case as well. I have refactored the test binary to read configs from stdin so its easiert to implement further tests from the bash part of the testsuite.
I will send out another iteration shortly.
Cheers Heiko