From: Tanay Abhra <tanayabh@gmail.com>
To: git@vger.kernel.org
Cc: Tanay Abhra <tanayabh@gmail.com>,
Ramkumar Ramachandra <artagnon@gmail.com>,
Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
Subject: [PATCH v3 3/3] test-config: add usage examples for non-callback query functions
Date: Mon, 23 Jun 2014 03:11:40 -0700 [thread overview]
Message-ID: <1403518300-23053-4-git-send-email-tanayabh@gmail.com> (raw)
In-Reply-To: <1403518300-23053-1-git-send-email-tanayabh@gmail.com>
Add different usage examples for 'git_config_get_string' and
`git_config_get_string_multi`. They will serve as documentation
on how to query for config values in a non-callback manner.
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
.gitignore | 1 +
Makefile | 1 +
test-config.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+)
create mode 100644 test-config.c
diff --git a/.gitignore b/.gitignore
index 42294e5..7677533 100644
--- a/.gitignore
+++ b/.gitignore
@@ -177,6 +177,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 07ea105..9544efb 100644
--- a/Makefile
+++ b/Makefile
@@ -549,6 +549,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/test-config.c b/test-config.c
new file mode 100644
index 0000000..ff24cb8
--- /dev/null
+++ b/test-config.c
@@ -0,0 +1,93 @@
+#include "cache.h"
+#include "hashmap.h"
+#include "string-list.h"
+
+/*
+ * This program gives examples on how to use non-callback based query
+ * functions like git_config_get_string & git_config_get_string_multi.
+ *
+ * Reads stdin and prints result of command to stdout:
+ *
+ * print_all -> prints all the key-value pairs contained in the hashmap
+ * also checks if all entries in the hashmap matches with
+ * the content of config files
+ *
+ * get_value -> prints the value with highest priority for the entered key
+ *
+ * get_all_values -> prints all values for the given key in increasing order
+ * of priority
+ * Examples:
+ *
+ * To print the value with highest priority for key "foo.bAr Baz.rock":
+ * test-config get_value "foo.bAr Baz.rock"
+ *
+ */
+
+static const char *v;
+static const struct string_list *strptr;
+static int i;
+static int *flag;
+
+static int config_cache_callback(const char *key, const char *value, void *unused)
+{
+ strptr = git_config_get_string_multi(key);
+ if (strptr) {
+ for (i = 0; i < strptr->nr; i++)
+ {
+ v = strptr->items[i].string;
+ flag = strptr->items[i].util;
+ /* NULL values are flagged as 1 */
+ if (*flag == 1)
+ printf("%s\n", key);
+ else if (*flag == 0)
+ printf("%s=%s\n", key, v);
+ /* key-value pair printed so flag them as done */
+ *flag = -1;
+ }
+ return 0;
+ } else {
+ printf("%s\n", "Config hashmap inconsistent\n");
+ return -1;
+ }
+}
+
+ int main(int argc, char **argv)
+{
+ if (argc == 2 && !strcmp(argv[1], "print_all")) {
+ git_config(config_cache_callback, NULL);
+ return 0;
+ } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
+ /* enter key in canonical form enclosed in quotes */
+ if (!git_config_get_string(argv[2], &v)) {
+ printf("%s\n", v);
+ return 0;
+ } else {
+ printf("%s\n", "Value not found for the entered key\n");
+ return -1;
+ }
+ } else if (argc == 3 && !strcmp(argv[1], "get_all_values")) {
+ /* enter key in canonical form enclosed in quotes */
+ strptr = git_config_get_string_multi(argv[2]);
+ if (strptr) {
+ for (i = 0; i < strptr->nr; i++)
+ {
+ v = strptr->items[i].string;
+ flag = strptr->items[i].util;
+ /* prints NULL values as "-" */
+ if (*flag)
+ printf("%s ", "-");
+ else
+ printf("%s ", v);
+ }
+ printf("\n");
+ return 0;
+ } else {
+ printf("%s\n", "Value not found for the entered key\n");
+ return -1;
+ }
+ }
+
+ fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
+ argv[1] ? argv[1] : "(there was none)");
+ return 1;
+}
--
1.9.0.GIT
next prev parent reply other threads:[~2014-06-23 10:13 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-23 10:11 [PATCH v3 0/3] git config cache & special querying api utilizing the cache Tanay Abhra
2014-06-23 10:11 ` [PATCH v3 1/3] string-list: add string_list initialiser helper functions Tanay Abhra
2014-06-23 12:36 ` Torsten Bögershausen
2014-06-23 13:19 ` Tanay Abhra
2014-06-23 10:11 ` [PATCH v3 2/3] config: add hashtable for config parsing & retrieval Tanay Abhra
2014-06-23 11:55 ` Matthieu Moy
2014-06-24 12:06 ` Tanay Abhra
2014-06-25 20:25 ` Karsten Blees
2014-06-23 14:57 ` Ramsay Jones
2014-06-23 16:20 ` Tanay Abhra
2014-06-24 15:32 ` Ramsay Jones
2014-06-26 16:15 ` Matthieu Moy
2014-06-23 23:25 ` Junio C Hamano
2014-06-24 7:23 ` Tanay Abhra
2014-06-25 18:21 ` Junio C Hamano
2014-06-24 7:25 ` Tanay Abhra
2014-06-24 15:57 ` Ramsay Jones
2014-06-25 18:13 ` Junio C Hamano
2014-06-25 20:23 ` Karsten Blees
2014-06-25 20:53 ` Junio C Hamano
2014-06-26 17:37 ` Matthieu Moy
2014-06-26 19:00 ` Junio C Hamano
2014-06-26 19:19 ` Karsten Blees
2014-06-26 21:21 ` Junio C Hamano
2014-06-27 8:19 ` Karsten Blees
2014-06-27 8:19 ` Matthieu Moy
2014-06-27 17:13 ` Junio C Hamano
2014-06-23 23:14 ` Junio C Hamano
2014-06-24 12:21 ` Tanay Abhra
2014-06-26 16:27 ` Matthieu Moy
2014-06-25 21:44 ` Karsten Blees
2014-06-26 16:43 ` Matthieu Moy
2014-06-23 10:11 ` Tanay Abhra [this message]
2014-06-25 11:19 ` [PATCH v3 3/3] test-config: add usage examples for non-callback query functions Eric Sunshine
2014-06-26 8:40 ` Tanay Abhra
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=1403518300-23053-4-git-send-email-tanayabh@gmail.com \
--to=tanayabh@gmail.com \
--cc=Matthieu.Moy@grenoble-inp.fr \
--cc=artagnon@gmail.com \
--cc=git@vger.kernel.org \
/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).