From patchwork Tue Jan 4 17:41:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Cai X-Patchwork-Id: 12703735 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D381AC433EF for ; Tue, 4 Jan 2022 17:41:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235860AbiADRlF (ORCPT ); Tue, 4 Jan 2022 12:41:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235846AbiADRlE (ORCPT ); Tue, 4 Jan 2022 12:41:04 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E109C061761 for ; Tue, 4 Jan 2022 09:41:04 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id w20so68662642wra.9 for ; Tue, 04 Jan 2022 09:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=iyccWfqtoPFUuhiChwRzz858sJT1amFqmf2FYBKLwnU=; b=cAMcCA5xMS3WgSTsXoj3DEL3h/8zGW87coAaYNwspx5FRtYUCyW1nlC5aeve8xRzuR G1eDAy/JgNPS5err+zRCsX2kNQsGWVvZVNlfBPijIzW/cN94U7f1Cr/eX8aiHZmbivXm VsxT+LSkvvIrho7apuf+K/8ycfux35DfIIhnguQjETepBs/F7o5oi2altoPv033d1f56 +ldA1K/8n7nHmxn/VbC0aEmM4MGAtTKMw9FgLKNlG/796s/fZNAHWVbC6GaiWOgTeYEH bH9RqR0vVKeUkhYETxBo6Hj6PuzLyvzwhk2/D6udTqzsvB4MeqFPshT+p3LqeSzXB6tL jM3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=iyccWfqtoPFUuhiChwRzz858sJT1amFqmf2FYBKLwnU=; b=3DYG0lmRF6N67ro6ws7azDdNN8+MZ7cguLWDkQ+izHrDugFH/tp2g9BzGL3w/q/3Ip z4R5xnpRBsxB0nQXw1FTV7nhdeCKHrc0HxBsGPItTUDNKr7wQK0cVD5eA0DB/opY5kuF LmSiw8+jJZtDi5hf77LKQSagabiSqz34bLqdsBjE6rRO3WcYzT5diabqEgP6beCegypU qX01sfz/FJrwQru+8Ohh830UyHS0K7Ot2pB3tMm64JUC2uDoORgFHrXhIIuu+CHgf7MZ clbjPCgNwKPXIPYdJyizbsAhmK6OO0sVS/8+zIdCwDmvPIBC45AGBHhCLDjtY0lLen9d NX5A== X-Gm-Message-State: AOAM533ME14Q4XwgNfo1nnwIRicS7iz+M6YPyHILp1eXm8aoRp1xVO66 qENaRQ9orE1t3gDT3E9ZS1Bk8URBB8U= X-Google-Smtp-Source: ABdhPJxYjeztNaMWPOG0UlLA3IWMn3i5BBCyj2btgItbL2M8Xjc+3U/8I836mINjnI9FSgNetVv/QA== X-Received: by 2002:a5d:64a1:: with SMTP id m1mr42318958wrp.503.1641318062472; Tue, 04 Jan 2022 09:41:02 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a3sm3181062wri.89.2022.01.04.09.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jan 2022 09:41:02 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 04 Jan 2022 17:41:01 +0000 Subject: [PATCH v3] builtin/reflog.c: use parse-options api for expire, delete subcommands Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: =?utf-8?b?UmVuw6k=?= Scharfe , John Cai , John Cai Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: John Cai 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