Commit d4a593ef by Etienne Samson

examples: modernize add code

parent c9a09b91
...@@ -26,48 +26,48 @@ ...@@ -26,48 +26,48 @@
* -u/--update: update the index instead of adding to it. * -u/--update: update the index instead of adding to it.
*/ */
enum print_options { enum index_mode {
SKIP = 1, INDEX_NONE,
VERBOSE = 2, INDEX_ADD,
UPDATE = 4,
}; };
struct print_payload { struct index_options {
enum print_options options; int dry_run;
int verbose;
git_repository *repo; git_repository *repo;
enum index_mode mode;
int add_update;
}; };
/* Forward declarations for helpers */ /* Forward declarations for helpers */
static void parse_opts(int *options, int *count, int argc, char *argv[]); static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args);
void init_array(git_strarray *array, int argc, char **argv);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload); int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
int lg2_add(git_repository *repo, int argc, char** argv) int lg2_add(git_repository *repo, int argc, char **argv)
{ {
git_index_matched_path_cb matched_cb = NULL; git_index_matched_path_cb matched_cb = NULL;
git_index *index; git_index *index;
git_strarray array = {0}; git_strarray array = {0};
int options = 0, count = 0; struct index_options options;
struct print_payload payload = {0}; struct args_info args = ARGS_INFO_INIT;
parse_opts(&options, &count, argc, argv); parse_opts(NULL, &options, &args);
init_array(&array, argc-count, argv+count); strarray_from_args(&array, &args);
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
/* Setup a callback if the requested options need it */ /* Setup a callback if the requested options need it */
if ((options & VERBOSE) || (options & SKIP)) { if (options.verbose || options.dry_run) {
matched_cb = &print_matched_cb; matched_cb = &print_matched_cb;
} }
/* Perform the requested action with the index and files */ options.repo = repo;
payload.options = options;
payload.repo = repo;
if (options & UPDATE) { /* Perform the requested action with the index and files */
git_index_update_all(index, &array, matched_cb, &payload); if (options.add_update) {
git_index_update_all(index, &array, matched_cb, &options);
} else { } else {
git_index_add_all(index, &array, 0, matched_cb, &payload); git_index_add_all(index, &array, 0, matched_cb, &options);
} }
/* Cleanup memory */ /* Cleanup memory */
...@@ -84,15 +84,14 @@ int lg2_add(git_repository *repo, int argc, char** argv) ...@@ -84,15 +84,14 @@ int lg2_add(git_repository *repo, int argc, char** argv)
*/ */
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload) int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
{ {
struct print_payload p = *(struct print_payload*)(payload); struct index_options options = *(struct index_options *)(payload);
int ret; int ret;
unsigned status; unsigned status;
(void)matched_pathspec; (void)matched_pathspec;
/* Get the file status */ /* Get the file status */
if (git_status_file(&status, p.repo, path)) { if (git_status_file(&status, options.repo, path) < 0)
return -1; return -1;
}
if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) { if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
printf("add '%s'\n", path); printf("add '%s'\n", path);
...@@ -101,9 +100,8 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo ...@@ -101,9 +100,8 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
ret = 1; ret = 1;
} }
if ((p.options & SKIP)) { if (options.dry_run)
ret = 1; ret = 1;
}
return ret; return ret;
} }
...@@ -132,33 +130,39 @@ void print_usage(void) ...@@ -132,33 +130,39 @@ void print_usage(void)
exit(1); exit(1);
} }
static void parse_opts(int *options, int *count, int argc, char *argv[]) static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args)
{ {
int i; if (args->argc <= 1)
print_usage();
for (i = 1; i < argc; ++i) { for (args->pos = 1; args->pos < args->argc; ++args->pos) {
if (argv[i][0] != '-') const char *curr = args->argv[args->pos];
break;
else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) if (curr[0] != '-') {
*options |= VERBOSE; if (!strcmp("add", curr)) {
else if (!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) options->mode = INDEX_ADD;
*options |= SKIP; continue;
else if (!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) } else if (options->mode == INDEX_NONE) {
*options |= UPDATE; fprintf(stderr, "missing command: %s", curr);
else if (!strcmp(argv[i], "-h")) { print_usage();
break;
} else {
/* We might be looking at a filename */
break;
}
} else if (match_bool_arg(&options->verbose, args, "--verbose") ||
match_bool_arg(&options->dry_run, args, "--dry-run") ||
match_str_arg(repo_path, args, "--git-dir") ||
(options->mode == INDEX_ADD && match_bool_arg(&options->add_update, args, "--update"))) {
continue;
} else if (match_bool_arg(NULL, args, "--help")) {
print_usage(); print_usage();
break; break;
} else if (!strcmp(argv[i], "--")) { } else if (match_arg_separator(args)) {
i++;
break; break;
} else { } else {
fprintf(stderr, "Unsupported option %s.\n", argv[i]); fprintf(stderr, "Unsupported option %s.\n", curr);
print_usage(); print_usage();
} }
} }
if (argc <= i)
print_usage();
*count = i;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment