From: John Cai via GitGitGadget Date: Tue, 04 Jan 2022 17:41:01 GMT Subject: [PATCH v3] builtin/reflog.c: use parse-options api for expire, delete subcommands Message-ID: In-Reply-To: From: John Cai Switching out manual arg parsing for the parse-options API for the expire and delete subcommands. Move explicit_expiry flag into cmd_reflog_expire_cb struct so callbacks can set both the value of the timestamp as well as the explicit_expiry flag. Signed-off-by: "John Cai" --- reflog.c: switch to use parse-options API Switch out manual argv parsing for the reflog expire subcommand to use the parse-options API. This reduces code redundancy by consolidating option parsing logic. changes since v2: * got rid of parse_opt_expiry_date helper based on feedback from Junio. Just call parse_expiry_date directly in the callbacks, and don't worry about unset, as we can just set the PARSE_OPT_NONEG flag for --expire and --expire-unreachable since the original code didn't support a negated version of those flags anyway. The documentation also explicitly states to use "never" as the argument. * fix places where we set int i inside a for loop construct. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1175%2Fjohn-cai%2Fjc%2Freflog-switch-to-option-parse-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1175/john-cai/jc/reflog-switch-to-option-parse-v3 Pull-Request: https://github.com/git/git/pull/1175 Range-diff vs v2: 1: bcd74559c24 < -: ----------- parse-options.h: add parse_opt_expiry_date helper 2: f9de21e0f26 ! 1: 99a97e6ee52 builtin/reflog.c: switch to use parse-options API for delete subcommand @@ Metadata Author: John Cai ## Commit message ## - builtin/reflog.c: switch to use parse-options API for delete subcommand + builtin/reflog.c: use parse-options api for expire, delete subcommands - Address NEEDSWORK by switching out manual arg parsing for the - parse-options API for the delete subcommand. + Switching out manual arg parsing for the parse-options API for the + expire and delete subcommands. - Moves explicit_expiry flag into cmd_reflog_expire_cb struct so a - callback can set both the value of the expiration as well as the - explicit_expiry flag. + Move explicit_expiry flag into cmd_reflog_expire_cb struct so callbacks + can set both the value of the timestamp as well as the explicit_expiry + flag. Signed-off-by: "John Cai" @@ builtin/reflog.c N_("git reflog exists "); @@ builtin/reflog.c: static timestamp_t default_reflog_expire_unreachable; + struct cmd_reflog_expire_cb { - struct rev_info revs; int stalefix; + int explicit_expiry; timestamp_t expire_total; @@ builtin/reflog.c: static int reflog_expire_config(const char *var, const char *v +static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref) { struct reflog_expire_cfg *ent; -- + - if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH)) + if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH)) return; /* both given explicitly -- nothing to tweak */ @@ builtin/reflog.c: static void set_reflog_expiry_param(struct cmd_reflog_expire_c + int unset) +{ + struct cmd_reflog_expire_cb *cmd = opt->value; ++ ++ if (parse_expiry_date(arg, &cmd->expire_unreachable)) ++ die(_("malformed expiration date '%s'"), arg); ++ + cmd->explicit_expiry |= EXPIRE_UNREACH; -+ return parse_opt_expiry_date(&cmd->expire_unreachable, arg, unset); ++ return 0; +} + +static int expire_total_callback(const struct option *opt, @@ builtin/reflog.c: static void set_reflog_expiry_param(struct cmd_reflog_expire_c + int unset) +{ + struct cmd_reflog_expire_cb *cmd = opt->value; ++ ++ if (parse_expiry_date(arg, &cmd->expire_total)) ++ die(_("malformed expiration date '%s'"), arg); ++ + cmd->explicit_expiry |= EXPIRE_TOTAL; -+ return parse_opt_expiry_date(&cmd->expire_total, arg, unset); ++ return 0; +} + static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) { - struct expire_reflog_policy_cb cb; + struct cmd_reflog_expire_cb cmd = { 0 }; timestamp_t now = time(NULL); int i, status, do_all, all_worktrees = 1; - int explicit_expiry = 0; unsigned int flags = 0; + int verbose = 0; + reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent; + const struct option options[] = { + OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"), -+ EXPIRE_REFLOGS_DRY_RUN), ++ EXPIRE_REFLOGS_DRY_RUN), + OPT_BIT(0, "rewrite", &flags, -+ N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"), -+ EXPIRE_REFLOGS_REWRITE), ++ N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"), ++ EXPIRE_REFLOGS_REWRITE), + OPT_BIT(0, "updateref", &flags, -+ N_("update the reference to the value of the top reflog entry"), -+ EXPIRE_REFLOGS_UPDATE_REF), -+ OPT_BIT(0, "verbose", &flags, N_("print extra information on screen."), -+ EXPIRE_REFLOGS_VERBOSE), -+ OPT_CALLBACK(0, "expire", &cb.cmd, N_("timestamp"), -+ N_("prune entries older than the specified time"), expire_total_callback), -+ OPT_CALLBACK(0, "expire-unreachable", &cb.cmd, N_("timestamp"), ++ N_("update the reference to the value of the top reflog entry"), ++ EXPIRE_REFLOGS_UPDATE_REF), ++ OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen.")), ++ OPT_CALLBACK_F(0, "expire", &cmd, N_("timestamp"), ++ N_("prune entries older than the specified time"), ++ PARSE_OPT_NONEG, ++ expire_total_callback), ++ OPT_CALLBACK_F(0, "expire-unreachable", &cmd, N_("timestamp"), + N_("prune entries older than