[PATCH v3 3/3] test-config: add usage examples for non-callback query functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 .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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]