Skip to content

Commit 036dbbf

Browse files
committed
commit: refactor option parsing
The options are declared as a static global, but really they need only be accessible from cmd_commit. Additionally, declare the "struct wt_status" in cmd_commit and cmd_status as static at the top of each function; this will let the options lists reference them directly, which will facilitate further cleanups. Signed-off-by: Jeff King <[email protected]>
1 parent edf1412 commit 036dbbf

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

builtin/commit.c

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -129,59 +129,6 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
129129
return 0;
130130
}
131131

132-
static struct option builtin_commit_options[] = {
133-
OPT__QUIET(&quiet, "suppress summary after successful commit"),
134-
OPT__VERBOSE(&verbose, "show diff in commit message template"),
135-
136-
OPT_GROUP("Commit message options"),
137-
OPT_FILENAME('F', "file", &logfile, "read message from file"),
138-
OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
139-
OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
140-
OPT_CALLBACK('m', "message", &message, "message", "commit message", opt_parse_m),
141-
OPT_STRING('c', "reedit-message", &edit_message, "commit", "reuse and edit message from specified commit"),
142-
OPT_STRING('C', "reuse-message", &use_message, "commit", "reuse message from specified commit"),
143-
OPT_STRING(0, "fixup", &fixup_message, "commit", "use autosquash formatted message to fixup specified commit"),
144-
OPT_STRING(0, "squash", &squash_message, "commit", "use autosquash formatted message to squash specified commit"),
145-
OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C/-c/--amend)"),
146-
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
147-
OPT_FILENAME('t', "template", &template_file, "use specified template file"),
148-
OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
149-
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
150-
OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
151-
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
152-
"GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
153-
/* end commit message options */
154-
155-
OPT_GROUP("Commit contents options"),
156-
OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
157-
OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
158-
OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
159-
OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
160-
OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
161-
OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
162-
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
163-
OPT_SET_INT(0, "short", &status_format, "show status concisely",
164-
STATUS_FORMAT_SHORT),
165-
OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
166-
OPT_SET_INT(0, "porcelain", &status_format,
167-
"machine-readable output", STATUS_FORMAT_PORCELAIN),
168-
OPT_BOOLEAN('z', "null", &null_termination,
169-
"terminate entries with NUL"),
170-
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
171-
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
172-
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
173-
/* end commit contents options */
174-
175-
{ OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
176-
"ok to record an empty change",
177-
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
178-
{ OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
179-
"ok to record a change with an empty message",
180-
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
181-
182-
OPT_END()
183-
};
184-
185132
static void determine_whence(struct wt_status *s)
186133
{
187134
if (file_exists(git_path("MERGE_HEAD")))
@@ -1046,15 +993,15 @@ static const char *read_commit_message(const char *name)
1046993
}
1047994

1048995
static int parse_and_validate_options(int argc, const char *argv[],
996+
const struct option *options,
1049997
const char * const usage[],
1050998
const char *prefix,
1051999
struct commit *current_head,
10521000
struct wt_status *s)
10531001
{
10541002
int f = 0;
10551003

1056-
argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
1057-
0);
1004+
argc = parse_options(argc, argv, prefix, options, usage, 0);
10581005

10591006
if (force_author && !strchr(force_author, '>'))
10601007
force_author = find_author_by_nickname(force_author);
@@ -1222,7 +1169,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
12221169

12231170
int cmd_status(int argc, const char **argv, const char *prefix)
12241171
{
1225-
struct wt_status s;
1172+
static struct wt_status s;
12261173
int fd;
12271174
unsigned char sha1[20];
12281175
static struct option builtin_status_options[] = {
@@ -1422,6 +1369,60 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
14221369

14231370
int cmd_commit(int argc, const char **argv, const char *prefix)
14241371
{
1372+
static struct wt_status s;
1373+
static struct option builtin_commit_options[] = {
1374+
OPT__QUIET(&quiet, "suppress summary after successful commit"),
1375+
OPT__VERBOSE(&verbose, "show diff in commit message template"),
1376+
1377+
OPT_GROUP("Commit message options"),
1378+
OPT_FILENAME('F', "file", &logfile, "read message from file"),
1379+
OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
1380+
OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
1381+
OPT_CALLBACK('m', "message", &message, "message", "commit message", opt_parse_m),
1382+
OPT_STRING('c', "reedit-message", &edit_message, "commit", "reuse and edit message from specified commit"),
1383+
OPT_STRING('C', "reuse-message", &use_message, "commit", "reuse message from specified commit"),
1384+
OPT_STRING(0, "fixup", &fixup_message, "commit", "use autosquash formatted message to fixup specified commit"),
1385+
OPT_STRING(0, "squash", &squash_message, "commit", "use autosquash formatted message to squash specified commit"),
1386+
OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C/-c/--amend)"),
1387+
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
1388+
OPT_FILENAME('t', "template", &template_file, "use specified template file"),
1389+
OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
1390+
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
1391+
OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
1392+
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
1393+
"GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1394+
/* end commit message options */
1395+
1396+
OPT_GROUP("Commit contents options"),
1397+
OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
1398+
OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
1399+
OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
1400+
OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
1401+
OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
1402+
OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
1403+
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
1404+
OPT_SET_INT(0, "short", &status_format, "show status concisely",
1405+
STATUS_FORMAT_SHORT),
1406+
OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
1407+
OPT_SET_INT(0, "porcelain", &status_format,
1408+
"machine-readable output", STATUS_FORMAT_PORCELAIN),
1409+
OPT_BOOLEAN('z', "null", &null_termination,
1410+
"terminate entries with NUL"),
1411+
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
1412+
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
1413+
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1414+
/* end commit contents options */
1415+
1416+
{ OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
1417+
"ok to record an empty change",
1418+
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1419+
{ OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
1420+
"ok to record a change with an empty message",
1421+
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1422+
1423+
OPT_END()
1424+
};
1425+
14251426
struct strbuf sb = STRBUF_INIT;
14261427
struct strbuf author_ident = STRBUF_INIT;
14271428
const char *index_file, *reflog_msg;
@@ -1431,7 +1432,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14311432
struct commit_list *parents = NULL, **pptr = &parents;
14321433
struct stat statbuf;
14331434
int allow_fast_forward = 1;
1434-
struct wt_status s;
14351435
struct commit *current_head = NULL;
14361436
struct commit_extra_header *extra = NULL;
14371437

@@ -1449,7 +1449,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14491449
if (!current_head || parse_commit(current_head))
14501450
die(_("could not parse HEAD commit"));
14511451
}
1452-
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1452+
argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1453+
builtin_commit_usage,
14531454
prefix, current_head, &s);
14541455
if (dry_run)
14551456
return dry_run_commit(argc, argv, prefix, current_head, &s);

0 commit comments

Comments
 (0)