From 4867523e60e18639637b5f4f8a787386f86d10d2 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:15 +0100 Subject: examples: add *.h files to IDEs --- examples/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 05e344df1..8cc72b35e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,7 +1,7 @@ INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES}) INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES}) -FILE(GLOB LG2_SOURCES *.c) +FILE(GLOB LG2_SOURCES *.c *.h) ADD_EXECUTABLE(lg2 ${LG2_SOURCES}) SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90) -- cgit v1.2.1 From 882220bf116677ff6b89218a5c8bb618463013f4 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:19 +0100 Subject: examples: add missing include barriers --- examples/common.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'examples') diff --git a/examples/common.h b/examples/common.h index 2ad897bca..4f0181e38 100644 --- a/examples/common.h +++ b/examples/common.h @@ -11,6 +11,8 @@ * with this software. If not, see * . */ +#ifndef INCLUDE_examples_common_h__ +#define INCLUDE_examples_common_h__ #include #include @@ -196,3 +198,5 @@ extern int cred_acquire_cb(git_cred **out, const char *username_from_url, unsigned int allowed_types, void *payload); + +#endif -- cgit v1.2.1 From cd5e33fbc2d11477aad954a6f343e791fdc7ef85 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:23 +0100 Subject: global: DRY includes of assert.h --- examples/add.c | 1 - examples/checkout.c | 1 - examples/common.c | 5 +++-- examples/common.h | 1 + examples/describe.c | 1 - examples/merge.c | 1 - 6 files changed, 4 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/add.c b/examples/add.c index 14e69e96f..b0c4f600f 100644 --- a/examples/add.c +++ b/examples/add.c @@ -13,7 +13,6 @@ */ #include "common.h" -#include /** * The following example demonstrates how to add files with libgit2. diff --git a/examples/checkout.c b/examples/checkout.c index d552eece8..b706e044e 100644 --- a/examples/checkout.c +++ b/examples/checkout.c @@ -13,7 +13,6 @@ */ #include "common.h" -#include /* Define the printf format specifer to use for size_t output */ #if defined(_MSC_VER) || defined(__MINGW32__) diff --git a/examples/common.c b/examples/common.c index 22807e129..47cfac6a0 100644 --- a/examples/common.c +++ b/examples/common.c @@ -12,7 +12,9 @@ * . */ -#include + +#include "common.h" + #include #include #include @@ -25,7 +27,6 @@ #include #include -#include "common.h" void check_lg2(int error, const char *message, const char *extra) { diff --git a/examples/common.h b/examples/common.h index 4f0181e38..3657e539b 100644 --- a/examples/common.h +++ b/examples/common.h @@ -14,6 +14,7 @@ #ifndef INCLUDE_examples_common_h__ #define INCLUDE_examples_common_h__ +#include #include #include #include diff --git a/examples/describe.c b/examples/describe.c index 53f548c8b..181e499b0 100644 --- a/examples/describe.c +++ b/examples/describe.c @@ -13,7 +13,6 @@ */ #include "common.h" -#include /** * The following example partially reimplements the `git describe` command diff --git a/examples/merge.c b/examples/merge.c index 995ac021b..0cdeeeadf 100644 --- a/examples/merge.c +++ b/examples/merge.c @@ -13,7 +13,6 @@ */ #include "common.h" -#include /** The following example demonstrates how to do merges with libgit2. * -- cgit v1.2.1 From 745ccc8ab98c9bb52099d5fa786158704602b1af Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:26 +0100 Subject: examples: remove duplicate includes from common.c --- examples/common.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/common.c b/examples/common.c index 47cfac6a0..d243785a5 100644 --- a/examples/common.c +++ b/examples/common.c @@ -15,19 +15,11 @@ #include "common.h" -#include -#include -#include -#ifdef _WIN32 -# include -#else -# include +#ifndef _WIN32 # include #endif -#include #include - void check_lg2(int error, const char *message, const char *extra) { const git_error *lg2err; -- cgit v1.2.1 From 025a93577d9cff75ba36816d8957470aac03f1c7 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:30 +0100 Subject: examples: move "args" to its own header --- examples/args.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/args.h | 79 +++++++++++++++++++++++++ examples/common.c | 168 ----------------------------------------------------- examples/common.h | 73 +---------------------- 4 files changed, 251 insertions(+), 239 deletions(-) create mode 100644 examples/args.c create mode 100644 examples/args.h (limited to 'examples') diff --git a/examples/args.c b/examples/args.c new file mode 100644 index 000000000..b228ae3dd --- /dev/null +++ b/examples/args.c @@ -0,0 +1,170 @@ +#include "common.h" +#include "args.h" + +size_t is_prefixed(const char *str, const char *pfx) +{ + size_t len = strlen(pfx); + return strncmp(str, pfx, len) ? 0 : len; +} + +int optional_str_arg( + const char **out, struct args_info *args, const char *opt, const char *def) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return 0; + + if (!found[len]) { + if (args->pos + 1 == args->argc) { + *out = def; + return 1; + } + args->pos += 1; + *out = args->argv[args->pos]; + return 1; + } + + if (found[len] == '=') { + *out = found + len + 1; + return 1; + } + + return 0; +} + +int match_str_arg( + const char **out, struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return 0; + + if (!found[len]) { + if (args->pos + 1 == args->argc) + fatal("expected value following argument", opt); + args->pos += 1; + *out = args->argv[args->pos]; + return 1; + } + + if (found[len] == '=') { + *out = found + len + 1; + return 1; + } + + return 0; +} + +static const char *match_numeric_arg(struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + size_t len = is_prefixed(found, opt); + + if (!len) + return NULL; + + if (!found[len]) { + if (args->pos + 1 == args->argc) + fatal("expected numeric value following argument", opt); + args->pos += 1; + found = args->argv[args->pos]; + } else { + found = found + len; + if (*found == '=') + found++; + } + + return found; +} + +int match_uint16_arg( + uint16_t *out, struct args_info *args, const char *opt) +{ + const char *found = match_numeric_arg(args, opt); + uint16_t val; + char *endptr = NULL; + + if (!found) + return 0; + + val = (uint16_t)strtoul(found, &endptr, 0); + if (!endptr || *endptr != '\0') + fatal("expected number after argument", opt); + + if (out) + *out = val; + return 1; +} + +int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt) +{ + const char *found = match_numeric_arg(args, opt); + uint16_t val; + char *endptr = NULL; + + if (!found) + return 0; + + val = (uint32_t)strtoul(found, &endptr, 0); + if (!endptr || *endptr != '\0') + fatal("expected number after argument", opt); + + if (out) + *out = val; + return 1; +} + +static int match_int_internal( + int *out, const char *str, int allow_negative, const char *opt) +{ + char *endptr = NULL; + int val = (int)strtol(str, &endptr, 10); + + if (!endptr || *endptr != '\0') + fatal("expected number", opt); + else if (val < 0 && !allow_negative) + fatal("negative values are not allowed", opt); + + if (out) + *out = val; + + return 1; +} + +int match_bool_arg(int *out, struct args_info *args, const char *opt) +{ + const char *found = args->argv[args->pos]; + + if (!strcmp(found, opt)) { + *out = 1; + return 1; + } + + if (!strncmp(found, "--no-", strlen("--no-")) && + !strcmp(found + strlen("--no-"), opt + 2)) { + *out = 0; + return 1; + } + + *out = -1; + return 0; +} + +int is_integer(int *out, const char *str, int allow_negative) +{ + return match_int_internal(out, str, allow_negative, NULL); +} + +int match_int_arg( + int *out, struct args_info *args, const char *opt, int allow_negative) +{ + const char *found = match_numeric_arg(args, opt); + if (!found) + return 0; + return match_int_internal(out, found, allow_negative, opt); +} diff --git a/examples/args.h b/examples/args.h new file mode 100644 index 000000000..b08a534f3 --- /dev/null +++ b/examples/args.h @@ -0,0 +1,79 @@ +#ifndef INCLUDE_examples_args_h__ +#define INCLUDE_examples_args_h__ + +/** + * Argument-processing helper structure + */ +struct args_info { + int argc; + char **argv; + int pos; +}; +#define ARGS_INFO_INIT { argc, argv, 0, 0 } +#define ARGS_CURRENT(args) args->argv[args->pos] + +/** + * Check if a string has the given prefix. Returns 0 if not prefixed + * or the length of the prefix if it is. + */ +extern size_t is_prefixed(const char *str, const char *pfx); + +/** + * Match an integer string, returning 1 if matched, 0 if not. + */ +extern int is_integer(int *out, const char *str, int allow_negative); + +/** + * Check current `args` entry against `opt` string. If it matches + * exactly, take the next arg as a string; if it matches as a prefix with + * an equal sign, take the remainder as a string; if value not supplied, + * default value `def` will be given. otherwise return 0. + */ +extern int optional_str_arg( + const char **out, struct args_info *args, const char *opt, const char *def); + +/** + * Check current `args` entry against `opt` string. If it matches + * exactly, take the next arg as a string; if it matches as a prefix with + * an equal sign, take the remainder as a string; otherwise return 0. + */ +extern int match_str_arg( + const char **out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as uint16. If + * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` + * is a prefix (equal sign optional), take the remainder of the arg as a + * uint16_t value; otherwise return 0. + */ +extern int match_uint16_arg( + uint16_t *out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as uint32. If + * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` + * is a prefix (equal sign optional), take the remainder of the arg as a + * uint32_t value; otherwise return 0. + */ +extern int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt); + +/** + * Check current `args` entry against `opt` string parsing as int. If + * `opt` matches exactly, take the next arg as an int value; if it matches + * as a prefix (equal sign optional), take the remainder of the arg as a + * int value; otherwise return 0. + */ +extern int match_int_arg( + int *out, struct args_info *args, const char *opt, int allow_negative); + +/** + * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress). + * If `opt` matches positively, out will be set to 1, or if `opt` matches + * negatively, out will be set to 0, and in both cases 1 will be returned. + * If neither the positive or the negative form of opt matched, out will be -1, + * and 0 will be returned. + */ +extern int match_bool_arg(int *out, struct args_info *args, const char *opt); + +#endif diff --git a/examples/common.c b/examples/common.c index d243785a5..30f6cdc3b 100644 --- a/examples/common.c +++ b/examples/common.c @@ -53,174 +53,6 @@ void fatal(const char *message, const char *extra) exit(1); } -size_t is_prefixed(const char *str, const char *pfx) -{ - size_t len = strlen(pfx); - return strncmp(str, pfx, len) ? 0 : len; -} - -int optional_str_arg( - const char **out, struct args_info *args, const char *opt, const char *def) -{ - const char *found = args->argv[args->pos]; - size_t len = is_prefixed(found, opt); - - if (!len) - return 0; - - if (!found[len]) { - if (args->pos + 1 == args->argc) { - *out = def; - return 1; - } - args->pos += 1; - *out = args->argv[args->pos]; - return 1; - } - - if (found[len] == '=') { - *out = found + len + 1; - return 1; - } - - return 0; -} - -int match_str_arg( - const char **out, struct args_info *args, const char *opt) -{ - const char *found = args->argv[args->pos]; - size_t len = is_prefixed(found, opt); - - if (!len) - return 0; - - if (!found[len]) { - if (args->pos + 1 == args->argc) - fatal("expected value following argument", opt); - args->pos += 1; - *out = args->argv[args->pos]; - return 1; - } - - if (found[len] == '=') { - *out = found + len + 1; - return 1; - } - - return 0; -} - -static const char *match_numeric_arg(struct args_info *args, const char *opt) -{ - const char *found = args->argv[args->pos]; - size_t len = is_prefixed(found, opt); - - if (!len) - return NULL; - - if (!found[len]) { - if (args->pos + 1 == args->argc) - fatal("expected numeric value following argument", opt); - args->pos += 1; - found = args->argv[args->pos]; - } else { - found = found + len; - if (*found == '=') - found++; - } - - return found; -} - -int match_uint16_arg( - uint16_t *out, struct args_info *args, const char *opt) -{ - const char *found = match_numeric_arg(args, opt); - uint16_t val; - char *endptr = NULL; - - if (!found) - return 0; - - val = (uint16_t)strtoul(found, &endptr, 0); - if (!endptr || *endptr != '\0') - fatal("expected number after argument", opt); - - if (out) - *out = val; - return 1; -} - -int match_uint32_arg( - uint32_t *out, struct args_info *args, const char *opt) -{ - const char *found = match_numeric_arg(args, opt); - uint16_t val; - char *endptr = NULL; - - if (!found) - return 0; - - val = (uint32_t)strtoul(found, &endptr, 0); - if (!endptr || *endptr != '\0') - fatal("expected number after argument", opt); - - if (out) - *out = val; - return 1; -} - -static int match_int_internal( - int *out, const char *str, int allow_negative, const char *opt) -{ - char *endptr = NULL; - int val = (int)strtol(str, &endptr, 10); - - if (!endptr || *endptr != '\0') - fatal("expected number", opt); - else if (val < 0 && !allow_negative) - fatal("negative values are not allowed", opt); - - if (out) - *out = val; - - return 1; -} - -int match_bool_arg(int *out, struct args_info *args, const char *opt) -{ - const char *found = args->argv[args->pos]; - - if (!strcmp(found, opt)) { - *out = 1; - return 1; - } - - if (!strncmp(found, "--no-", strlen("--no-")) && - !strcmp(found + strlen("--no-"), opt + 2)) { - *out = 0; - return 1; - } - - *out = -1; - return 0; -} - -int is_integer(int *out, const char *str, int allow_negative) -{ - return match_int_internal(out, str, allow_negative, NULL); -} - -int match_int_arg( - int *out, struct args_info *args, const char *opt, int allow_negative) -{ - const char *found = match_numeric_arg(args, opt); - if (!found) - return 0; - return match_int_internal(out, found, allow_negative, opt); -} - int diff_output( const git_diff_delta *d, const git_diff_hunk *h, diff --git a/examples/common.h b/examples/common.h index 3657e539b..5a029b49a 100644 --- a/examples/common.h +++ b/examples/common.h @@ -52,6 +52,8 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x)) #define UNUSED(x) (void)(x) +#include "args.h" + extern int lg2_add(git_repository *repo, int argc, char **argv); extern int lg2_blame(git_repository *repo, int argc, char **argv); extern int lg2_cat_file(git_repository *repo, int argc, char **argv); @@ -96,77 +98,6 @@ extern char *read_file(const char *path); */ extern void fatal(const char *message, const char *extra); -/** - * Check if a string has the given prefix. Returns 0 if not prefixed - * or the length of the prefix if it is. - */ -extern size_t is_prefixed(const char *str, const char *pfx); - -/** - * Match an integer string, returning 1 if matched, 0 if not. - */ -extern int is_integer(int *out, const char *str, int allow_negative); - -struct args_info { - int argc; - char **argv; - int pos; -}; -#define ARGS_INFO_INIT { argc, argv, 0 } - -/** - * Check current `args` entry against `opt` string. If it matches - * exactly, take the next arg as a string; if it matches as a prefix with - * an equal sign, take the remainder as a string; if value not supplied, - * default value `def` will be given. otherwise return 0. - */ -extern int optional_str_arg( - const char **out, struct args_info *args, const char *opt, const char *def); - -/** - * Check current `args` entry against `opt` string. If it matches - * exactly, take the next arg as a string; if it matches as a prefix with - * an equal sign, take the remainder as a string; otherwise return 0. - */ -extern int match_str_arg( - const char **out, struct args_info *args, const char *opt); - -/** - * Check current `args` entry against `opt` string parsing as uint16. If - * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` - * is a prefix (equal sign optional), take the remainder of the arg as a - * uint16_t value; otherwise return 0. - */ -extern int match_uint16_arg( - uint16_t *out, struct args_info *args, const char *opt); - -/** - * Check current `args` entry against `opt` string parsing as uint32. If - * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` - * is a prefix (equal sign optional), take the remainder of the arg as a - * uint32_t value; otherwise return 0. - */ -extern int match_uint32_arg( - uint32_t *out, struct args_info *args, const char *opt); - -/** - * Check current `args` entry against `opt` string parsing as int. If - * `opt` matches exactly, take the next arg as an int value; if it matches - * as a prefix (equal sign optional), take the remainder of the arg as a - * int value; otherwise return 0. - */ -extern int match_int_arg( - int *out, struct args_info *args, const char *opt, int allow_negative); - -/** - * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress). - * If `opt` matches positively, out will be set to 1, or if `opt` matches - * negatively, out will be set to 0, and in both cases 1 will be returned. - * If neither the positive or the negative form of opt matched, out will be -1, - * and 0 will be returned. - */ -extern int match_bool_arg(int *out, struct args_info *args, const char *opt); - /** * Basic output function for plain text diff output * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`) -- cgit v1.2.1 From c924f36a8bc4aa8e27cc3adabcb090f925d24be0 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:35 +0100 Subject: examples: keep track of whether we processed a "--" arg --- examples/args.c | 13 +++++++++++++ examples/args.h | 6 ++++++ examples/checkout.c | 8 ++------ examples/lg2.c | 3 +-- examples/log.c | 3 +-- 5 files changed, 23 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/args.c b/examples/args.c index b228ae3dd..208c38256 100644 --- a/examples/args.c +++ b/examples/args.c @@ -168,3 +168,16 @@ int match_int_arg( return 0; return match_int_internal(out, found, allow_negative, opt); } + +int match_arg_separator(struct args_info *args) +{ + if (args->opts_done) + return 1; + + if (strcmp(args->argv[args->pos], "--") != 0) + return 0; + + args->opts_done = 1; + args->pos++; + return 1; +} diff --git a/examples/args.h b/examples/args.h index b08a534f3..2c68bdb8b 100644 --- a/examples/args.h +++ b/examples/args.h @@ -8,6 +8,7 @@ struct args_info { int argc; char **argv; int pos; + int opts_done : 1; /**< Did we see a -- separator */ }; #define ARGS_INFO_INIT { argc, argv, 0, 0 } #define ARGS_CURRENT(args) args->argv[args->pos] @@ -76,4 +77,9 @@ extern int match_int_arg( */ extern int match_bool_arg(int *out, struct args_info *args, const char *opt); +/** + * Check if we're processing past the single -- separator + */ +extern int match_arg_separator(struct args_info *args); + #endif diff --git a/examples/checkout.c b/examples/checkout.c index b706e044e..70b51859c 100644 --- a/examples/checkout.c +++ b/examples/checkout.c @@ -65,7 +65,7 @@ static void parse_options(const char **repo_path, checkout_options *opts, struct const char *curr = args->argv[args->pos]; int bool_arg; - if (strcmp(curr, "--") == 0) { + if (match_arg_separator(args)) { break; } else if (!strcmp(curr, "--force")) { opts->force = 1; @@ -190,11 +190,7 @@ int lg2_checkout(git_repository *repo, int argc, char **argv) goto cleanup; } - if (args.pos >= args.argc) { - fprintf(stderr, "unhandled\n"); - err = -1; - goto cleanup; - } else if (!strcmp("--", args.argv[args.pos])) { + if (match_arg_separator(&args)) { /** * Try to checkout the given path */ diff --git a/examples/lg2.c b/examples/lg2.c index f1a8843d2..a3987c34d 100644 --- a/examples/lg2.c +++ b/examples/lg2.c @@ -84,8 +84,7 @@ int main(int argc, char **argv) break; } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) { continue; - } else if (!strcmp(a, "--")) { - /* arg separator */ + } else if (match_arg_separator(&args)) { break; } } diff --git a/examples/log.c b/examples/log.c index a6bd957ae..3eee7d421 100644 --- a/examples/log.c +++ b/examples/log.c @@ -424,8 +424,7 @@ static int parse_options( else /** Try failed revision parse as filename. */ break; - } else if (!strcmp(a, "--")) { - ++args.pos; + } else if (!match_arg_separator(&args)) { break; } else if (!strcmp(a, "--date-order")) -- cgit v1.2.1 From 204a464f424ccaf49752f1c2d5bfc5536b4e0fd2 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:39 +0100 Subject: examples: fixup for-each-ref style --- examples/for-each-ref.c | 66 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'examples') diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c index 900792c12..020dab474 100644 --- a/examples/for-each-ref.c +++ b/examples/for-each-ref.c @@ -3,42 +3,42 @@ static int show_ref(git_reference *ref, void *data) { - git_repository *repo = data; - git_reference *resolved = NULL; - char hex[GIT_OID_HEXSZ+1]; - const git_oid *oid; - git_object *obj; - - if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) - check_lg2(git_reference_resolve(&resolved, ref), - "Unable to resolve symbolic reference", - git_reference_name(ref)); - - oid = git_reference_target(resolved ? resolved : ref); - git_oid_fmt(hex, oid); - hex[GIT_OID_HEXSZ] = 0; - check_lg2(git_object_lookup(&obj, repo, oid, GIT_OBJECT_ANY), - "Unable to lookup object", hex); - - printf("%s %-6s\t%s\n", - hex, - git_object_type2string(git_object_type(obj)), - git_reference_name(ref)); - - if (resolved) - git_reference_free(resolved); - return 0; + git_repository *repo = data; + git_reference *resolved = NULL; + char hex[GIT_OID_HEXSZ+1]; + const git_oid *oid; + git_object *obj; + + if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) + check_lg2(git_reference_resolve(&resolved, ref), + "Unable to resolve symbolic reference", + git_reference_name(ref)); + + oid = git_reference_target(resolved ? resolved : ref); + git_oid_fmt(hex, oid); + hex[GIT_OID_HEXSZ] = 0; + check_lg2(git_object_lookup(&obj, repo, oid, GIT_OBJECT_ANY), + "Unable to lookup object", hex); + + printf("%s %-6s\t%s\n", + hex, + git_object_type2string(git_object_type(obj)), + git_reference_name(ref)); + + if (resolved) + git_reference_free(resolved); + return 0; } int lg2_for_each_ref(git_repository *repo, int argc, char **argv) { UNUSED(argv); - - if (argc != 1) - fatal("Sorry, no for-each-ref options supported yet", NULL); - - check_lg2(git_reference_foreach(repo, show_ref, repo), - "Could not iterate over references", NULL); - - return 0; + + if (argc != 1) + fatal("Sorry, no for-each-ref options supported yet", NULL); + + check_lg2(git_reference_foreach(repo, show_ref, repo), + "Could not iterate over references", NULL); + + return 0; } -- cgit v1.2.1 From c9a09b91c4c4fb1c8c26c837c2f4807aff34e8fd Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:16:47 +0100 Subject: examples: extract argument conversion helper --- examples/args.c | 14 ++++++++++++++ examples/args.h | 5 +++++ 2 files changed, 19 insertions(+) (limited to 'examples') diff --git a/examples/args.c b/examples/args.c index 208c38256..533e1579b 100644 --- a/examples/args.c +++ b/examples/args.c @@ -181,3 +181,17 @@ int match_arg_separator(struct args_info *args) args->pos++; return 1; } + +void strarray_from_args(git_strarray *array, struct args_info *args) +{ + size_t i; + + array->count = args->argc - args->pos; + array->strings = calloc(array->count, sizeof(char *)); + assert(array->strings != NULL); + + for (i = 0; args->pos < args->argc; ++args->pos) { + array->strings[i++] = args->argv[args->pos]; + } + args->pos = args->argc; +} diff --git a/examples/args.h b/examples/args.h index 2c68bdb8b..d626f98c8 100644 --- a/examples/args.h +++ b/examples/args.h @@ -82,4 +82,9 @@ extern int match_bool_arg(int *out, struct args_info *args, const char *opt); */ extern int match_arg_separator(struct args_info *args); +/** + * Consume all remaining arguments in a git_strarray + */ +extern void strarray_from_args(git_strarray *array, struct args_info *args); + #endif -- cgit v1.2.1 From d4a593ef78e72b25a5ca99c0595b1957da5daa20 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:17:52 +0100 Subject: examples: modernize add code --- examples/add.c | 94 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 45 deletions(-) (limited to 'examples') diff --git a/examples/add.c b/examples/add.c index b0c4f600f..f31fce8ab 100644 --- a/examples/add.c +++ b/examples/add.c @@ -26,48 +26,48 @@ * -u/--update: update the index instead of adding to it. */ -enum print_options { - SKIP = 1, - VERBOSE = 2, - UPDATE = 4, +enum index_mode { + INDEX_NONE, + INDEX_ADD, }; -struct print_payload { - enum print_options options; +struct index_options { + int dry_run; + int verbose; git_repository *repo; + enum index_mode mode; + int add_update; }; /* Forward declarations for helpers */ -static void parse_opts(int *options, int *count, int argc, char *argv[]); -void init_array(git_strarray *array, int argc, char **argv); +static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args); int print_matched_cb(const char *path, const char *matched_pathspec, void *payload); -int lg2_add(git_repository *repo, int argc, char** argv) +int lg2_add(git_repository *repo, int argc, char **argv) { git_index_matched_path_cb matched_cb = NULL; git_index *index; git_strarray array = {0}; - int options = 0, count = 0; - struct print_payload payload = {0}; + struct index_options options; + struct args_info args = ARGS_INFO_INIT; - parse_opts(&options, &count, argc, argv); - init_array(&array, argc-count, argv+count); + parse_opts(NULL, &options, &args); + strarray_from_args(&array, &args); check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); /* Setup a callback if the requested options need it */ - if ((options & VERBOSE) || (options & SKIP)) { + if (options.verbose || options.dry_run) { matched_cb = &print_matched_cb; } - /* Perform the requested action with the index and files */ - payload.options = options; - payload.repo = repo; + options.repo = repo; - if (options & UPDATE) { - git_index_update_all(index, &array, matched_cb, &payload); + /* Perform the requested action with the index and files */ + if (options.add_update) { + git_index_update_all(index, &array, matched_cb, &options); } else { - git_index_add_all(index, &array, 0, matched_cb, &payload); + git_index_add_all(index, &array, 0, matched_cb, &options); } /* Cleanup memory */ @@ -84,15 +84,14 @@ int lg2_add(git_repository *repo, int argc, char** argv) */ int print_matched_cb(const char *path, const char *matched_pathspec, void *payload) { - struct print_payload p = *(struct print_payload*)(payload); + struct index_options options = *(struct index_options *)(payload); int ret; unsigned status; (void)matched_pathspec; /* Get the file status */ - if (git_status_file(&status, p.repo, path)) { + if (git_status_file(&status, options.repo, path) < 0) return -1; - } if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) { printf("add '%s'\n", path); @@ -101,9 +100,8 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo ret = 1; } - if ((p.options & SKIP)) { + if (options.dry_run) ret = 1; - } return ret; } @@ -132,33 +130,39 @@ void print_usage(void) exit(1); } -static void parse_opts(int *options, int *count, int argc, char *argv[]) +static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args) { - int i; + if (args->argc <= 1) + print_usage(); - for (i = 1; i < argc; ++i) { - if (argv[i][0] != '-') - break; - else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) - *options |= VERBOSE; - else if (!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) - *options |= SKIP; - else if (!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) - *options |= UPDATE; - else if (!strcmp(argv[i], "-h")) { + for (args->pos = 1; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (curr[0] != '-') { + if (!strcmp("add", curr)) { + options->mode = INDEX_ADD; + continue; + } else if (options->mode == INDEX_NONE) { + fprintf(stderr, "missing command: %s", curr); + print_usage(); + break; + } else { + /* We might be looking at a filename */ + break; + } + } else if (match_bool_arg(&options->verbose, args, "--verbose") || + match_bool_arg(&options->dry_run, args, "--dry-run") || + match_str_arg(repo_path, args, "--git-dir") || + (options->mode == INDEX_ADD && match_bool_arg(&options->add_update, args, "--update"))) { + continue; + } else if (match_bool_arg(NULL, args, "--help")) { print_usage(); break; - } else if (!strcmp(argv[i], "--")) { - i++; + } else if (match_arg_separator(args)) { break; } else { - fprintf(stderr, "Unsupported option %s.\n", argv[i]); + fprintf(stderr, "Unsupported option %s.\n", curr); print_usage(); } } - - if (argc <= i) - print_usage(); - - *count = i; } -- cgit v1.2.1 From 4a4ad2bc8374db6e82464a6f066840430b6657ec Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:45 +0100 Subject: examples: add comments to add.c --- examples/add.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/add.c b/examples/add.c index f31fce8ab..b8351721f 100644 --- a/examples/add.c +++ b/examples/add.c @@ -51,9 +51,11 @@ int lg2_add(git_repository *repo, int argc, char **argv) struct index_options options; struct args_info args = ARGS_INFO_INIT; + /* Parse the options & arguments. */ parse_opts(NULL, &options, &args); strarray_from_args(&array, &args); + /* Grab the repository's index. */ check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); /* Setup a callback if the requested options need it */ -- cgit v1.2.1 From 313908f9f5322f623ef1924f859726b39cb9740c Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:49 +0100 Subject: examples: normalize decls and usage of options structs --- examples/add.c | 20 ++++++++++---------- examples/blame.c | 8 ++++---- examples/cat-file.c | 15 ++++++++------- examples/describe.c | 20 +++++++++----------- examples/diff.c | 20 +++++++++----------- examples/init.c | 8 ++++---- examples/ls-files.c | 12 ++++++------ examples/merge.c | 16 ++++++++-------- examples/remote.c | 28 ++++++++++++++-------------- examples/show-index.c | 2 +- examples/status.c | 8 ++++---- examples/tag.c | 18 +++++++++--------- 12 files changed, 86 insertions(+), 89 deletions(-) (limited to 'examples') diff --git a/examples/add.c b/examples/add.c index b8351721f..6e3c239fc 100644 --- a/examples/add.c +++ b/examples/add.c @@ -40,7 +40,7 @@ struct index_options { }; /* Forward declarations for helpers */ -static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args); +static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args); int print_matched_cb(const char *path, const char *matched_pathspec, void *payload); int lg2_add(git_repository *repo, int argc, char **argv) @@ -86,13 +86,13 @@ int lg2_add(git_repository *repo, int argc, char **argv) */ int print_matched_cb(const char *path, const char *matched_pathspec, void *payload) { - struct index_options options = *(struct index_options *)(payload); + struct index_options *opts = (struct index_options *)(payload); int ret; unsigned status; (void)matched_pathspec; /* Get the file status */ - if (git_status_file(&status, options.repo, path) < 0) + if (git_status_file(&status, opts->repo, path) < 0) return -1; if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) { @@ -102,7 +102,7 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo ret = 1; } - if (options.dry_run) + if (opts->dry_run) ret = 1; return ret; @@ -132,7 +132,7 @@ void print_usage(void) exit(1); } -static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args) +static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args) { if (args->argc <= 1) print_usage(); @@ -142,9 +142,9 @@ static void parse_opts(const char **repo_path, struct index_options *options, st if (curr[0] != '-') { if (!strcmp("add", curr)) { - options->mode = INDEX_ADD; + opts->mode = INDEX_ADD; continue; - } else if (options->mode == INDEX_NONE) { + } else if (opts->mode == INDEX_NONE) { fprintf(stderr, "missing command: %s", curr); print_usage(); break; @@ -152,10 +152,10 @@ static void parse_opts(const char **repo_path, struct index_options *options, st /* We might be looking at a filename */ break; } - } else if (match_bool_arg(&options->verbose, args, "--verbose") || - match_bool_arg(&options->dry_run, args, "--dry-run") || + } else if (match_bool_arg(&opts->verbose, args, "--verbose") || + match_bool_arg(&opts->dry_run, args, "--dry-run") || match_str_arg(repo_path, args, "--git-dir") || - (options->mode == INDEX_ADD && match_bool_arg(&options->add_update, args, "--update"))) { + (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) { continue; } else if (match_bool_arg(NULL, args, "--help")) { print_usage(); diff --git a/examples/blame.c b/examples/blame.c index 76f5ed2bd..1b78d1cea 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -19,7 +19,7 @@ * simulate the output of `git blame` and a few of its command line arguments. */ -struct opts { +struct blame_opts { char *path; char *commitspec; int C; @@ -28,14 +28,14 @@ struct opts { int end_line; int F; }; -static void parse_opts(struct opts *o, int argc, char *argv[]); +static void parse_opts(struct blame_opts *o, int argc, char *argv[]); int lg2_blame(git_repository *repo, int argc, char *argv[]) { int line, break_on_null_hunk; git_off_t i, rawsize; char spec[1024] = {0}; - struct opts o = {0}; + struct blame_opts o = {0}; const char *rawdata; git_revspec revspec = {0}; git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT; @@ -143,7 +143,7 @@ static void usage(const char *msg, const char *arg) } /** Parse the arguments. */ -static void parse_opts(struct opts *o, int argc, char *argv[]) +static void parse_opts(struct blame_opts *o, int argc, char *argv[]) { int i; char *bare_args[3] = {0}; diff --git a/examples/cat-file.c b/examples/cat-file.c index 9b14a7048..b81e9a8da 100644 --- a/examples/cat-file.c +++ b/examples/cat-file.c @@ -102,27 +102,28 @@ static void show_tag(const git_tag *tag) printf("\n%s\n", git_tag_message(tag)); } -enum { +typedef enum { SHOW_TYPE = 1, SHOW_SIZE = 2, SHOW_NONE = 3, SHOW_PRETTY = 4 -}; +} catfile_mode; /* Forward declarations for option-parsing helper */ -struct opts { +struct catfile_options { const char *dir; const char *rev; - int action; + catfile_mode action; int verbose; }; -static void parse_opts(struct opts *o, int argc, char *argv[]); + +static void parse_opts(struct catfile_options *o, int argc, char *argv[]); /** Entry point for this command */ int lg2_cat_file(git_repository *repo, int argc, char *argv[]) { - struct opts o = { ".", NULL, 0, 0 }; + struct catfile_options o = { ".", NULL, 0, 0 }; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; @@ -201,7 +202,7 @@ static void usage(const char *message, const char *arg) } /** Parse the command-line options taken from git */ -static void parse_opts(struct opts *o, int argc, char *argv[]) +static void parse_opts(struct catfile_options *o, int argc, char *argv[]) { struct args_info args = ARGS_INFO_INIT; diff --git a/examples/describe.c b/examples/describe.c index 181e499b0..1236272a1 100644 --- a/examples/describe.c +++ b/examples/describe.c @@ -37,16 +37,14 @@ */ /** describe_options represents the parsed command line options */ -typedef struct { +struct describe_options { const char **commits; size_t commit_count; git_describe_options describe_options; git_describe_format_options format_options; -} describe_options; +}; -typedef struct args_info args_info; - -static void opts_add_commit(describe_options *opts, const char *commit) +static void opts_add_commit(struct describe_options *opts, const char *commit) { size_t sz; @@ -57,7 +55,7 @@ static void opts_add_commit(describe_options *opts, const char *commit) opts->commits[opts->commit_count - 1] = commit; } -static void do_describe_single(git_repository *repo, describe_options *opts, const char *rev) +static void do_describe_single(git_repository *repo, struct describe_options *opts, const char *rev) { git_object *commit; git_describe_result *describe_result; @@ -80,7 +78,7 @@ static void do_describe_single(git_repository *repo, describe_options *opts, con printf("%s\n", buf.ptr); } -static void do_describe(git_repository *repo, describe_options *opts) +static void do_describe(git_repository *repo, struct describe_options *opts) { if (opts->commit_count == 0) do_describe_single(repo, opts, NULL); @@ -99,9 +97,9 @@ static void print_usage(void) } /** Parse command line arguments */ -static void parse_options(describe_options *opts, int argc, char **argv) +static void parse_options(struct describe_options *opts, int argc, char **argv) { - args_info args = ARGS_INFO_INIT; + struct args_info args = ARGS_INFO_INIT; for (args.pos = 1; args.pos < argc; ++args.pos) { const char *curr = argv[args.pos]; @@ -141,7 +139,7 @@ static void parse_options(describe_options *opts, int argc, char **argv) } /** Initialize describe_options struct */ -static void describe_options_init(describe_options *opts) +static void describe_options_init(struct describe_options *opts) { memset(opts, 0, sizeof(*opts)); @@ -153,7 +151,7 @@ static void describe_options_init(describe_options *opts) int lg2_describe(git_repository *repo, int argc, char **argv) { - describe_options opts; + struct describe_options opts; describe_options_init(&opts); parse_options(&opts, argc, argv); diff --git a/examples/diff.c b/examples/diff.c index 9e2aa9c41..2305c8652 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -47,8 +47,8 @@ enum { CACHE_NONE = 2 }; -/** The 'opts' struct captures all the various parsed command line options. */ -struct opts { +/** The 'diff_options' struct captures all the various parsed command line options. */ +struct diff_options { git_diff_options diffopts; git_diff_find_options findopts; int color; @@ -63,18 +63,17 @@ struct opts { /** These functions are implemented at the end */ static void usage(const char *message, const char *arg); -static void parse_opts(struct opts *o, int argc, char *argv[]); +static void parse_opts(struct diff_options *o, int argc, char *argv[]); static int color_printer( const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*); -static void diff_print_stats(git_diff *diff, struct opts *o); -static void compute_diff_no_index(git_diff **diff, struct opts *o); +static void diff_print_stats(git_diff *diff, struct diff_options *o); +static void compute_diff_no_index(git_diff **diff, struct diff_options *o); int lg2_diff(git_repository *repo, int argc, char *argv[]) { git_tree *t1 = NULL, *t2 = NULL; git_diff *diff; - - struct opts o = { + struct diff_options o = { GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT, -1, -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "." }; @@ -166,7 +165,7 @@ int lg2_diff(git_repository *repo, int argc, char *argv[]) return 0; } -static void compute_diff_no_index(git_diff **diff, struct opts *o) { +static void compute_diff_no_index(git_diff **diff, struct diff_options *o) { git_patch *patch = NULL; char *file1_str = NULL; char *file2_str = NULL; @@ -242,11 +241,10 @@ static int color_printer( } /** Parse arguments as copied from git-diff. */ -static void parse_opts(struct opts *o, int argc, char *argv[]) +static void parse_opts(struct diff_options *o, int argc, char *argv[]) { struct args_info args = ARGS_INFO_INIT; - for (args.pos = 1; args.pos < argc; ++args.pos) { const char *a = argv[args.pos]; @@ -343,7 +341,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[]) } /** Display diff output with "--stat", "--numstat", or "--shortstat" */ -static void diff_print_stats(git_diff *diff, struct opts *o) +static void diff_print_stats(git_diff *diff, struct diff_options *o) { git_diff_stats *stats; git_buf b = GIT_BUF_INIT_CONST(NULL, 0); diff --git a/examples/init.c b/examples/init.c index e9841bc93..2f681c5ae 100644 --- a/examples/init.c +++ b/examples/init.c @@ -27,7 +27,7 @@ */ /** Forward declarations of helpers */ -struct opts { +struct init_opts { int no_options; int quiet; int bare; @@ -38,11 +38,11 @@ struct opts { const char *dir; }; static void create_initial_commit(git_repository *repo); -static void parse_opts(struct opts *o, int argc, char *argv[]); +static void parse_opts(struct init_opts *o, int argc, char *argv[]); int lg2_init(git_repository *repo, int argc, char *argv[]) { - struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 }; + struct init_opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 }; parse_opts(&o, argc, argv); @@ -210,7 +210,7 @@ static uint32_t parse_shared(const char *shared) return 0; } -static void parse_opts(struct opts *o, int argc, char *argv[]) +static void parse_opts(struct init_opts *o, int argc, char *argv[]) { struct args_info args = ARGS_INFO_INIT; const char *sharedarg; diff --git a/examples/ls-files.c b/examples/ls-files.c index b82af0124..a23506962 100644 --- a/examples/ls-files.c +++ b/examples/ls-files.c @@ -25,11 +25,11 @@ * This currently supports the default behavior and the `--error-unmatch` option. */ -typedef struct { +struct ls_options { int error_unmatch; char *files[1024]; size_t file_count; -} ls_options; +}; static void usage(const char *message, const char *arg) { @@ -41,12 +41,12 @@ static void usage(const char *message, const char *arg) exit(1); } -static int parse_options(ls_options *opts, int argc, char *argv[]) +static int parse_options(struct ls_options *opts, int argc, char *argv[]) { int parsing_files = 0; int i; - memset(opts, 0, sizeof(ls_options)); + memset(opts, 0, sizeof(struct ls_options)); if (argc < 2) return 0; @@ -78,7 +78,7 @@ static int parse_options(ls_options *opts, int argc, char *argv[]) return 0; } -static int print_paths(ls_options *opts, git_index *index) +static int print_paths(struct ls_options *opts, git_index *index) { size_t i; const git_index_entry *entry; @@ -113,7 +113,7 @@ static int print_paths(ls_options *opts, git_index *index) int lg2_ls_files(git_repository *repo, int argc, char *argv[]) { git_index *index = NULL; - ls_options opts; + struct ls_options opts; int error; if ((error = parse_options(&opts, argc, argv)) < 0) diff --git a/examples/merge.c b/examples/merge.c index 0cdeeeadf..460c06a25 100644 --- a/examples/merge.c +++ b/examples/merge.c @@ -23,7 +23,7 @@ * */ -typedef struct { +struct merge_options { const char **heads; size_t heads_count; @@ -31,7 +31,7 @@ typedef struct { size_t annotated_count; int no_commit : 1; -} merge_options; +}; static void print_usage(void) { @@ -39,7 +39,7 @@ static void print_usage(void) exit(1); } -static void merge_options_init(merge_options *opts) +static void merge_options_init(struct merge_options *opts) { memset(opts, 0, sizeof(*opts)); @@ -49,7 +49,7 @@ static void merge_options_init(merge_options *opts) opts->annotated_count = 0; } -static void opts_add_refish(merge_options *opts, const char *refish) +static void opts_add_refish(struct merge_options *opts, const char *refish) { size_t sz; @@ -60,7 +60,7 @@ static void opts_add_refish(merge_options *opts, const char *refish) opts->heads[opts->heads_count - 1] = refish; } -static void parse_options(const char **repo_path, merge_options *opts, int argc, char **argv) +static void parse_options(const char **repo_path, struct merge_options *opts, int argc, char **argv) { struct args_info args = ARGS_INFO_INIT; @@ -82,7 +82,7 @@ static void parse_options(const char **repo_path, merge_options *opts, int argc, } } -static int resolve_heads(git_repository *repo, merge_options *opts) +static int resolve_heads(git_repository *repo, struct merge_options *opts) { git_annotated_commit **annotated = calloc(opts->heads_count, sizeof(git_annotated_commit *)); size_t annotated_count = 0, i; @@ -200,7 +200,7 @@ static void output_conflicts(git_index *index) git_index_conflict_iterator_free(conflicts); } -static int create_merge_commit(git_repository *repo, git_index *index, merge_options *opts) +static int create_merge_commit(git_repository *repo, git_index *index, struct merge_options *opts) { git_oid tree_oid, commit_oid; git_tree *tree; @@ -276,7 +276,7 @@ cleanup: int lg2_merge(git_repository *repo, int argc, char **argv) { - merge_options opts; + struct merge_options opts; git_index *index; git_repository_state_t state; git_merge_analysis_t analysis; diff --git a/examples/remote.c b/examples/remote.c index 7345f82de..34d886526 100644 --- a/examples/remote.c +++ b/examples/remote.c @@ -30,7 +30,7 @@ enum subcmd { subcmd_show, }; -struct opts { +struct remote_opts { enum subcmd cmd; /* for command-specific args */ @@ -38,20 +38,20 @@ struct opts { char **argv; }; -static int cmd_add(git_repository *repo, struct opts *o); -static int cmd_remove(git_repository *repo, struct opts *o); -static int cmd_rename(git_repository *repo, struct opts *o); -static int cmd_seturl(git_repository *repo, struct opts *o); -static int cmd_show(git_repository *repo, struct opts *o); +static int cmd_add(git_repository *repo, struct remote_opts *o); +static int cmd_remove(git_repository *repo, struct remote_opts *o); +static int cmd_rename(git_repository *repo, struct remote_opts *o); +static int cmd_seturl(git_repository *repo, struct remote_opts *o); +static int cmd_show(git_repository *repo, struct remote_opts *o); static void parse_subcmd( - struct opts *opt, int argc, char **argv); + struct remote_opts *opt, int argc, char **argv); static void usage(const char *msg, const char *arg); int lg2_remote(git_repository *repo, int argc, char *argv[]) { int retval = 0; - struct opts opt = {0}; + struct remote_opts opt = {0}; parse_subcmd(&opt, argc, argv); @@ -77,7 +77,7 @@ int lg2_remote(git_repository *repo, int argc, char *argv[]) return retval; } -static int cmd_add(git_repository *repo, struct opts *o) +static int cmd_add(git_repository *repo, struct remote_opts *o) { char *name, *url; git_remote *remote = {0}; @@ -94,7 +94,7 @@ static int cmd_add(git_repository *repo, struct opts *o) return 0; } -static int cmd_remove(git_repository *repo, struct opts *o) +static int cmd_remove(git_repository *repo, struct remote_opts *o) { char *name; @@ -109,7 +109,7 @@ static int cmd_remove(git_repository *repo, struct opts *o) return 0; } -static int cmd_rename(git_repository *repo, struct opts *o) +static int cmd_rename(git_repository *repo, struct remote_opts *o) { int i, retval; char *old, *new; @@ -134,7 +134,7 @@ static int cmd_rename(git_repository *repo, struct opts *o) return retval; } -static int cmd_seturl(git_repository *repo, struct opts *o) +static int cmd_seturl(git_repository *repo, struct remote_opts *o) { int i, retval, push = 0; char *name = NULL, *url = NULL; @@ -166,7 +166,7 @@ static int cmd_seturl(git_repository *repo, struct opts *o) return 0; } -static int cmd_show(git_repository *repo, struct opts *o) +static int cmd_show(git_repository *repo, struct remote_opts *o) { int i; const char *arg, *name, *fetch, *push; @@ -213,7 +213,7 @@ static int cmd_show(git_repository *repo, struct opts *o) } static void parse_subcmd( - struct opts *opt, int argc, char **argv) + struct remote_opts *opt, int argc, char **argv) { char *arg = argv[1]; enum subcmd cmd = 0; diff --git a/examples/show-index.c b/examples/show-index.c index 34eb41c7b..7aaa45e65 100644 --- a/examples/show-index.c +++ b/examples/show-index.c @@ -14,7 +14,7 @@ #include "common.h" -int lg2_show_index(git_repository *repo, int argc, char** argv) +int lg2_show_index(git_repository *repo, int argc, char **argv) { git_index *index; size_t i, ecount; diff --git a/examples/status.c b/examples/status.c index 979ab7c97..8cf922127 100644 --- a/examples/status.c +++ b/examples/status.c @@ -43,7 +43,7 @@ enum { #define MAX_PATHSPEC 8 -struct opts { +struct status_opts { git_status_options statusopt; char *repodir; char *pathspec[MAX_PATHSPEC]; @@ -55,7 +55,7 @@ struct opts { int repeat; }; -static void parse_opts(struct opts *o, int argc, char *argv[]); +static void parse_opts(struct status_opts *o, int argc, char *argv[]); static void show_branch(git_repository *repo, int format); static void print_long(git_status_list *status); static void print_short(git_repository *repo, git_status_list *status); @@ -64,7 +64,7 @@ static int print_submod(git_submodule *sm, const char *name, void *payload); int lg2_status(git_repository *repo, int argc, char *argv[]) { git_status_list *status; - struct opts o = { GIT_STATUS_OPTIONS_INIT, "." }; + struct status_opts o = { GIT_STATUS_OPTIONS_INIT, "." }; o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | @@ -435,7 +435,7 @@ static int print_submod(git_submodule *sm, const char *name, void *payload) /** * Parse options that git's status command supports. */ -static void parse_opts(struct opts *o, int argc, char *argv[]) +static void parse_opts(struct status_opts *o, int argc, char *argv[]) { struct args_info args = ARGS_INFO_INIT; diff --git a/examples/tag.c b/examples/tag.c index 7bfb92a3e..440829419 100644 --- a/examples/tag.c +++ b/examples/tag.c @@ -31,19 +31,19 @@ */ /** tag_options represents the parsed command line options */ -typedef struct { +struct tag_options { const char *message; const char *pattern; const char *tag_name; const char *target; int num_lines; int force; -} tag_options; +}; /** tag_state represents the current program state for dragging around */ typedef struct { git_repository *repo; - tag_options *opts; + struct tag_options *opts; } tag_state; /** An action to execute based on the command line arguments */ @@ -167,7 +167,7 @@ static void action_list_tags(tag_state *state) static void action_delete_tag(tag_state *state) { - tag_options *opts = state->opts; + struct tag_options *opts = state->opts; git_object *obj; git_buf abbrev_oid = {0}; @@ -191,7 +191,7 @@ static void action_delete_tag(tag_state *state) static void action_create_lighweight_tag(tag_state *state) { git_repository *repo = state->repo; - tag_options *opts = state->opts; + struct tag_options *opts = state->opts; git_oid oid; git_object *target; @@ -213,7 +213,7 @@ static void action_create_lighweight_tag(tag_state *state) static void action_create_tag(tag_state *state) { git_repository *repo = state->repo; - tag_options *opts = state->opts; + struct tag_options *opts = state->opts; git_signature *tagger; git_oid oid; git_object *target; @@ -243,7 +243,7 @@ static void print_usage(void) } /** Parse command line arguments and choose action to run when done */ -static void parse_options(tag_action *action, tag_options *opts, int argc, char **argv) +static void parse_options(tag_action *action, struct tag_options *opts, int argc, char **argv) { args_info args = ARGS_INFO_INIT; *action = &action_list_tags; @@ -281,7 +281,7 @@ static void parse_options(tag_action *action, tag_options *opts, int argc, char } /** Initialize tag_options struct */ -static void tag_options_init(tag_options *opts) +static void tag_options_init(struct tag_options *opts) { memset(opts, 0, sizeof(*opts)); @@ -295,7 +295,7 @@ static void tag_options_init(tag_options *opts) int lg2_tag(git_repository *repo, int argc, char **argv) { - tag_options opts; + struct tag_options opts; tag_action action; tag_state state; -- cgit v1.2.1 From fe42557a6f17db9345f1b5f7cb17a9784e59ea5f Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 6 Nov 2019 11:08:52 +0100 Subject: examples: buff up rev-list by adding OID support This allows the example to be used as a quick revwalk test harness. --- examples/rev-list.c | 80 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/rev-list.c b/examples/rev-list.c index c366ecea2..75fb19e70 100644 --- a/examples/rev-list.c +++ b/examples/rev-list.c @@ -15,16 +15,24 @@ #include "common.h" -static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts); +#include + +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args); +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args); int lg2_rev_list(git_repository *repo, int argc, char **argv) { + struct args_info args = ARGS_INFO_INIT; git_revwalk *walk; git_oid oid; + git_sort_t sort; char buf[GIT_OID_HEXSZ+1]; + check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL); + check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL); - check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL); + git_revwalk_sorting(walk, sort); + check_lg2(revwalk_parse_revs(repo, walk, &args), "parsing revs", NULL); while (!git_revwalk_next(&oid, walk)) { git_oid_fmt(buf, &oid); @@ -32,6 +40,7 @@ int lg2_rev_list(git_repository *repo, int argc, char **argv) printf("%s\n", buf); } + git_revwalk_free(walk); return 0; } @@ -80,33 +89,60 @@ out: return error; } -static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts) +static void print_usage(void) +{ + fprintf(stderr, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] \n"); + exit(-1); +} + +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args) { - int hide, i, error; - unsigned int sorting = GIT_SORT_NONE; + assert(sort && args); + *sort = GIT_SORT_NONE; + + if (args->argc < 1) + print_usage(); + + for (args->pos = 1; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (!strcmp(curr, "--topo-order")) { + *sort |= GIT_SORT_TOPOLOGICAL; + } else if (!strcmp(curr, "--date-order")) { + *sort |= GIT_SORT_TIME; + } else if (!strcmp(curr, "--reverse")) { + *sort |= (*sort & ~GIT_SORT_REVERSE) ^ GIT_SORT_REVERSE; + } else { + break; + } + } + return 0; +} + +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args) +{ + int hide, error; + git_oid oid; hide = 0; - for (i = 0; i < nopts; i++) { - if (!strcmp(opts[i], "--topo-order")) { - sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE); - git_revwalk_sorting(walk, sorting); - } else if (!strcmp(opts[i], "--date-order")) { - sorting = GIT_SORT_TIME | (sorting & GIT_SORT_REVERSE); - git_revwalk_sorting(walk, sorting); - } else if (!strcmp(opts[i], "--reverse")) { - sorting = (sorting & ~GIT_SORT_REVERSE) - | ((sorting & GIT_SORT_REVERSE) ? 0 : GIT_SORT_REVERSE); - git_revwalk_sorting(walk, sorting); - } else if (!strcmp(opts[i], "--not")) { + for (; args->pos < args->argc; ++args->pos) { + const char *curr = args->argv[args->pos]; + + if (!strcmp(curr, "--not")) { hide = !hide; - } else if (opts[i][0] == '^') { - if ((error = push_spec(repo, walk, opts[i] + 1, !hide))) + } else if (curr[0] == '^') { + if ((error = push_spec(repo, walk, curr + 1, !hide))) return error; - } else if (strstr(opts[i], "..")) { - if ((error = push_range(repo, walk, opts[i], hide))) + } else if (strstr(curr, "..")) { + if ((error = push_range(repo, walk, curr, hide))) return error; } else { - if ((error = push_spec(repo, walk, opts[i], hide))) + if (push_spec(repo, walk, curr, hide) == 0) + continue; + + if ((error = git_oid_fromstr(&oid, curr))) + return error; + if ((error = push_commit(walk, &oid, hide))) return error; } } -- cgit v1.2.1