Skip to content

Commit 6b61ec0

Browse files
MadCodergitster
authored andcommitted
revisions: refactor handle_revision_opt into parse_revision_opt.
It seems we're using handle_revision_opt the same way each time, have a wrapper around it that does the 9-liner we copy each time instead. handle_revision_opt can be static in the module for now, it's always possible to make it public again if needed. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 14ec9cb commit 6b61ec0

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

builtin-blame.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,8 +2305,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23052305
parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
23062306
PARSE_OPT_KEEP_ARGV0);
23072307
for (;;) {
2308-
int n;
2309-
23102308
switch (parse_options_step(&ctx, options, blame_opt_usage)) {
23112309
case PARSE_OPT_HELP:
23122310
exit(129);
@@ -2320,14 +2318,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23202318
ctx.argv[0] = "--children";
23212319
reverse = 1;
23222320
}
2323-
n = handle_revision_opt(&revs, ctx.argc, ctx.argv,
2324-
&ctx.cpidx, ctx.out);
2325-
if (n <= 0) {
2326-
error("unknown option `%s'", ctx.argv[0]);
2327-
usage_with_options(blame_opt_usage, options);
2328-
}
2329-
ctx.argv += n;
2330-
ctx.argc -= n;
2321+
parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
23312322
}
23322323
parse_done:
23332324
argc = parse_options_end(&ctx);

builtin-shortlog.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,13 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
255255
PARSE_OPT_KEEP_ARGV0);
256256

257257
for (;;) {
258-
int n;
259258
switch (parse_options_step(&ctx, options, shortlog_usage)) {
260259
case PARSE_OPT_HELP:
261260
exit(129);
262261
case PARSE_OPT_DONE:
263262
goto parse_done;
264263
}
265-
n = handle_revision_opt(&rev, ctx.argc, ctx.argv,
266-
&ctx.cpidx, ctx.out);
267-
if (n <= 0) {
268-
error("unknown option `%s'", ctx.argv[0]);
269-
usage_with_options(shortlog_usage, options);
270-
}
271-
ctx.argv += n;
272-
ctx.argc -= n;
264+
parse_revision_opt(&rev, &ctx, options, shortlog_usage);
273265
}
274266
parse_done:
275267
argc = parse_options_end(&ctx);

revision.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,8 @@ static void add_ignore_packed(struct rev_info *revs, const char *name)
957957
revs->ignore_packed[num] = NULL;
958958
}
959959

960-
int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
961-
int *unkc, const char **unkv)
960+
static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
961+
int *unkc, const char **unkv)
962962
{
963963
const char *arg = argv[0];
964964

@@ -1163,6 +1163,20 @@ int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
11631163
return 1;
11641164
}
11651165

1166+
void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1167+
const struct option *options,
1168+
const char * const usagestr[])
1169+
{
1170+
int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1171+
&ctx->cpidx, ctx->out);
1172+
if (n <= 0) {
1173+
error("unknown option `%s'", ctx->argv[0]);
1174+
usage_with_options(usagestr, options);
1175+
}
1176+
ctx->argv += n;
1177+
ctx->argc -= n;
1178+
}
1179+
11661180
/*
11671181
* Parse revision information, filling in the "rev_info" structure,
11681182
* and removing the used arguments from the argument list.

revision.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef REVISION_H
22
#define REVISION_H
33

4+
#include "parse-options.h"
5+
46
#define SEEN (1u<<0)
57
#define UNINTERESTING (1u<<1)
68
#define TREESAME (1u<<2)
@@ -119,8 +121,9 @@ volatile show_early_output_fn_t show_early_output;
119121

120122
extern void init_revisions(struct rev_info *revs, const char *prefix);
121123
extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
122-
extern int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
123-
int *unkc, const char **unkv);
124+
extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
125+
const struct option *options,
126+
const char * const usagestr[]);
124127
extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
125128

126129
extern int prepare_revision_walk(struct rev_info *revs);

0 commit comments

Comments
 (0)