Blame examples/network/git2.c

Packit Service 20376f
#include <stdlib.h>
Packit Service 20376f
#include <stdio.h>
Packit Service 20376f
#include <string.h>
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
// This part is not strictly libgit2-dependent, but you can use this
Packit Service 20376f
// as a starting point for a git-like tool
Packit Service 20376f
Packit Service 20376f
struct {
Packit Service 20376f
	char *name;
Packit Service 20376f
	git_cb fn;
Packit Service 20376f
} commands[] = {
Packit Service 20376f
	{"ls-remote", ls_remote},
Packit Service 20376f
	{"fetch", fetch},
Packit Service 20376f
	{"clone", do_clone},
Packit Service 20376f
	{"index-pack", index_pack},
Packit Service 20376f
	{ NULL, NULL}
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int run_command(git_cb fn, int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	// Before running the actual command, create an instance of the local
Packit Service 20376f
	// repository and pass it to the function.
Packit Service 20376f
Packit Service 20376f
	error = git_repository_open(&repo, ".git");
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		repo = NULL;
Packit Service 20376f
Packit Service 20376f
	// Run the command. If something goes wrong, print the error message to stderr
Packit Service 20376f
	error = fn(repo, argc, argv);
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		if (giterr_last() == NULL)
Packit Service 20376f
			fprintf(stderr, "Error without message");
Packit Service 20376f
		else
Packit Service 20376f
			fprintf(stderr, "Bad news:\n %s\n", giterr_last()->message);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if(repo)
Packit Service 20376f
		git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	return !!error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int main(int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	int return_code = 1;
Packit Service 20376f
Packit Service 20376f
	if (argc < 2) {
Packit Service 20376f
		fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
Packit Service 20376f
		exit(EXIT_FAILURE);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	for (i = 0; commands[i].name != NULL; ++i) {
Packit Service 20376f
		if (!strcmp(argv[1], commands[i].name)) {
Packit Service 20376f
			return_code = run_command(commands[i].fn, --argc, ++argv);
Packit Service 20376f
			goto shutdown;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	fprintf(stderr, "Command not found: %s\n", argv[1]);
Packit Service 20376f
Packit Service 20376f
shutdown:
Packit Service 20376f
	git_libgit2_shutdown();
Packit Service 20376f
Packit Service 20376f
	return return_code;
Packit Service 20376f
}