Skip to content

Commit 8ee3d39

Browse files
committed
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.
1 parent df54c7f commit 8ee3d39

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern int lg2_blame(git_repository *repo, int argc, char **argv);
3939
extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
4040
extern int lg2_checkout(git_repository *repo, int argc, char **argv);
4141
extern int lg2_clone(git_repository *repo, int argc, char **argv);
42+
extern int lg2_config(git_repository *repo, int argc, char **argv);
4243
extern int lg2_describe(git_repository *repo, int argc, char **argv);
4344
extern int lg2_diff(git_repository *repo, int argc, char **argv);
4445
extern int lg2_fetch(git_repository *repo, int argc, char **argv);

examples/config.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* libgit2 "config" example - shows how to use the config API
3+
*
4+
* Written by the libgit2 contributors
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all copyright
7+
* and related and neighboring rights to this software to the public domain
8+
* worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
#include "common.h"
16+
17+
static int config_get(git_config *cfg, const char *key)
18+
{
19+
git_config_entry *entry;
20+
int error;
21+
22+
if ((error = git_config_get_entry(&entry, cfg, key)) < 0) {
23+
if (error != GIT_ENOTFOUND)
24+
printf("Unable to get configuration: %s\n", git_error_last()->message);
25+
return 1;
26+
}
27+
28+
puts(entry->value);
29+
return 0;
30+
}
31+
32+
static int config_set(git_config *cfg, const char *key, const char *value)
33+
{
34+
if (git_config_set_string(cfg, key, value) < 0) {
35+
printf("Unable to set configuration: %s\n", git_error_last()->message);
36+
return 1;
37+
}
38+
return 0;
39+
}
40+
41+
int lg2_config(git_repository *repo, int argc, char **argv)
42+
{
43+
git_config *cfg;
44+
int error;
45+
46+
if ((error = git_repository_config(&cfg, repo)) < 0) {
47+
printf("Unable to obtain repository config: %s\n", git_error_last()->message);
48+
goto out;
49+
}
50+
51+
if (argc == 2) {
52+
error = config_get(cfg, argv[1]);
53+
} else if (argc == 3) {
54+
error = config_set(cfg, argv[1], argv[2]);
55+
} else {
56+
printf("USAGE: %s config <KEY> [<VALUE>]\n", argv[0]);
57+
error = 1;
58+
}
59+
60+
out:
61+
return error;
62+
}

examples/lg2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct {
1515
{ "cat-file", lg2_cat_file, 1 },
1616
{ "checkout", lg2_checkout, 1 },
1717
{ "clone", lg2_clone, 0 },
18+
{ "config", lg2_config, 1 },
1819
{ "describe", lg2_describe, 1 },
1920
{ "diff", lg2_diff, 1 },
2021
{ "fetch", lg2_fetch, 1 },

0 commit comments

Comments
 (0)