Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Created April 24, 2017 22:33
Show Gist options
  • Save sentientmonkey/302a1ccceff64e254337eeb78536352c to your computer and use it in GitHub Desktop.
Save sentientmonkey/302a1ccceff64e254337eeb78536352c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "git2.h"
// Build: gcc -o main -lgit2 main.c
void print_error(int error_code, const char *action)
{
const git_error *error = giterr_last();
if (!error_code)
return;
printf("Error %d %s - %s\n", error_code, action,
(error && error->message) ? error->message : "???");
exit(1);
}
int main() {
const char* name = "foo.bar";
const char* value = "baz";
git_libgit2_init();
int error = 0;
git_config *cfg = NULL;
error = git_config_open_default(&cfg);
print_error(error, "open_default");
git_config *global_cfg = NULL;
error = git_config_open_global(&global_cfg, cfg);
print_error(error, "open_global");
error = git_config_set_string(global_cfg, name, value);
print_error(error, "set_string");
// This fails with
// Error -1 set_string - failed to stat '/Users/user/.gitconfig/Users/user/workspace/dotfiles/.gitconfig': Not a directory
// when /Users/user/.gitconfig -> /Users/user/workspace/dotfiles/.gitconfig
git_config *snap_cfg = NULL;
error = git_config_snapshot(&snap_cfg, global_cfg);
print_error(error, "config_snapshot");
const char* result = NULL;
error = git_config_get_string(&value, snap_cfg, name);
print_error(error, "get_string");
printf("foo.bar is %s\n", result);
git_libgit2_shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment