Skip to content

Commit cd39273

Browse files
cjhoward92Carson Howard
authored andcommitted
examples: ls-files: fix style and refactor
1 parent 52d83dd commit cd39273

File tree

1 file changed

+65
-70
lines changed

1 file changed

+65
-70
lines changed

examples/ls-files.c

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#include "common.h"
16+
#include "array.h"
1617

1718
/**
1819
* This example demonstrates the libgit2 index APIs to roughly
@@ -31,66 +32,12 @@
3132
*
3233
*/
3334

34-
#define MAX_FILES 64
35-
36-
typedef struct ls_options {
35+
typedef struct {
3736
int error_unmatch;
38-
char *files[MAX_FILES];
37+
char **files;
3938
int file_count;
4039
} ls_options;
4140

42-
static void usage(const char *message, const char *arg);
43-
void parse_options(ls_options *opts, int argc, char *argv[]);
44-
int print_error_unmatch(ls_options *opts, git_index *index);
45-
46-
int main(int argc, char *argv[]) {
47-
ls_options opts;
48-
git_repository *repo;
49-
git_index *index;
50-
const git_index_entry *entry;
51-
size_t entry_count;
52-
size_t i = 0;
53-
int error;
54-
55-
parse_options(&opts, argc, argv);
56-
57-
/* we need to initialize libgit2 */
58-
git_libgit2_init();
59-
60-
/* we need to open the repo */
61-
if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) != 0)
62-
goto cleanup;
63-
64-
/* we need to load the repo's index */
65-
if ((error = git_repository_index(&index, repo)) != 0)
66-
goto cleanup;
67-
68-
/* if the error_unmatch flag is set, we need to print it differently */
69-
if (opts.error_unmatch) {
70-
error = print_error_unmatch(&opts, index);
71-
goto cleanup;
72-
}
73-
74-
/* we need to know how many entries exist in the index */
75-
entry_count = git_index_entrycount(index);
76-
77-
/* loop through the entries by index and display their pathes */
78-
for (i = 0; i < entry_count; i++) {
79-
entry = git_index_get_byindex(index, i);
80-
printf("%s\n", entry->path);
81-
}
82-
83-
cleanup:
84-
/* free our allocated resources */
85-
git_index_free(index);
86-
git_repository_free(repo);
87-
88-
/* we need to shutdown libgit2 */
89-
git_libgit2_shutdown();
90-
91-
return error;
92-
}
93-
9441
/* Print a usage message for the program. */
9542
static void usage(const char *message, const char *arg)
9643
{
@@ -102,13 +49,13 @@ static void usage(const char *message, const char *arg)
10249
exit(1);
10350
}
10451

105-
void parse_options(ls_options *opts, int argc, char *argv[]) {
52+
static void parse_options(ls_options *opts, int argc, char *argv[])
53+
{
10654
int parsing_files = 0;
10755
struct args_info args = ARGS_INFO_INIT;
108-
56+
git_array_t(char *) files = GIT_ARRAY_INIT;
57+
10958
memset(opts, 0, sizeof(ls_options));
110-
opts->error_unmatch = 0;
111-
opts->file_count = 0;
11259

11360
if (argc < 2)
11461
return;
@@ -117,22 +64,25 @@ void parse_options(ls_options *opts, int argc, char *argv[]) {
11764
char *a = argv[args.pos];
11865

11966
/* if it doesn't start with a '-' or is after the '--' then it is a file */
120-
if (a[0] != '-' || !strcmp(a, "--")) {
121-
if (parsing_files) {
122-
opts->files[opts->file_count++] = a;
123-
} else {
124-
parsing_files = 1;
125-
}
126-
} else if (!strcmp(a, "--error-unmatch")) {
127-
opts->error_unmatch = 1;
67+
if (a[0] != '-') {
12868
parsing_files = 1;
69+
70+
opts->files = git_array_alloc(files);
71+
GITERR_CHECK_ALLOC(opts->files);
72+
73+
opts->files[opts->file_count++] = a;
74+
} else if (!strcmp(a, "--")) {
75+
parsing_files = 1;
76+
} else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
77+
opts->error_unmatch = 1;
12978
} else {
13079
usage("Unsupported argument", a);
13180
}
13281
}
13382
}
13483

135-
int print_error_unmatch(ls_options *opts, git_index *index) {
84+
static int print_paths(ls_options *opts, git_index *index)
85+
{
13686
int i;
13787
const git_index_entry *entry;
13888

@@ -141,13 +91,58 @@ int print_error_unmatch(ls_options *opts, git_index *index) {
14191
const char *path = opts->files[i];
14292

14393
entry = git_index_get_bypath(index, path, GIT_INDEX_STAGE_NORMAL);
144-
if (!entry) {
94+
if (!entry && opts->error_unmatch) {
14595
printf("error: pathspec '%s' did not match any file(s) known to git.\n", path);
14696
printf("Did you forget to 'git add'?\n");
14797
return -1;
14898
}
14999

150100
printf("%s\n", path);
151101
}
102+
152103
return 0;
153104
}
105+
106+
int main(int argc, char *argv[])
107+
{
108+
ls_options opts;
109+
git_repository *repo;
110+
git_index *index;
111+
const git_index_entry *entry;
112+
size_t entry_count;
113+
size_t i = 0;
114+
int error;
115+
116+
parse_options(&opts, argc, argv);
117+
118+
git_libgit2_init();
119+
120+
if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) != 0)
121+
goto cleanup;
122+
123+
if ((error = git_repository_index(&index, repo)) != 0)
124+
goto cleanup;
125+
126+
/* if there are files explicitly listed by the user, we need to treat this command differently */
127+
if (opts.file_count > 0) {
128+
error = print_paths(&opts, index);
129+
goto cleanup;
130+
}
131+
132+
/* we need to know how many entries exist in the index */
133+
entry_count = git_index_entrycount(index);
134+
135+
/* loop through the entries by index and display their pathes */
136+
for (i = 0; i < entry_count; i++) {
137+
entry = git_index_get_byindex(index, i);
138+
printf("%s\n", entry->path);
139+
}
140+
141+
cleanup:
142+
/* free our allocated resources */
143+
git_index_free(index);
144+
git_repository_free(repo);
145+
git_libgit2_shutdown();
146+
147+
return error;
148+
}

0 commit comments

Comments
 (0)