diff options
author | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-10-03 00:33:13 +0200 |
---|---|---|
committer | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-10-03 02:32:32 +0200 |
commit | 2401262778fa50cea30d1988cec45dcb82b50712 (patch) | |
tree | 80adb600f2bb02c941314dc725706827890e07c9 /examples/network/git2.c | |
parent | cd19ca9584bd01925e05e94e7f3bddae6880acda (diff) | |
download | libgit2-2401262778fa50cea30d1988cec45dcb82b50712.tar.gz |
examples: add ls-remote, fetch and index-pack examples
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
Diffstat (limited to 'examples/network/git2.c')
-rw-r--r-- | examples/network/git2.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/network/git2.c b/examples/network/git2.c new file mode 100644 index 000000000..0468c8ace --- /dev/null +++ b/examples/network/git2.c @@ -0,0 +1,57 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "common.h" + +// This part is not strictly libgit2-dependent, but you can use this +// as a starting point for a git-like tool + +struct { + char *name; + git_cb fn; +} commands[] = { + {"ls-remote", ls_remote}, + {"fetch", fetch}, + {"index-pack", index_pack}, + { NULL, NULL} +}; + +int run_command(git_cb fn, int argc, char **argv) +{ + int error; + git_repository *repo; + +// Before running the actual command, create an instance of the local +// repository and pass it to the function. + + error = git_repository_open(&repo, ".git"); + if (error < GIT_SUCCESS) + repo = NULL; + + // Run the command. If something goes wrong, print the error message to stderr + error = fn(repo, argc, argv); + if (error < GIT_SUCCESS) + fprintf(stderr, "Bad news:\n %s\n", git_lasterror()); + + if(repo) + git_repository_free(repo); + + return !!error; +} + +int main(int argc, char **argv) +{ + int i, error; + + if (argc < 2) { + fprintf(stderr, "usage: %s <cmd> [repo]", argv[0]); + } + + for (i = 0; commands[i].name != NULL; ++i) { + if (!strcmp(argv[1], commands[i].name)) + return run_command(commands[i].fn, --argc, ++argv); + } + + fprintf(stderr, "Command not found: %s\n", argv[1]); + +} |