12 struct print_payload
{
13 enum print_options options
;
17 void init_array(git_strarray
*array
, int argc
, char **argv
)
22 array
->strings
= malloc(sizeof(char*) * array
->count
);
23 assert(array
->strings
!=NULL
);
25 for(i
=0; i
<array
->count
; i
++) {
26 array
->strings
[i
]=argv
[i
];
32 int print_matched_cb(const char *path
, const char *matched_pathspec
, void *payload
)
34 struct print_payload p
= *(struct print_payload
*)(payload
);
37 (void)matched_pathspec
;
39 if (git_status_file(&status
, p
.repo
, path
)) {
43 if (status
& GIT_STATUS_WT_MODIFIED
||
44 status
& GIT_STATUS_WT_NEW
) {
45 printf("add '%s'\n", path
);
51 if(p
.options
& SKIP
) {
58 void print_usage(void)
60 fprintf(stderr
, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
61 fprintf(stderr
, "\t-n, --dry-run dry run\n");
62 fprintf(stderr
, "\t-v, --verbose be verbose\n");
63 fprintf(stderr
, "\t-u, --update update tracked files\n");
67 int main (int argc
, char** argv
)
69 git_index_matched_path_cb matched_cb
= NULL
;
70 git_repository
*repo
= NULL
;
72 git_strarray array
= {0};
74 struct print_payload payload
= {0};
76 for (i
= 1; i
< argc
; ++i
) {
77 if (argv
[i
][0] != '-') {
80 else if(!strcmp(argv
[i
], "--verbose") || !strcmp(argv
[i
], "-v")) {
83 else if(!strcmp(argv
[i
], "--dry-run") || !strcmp(argv
[i
], "-n")) {
86 else if(!strcmp(argv
[i
], "--update") || !strcmp(argv
[i
], "-u")) {
89 else if(!strcmp(argv
[i
], "-h")) {
93 else if(!strcmp(argv
[i
], "--")) {
98 fprintf(stderr
, "Unsupported option %s.\n", argv
[i
]);
111 init_array(&array
, argc
-i
, argv
+i
);
113 if (git_repository_open(&repo
, ".") < 0) {
114 fprintf(stderr
, "No git repository\n");
118 if (git_repository_index(&index
, repo
) < 0) {
119 fprintf(stderr
, "Could not open repository index\n");
123 if (options
&VERBOSE
|| options
&SKIP
) {
124 matched_cb
= &print_matched_cb
;
127 payload
.options
= options
;
130 if (options
&UPDATE
) {
131 git_index_update_all(index
, &array
, matched_cb
, &payload
);
133 git_index_add_all(index
, &array
, 0, matched_cb
, &payload
);
136 git_index_write(index
);
137 git_index_free(index
);
138 git_repository_free(repo
);
140 git_threads_shutdown();