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