]> git.proxmox.com Git - libgit2.git/blob - examples/add.c
Move statement after declarations in add example.
[libgit2.git] / examples / add.c
1 #include <git2.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <assert.h>
5
6 enum print_options {
7 SKIP = 1,
8 VERBOSE = 2,
9 UPDATE = 4,
10 };
11
12 struct print_payload {
13 enum print_options options;
14 git_repository *repo;
15 };
16
17 void init_array(git_strarray *array, int argc, char **argv)
18 {
19 unsigned int i;
20
21 array->count = argc;
22 array->strings = malloc(sizeof(char*) * array->count);
23 assert(array->strings!=NULL);
24
25 for(i=0; i<array->count; i++) {
26 array->strings[i]=argv[i];
27 }
28
29 return;
30 }
31
32 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
33 {
34 struct print_payload p = *(struct print_payload*)(payload);
35 int ret;
36 git_status_t status;
37 (void)matched_pathspec;
38
39 if (git_status_file(&status, p.repo, path)) {
40 return -1; //abort
41 }
42
43 if (status & GIT_STATUS_WT_MODIFIED ||
44 status & GIT_STATUS_WT_NEW) {
45 if (p.options & VERBOSE || p.options & SKIP) {
46 printf("add '%s'\n", path);
47 }
48 ret = 0;
49 } else {
50 ret = 1;
51 }
52
53 if(p.options & SKIP) {
54 ret = 1;
55 }
56
57 return ret;
58 }
59
60 void print_usage(void)
61 {
62 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
63 fprintf(stderr, "\t-n, --dry-run dry run\n");
64 fprintf(stderr, "\t-v, --verbose be verbose\n");
65 fprintf(stderr, "\t-u, --update update tracked files\n");
66 }
67
68
69 int main (int argc, char** argv)
70 {
71 git_index_matched_path_cb matched_cb = NULL;
72 git_repository *repo = NULL;
73 git_index *index;
74 git_strarray array = {0};
75 int i, options = 0;
76 struct print_payload payload = {0};
77
78 for (i = 1; i < argc; ++i) {
79 if (argv[i][0] != '-') {
80 break;
81 }
82 else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
83 options |= VERBOSE;
84 }
85 else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
86 options |= SKIP;
87 }
88 else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
89 options |= UPDATE;
90 }
91 else if(!strcmp(argv[i], "-h")) {
92 print_usage();
93 break;
94 }
95 else if(!strcmp(argv[i], "--")) {
96 i++;
97 break;
98 }
99 else {
100 fprintf(stderr, "Unsupported option %s.\n", argv[i]);
101 print_usage();
102 return 1;
103 }
104 }
105
106 if (argc<=i) {
107 print_usage();
108 return 1;
109 }
110
111 init_array(&array, argc-i, argv+i);
112
113 if (git_repository_open(&repo, ".") < 0) {
114 fprintf(stderr, "No git repository\n");
115 return 1;
116 }
117
118 if (git_repository_index(&index, repo) < 0) {
119 fprintf(stderr, "Could not open repository index\n");
120 return 1;
121 }
122
123 matched_cb = &print_matched_cb;
124
125 payload.options = options;
126 payload.repo = repo;
127
128 if (options&UPDATE) {
129 git_index_update_all(index, &array, matched_cb, &payload);
130 } else {
131 git_index_add_all(index, &array, 0, matched_cb, &payload);
132 }
133
134 git_index_write(index);
135 git_index_free(index);
136 git_repository_free(repo);
137
138 return 0;
139 }