From: Heiko Voigt <hvoigt@hvoigt.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Jens Lehmann <jens.lehmann@web.de>,
Jeff King <peff@peff.net>,
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Subject: [PATCH v2 4/4] teach config parsing to read from strbuf
Date: Sun, 10 Mar 2013 18:00:52 +0100 [thread overview]
Message-ID: <20130310170052.GE1136@sandbox-ub.fritz.box> (raw)
In-Reply-To: <20130310165642.GA1136@sandbox-ub.fritz.box>
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 | 2 ++
config.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
t/t1300-repo-config.sh | 24 ++++++++++++++++++++++++
test-config.c | 40 ++++++++++++++++++++++++++++++++++++++++
6 files changed, 116 insertions(+)
create mode 100644 test-config.c
diff --git a/.gitignore b/.gitignore
index 6669bf0..386b7f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -178,6 +178,7 @@
/gitweb/static/gitweb.min.*
/test-chmtime
/test-ctype
+/test-config
/test-date
/test-delta
/test-dump-cache-tree
diff --git a/Makefile b/Makefile
index 26d3332..1a9ea10 100644
--- a/Makefile
+++ b/Makefile
@@ -541,6 +541,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
diff --git a/cache.h b/cache.h
index e493563..a2621fa 100644
--- a/cache.h
+++ b/cache.h
@@ -1128,6 +1128,8 @@ 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, const char *name,
+ 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 *);
diff --git a/config.c b/config.c
index fe1c0e5..b8c8640 100644
--- a/config.c
+++ b/config.c
@@ -46,6 +46,37 @@ static long config_file_ftell(struct config_source *conf)
return ftell(f);
}
+struct config_strbuf {
+ struct strbuf *strbuf;
+ int pos;
+};
+
+static int config_strbuf_fgetc(struct config_source *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_source *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_source *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,23 @@ 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, const char *name, struct strbuf *strbuf, void *data)
+{
+ struct config_source top;
+ struct config_strbuf str;
+
+ str.strbuf = strbuf;
+ str.pos = 0;
+
+ top.data = &str;
+ top.name = name;
+ top.fgetc = config_strbuf_fgetc;
+ top.ungetc = config_strbuf_ungetc;
+ top.ftell = config_strbuf_ftell;
+
+ return do_config_from_source(&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..5103f66 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -1087,4 +1087,28 @@ test_expect_success 'barf on incomplete string' '
grep " line 3 " error
'
+test_expect_success 'reading config from strbuf' '
+ cat >expect <<-\EOF &&
+ var: some.value, value: content
+ EOF
+ test-config strbuf 12345:.gitmodules >actual <<-\EOF &&
+ [some]
+ value = content
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'reading config from strbuf with error' '
+ touch expect.out &&
+ cat >expect.err <<-\EOF &&
+ fatal: bad config file line 2 in 12345:.gitmodules
+ EOF
+ test_must_fail test-config strbuf 12345:.gitmodules >actual.out 2>actual.err <<-\EOF &&
+ [some]
+ value = "
+ EOF
+ test_cmp expect.out actual.out &&
+ test_cmp expect.err actual.err
+'
+
test_done
diff --git a/test-config.c b/test-config.c
new file mode 100644
index 0000000..c650837
--- /dev/null
+++ b/test-config.c
@@ -0,0 +1,40 @@
+#include "cache.h"
+
+static int config_strbuf(const char *var, const char *value, void *data)
+{
+ printf("var: %s, value: %s\n", var, value);
+
+ return 1;
+}
+
+static void die_usage(int argc, char **argv)
+{
+ fprintf(stderr, "Usage: %s strbuf <name>\n", argv[0]);
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 3)
+ die_usage(argc, argv);
+
+ if (!strcmp(argv[1], "strbuf")) {
+ struct strbuf buf = STRBUF_INIT;
+ const char *name = argv[2];
+
+ while (strbuf_fread(&buf, 1024, stdin) == 1024)
+ ;
+
+ if (ferror(stdin))
+ die("An error occurred while reading from stdin");
+
+ git_config_from_strbuf(config_strbuf, name, &buf, NULL);
+ strbuf_release(&buf);
+
+ return 0;
+ }
+
+ die_usage(argc, argv);
+
+ return 1;
+}
--
1.8.2.rc0.26.gf7384c5
next prev parent reply other threads:[~2013-03-10 17:01 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-03-10 16:56 [PATCH v2 0/4] allow more sources for config values Heiko Voigt
2013-03-10 16:57 ` [PATCH v2 1/4] config: factor out config file stack management Heiko Voigt
2013-03-12 10:52 ` Jeff King
2013-03-12 15:44 ` Heiko Voigt
2013-03-12 19:04 ` Jeff King
2013-03-14 6:36 ` Heiko Voigt
2013-03-10 16:58 ` [PATCH v2 2/4] config: drop file pointer validity check in get_next_char() Heiko Voigt
2013-03-12 11:00 ` Jeff King
2013-03-12 16:00 ` Heiko Voigt
2013-03-12 16:16 ` Heiko Voigt
2013-03-12 19:26 ` Jeff King
2013-03-12 19:18 ` Jeff King
2013-03-10 16:59 ` [PATCH v2 3/4] config: make parsing stack struct independent from actual data source Heiko Voigt
2013-03-12 11:03 ` Jeff King
2013-03-12 16:27 ` Heiko Voigt
2013-03-12 19:27 ` Jeff King
2013-03-10 17:00 ` Heiko Voigt [this message]
2013-03-12 11:18 ` [PATCH v2 4/4] teach config parsing to read from strbuf Jeff King
2013-03-12 16:42 ` Heiko Voigt
2013-03-12 19:29 ` Jeff King
2013-03-14 6:39 ` Heiko Voigt
2013-03-14 7:10 ` Jeff King
2013-03-14 7:39 ` Jeff King
2013-03-18 14:18 ` 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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130310170052.GE1136@sandbox-ub.fritz.box \
--to=hvoigt@hvoigt.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jens.lehmann@web.de \
--cc=peff@peff.net \
--cc=ramsay@ramsay1.demon.co.uk \
/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.
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 NNTP newsgroup(s).