Skip to content

Commit 29ca3f3

Browse files
Carson HowardCarson Howard
authored andcommitted
examples: ls-files: update print_paths to print all cases
1 parent 7d07941 commit 29ca3f3

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

examples/ls-files.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
typedef struct {
3030
int error_unmatch;
3131
char * files[1024];
32-
int file_count;
32+
size_t file_count;
3333
} ls_options;
3434

35-
/* Print a usage message for the program. */
3635
static void usage(const char *message, const char *arg)
3736
{
3837
if (message && arg)
@@ -60,9 +59,10 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
6059
if (a[0] != '-' || parsing_files) {
6160
parsing_files = 1;
6261

63-
// watch for overflows (just in case)
62+
/* watch for overflows (just in case) */
6463
if (opts->file_count == 1024) {
65-
break;
64+
fprintf(stderr, "ls-files can only support 1024 files at this time.\n");
65+
return -1;
6666
}
6767

6868
opts->files[opts->file_count++] = a;
@@ -81,8 +81,19 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
8181

8282
static int print_paths(ls_options *opts, git_index *index)
8383
{
84-
int i;
84+
size_t i;
8585
const git_index_entry *entry;
86+
87+
/* if there are not files explicitly listed by the user print all entries in the index */
88+
if (opts->file_count == 0) {
89+
size_t entry_count = git_index_entrycount(index);
90+
91+
for (i = 0; i < entry_count; i++) {
92+
entry = git_index_get_byindex(index, i);
93+
printf("%s\n", entry->path);
94+
}
95+
return 0;
96+
}
8697

8798
/* loop through the files found in the args and print them if they exist */
8899
for (i = 0; i < opts->file_count; ++i) {
@@ -106,9 +117,6 @@ int main(int argc, char *argv[])
106117
ls_options opts;
107118
git_repository *repo = NULL;
108119
git_index *index = NULL;
109-
const git_index_entry *entry;
110-
size_t entry_count;
111-
size_t i = 0;
112120
int error;
113121

114122
if ((error = parse_options(&opts, argc, argv)) < 0)
@@ -122,17 +130,7 @@ int main(int argc, char *argv[])
122130
if ((error = git_repository_index(&index, repo)) < 0)
123131
goto cleanup;
124132

125-
/* if there are files explicitly listed by the user, we need to treat this command differently */
126-
if (opts.file_count > 0) {
127-
error = print_paths(&opts, index);
128-
} else {
129-
entry_count = git_index_entrycount(index);
130-
131-
for (i = 0; i < entry_count; i++) {
132-
entry = git_index_get_byindex(index, i);
133-
printf("%s\n", entry->path);
134-
}
135-
}
133+
error = print_paths(&opts, index);
136134

137135
cleanup:
138136
git_index_free(index);

0 commit comments

Comments
 (0)