2 * libgit2 "add" example - shows how to modify the index
4 * Written by the libgit2 contributors
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.
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/>.
24 struct print_payload
{
25 enum print_options options
;
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
);
34 int main (int argc
, char** argv
)
36 git_index_matched_path_cb matched_cb
= NULL
;
37 git_repository
*repo
= NULL
;
39 git_strarray array
= {0};
40 int options
= 0, count
= 0;
41 struct print_payload payload
= {0};
45 parse_opts(&options
, &count
, argc
, argv
);
47 init_array(&array
, argc
-count
, argv
+count
);
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
);
52 if (options
&VERBOSE
|| options
&SKIP
) {
53 matched_cb
= &print_matched_cb
;
56 payload
.options
= options
;
60 git_index_update_all(index
, &array
, matched_cb
, &payload
);
62 git_index_add_all(index
, &array
, 0, matched_cb
, &payload
);
65 git_index_write(index
);
66 git_index_free(index
);
67 git_repository_free(repo
);
69 git_libgit2_shutdown();
74 int print_matched_cb(const char *path
, const char *matched_pathspec
, void *payload
)
76 struct print_payload p
= *(struct print_payload
*)(payload
);
79 (void)matched_pathspec
;
81 if (git_status_file(&status
, p
.repo
, path
)) {
85 if (status
& GIT_STATUS_WT_MODIFIED
|| status
& GIT_STATUS_WT_NEW
) {
86 printf("add '%s'\n", path
);
92 if(p
.options
& SKIP
) {
99 void init_array(git_strarray
*array
, int argc
, char **argv
)
104 array
->strings
= malloc(sizeof(char*) * array
->count
);
105 assert(array
->strings
!=NULL
);
107 for(i
=0; i
<array
->count
; i
++) {
108 array
->strings
[i
]=argv
[i
];
114 void print_usage(void)
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");
123 static void parse_opts(int *options
, int *count
, int argc
, char *argv
[])
127 for (i
= 1; i
< argc
; ++i
) {
128 if (argv
[i
][0] != '-') {
131 else if(!strcmp(argv
[i
], "--verbose") || !strcmp(argv
[i
], "-v")) {
134 else if(!strcmp(argv
[i
], "--dry-run") || !strcmp(argv
[i
], "-n")) {
137 else if(!strcmp(argv
[i
], "--update") || !strcmp(argv
[i
], "-u")) {
140 else if(!strcmp(argv
[i
], "-h")) {
144 else if(!strcmp(argv
[i
], "--")) {
149 fprintf(stderr
, "Unsupported option %s.\n", argv
[i
]);