+
Skip to content

[Outreachy] check-ref-format: parse-options #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions builtin/check-ref-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include "refs.h"
#include "builtin.h"
#include "strbuf.h"
#include "parse-options.h"

static const char builtin_check_ref_format_usage[] =
"git check-ref-format [--normalize] [<options>] <refname>\n"
" or: git check-ref-format --branch <branchname-shorthand>";
static const char * const builtin_check_ref_format_usage[] = {
N_("git check-ref-format [--normalize] [<options>] <refname>\n"),
N_(" or: git check-ref-format --branch <branchname-shorthand>"),
NULL,
};

/*
* Return a copy of refname but with leading slashes removed and runs
Expand Down Expand Up @@ -53,31 +56,28 @@ static int check_ref_format_branch(const char *arg)

int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
{
int i;
int normalize = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think normalize still needs to be initialized, lest it is used in an uninitialized state.

enum {
CHECK_REF_FORMAT_BRANCH,
};
int i = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to get rid of i now. Its role was to iterate through the command-line options, which is now no longer performed in this function.

int verbose;
int normalize;
int allow_onelevel;
int refspec_pattern;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these need to be initialized to default values.

int flags = 0;
const char *refname;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage(builtin_check_ref_format_usage);
struct option options[] = {
OPT__VERBOSE(&verbose, N_("be verbose")),
OPT_GROUP(""),
OPT_CMDMODE( 0 , "branch", &check_ref_format_branch, N_("branch"), CHECK_REF_FORMAT_BRANCH),
OPT_BOOL( 0 , "normalize", &normalize, N_("normalize tracked files")),
OPT_BOOL( 0 , "allow-onelevel", &allow_onelevel, N_("allow one level")),
OPT_BOOL( 0 , "refspec-pattern", &refspec_pattern, N_("refspec pattern")),
OPT_END(),
};

if (argc == 3 && !strcmp(argv[1], "--branch"))
return check_ref_format_branch(argv[2]);

for (i = 1; i < argc && argv[i][0] == '-'; i++) {
if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
normalize = 1;
else if (!strcmp(argv[i], "--allow-onelevel"))
flags |= REFNAME_ALLOW_ONELEVEL;
else if (!strcmp(argv[i], "--no-allow-onelevel"))
flags &= ~REFNAME_ALLOW_ONELEVEL;
else if (!strcmp(argv[i], "--refspec-pattern"))
flags |= REFNAME_REFSPEC_PATTERN;
else
usage(builtin_check_ref_format_usage);
}
if (! (i == argc - 1))
usage(builtin_check_ref_format_usage);
argc = parse_options(argc, argv, prefix, options, builtin_check_ref_format_usage, PARSE_OPT_KEEP_ARGV0);

refname = argv[i];
if (normalize)
Expand Down
2 changes: 1 addition & 1 deletion git.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ static struct cmd_struct commands[] = {
{ "check-attr", cmd_check_attr, RUN_SETUP },
{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
{ "check-ref-format", cmd_check_ref_format, NO_PARSEOPT },
{ "check-ref-format", cmd_check_ref_format },
{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
{ "checkout-index", cmd_checkout_index,
RUN_SETUP | NEED_WORK_TREE},
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载