examples: ls-files: address PR and style
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/examples/ls-files.c b/examples/ls-files.c
index a73cfbc..c29a93d 100644
--- a/examples/ls-files.c
+++ b/examples/ls-files.c
@@ -23,13 +23,7 @@
* `git ls-files` base command shows all paths in the index at that time.
* This includes staged and committed files, but unstaged files will not display.
*
- * This currently supports:
- * - The --error-unmatch paramter with the same output as the git cli
- * - default ls-files behavior
- *
- * This currently does not support:
- * - anything else
- *
+ * This currently supports the default behavior and the `--error-unmatch` option.
*/
typedef struct {
@@ -51,7 +45,6 @@ static void usage(const char *message, const char *arg)
static int parse_options(ls_options *opts, int argc, char *argv[])
{
int parsing_files = 0;
- struct args_info args = ARGS_INFO_INIT;
char **file;
memset(opts, 0, sizeof(ls_options));
@@ -60,11 +53,12 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
if (argc < 2)
return 0;
- for (args.pos = 1; args.pos < argc; ++args.pos) {
- char *a = argv[args.pos];
+ int i;
+ for (i = 1; i < argc; ++i) {
+ char *a = argv[i];
/* if it doesn't start with a '-' or is after the '--' then it is a file */
- if (a[0] != '-') {
+ if (a[0] != '-' || parsing_files) {
parsing_files = 1;
file = git_array_alloc(opts->files);
@@ -72,7 +66,7 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
*file = a;
} else if (!strcmp(a, "--")) {
parsing_files = 1;
- } else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
+ } else if (!strcmp(a, "--error-unmatch")) {
opts->error_unmatch = 1;
} else {
usage("Unsupported argument", a);
@@ -94,8 +88,8 @@ static int print_paths(ls_options *opts, git_index *index)
entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL);
if (!entry && opts->error_unmatch) {
- printf("error: pathspec '%s' did not match any file(s) known to git.\n", path);
- printf("Did you forget to 'git add'?\n");
+ fprintf(stderr, "error: pathspec '%s' did not match any file(s) known to git.\n", path);
+ fprintf(stderr, "Did you forget to 'git add'?\n");
return -1;
}
@@ -129,20 +123,16 @@ int main(int argc, char *argv[])
/* if there are files explicitly listed by the user, we need to treat this command differently */
if (git_array_size(opts.files) > 0) {
error = print_paths(&opts, index);
- goto cleanup;
- }
-
- /* we need to know how many entries exist in the index */
- entry_count = git_index_entrycount(index);
+ } else {
+ entry_count = git_index_entrycount(index);
- /* loop through the entries by index and display their pathes */
- for (i = 0; i < entry_count; i++) {
- entry = git_index_get_byindex(index, i);
- printf("%s\n", entry->path);
+ for (i = 0; i < entry_count; i++) {
+ entry = git_index_get_byindex(index, i);
+ printf("%s\n", entry->path);
+ }
}
cleanup:
- /* free our allocated resources */
git_array_clear(opts.files);
git_index_free(index);
git_repository_free(repo);