]> git.proxmox.com Git - libgit2.git/blob - examples/add.c
Rename git_threads_ to git_libgit2_
[libgit2.git] / examples / add.c
1 /*
2 * libgit2 "add" example - shows how to modify the index
3 *
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14
15 #include "common.h"
16 #include <assert.h>
17
18 enum print_options {
19 SKIP = 1,
20 VERBOSE = 2,
21 UPDATE = 4,
22 };
23
24 struct print_payload {
25 enum print_options options;
26 git_repository *repo;
27 };
28
29 /* Forward declarations for helpers */
30 static void parse_opts(int *options, int *count, int argc, char *argv[]);
31 void init_array(git_strarray *array, int argc, char **argv);
32 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
33
34 int main (int argc, char** argv)
35 {
36 git_index_matched_path_cb matched_cb = NULL;
37 git_repository *repo = NULL;
38 git_index *index;
39 git_strarray array = {0};
40 int options = 0, count = 0;
41 struct print_payload payload = {0};
42
43 git_libgit2_init();
44
45 parse_opts(&options, &count, argc, argv);
46
47 init_array(&array, argc-count, argv+count);
48
49 check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
50 check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
51
52 if (options&VERBOSE || options&SKIP) {
53 matched_cb = &print_matched_cb;
54 }
55
56 payload.options = options;
57 payload.repo = repo;
58
59 if (options&UPDATE) {
60 git_index_update_all(index, &array, matched_cb, &payload);
61 } else {
62 git_index_add_all(index, &array, 0, matched_cb, &payload);
63 }
64
65 git_index_write(index);
66 git_index_free(index);
67 git_repository_free(repo);
68
69 git_libgit2_shutdown();
70
71 return 0;
72 }
73
74 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
75 {
76 struct print_payload p = *(struct print_payload*)(payload);
77 int ret;
78 git_status_t status;
79 (void)matched_pathspec;
80
81 if (git_status_file((unsigned int*)(&status), p.repo, path)) {
82 return -1; //abort
83 }
84
85 if (status & GIT_STATUS_WT_MODIFIED ||
86 status & GIT_STATUS_WT_NEW) {
87 printf("add '%s'\n", path);
88 ret = 0;
89 } else {
90 ret = 1;
91 }
92
93 if(p.options & SKIP) {
94 ret = 1;
95 }
96
97 return ret;
98 }
99
100 void init_array(git_strarray *array, int argc, char **argv)
101 {
102 unsigned int i;
103
104 array->count = argc;
105 array->strings = malloc(sizeof(char*) * array->count);
106 assert(array->strings!=NULL);
107
108 for(i=0; i<array->count; i++) {
109 array->strings[i]=argv[i];
110 }
111
112 return;
113 }
114
115 void print_usage(void)
116 {
117 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
118 fprintf(stderr, "\t-n, --dry-run dry run\n");
119 fprintf(stderr, "\t-v, --verbose be verbose\n");
120 fprintf(stderr, "\t-u, --update update tracked files\n");
121 exit(1);
122 }
123
124 static void parse_opts(int *options, int *count, int argc, char *argv[])
125 {
126 int i;
127
128 for (i = 1; i < argc; ++i) {
129 if (argv[i][0] != '-') {
130 break;
131 }
132 else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
133 *options |= VERBOSE;
134 }
135 else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
136 *options |= SKIP;
137 }
138 else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
139 *options |= UPDATE;
140 }
141 else if(!strcmp(argv[i], "-h")) {
142 print_usage();
143 break;
144 }
145 else if(!strcmp(argv[i], "--")) {
146 i++;
147 break;
148 }
149 else {
150 fprintf(stderr, "Unsupported option %s.\n", argv[i]);
151 print_usage();
152 }
153 }
154
155 if (argc<=i)
156 print_usage();
157
158 *count = i;
159 }