2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
17 struct print_payload
{
18 enum print_options options
;
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
);
27 int main (int argc
, char** argv
)
29 git_index_matched_path_cb matched_cb
= NULL
;
30 git_repository
*repo
= NULL
;
32 git_strarray array
= {0};
33 int options
= 0, count
= 0;
34 struct print_payload payload
= {0};
38 parse_opts(&options
, &count
, argc
, argv
);
40 init_array(&array
, argc
-count
, argv
+count
);
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
);
45 if (options
&VERBOSE
|| options
&SKIP
) {
46 matched_cb
= &print_matched_cb
;
49 payload
.options
= options
;
53 git_index_update_all(index
, &array
, matched_cb
, &payload
);
55 git_index_add_all(index
, &array
, 0, matched_cb
, &payload
);
58 git_index_write(index
);
59 git_index_free(index
);
60 git_repository_free(repo
);
62 git_threads_shutdown();
67 int print_matched_cb(const char *path
, const char *matched_pathspec
, void *payload
)
69 struct print_payload p
= *(struct print_payload
*)(payload
);
72 (void)matched_pathspec
;
74 if (git_status_file(&status
, p
.repo
, path
)) {
78 if (status
& GIT_STATUS_WT_MODIFIED
||
79 status
& GIT_STATUS_WT_NEW
) {
80 printf("add '%s'\n", path
);
86 if(p
.options
& SKIP
) {
93 void init_array(git_strarray
*array
, int argc
, char **argv
)
98 array
->strings
= malloc(sizeof(char*) * array
->count
);
99 assert(array
->strings
!=NULL
);
101 for(i
=0; i
<array
->count
; i
++) {
102 array
->strings
[i
]=argv
[i
];
108 void print_usage(void)
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");
117 static void parse_opts(int *options
, int *count
, int argc
, char *argv
[])
121 for (i
= 1; i
< argc
; ++i
) {
122 if (argv
[i
][0] != '-') {
125 else if(!strcmp(argv
[i
], "--verbose") || !strcmp(argv
[i
], "-v")) {
128 else if(!strcmp(argv
[i
], "--dry-run") || !strcmp(argv
[i
], "-n")) {
131 else if(!strcmp(argv
[i
], "--update") || !strcmp(argv
[i
], "-u")) {
134 else if(!strcmp(argv
[i
], "-h")) {
138 else if(!strcmp(argv
[i
], "--")) {
143 fprintf(stderr
, "Unsupported option %s.\n", argv
[i
]);