Hash :
7562422a
Author :
Date :
2019-01-24T10:30:58
examples: merge common network code Right now, we have two sets of "common" code, one containing general common code and one containing network common code. As we intend to get rid of the network subdirectory and instead merge all examples into a single standalone executable, this distinction doesn't make a lot of sense now. Furthermore, the common network code is not that big. Let's get rid of the common network code by merging it into the general common code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include "../common.h"
/* This part is not strictly libgit2-dependent, but you can use this
* as a starting point for a git-like tool */
typedef int (*git_cb)(git_repository *, int , char **);
struct {
char *name;
git_cb fn;
} commands[] = {
{"ls-remote", ls_remote},
{"fetch", fetch},
{"clone", do_clone},
{"index-pack", index_pack},
{ NULL, NULL}
};
static int run_command(git_cb fn, git_repository *repo, struct args_info args)
{
int error;
/* Run the command. If something goes wrong, print the error message to stderr */
error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
if (error < 0) {
if (git_error_last() == NULL)
fprintf(stderr, "Error without message");
else
fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
}
return !!error;
}
int main(int argc, char **argv)
{
int i;
int return_code = 1;
int error;
git_repository *repo;
struct args_info args = ARGS_INFO_INIT;
const char *git_dir = NULL;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
exit(EXIT_FAILURE);
}
git_libgit2_init();
for (args.pos = 1; args.pos < args.argc; ++args.pos) {
char *a = args.argv[args.pos];
if (a[0] != '-') {
/* non-arg */
break;
} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
continue;
} else if (!strcmp(a, "--")) {
/* arg separator */
break;
}
}
/* Before running the actual command, create an instance of the local
* repository and pass it to the function. */
error = git_repository_open(&repo, git_dir);
if (error < 0)
repo = NULL;
for (i = 0; commands[i].name != NULL; ++i) {
if (!strcmp(args.argv[args.pos], commands[i].name)) {
return_code = run_command(commands[i].fn, repo, args);
goto shutdown;
}
}
fprintf(stderr, "Command not found: %s\n", argv[1]);
shutdown:
git_repository_free(repo);
git_libgit2_shutdown();
return return_code;
}