]> git.proxmox.com Git - libgit2.git/blob - examples/add.c
Merge branch 'development' into blame_rebased
[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 printf("add '%s'\n", path);
46 ret = 0;
47 } else {
48 ret = 1;
49 }
50
51 if(p.options & SKIP) {
52 ret = 1;
53 }
54
55 return ret;
56 }
57
58 void print_usage(void)
59 {
60 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
61 fprintf(stderr, "\t-n, --dry-run dry run\n");
62 fprintf(stderr, "\t-v, --verbose be verbose\n");
63 fprintf(stderr, "\t-u, --update update tracked files\n");
64 }
65
66
67 int main (int argc, char** argv)
68 {
69 git_index_matched_path_cb matched_cb = NULL;
70 git_repository *repo = NULL;
71 git_index *index;
72 git_strarray array = {0};
73 int i, options = 0;
74 struct print_payload payload = {0};
75
76 for (i = 1; i < argc; ++i) {
77 if (argv[i][0] != '-') {
78 break;
79 }
80 else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
81 options |= VERBOSE;
82 }
83 else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
84 options |= SKIP;
85 }
86 else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
87 options |= UPDATE;
88 }
89 else if(!strcmp(argv[i], "-h")) {
90 print_usage();
91 break;
92 }
93 else if(!strcmp(argv[i], "--")) {
94 i++;
95 break;
96 }
97 else {
98 fprintf(stderr, "Unsupported option %s.\n", argv[i]);
99 print_usage();
100 return 1;
101 }
102 }
103
104 if (argc<=i) {
105 print_usage();
106 return 1;
107 }
108
109 git_threads_init();
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 if (options&VERBOSE || options&SKIP) {
124 matched_cb = &print_matched_cb;
125 }
126
127 payload.options = options;
128 payload.repo = repo;
129
130 if (options&UPDATE) {
131 git_index_update_all(index, &array, matched_cb, &payload);
132 } else {
133 git_index_add_all(index, &array, 0, matched_cb, &payload);
134 }
135
136 git_index_write(index);
137 git_index_free(index);
138 git_repository_free(repo);
139
140 git_threads_shutdown();
141
142 return 0;
143 }