git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
To: Heiko Voigt <hvoigt@hvoigt.net>
Cc: Junio C Hamano <gitster@pobox.com>,
	git@vger.kernel.org, Jens Lehmann <jens.lehmann@web.de>,
	Jeff King <peff@peff.net>
Subject: Re: [PATCH 4/4] teach config parsing to read from strbuf
Date: Thu, 07 Mar 2013 18:42:43 +0000	[thread overview]
Message-ID: <5138DFA3.8040308@ramsay1.demon.co.uk> (raw)
In-Reply-To: <20130226194313.GE22756@sandbox-ub>

Heiko Voigt wrote:
> This can be used to read configuration values directly from gits
> database.
> 
> Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>
> ---
>  .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
> 

[...]

> diff --git a/config.c b/config.c
> index 19aa205..492873a 100644
> --- a/config.c
> +++ b/config.c
> @@ -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 10
>  static 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;
>  }
>  
> +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.

> +	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;
> diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
> index 3c96fda..3304bcd 100755
> --- a/t/t1300-repo-config.sh
> +++ b/t/t1300-repo-config.sh
> @@ -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
> diff --git a/test-config.c b/test-config.c
> new file mode 100644
> index 0000000..7a4103c
> --- /dev/null
> +++ b/test-config.c
> @@ -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;
> +
> +		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

  reply	other threads:[~2013-03-07 18:44 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-25  1:02 [RFC/WIP PATCH 0/3] fetch moved submodules on-demand Heiko Voigt
2013-02-25  1:04 ` [RFC/WIP PATCH 1/3] teach config parsing to read from strbuf Heiko Voigt
2013-02-25  5:54   ` Junio C Hamano
2013-02-25 17:29     ` Heiko Voigt
2013-02-26 19:30     ` [PATCH 0/4] allow more sources for config values Heiko Voigt
2013-02-26 19:38       ` [PATCH 1/4] config: factor out config file stack management Heiko Voigt
2013-02-26 19:54         ` Jeff King
2013-02-26 20:09           ` Heiko Voigt
2013-02-26 20:15             ` Jeff King
2013-02-26 22:10               ` Junio C Hamano
2013-02-27  7:51                 ` Heiko Voigt
2013-02-26 22:12             ` Junio C Hamano
2013-02-27  7:56               ` Heiko Voigt
2013-02-26 19:40       ` [PATCH 2/4] config: drop file pointer validity check in get_next_char() Heiko Voigt
2013-02-26 20:05         ` Jeff King
2013-02-27  7:52           ` Heiko Voigt
2013-02-28  0:42             ` Heiko Voigt
2013-02-28  0:54               ` Heiko Voigt
2013-02-26 19:42       ` [PATCH 3/4] config: make parsing stack struct independent from actual data source Heiko Voigt
2013-02-26 19:43       ` [PATCH 4/4] teach config parsing to read from strbuf Heiko Voigt
2013-03-07 18:42         ` Ramsay Jones [this message]
2013-03-10 16:39           ` Heiko Voigt
2013-02-26  4:55   ` [RFC/WIP PATCH 1/3] " Jeff King
2013-02-25  1:05 ` [RFC/WIP PATCH 2/3] implement fetching of moved submodules Heiko Voigt
2013-02-25  1:06 ` [RFC/WIP PATCH 3/3] submodule: simplify decision tree whether to or not to fetch Heiko Voigt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5138DFA3.8040308@ramsay1.demon.co.uk \
    --to=ramsay@ramsay1.demon.co.uk \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=hvoigt@hvoigt.net \
    --cc=jens.lehmann@web.de \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).