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.
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.
Thanks for taking a look. You suggestion sounds reasonable, I will
modify my patch accordingly.
Cheers Heiko
Hi,
On Sun, Feb 24, 2013 at 09:54:43PM -0800, Junio C Hamano wrote:
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.
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
--
1.8.2.rc0.26.gf7384c5
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 <redacted>
---
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
Commit-Message-by: Jeff King [off-list ref]
here :-)
Cheers Heiko
config.c | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
To 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.
Signed-off-by: Heiko Voigt <redacted>
---
config.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 40 insertions(+), 17 deletions(-)
@@ -10,20 +10,42 @@#include"strbuf.h"#include"quote.h"-typedefstructconfig_file{-structconfig_file*prev;-FILE*f;+structconfig{+structconfig*prev;+void*data;constchar*name;intlinenr;inteof;structstrbufvalue;structstrbufvar;-}config_file;-staticconfig_file*cf;+int(*fgetc)(structconfig*c);+int(*ungetc)(intc,structconfig*conf);+long(*ftell)(structconfig*c);+};++staticstructconfig*cf;staticintzlib_compression_seen;+staticintconfig_file_fgetc(structconfig*conf)+{+FILE*f=conf->data;+returnfgetc(f);+}++staticintconfig_file_ungetc(intc,structconfig*conf)+{+FILE*f=conf->data;+returnungetc(c,f);+}++staticlongconfig_file_ftell(structconfig*conf)+{+FILE*f=conf->data;+returnftell(f);+}+#define MAX_INCLUDE_DEPTH 10staticconstcharinclude_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';}}
@@ -46,6 +46,37 @@ static long config_file_ftell(struct config *conf)returnftell(f);}+structconfig_strbuf{+structstrbuf*strbuf;+intpos;+};++staticintconfig_strbuf_fgetc(structconfig*conf)+{+structconfig_strbuf*str=conf->data;++if(str->pos<str->strbuf->len)+returnstr->strbuf->buf[str->pos++];++returnEOF;+}++staticintconfig_strbuf_ungetc(intc,structconfig*conf)+{+structconfig_strbuf*str=conf->data;++if(str->pos>0)+returnstr->strbuf->buf[--str->pos];++returnEOF;+}++staticlongconfig_strbuf_ftell(structconfig*conf)+{+structconfig_strbuf*str=conf->data;+returnstr->pos;+}+#define MAX_INCLUDE_DEPTH 10staticconstcharinclude_depth_advice[]="exceeded maximum include depth (%d) while including\n"
The 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.
Signed-off-by: Heiko Voigt <redacted>
---
config.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
@@ -169,10 +169,10 @@ int git_config_from_parameters(config_fn_t fn, void *data)staticintget_next_char(void){intc;-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 */
From: Jeff King <hidden> Date: 2016-06-15 22:56:16
On Tue, Feb 26, 2013 at 08:38:50PM +0100, Heiko Voigt wrote:
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 <redacted>
---
Peff, I hope you do not mind that I totally copied your commit message
here.
I don't mind at all.
The patch takes a different approach though. If you like we can add a
Commit-Message-by: Jeff King [off-list ref]
here :-)
I think my name is plastered all over "git log" enough as it is.
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
From: Jeff King <hidden> Date: 2016-06-15 22:56:16
On Tue, Feb 26, 2013 at 08:40:23PM +0100, Heiko Voigt wrote:
The 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.
Makes sense, although...
- 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
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
From: Jeff King <hidden> Date: 2016-06-15 22:56:16
On Tue, Feb 26, 2013 at 09:09:41PM +0100, Heiko Voigt wrote:
quoted
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.
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
On Tue, Feb 26, 2013 at 03:05:56PM -0500, Jeff King wrote:
On Tue, Feb 26, 2013 at 08:40:23PM +0100, Heiko Voigt wrote:
quoted
The 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.
Makes sense, although...
quoted
- 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
On Wed, Feb 27, 2013 at 08:52:57AM +0100, Heiko Voigt wrote:
On Tue, Feb 26, 2013 at 03:05:56PM -0500, Jeff King wrote:
quoted
On Tue, Feb 26, 2013 at 08:40:23PM +0100, Heiko Voigt wrote:
quoted
The 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.
Makes sense, although...
quoted
- 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.
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
On Thu, Feb 28, 2013 at 01:41:47AM +0100, Heiko Voigt wrote:
On Wed, Feb 27, 2013 at 08:52:57AM +0100, Heiko Voigt wrote:
quoted
On Tue, Feb 26, 2013 at 03:05:56PM -0500, Jeff King wrote:
quoted
On Tue, Feb 26, 2013 at 08:40:23PM +0100, Heiko Voigt wrote:
quoted
The 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.
Makes sense, although...
quoted
- 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.
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.
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
@@ -46,6 +46,37 @@ static long config_file_ftell(struct config *conf)returnftell(f);}+structconfig_strbuf{+structstrbuf*strbuf;+intpos;+};++staticintconfig_strbuf_fgetc(structconfig*conf)+{+structconfig_strbuf*str=conf->data;++if(str->pos<str->strbuf->len)+returnstr->strbuf->buf[str->pos++];++returnEOF;+}++staticintconfig_strbuf_ungetc(intc,structconfig*conf)+{+structconfig_strbuf*str=conf->data;++if(str->pos>0)+returnstr->strbuf->buf[--str->pos];++returnEOF;+}++staticlongconfig_strbuf_ftell(structconfig*conf)+{+structconfig_strbuf*str=conf->data;+returnstr->pos;+}+#define MAX_INCLUDE_DEPTH 10staticconstcharinclude_depth_advice[]="exceeded maximum include depth (%d) while including\n"
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.
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
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.
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.
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.
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