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 <peff@peff.net>
Jeff King committed
May 7, 2012 at 15:18 UTC
036dbbfb2d9127dcef3d742b99ac8677006f6d33
1 file changed
+59
-58
builtin/commit.c
+59
-58
@@ -129,59 +129,6 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
129
return 0;
130
}
131
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
-
132
static void determine_whence(struct wt_status *s)
133
{
134
if (file_exists(git_path("MERGE_HEAD")))
@@ -1046,6 +993,7 @@ static const char *read_commit_message(const char *name)
993
}
994
995
static int parse_and_validate_options(int argc, const char *argv[],
996
+ const struct option *options,
997
const char * const usage[],
998
const char *prefix,
999
struct commit *current_head,
@@ -1053,8 +1001,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
1001
{
1002
int f = 0;
1003
1056
- argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
1057
- 0);
1004
+ argc = parse_options(argc, argv, prefix, options, usage, 0);
1005
1006
if (force_author && !strchr(force_author, '>'))
1007
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)
1169
1170
int cmd_status(int argc, const char **argv, const char *prefix)
1171
{
1225
- struct wt_status s;
1172
+ static struct wt_status s;
1173
int fd;
1174
unsigned char sha1[20];
1175
static struct option builtin_status_options[] = {
@@ -1422,6 +1369,60 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
1369
1370
int cmd_commit(int argc, const char **argv, const char *prefix)
1371
{
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
+
1426
struct strbuf sb = STRBUF_INIT;
1427
struct strbuf author_ident = STRBUF_INIT;
1428
const char *index_file, *reflog_msg;
@@ -1431,7 +1432,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1432
struct commit_list *parents = NULL, **pptr = &parents;
1433
struct stat statbuf;
1434
int allow_fast_forward = 1;
1434
- struct wt_status s;
1435
struct commit *current_head = NULL;
1436
struct commit_extra_header *extra = NULL;
1437
@@ -1449,7 +1449,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1449
if (!current_head || parse_commit(current_head))
1450
die(_("could not parse HEAD commit"));
1451
}
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,
1454
prefix, current_head, &s);
1455
if (dry_run)
1456
return dry_run_commit(argc, argv, prefix, current_head, &s);