Commit 8ee3d39afd7dbcfed470f4b315b16d8ad62b7374

Patrick Steinhardt 2019-06-27T09:18:19

examples: implement config example Implement a new example that resembles git-config(1). Right now, this example can both read and set configuration keys, only.

diff --git a/examples/common.h b/examples/common.h
index c1d444e..724a5c0 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -39,6 +39,7 @@ extern int lg2_blame(git_repository *repo, int argc, char **argv);
 extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
 extern int lg2_checkout(git_repository *repo, int argc, char **argv);
 extern int lg2_clone(git_repository *repo, int argc, char **argv);
+extern int lg2_config(git_repository *repo, int argc, char **argv);
 extern int lg2_describe(git_repository *repo, int argc, char **argv);
 extern int lg2_diff(git_repository *repo, int argc, char **argv);
 extern int lg2_fetch(git_repository *repo, int argc, char **argv);
diff --git a/examples/config.c b/examples/config.c
new file mode 100644
index 0000000..f7fa70e
--- /dev/null
+++ b/examples/config.c
@@ -0,0 +1,62 @@
+/*
+ * libgit2 "config" example - shows how to use the config API
+ *
+ * Written by the libgit2 contributors
+ *
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * You should have received a copy of the CC0 Public Domain Dedication along
+ * with this software. If not, see
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
+ */
+
+#include "common.h"
+
+static int config_get(git_config *cfg, const char *key)
+{
+	git_config_entry *entry;
+	int error;
+
+	if ((error = git_config_get_entry(&entry, cfg, key)) < 0) {
+		if (error != GIT_ENOTFOUND)
+			printf("Unable to get configuration: %s\n", git_error_last()->message);
+		return 1;
+	}
+
+	puts(entry->value);
+	return 0;
+}
+
+static int config_set(git_config *cfg, const char *key, const char *value)
+{
+	if (git_config_set_string(cfg, key, value) < 0) {
+		printf("Unable to set configuration: %s\n", git_error_last()->message);
+		return 1;
+	}
+	return 0;
+}
+
+int lg2_config(git_repository *repo, int argc, char **argv)
+{
+	git_config *cfg;
+	int error;
+
+	if ((error = git_repository_config(&cfg, repo)) < 0) {
+		printf("Unable to obtain repository config: %s\n", git_error_last()->message);
+		goto out;
+	}
+
+	if (argc == 2) {
+		error = config_get(cfg, argv[1]);
+	} else if (argc == 3) {
+		error = config_set(cfg, argv[1], argv[2]);
+	} else {
+		printf("USAGE: %s config <KEY> [<VALUE>]\n", argv[0]);
+		error = 1;
+	}
+
+out:
+	return error;
+}
diff --git a/examples/lg2.c b/examples/lg2.c
index b3df02d..4ee0110 100644
--- a/examples/lg2.c
+++ b/examples/lg2.c
@@ -15,6 +15,7 @@ struct {
 	{ "cat-file",     lg2_cat_file,     1 },
 	{ "checkout",     lg2_checkout,     1 },
 	{ "clone",        lg2_clone,        0 },
+	{ "config",       lg2_config,       1 },
 	{ "describe",     lg2_describe,     1 },
 	{ "diff",         lg2_diff,         1 },
 	{ "fetch",        lg2_fetch,        1 },