-
Notifications
You must be signed in to change notification settings - Fork 1
/
git2.c
54 lines (42 loc) · 1.02 KB
/
git2.c
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
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
struct {
char *name;
git_cb fn;
} commands[] = {
{"ls-remote", ls_remote},
// {"parse-pkt-line", parse_pkt_line},
// {"show-remote", show_remote},
{"fetch", fetch},
{"index-pack", index_pack},
{"diff-tree", diff_tree},
{ NULL, NULL}
};
int run_command(git_cb fn, int argc, char **argv)
{
int error;
git_repository *repo;
error = git_repository_open(&repo, ".git");
if (error < GIT_SUCCESS)
repo = NULL;
error = fn(repo, argc, argv);
if (error < GIT_SUCCESS)
fprintf(stderr, "booh:\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]))
return !!run_command(commands[i].fn, --argc, ++argv);
}
fprintf(stderr, "Command not found: %s\n", argv[1]);
return EXIT_FAILURE;
}