-
Notifications
You must be signed in to change notification settings - Fork 2.6k
examples: ls-files: add ls-files to list paths in the index #4380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
30e919c
991fc08
52d83dd
cd39273
d2f99e0
37cbc3e
3f64a9d
e866116
ee14465
7d07941
29ca3f3
d7394c3
8aa437e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,8 @@ | |
|
|
||
| typedef struct { | ||
| int error_unmatch; | ||
| git_array_t(char *) files; | ||
| char * files[1024]; | ||
| int file_count; | ||
| } ls_options; | ||
|
|
||
| /* Print a usage message for the program. */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment does not provide any useful information |
||
|
|
@@ -46,10 +47,8 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) | |
| { | ||
| int parsing_files = 0; | ||
| int i; | ||
| char **file; | ||
|
|
||
| memset(opts, 0, sizeof(ls_options)); | ||
| git_array_init(opts->files); | ||
|
|
||
| if (argc < 2) | ||
| return 0; | ||
|
|
@@ -61,9 +60,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) | |
| if (a[0] != '-' || parsing_files) { | ||
| parsing_files = 1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You set this, but you don't use that value to distinguish files/non-files. It should probably be So as soon as we find the first non-argument, we treat all remaining arguments as files. |
||
|
|
||
| file = git_array_alloc(opts->files); | ||
| GITERR_CHECK_ALLOC(file); | ||
| *file = a; | ||
| // watch for overflows (just in case) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++-style comments are not allowed |
||
| if (opts->file_count == 1024) { | ||
| break; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably shouldn't break here but print an error and return an error code. |
||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for braces here. |
||
|
|
||
| opts->files[opts->file_count++] = a; | ||
| } else if (!strcmp(a, "--")) { | ||
| parsing_files = 1; | ||
| } else if (!strcmp(a, "--error-unmatch")) { | ||
|
|
@@ -79,12 +81,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) | |
|
|
||
| static int print_paths(ls_options *opts, git_index *index) | ||
| { | ||
| size_t i; | ||
| int i; | ||
| const git_index_entry *entry; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could also be scoped to the loop |
||
|
|
||
| /* loop through the files found in the args and print them if they exist */ | ||
| for (i = 0; i < git_array_size(opts->files); ++i) { | ||
| const char *path = *(char **)git_array_get(opts->files, i); | ||
| for (i = 0; i < opts->file_count; ++i) { | ||
| const char *path = opts->files[i]; | ||
|
|
||
| entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL); | ||
| if (!entry && opts->error_unmatch) { | ||
|
|
@@ -121,7 +123,7 @@ int main(int argc, char *argv[]) | |
| goto cleanup; | ||
|
|
||
| /* if there are files explicitly listed by the user, we need to treat this command differently */ | ||
| if (git_array_size(opts.files) > 0) { | ||
| if (opts.file_count > 0) { | ||
| error = print_paths(&opts, index); | ||
| } else { | ||
| entry_count = git_index_entrycount(index); | ||
|
|
@@ -133,7 +135,6 @@ int main(int argc, char *argv[]) | |
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a small style issue: I do find it a bit funny that some printing logic is in |
||
|
|
||
| cleanup: | ||
| git_array_clear(opts.files); | ||
| git_index_free(index); | ||
| git_repository_free(repo); | ||
| git_libgit2_shutdown(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*goes with the variable name, so this should rather bechar *files[1024];.