]> git.proxmox.com Git - libgit2.git/blob - examples/lg2.c
Update d/ch for 0.99.0+dfsg.1-1 experimental release
[libgit2.git] / examples / lg2.c
1 #include "common.h"
2
3 /* This part is not strictly libgit2-dependent, but you can use this
4 * as a starting point for a git-like tool */
5
6 typedef int (*git_command_fn)(git_repository *, int , char **);
7
8 struct {
9 char *name;
10 git_command_fn fn;
11 char requires_repo;
12 } commands[] = {
13 { "add", lg2_add, 1 },
14 { "blame", lg2_blame, 1 },
15 { "cat-file", lg2_cat_file, 1 },
16 { "checkout", lg2_checkout, 1 },
17 { "clone", lg2_clone, 0 },
18 { "config", lg2_config, 1 },
19 { "describe", lg2_describe, 1 },
20 { "diff", lg2_diff, 1 },
21 { "fetch", lg2_fetch, 1 },
22 { "for-each-ref", lg2_for_each_ref, 1 },
23 { "general", lg2_general, 0 },
24 { "index-pack", lg2_index_pack, 1 },
25 { "init", lg2_init, 0 },
26 { "log", lg2_log, 1 },
27 { "ls-files", lg2_ls_files, 1 },
28 { "ls-remote", lg2_ls_remote, 1 },
29 { "merge", lg2_merge, 1 },
30 { "remote", lg2_remote, 1 },
31 { "rev-list", lg2_rev_list, 1 },
32 { "rev-parse", lg2_rev_parse, 1 },
33 { "show-index", lg2_show_index, 0 },
34 { "stash", lg2_stash, 1 },
35 { "status", lg2_status, 1 },
36 { "tag", lg2_tag, 1 },
37 };
38
39 static int run_command(git_command_fn fn, git_repository *repo, struct args_info args)
40 {
41 int error;
42
43 /* Run the command. If something goes wrong, print the error message to stderr */
44 error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
45 if (error < 0) {
46 if (git_error_last() == NULL)
47 fprintf(stderr, "Error without message");
48 else
49 fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
50 }
51
52 return !!error;
53 }
54
55 static int usage(const char *prog)
56 {
57 size_t i;
58
59 fprintf(stderr, "usage: %s <cmd>...\n\nAvailable commands:\n\n", prog);
60 for (i = 0; i < ARRAY_SIZE(commands); i++)
61 fprintf(stderr, "\t%s\n", commands[i].name);
62
63 exit(EXIT_FAILURE);
64 }
65
66 int main(int argc, char **argv)
67 {
68 struct args_info args = ARGS_INFO_INIT;
69 git_repository *repo = NULL;
70 const char *git_dir = NULL;
71 int return_code = 1;
72 size_t i;
73
74 if (argc < 2)
75 usage(argv[0]);
76
77 git_libgit2_init();
78
79 for (args.pos = 1; args.pos < args.argc; ++args.pos) {
80 char *a = args.argv[args.pos];
81
82 if (a[0] != '-') {
83 /* non-arg */
84 break;
85 } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
86 continue;
87 } else if (match_arg_separator(&args)) {
88 break;
89 }
90 }
91
92 if (args.pos == args.argc)
93 usage(argv[0]);
94
95 if (!git_dir)
96 git_dir = ".";
97
98 for (i = 0; i < ARRAY_SIZE(commands); ++i) {
99 if (strcmp(args.argv[args.pos], commands[i].name))
100 continue;
101
102 /*
103 * Before running the actual command, create an instance
104 * of the local repository and pass it to the function.
105 * */
106 if (commands[i].requires_repo) {
107 check_lg2(git_repository_open_ext(&repo, git_dir, 0, NULL),
108 "Unable to open repository '%s'", git_dir);
109 }
110
111 return_code = run_command(commands[i].fn, repo, args);
112 goto shutdown;
113 }
114
115 fprintf(stderr, "Command not found: %s\n", argv[1]);
116
117 shutdown:
118 git_repository_free(repo);
119 git_libgit2_shutdown();
120
121 return return_code;
122 }