|
| 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 | +} |
0 commit comments