Commit 3f64a9db by Carson Howard Committed by Carson Howard

examples: ls-files: use git_array_t to handle files

parent 37cbc3ea
...@@ -34,8 +34,7 @@ ...@@ -34,8 +34,7 @@
typedef struct { typedef struct {
int error_unmatch; int error_unmatch;
char **files; git_array_t(char *) files;
int file_count;
} ls_options; } ls_options;
/* Print a usage message for the program. */ /* Print a usage message for the program. */
...@@ -53,9 +52,10 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) ...@@ -53,9 +52,10 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
{ {
int parsing_files = 0; int parsing_files = 0;
struct args_info args = ARGS_INFO_INIT; struct args_info args = ARGS_INFO_INIT;
git_array_t(char *) files = GIT_ARRAY_INIT; char **file;
memset(opts, 0, sizeof(ls_options)); memset(opts, 0, sizeof(ls_options));
git_array_init(opts->files);
if (argc < 2) if (argc < 2)
return 0; return 0;
...@@ -67,10 +67,9 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) ...@@ -67,10 +67,9 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
if (a[0] != '-') { if (a[0] != '-') {
parsing_files = 1; parsing_files = 1;
opts->files = git_array_alloc(files); file = git_array_alloc(opts->files);
GITERR_CHECK_ALLOC(opts->files); GITERR_CHECK_ALLOC(file);
*file = a;
opts->files[opts->file_count++] = a;
} else if (!strcmp(a, "--")) { } else if (!strcmp(a, "--")) {
parsing_files = 1; parsing_files = 1;
} else if (!strcmp(a, "--error-unmatch") && !parsing_files) { } else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
...@@ -86,12 +85,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) ...@@ -86,12 +85,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
static int print_paths(ls_options *opts, git_index *index) static int print_paths(ls_options *opts, git_index *index)
{ {
int i; size_t i;
const git_index_entry *entry; const git_index_entry *entry;
/* loop through the files found in the args and print them if they exist */ /* loop through the files found in the args and print them if they exist */
for (i = 0; i < opts->file_count; i++) { for (i = 0; i < git_array_size(opts->files); ++i) {
const char *path = opts->files[i]; const char *path = *(char **)git_array_get(opts->files, i);
entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL); entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL);
if (!entry && opts->error_unmatch) { if (!entry && opts->error_unmatch) {
...@@ -128,7 +127,7 @@ int main(int argc, char *argv[]) ...@@ -128,7 +127,7 @@ int main(int argc, char *argv[])
goto cleanup; goto cleanup;
/* if there are files explicitly listed by the user, we need to treat this command differently */ /* if there are files explicitly listed by the user, we need to treat this command differently */
if (opts.file_count > 0) { if (git_array_size(opts.files) > 0) {
error = print_paths(&opts, index); error = print_paths(&opts, index);
goto cleanup; goto cleanup;
} }
...@@ -144,6 +143,7 @@ int main(int argc, char *argv[]) ...@@ -144,6 +143,7 @@ int main(int argc, char *argv[])
cleanup: cleanup:
/* free our allocated resources */ /* free our allocated resources */
git_array_clear(opts.files);
git_index_free(index); git_index_free(index);
git_repository_free(repo); git_repository_free(repo);
git_libgit2_shutdown(); git_libgit2_shutdown();
......
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