Skip to content

Commit 3f64a9d

Browse files
Carson HowardCarson Howard
authored andcommitted
examples: ls-files: use git_array_t to handle files
1 parent 37cbc3e commit 3f64a9d

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

examples/ls-files.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
typedef struct {
3636
int error_unmatch;
37-
char **files;
38-
int file_count;
37+
git_array_t(char *) files;
3938
} ls_options;
4039

4140
/* Print a usage message for the program. */
@@ -53,9 +52,10 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
5352
{
5453
int parsing_files = 0;
5554
struct args_info args = ARGS_INFO_INIT;
56-
git_array_t(char *) files = GIT_ARRAY_INIT;
55+
char **file;
5756

5857
memset(opts, 0, sizeof(ls_options));
58+
git_array_init(opts->files);
5959

6060
if (argc < 2)
6161
return 0;
@@ -67,10 +67,9 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
6767
if (a[0] != '-') {
6868
parsing_files = 1;
6969

70-
opts->files = git_array_alloc(files);
71-
GITERR_CHECK_ALLOC(opts->files);
72-
73-
opts->files[opts->file_count++] = a;
70+
file = git_array_alloc(opts->files);
71+
GITERR_CHECK_ALLOC(file);
72+
*file = a;
7473
} else if (!strcmp(a, "--")) {
7574
parsing_files = 1;
7675
} else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
@@ -86,12 +85,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
8685

8786
static int print_paths(ls_options *opts, git_index *index)
8887
{
89-
int i;
88+
size_t i;
9089
const git_index_entry *entry;
9190

9291
/* loop through the files found in the args and print them if they exist */
93-
for (i = 0; i < opts->file_count; i++) {
94-
const char *path = opts->files[i];
92+
for (i = 0; i < git_array_size(opts->files); ++i) {
93+
const char *path = *(char **)git_array_get(opts->files, i);
9594

9695
entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL);
9796
if (!entry && opts->error_unmatch) {
@@ -128,7 +127,7 @@ int main(int argc, char *argv[])
128127
goto cleanup;
129128

130129
/* if there are files explicitly listed by the user, we need to treat this command differently */
131-
if (opts.file_count > 0) {
130+
if (git_array_size(opts.files) > 0) {
132131
error = print_paths(&opts, index);
133132
goto cleanup;
134133
}
@@ -144,6 +143,7 @@ int main(int argc, char *argv[])
144143

145144
cleanup:
146145
/* free our allocated resources */
146+
git_array_clear(opts.files);
147147
git_index_free(index);
148148
git_repository_free(repo);
149149
git_libgit2_shutdown();

0 commit comments

Comments
 (0)