]> git.proxmox.com Git - libgit2.git/blob - examples/add.c
e5849892e73aad8e8941a14b6294997670ad9431
[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 unsigned status;
79 (void)matched_pathspec;
80
81 if (git_status_file(&status, p.repo, path)) {
82 return -1;
83 }
84
85 if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) {
86 printf("add '%s'\n", path);
87 ret = 0;
88 } else {
89 ret = 1;
90 }
91
92 if(p.options & SKIP) {
93 ret = 1;
94 }
95
96 return ret;
97 }
98
99 void init_array(git_strarray *array, int argc, char **argv)
100 {
101 unsigned int i;
102
103 array->count = argc;
104 array->strings = malloc(sizeof(char*) * array->count);
105 assert(array->strings!=NULL);
106
107 for(i=0; i<array->count; i++) {
108 array->strings[i]=argv[i];
109 }
110
111 return;
112 }
113
114 void print_usage(void)
115 {
116 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
117 fprintf(stderr, "\t-n, --dry-run dry run\n");
118 fprintf(stderr, "\t-v, --verbose be verbose\n");
119 fprintf(stderr, "\t-u, --update update tracked files\n");
120 exit(1);
121 }
122
123 static void parse_opts(int *options, int *count, int argc, char *argv[])
124 {
125 int i;
126
127 for (i = 1; i < argc; ++i) {
128 if (argv[i][0] != '-') {
129 break;
130 }
131 else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
132 *options |= VERBOSE;
133 }
134 else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
135 *options |= SKIP;
136 }
137 else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
138 *options |= UPDATE;
139 }
140 else if(!strcmp(argv[i], "-h")) {
141 print_usage();
142 break;
143 }
144 else if(!strcmp(argv[i], "--")) {
145 i++;
146 break;
147 }
148 else {
149 fprintf(stderr, "Unsupported option %s.\n", argv[i]);
150 print_usage();
151 }
152 }
153
154 if (argc<=i)
155 print_usage();
156
157 *count = i;
158 }