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 if (p
.options
& VERBOSE
|| p
.options
& SKIP
) {
46 printf("add '%s'\n", path
);
53 if(p
.options
& SKIP
) {
60 void print_usage(void)
62 fprintf(stderr
, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
63 fprintf(stderr
, "\t-n, --dry-run dry run\n");
64 fprintf(stderr
, "\t-v, --verbose be verbose\n");
65 fprintf(stderr
, "\t-u, --update update tracked files\n");
69 int main (int argc
, char** argv
)
71 git_index_matched_path_cb matched_cb
= NULL
;
72 git_repository
*repo
= NULL
;
74 git_strarray array
= {0};
76 struct print_payload payload
= {0};
78 for (i
= 1; i
< argc
; ++i
) {
79 if (argv
[i
][0] != '-') {
82 else if(!strcmp(argv
[i
], "--verbose") || !strcmp(argv
[i
], "-v")) {
85 else if(!strcmp(argv
[i
], "--dry-run") || !strcmp(argv
[i
], "-n")) {
88 else if(!strcmp(argv
[i
], "--update") || !strcmp(argv
[i
], "-u")) {
91 else if(!strcmp(argv
[i
], "-h")) {
95 else if(!strcmp(argv
[i
], "--")) {
100 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 matched_cb
= &print_matched_cb
;
125 payload
.options
= options
;
128 if (options
&UPDATE
) {
129 git_index_update_all(index
, &array
, matched_cb
, &payload
);
131 git_index_add_all(index
, &array
, 0, matched_cb
, &payload
);
134 git_index_write(index
);
135 git_index_free(index
);
136 git_repository_free(repo
);