|
5 | 5 | #include "worktree.h"
|
6 | 6 | #include "reflog.h"
|
7 | 7 |
|
8 |
| -static const char reflog_exists_usage[] = |
9 |
| -N_("git reflog exists <ref>"); |
| 8 | +#define BUILTIN_REFLOG_SHOW_USAGE \ |
| 9 | + N_("git reflog [show] [<log-options>] [<ref>]") |
| 10 | + |
| 11 | +#define BUILTIN_REFLOG_EXPIRE_USAGE \ |
| 12 | + N_("git reflog expire [--expire=<time>] [--expire-unreachable=<time>]\n" \ |
| 13 | + " [--rewrite] [--updateref] [--stale-fix]\n" \ |
| 14 | + " [--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>...]") |
| 15 | + |
| 16 | +#define BUILTIN_REFLOG_DELETE_USAGE \ |
| 17 | + N_("git reflog delete [--rewrite] [--updateref]\n" \ |
| 18 | + " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...") |
| 19 | + |
| 20 | +#define BUILTIN_REFLOG_EXISTS_USAGE \ |
| 21 | + N_("git reflog exists <ref>") |
| 22 | + |
| 23 | +static const char *const reflog_show_usage[] = { |
| 24 | + BUILTIN_REFLOG_SHOW_USAGE, |
| 25 | + NULL, |
| 26 | +}; |
| 27 | + |
| 28 | +static const char *const reflog_expire_usage[] = { |
| 29 | + BUILTIN_REFLOG_EXPIRE_USAGE, |
| 30 | + NULL |
| 31 | +}; |
| 32 | + |
| 33 | +static const char *const reflog_delete_usage[] = { |
| 34 | + BUILTIN_REFLOG_DELETE_USAGE, |
| 35 | + NULL |
| 36 | +}; |
| 37 | + |
| 38 | +static const char *const reflog_exists_usage[] = { |
| 39 | + BUILTIN_REFLOG_EXISTS_USAGE, |
| 40 | + NULL, |
| 41 | +}; |
| 42 | + |
| 43 | +static const char *const reflog_usage[] = { |
| 44 | + BUILTIN_REFLOG_SHOW_USAGE, |
| 45 | + BUILTIN_REFLOG_EXPIRE_USAGE, |
| 46 | + BUILTIN_REFLOG_DELETE_USAGE, |
| 47 | + BUILTIN_REFLOG_EXISTS_USAGE, |
| 48 | + NULL |
| 49 | +}; |
10 | 50 |
|
11 | 51 | static timestamp_t default_reflog_expire;
|
12 | 52 | static timestamp_t default_reflog_expire_unreachable;
|
@@ -147,14 +187,6 @@ static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char
|
147 | 187 | cb->expire_unreachable = default_reflog_expire_unreachable;
|
148 | 188 | }
|
149 | 189 |
|
150 |
| -static const char * reflog_expire_usage[] = { |
151 |
| - N_("git reflog expire [--expire=<time>] " |
152 |
| - "[--expire-unreachable=<time>] " |
153 |
| - "[--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] " |
154 |
| - "[--verbose] [--all] <refs>..."), |
155 |
| - NULL |
156 |
| -}; |
157 |
| - |
158 | 190 | static int expire_unreachable_callback(const struct option *opt,
|
159 | 191 | const char *arg,
|
160 | 192 | int unset)
|
@@ -183,6 +215,19 @@ static int expire_total_callback(const struct option *opt,
|
183 | 215 | return 0;
|
184 | 216 | }
|
185 | 217 |
|
| 218 | +static int cmd_reflog_show(int argc, const char **argv, const char *prefix) |
| 219 | +{ |
| 220 | + struct option options[] = { |
| 221 | + OPT_END() |
| 222 | + }; |
| 223 | + |
| 224 | + parse_options(argc, argv, prefix, options, reflog_show_usage, |
| 225 | + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 | |
| 226 | + PARSE_OPT_KEEP_UNKNOWN); |
| 227 | + |
| 228 | + return cmd_log_reflog(argc, argv, prefix); |
| 229 | +} |
| 230 | + |
186 | 231 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
|
187 | 232 | {
|
188 | 233 | struct cmd_reflog_expire_cb cmd = { 0 };
|
@@ -304,12 +349,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
|
304 | 349 | return status;
|
305 | 350 | }
|
306 | 351 |
|
307 |
| -static const char * reflog_delete_usage[] = { |
308 |
| - N_("git reflog delete [--rewrite] [--updateref] " |
309 |
| - "[--dry-run | -n] [--verbose] <refs>..."), |
310 |
| - NULL |
311 |
| -}; |
312 |
| - |
313 | 352 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
|
314 | 353 | {
|
315 | 354 | int i, status = 0;
|
@@ -342,57 +381,62 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
|
342 | 381 |
|
343 | 382 | static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
|
344 | 383 | {
|
345 |
| - int i, start = 0; |
346 |
| - |
347 |
| - for (i = 1; i < argc; i++) { |
348 |
| - const char *arg = argv[i]; |
349 |
| - if (!strcmp(arg, "--")) { |
350 |
| - i++; |
351 |
| - break; |
352 |
| - } |
353 |
| - else if (arg[0] == '-') |
354 |
| - usage(_(reflog_exists_usage)); |
355 |
| - else |
356 |
| - break; |
357 |
| - } |
358 |
| - |
359 |
| - start = i; |
| 384 | + struct option options[] = { |
| 385 | + OPT_END() |
| 386 | + }; |
| 387 | + const char *refname; |
360 | 388 |
|
361 |
| - if (argc - start != 1) |
362 |
| - usage(_(reflog_exists_usage)); |
| 389 | + argc = parse_options(argc, argv, prefix, options, reflog_exists_usage, |
| 390 | + 0); |
| 391 | + if (!argc) |
| 392 | + usage_with_options(reflog_exists_usage, options); |
363 | 393 |
|
364 |
| - if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL)) |
365 |
| - die(_("invalid ref format: %s"), argv[start]); |
366 |
| - return !reflog_exists(argv[start]); |
| 394 | + refname = argv[0]; |
| 395 | + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) |
| 396 | + die(_("invalid ref format: %s"), refname); |
| 397 | + return !reflog_exists(refname); |
367 | 398 | }
|
368 | 399 |
|
369 | 400 | /*
|
370 | 401 | * main "reflog"
|
371 | 402 | */
|
372 | 403 |
|
373 |
| -static const char reflog_usage[] = |
374 |
| -"git reflog [ show | expire | delete | exists ]"; |
375 |
| - |
376 | 404 | int cmd_reflog(int argc, const char **argv, const char *prefix)
|
377 | 405 | {
|
378 |
| - if (argc > 1 && !strcmp(argv[1], "-h")) |
379 |
| - usage(_(reflog_usage)); |
| 406 | + struct option options[] = { |
| 407 | + OPT_END() |
| 408 | + }; |
380 | 409 |
|
381 |
| - /* With no command, we default to showing it. */ |
382 |
| - if (argc < 2 || *argv[1] == '-') |
383 |
| - return cmd_log_reflog(argc, argv, prefix); |
| 410 | + argc = parse_options(argc, argv, prefix, options, reflog_usage, |
| 411 | + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 | |
| 412 | + PARSE_OPT_KEEP_UNKNOWN | |
| 413 | + PARSE_OPT_NO_INTERNAL_HELP); |
384 | 414 |
|
385 |
| - if (!strcmp(argv[1], "show")) |
386 |
| - return cmd_log_reflog(argc - 1, argv + 1, prefix); |
| 415 | + /* |
| 416 | + * With "git reflog" we default to showing it. !argc is |
| 417 | + * impossible with PARSE_OPT_KEEP_ARGV0. |
| 418 | + */ |
| 419 | + if (argc == 1) |
| 420 | + goto log_reflog; |
387 | 421 |
|
388 |
| - if (!strcmp(argv[1], "expire")) |
389 |
| - return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
| 422 | + if (!strcmp(argv[1], "-h")) |
| 423 | + usage_with_options(reflog_usage, options); |
| 424 | + else if (*argv[1] == '-') |
| 425 | + goto log_reflog; |
390 | 426 |
|
391 |
| - if (!strcmp(argv[1], "delete")) |
| 427 | + if (!strcmp(argv[1], "show")) |
| 428 | + return cmd_reflog_show(argc - 1, argv + 1, prefix); |
| 429 | + else if (!strcmp(argv[1], "expire")) |
| 430 | + return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
| 431 | + else if (!strcmp(argv[1], "delete")) |
392 | 432 | return cmd_reflog_delete(argc - 1, argv + 1, prefix);
|
393 |
| - |
394 |
| - if (!strcmp(argv[1], "exists")) |
| 433 | + else if (!strcmp(argv[1], "exists")) |
395 | 434 | return cmd_reflog_exists(argc - 1, argv + 1, prefix);
|
396 | 435 |
|
| 436 | + /* |
| 437 | + * Fall-through for e.g. "git reflog -1", "git reflog master", |
| 438 | + * as well as the plain "git reflog" above goto above. |
| 439 | + */ |
| 440 | +log_reflog: |
397 | 441 | return cmd_log_reflog(argc, argv, prefix);
|
398 | 442 | }
|
0 commit comments