]> git.proxmox.com Git - libgit2.git/blob - examples/add.c
Update d/ch for 0.99.0+dfsg.1-1 experimental release
[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
17 /**
18 * The following example demonstrates how to add files with libgit2.
19 *
20 * It will use the repository in the current working directory, and act
21 * on files passed as its parameters.
22 *
23 * Recognized options are:
24 * -v/--verbose: show the file's status after acting on it.
25 * -n/--dry-run: do not actually change the index.
26 * -u/--update: update the index instead of adding to it.
27 */
28
29 enum index_mode {
30 INDEX_NONE,
31 INDEX_ADD,
32 };
33
34 struct index_options {
35 int dry_run;
36 int verbose;
37 git_repository *repo;
38 enum index_mode mode;
39 int add_update;
40 };
41
42 /* Forward declarations for helpers */
43 static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args);
44 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
45
46 int lg2_add(git_repository *repo, int argc, char **argv)
47 {
48 git_index_matched_path_cb matched_cb = NULL;
49 git_index *index;
50 git_strarray array = {0};
51 struct index_options options;
52 struct args_info args = ARGS_INFO_INIT;
53
54 /* Parse the options & arguments. */
55 parse_opts(NULL, &options, &args);
56 strarray_from_args(&array, &args);
57
58 /* Grab the repository's index. */
59 check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
60
61 /* Setup a callback if the requested options need it */
62 if (options.verbose || options.dry_run) {
63 matched_cb = &print_matched_cb;
64 }
65
66 options.repo = repo;
67
68 /* Perform the requested action with the index and files */
69 if (options.add_update) {
70 git_index_update_all(index, &array, matched_cb, &options);
71 } else {
72 git_index_add_all(index, &array, 0, matched_cb, &options);
73 }
74
75 /* Cleanup memory */
76 git_index_write(index);
77 git_index_free(index);
78
79 return 0;
80 }
81
82 /*
83 * This callback is called for each file under consideration by
84 * git_index_(update|add)_all above.
85 * It makes uses of the callback's ability to abort the action.
86 */
87 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
88 {
89 struct index_options *opts = (struct index_options *)(payload);
90 int ret;
91 unsigned status;
92 (void)matched_pathspec;
93
94 /* Get the file status */
95 if (git_status_file(&status, opts->repo, path) < 0)
96 return -1;
97
98 if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
99 printf("add '%s'\n", path);
100 ret = 0;
101 } else {
102 ret = 1;
103 }
104
105 if (opts->dry_run)
106 ret = 1;
107
108 return ret;
109 }
110
111 void init_array(git_strarray *array, int argc, char **argv)
112 {
113 unsigned int i;
114
115 array->count = argc;
116 array->strings = calloc(array->count, sizeof(char *));
117 assert(array->strings != NULL);
118
119 for (i = 0; i < array->count; i++) {
120 array->strings[i] = argv[i];
121 }
122
123 return;
124 }
125
126 void print_usage(void)
127 {
128 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
129 fprintf(stderr, "\t-n, --dry-run dry run\n");
130 fprintf(stderr, "\t-v, --verbose be verbose\n");
131 fprintf(stderr, "\t-u, --update update tracked files\n");
132 exit(1);
133 }
134
135 static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
136 {
137 if (args->argc <= 1)
138 print_usage();
139
140 for (args->pos = 1; args->pos < args->argc; ++args->pos) {
141 const char *curr = args->argv[args->pos];
142
143 if (curr[0] != '-') {
144 if (!strcmp("add", curr)) {
145 opts->mode = INDEX_ADD;
146 continue;
147 } else if (opts->mode == INDEX_NONE) {
148 fprintf(stderr, "missing command: %s", curr);
149 print_usage();
150 break;
151 } else {
152 /* We might be looking at a filename */
153 break;
154 }
155 } else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
156 match_bool_arg(&opts->dry_run, args, "--dry-run") ||
157 match_str_arg(repo_path, args, "--git-dir") ||
158 (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) {
159 continue;
160 } else if (match_bool_arg(NULL, args, "--help")) {
161 print_usage();
162 break;
163 } else if (match_arg_separator(args)) {
164 break;
165 } else {
166 fprintf(stderr, "Unsupported option %s.\n", curr);
167 print_usage();
168 }
169 }
170 }