summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCarson Howard <carsonh@axosoft.com>2017-11-13 20:52:31 -0700
committerCarson Howard <tylerw+systemtest@axosoft.com>2018-03-27 07:18:31 -0700
commite86611690e9c6c735c47530975cec45ea6c16f1d (patch)
tree9375da19cca6f3b0ab671473a1e7e59514a1d316 /examples
parent3f64a9dbc08785207c90fbef7df51a7a43ce103a (diff)
downloadlibgit2-e86611690e9c6c735c47530975cec45ea6c16f1d.tar.gz
examples: ls-files: address PR and style
Diffstat (limited to 'examples')
-rw-r--r--examples/ls-files.c38
1 files changed, 14 insertions, 24 deletions
diff --git a/examples/ls-files.c b/examples/ls-files.c
index a73cfbc2e..c29a93dd8 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);