Skip to content

Commit e866116

Browse files
Carson HowardCarson Howard
authored andcommitted
examples: ls-files: address PR and style
1 parent 3f64a9d commit e866116

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

examples/ls-files.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@
2323
* `git ls-files` base command shows all paths in the index at that time.
2424
* This includes staged and committed files, but unstaged files will not display.
2525
*
26-
* This currently supports:
27-
* - The --error-unmatch paramter with the same output as the git cli
28-
* - default ls-files behavior
29-
*
30-
* This currently does not support:
31-
* - anything else
32-
*
26+
* This currently supports the default behavior and the `--error-unmatch` option.
3327
*/
3428

3529
typedef struct {
@@ -51,7 +45,6 @@ static void usage(const char *message, const char *arg)
5145
static int parse_options(ls_options *opts, int argc, char *argv[])
5246
{
5347
int parsing_files = 0;
54-
struct args_info args = ARGS_INFO_INIT;
5548
char **file;
5649

5750
memset(opts, 0, sizeof(ls_options));
@@ -60,19 +53,20 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
6053
if (argc < 2)
6154
return 0;
6255

63-
for (args.pos = 1; args.pos < argc; ++args.pos) {
64-
char *a = argv[args.pos];
56+
int i;
57+
for (i = 1; i < argc; ++i) {
58+
char *a = argv[i];
6559

6660
/* if it doesn't start with a '-' or is after the '--' then it is a file */
67-
if (a[0] != '-') {
61+
if (a[0] != '-' || parsing_files) {
6862
parsing_files = 1;
6963

7064
file = git_array_alloc(opts->files);
7165
GITERR_CHECK_ALLOC(file);
7266
*file = a;
7367
} else if (!strcmp(a, "--")) {
7468
parsing_files = 1;
75-
} else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
69+
} else if (!strcmp(a, "--error-unmatch")) {
7670
opts->error_unmatch = 1;
7771
} else {
7872
usage("Unsupported argument", a);
@@ -94,8 +88,8 @@ static int print_paths(ls_options *opts, git_index *index)
9488

9589
entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL);
9690
if (!entry && opts->error_unmatch) {
97-
printf("error: pathspec '%s' did not match any file(s) known to git.\n", path);
98-
printf("Did you forget to 'git add'?\n");
91+
fprintf(stderr, "error: pathspec '%s' did not match any file(s) known to git.\n", path);
92+
fprintf(stderr, "Did you forget to 'git add'?\n");
9993
return -1;
10094
}
10195

@@ -129,20 +123,16 @@ int main(int argc, char *argv[])
129123
/* if there are files explicitly listed by the user, we need to treat this command differently */
130124
if (git_array_size(opts.files) > 0) {
131125
error = print_paths(&opts, index);
132-
goto cleanup;
133-
}
134-
135-
/* we need to know how many entries exist in the index */
136-
entry_count = git_index_entrycount(index);
126+
} else {
127+
entry_count = git_index_entrycount(index);
137128

138-
/* loop through the entries by index and display their pathes */
139-
for (i = 0; i < entry_count; i++) {
140-
entry = git_index_get_byindex(index, i);
141-
printf("%s\n", entry->path);
129+
for (i = 0; i < entry_count; i++) {
130+
entry = git_index_get_byindex(index, i);
131+
printf("%s\n", entry->path);
132+
}
142133
}
143134

144135
cleanup:
145-
/* free our allocated resources */
146136
git_array_clear(opts.files);
147137
git_index_free(index);
148138
git_repository_free(repo);

0 commit comments

Comments
 (0)