mirror of
https://github.com/libgit2/libgit2.git
synced 2026-06-22 06:26:26 +00:00
examples: ls-files: fix style and refactor
This commit is contained in:
committed by
Carson Howard
parent
52d83dde97
commit
cd39273dbb
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "array.h"
|
||||
|
||||
/**
|
||||
* This example demonstrates the libgit2 index APIs to roughly
|
||||
@@ -31,19 +32,79 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#define MAX_FILES 64
|
||||
|
||||
typedef struct ls_options {
|
||||
typedef struct {
|
||||
int error_unmatch;
|
||||
char *files[MAX_FILES];
|
||||
char **files;
|
||||
int file_count;
|
||||
} ls_options;
|
||||
|
||||
static void usage(const char *message, const char *arg);
|
||||
void parse_options(ls_options *opts, int argc, char *argv[]);
|
||||
int print_error_unmatch(ls_options *opts, git_index *index);
|
||||
/* Print a usage message for the program. */
|
||||
static void usage(const char *message, const char *arg)
|
||||
{
|
||||
if (message && arg)
|
||||
fprintf(stderr, "%s: %s\n", message, arg);
|
||||
else if (message)
|
||||
fprintf(stderr, "%s\n", message);
|
||||
fprintf(stderr, "usage: ls-files [--error-unmatch] [--] [<file>...]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
static void parse_options(ls_options *opts, int argc, char *argv[])
|
||||
{
|
||||
int parsing_files = 0;
|
||||
struct args_info args = ARGS_INFO_INIT;
|
||||
git_array_t(char *) files = GIT_ARRAY_INIT;
|
||||
|
||||
memset(opts, 0, sizeof(ls_options));
|
||||
|
||||
if (argc < 2)
|
||||
return;
|
||||
|
||||
for (args.pos = 1; args.pos < argc; ++args.pos) {
|
||||
char *a = argv[args.pos];
|
||||
|
||||
/* if it doesn't start with a '-' or is after the '--' then it is a file */
|
||||
if (a[0] != '-') {
|
||||
parsing_files = 1;
|
||||
|
||||
opts->files = git_array_alloc(files);
|
||||
GITERR_CHECK_ALLOC(opts->files);
|
||||
|
||||
opts->files[opts->file_count++] = a;
|
||||
} else if (!strcmp(a, "--")) {
|
||||
parsing_files = 1;
|
||||
} else if (!strcmp(a, "--error-unmatch") && !parsing_files) {
|
||||
opts->error_unmatch = 1;
|
||||
} else {
|
||||
usage("Unsupported argument", a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int print_paths(ls_options *opts, git_index *index)
|
||||
{
|
||||
int i;
|
||||
const git_index_entry *entry;
|
||||
|
||||
/* loop through the files found in the args and print them if they exist */
|
||||
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) {
|
||||
printf("error: pathspec '%s' did not match any file(s) known to git.\n", path);
|
||||
printf("Did you forget to 'git add'?\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s\n", path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
ls_options opts;
|
||||
git_repository *repo;
|
||||
git_index *index;
|
||||
@@ -54,20 +115,17 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
parse_options(&opts, argc, argv);
|
||||
|
||||
/* we need to initialize libgit2 */
|
||||
git_libgit2_init();
|
||||
|
||||
/* we need to open the repo */
|
||||
if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) != 0)
|
||||
goto cleanup;
|
||||
|
||||
/* we need to load the repo's index */
|
||||
if ((error = git_repository_index(&index, repo)) != 0)
|
||||
goto cleanup;
|
||||
|
||||
/* if the error_unmatch flag is set, we need to print it differently */
|
||||
if (opts.error_unmatch) {
|
||||
error = print_error_unmatch(&opts, index);
|
||||
/* if there are files explicitly listed by the user, we need to treat this command differently */
|
||||
if (opts.file_count > 0) {
|
||||
error = print_paths(&opts, index);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -84,70 +142,7 @@ cleanup:
|
||||
/* free our allocated resources */
|
||||
git_index_free(index);
|
||||
git_repository_free(repo);
|
||||
|
||||
/* we need to shutdown libgit2 */
|
||||
git_libgit2_shutdown();
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Print a usage message for the program. */
|
||||
static void usage(const char *message, const char *arg)
|
||||
{
|
||||
if (message && arg)
|
||||
fprintf(stderr, "%s: %s\n", message, arg);
|
||||
else if (message)
|
||||
fprintf(stderr, "%s\n", message);
|
||||
fprintf(stderr, "usage: ls-files [--error-unmatch] [--] [<file>...]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void parse_options(ls_options *opts, int argc, char *argv[]) {
|
||||
int parsing_files = 0;
|
||||
struct args_info args = ARGS_INFO_INIT;
|
||||
|
||||
memset(opts, 0, sizeof(ls_options));
|
||||
opts->error_unmatch = 0;
|
||||
opts->file_count = 0;
|
||||
|
||||
if (argc < 2)
|
||||
return;
|
||||
|
||||
for (args.pos = 1; args.pos < argc; ++args.pos) {
|
||||
char *a = argv[args.pos];
|
||||
|
||||
/* if it doesn't start with a '-' or is after the '--' then it is a file */
|
||||
if (a[0] != '-' || !strcmp(a, "--")) {
|
||||
if (parsing_files) {
|
||||
opts->files[opts->file_count++] = a;
|
||||
} else {
|
||||
parsing_files = 1;
|
||||
}
|
||||
} else if (!strcmp(a, "--error-unmatch")) {
|
||||
opts->error_unmatch = 1;
|
||||
parsing_files = 1;
|
||||
} else {
|
||||
usage("Unsupported argument", a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int print_error_unmatch(ls_options *opts, git_index *index) {
|
||||
int i;
|
||||
const git_index_entry *entry;
|
||||
|
||||
/* loop through the files found in the args and print them if they exist */
|
||||
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) {
|
||||
printf("error: pathspec '%s' did not match any file(s) known to git.\n", path);
|
||||
printf("Did you forget to 'git add'?\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s\n", path);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user