git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] builtin/reflog.c: switch to use parse-options API for delete subcommand

From
John Cai via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 31, 2021, 06:29 UTC
Message-ID
<fffea298642efe85582e4d303bd483979ce64e1d.1640932151.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1175.git.git.1640932151.gitgitgadget@gmail.com>
From: John Cai <johncai86@gmail.com>

Address NEEDSWORK by switching out manual arg parsing for the parse-options API for the delete subcommand.

Signed-off-by: "John Cai" <johncai86@gmail.com>
---
 builtin/reflog.c | 52 +++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 27 deletions(-)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index afaf5ba67e2..a405c38a139 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -11,11 +11,7 @@
 #include "revision.h"
 #include "reachable.h"
 #include "worktree.h"
-#include "parse-options.h"
 
-static const char reflog_delete_usage[] =
-N_("git reflog delete [--rewrite] [--updateref] "
-   "[--dry-run | -n] [--verbose] <refs>...");
 static const char reflog_exists_usage[] =
 N_("git reflog exists <ref>");
 
@@ -641,7 +637,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 		free(collected.e);
 	}
 
-	for (; i < argc; i++) {
+	for (i = 0; i < argc; i++) {
 		char *ref;
 		if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
 			status |= error(_("%s points nowhere!"), argv[i]);
@@ -668,38 +664,40 @@ static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
 	return 0;
 }
 
+static const char * reflog_delete_usage[] = {
+	N_("git reflog delete [--rewrite] [--updateref] "
+   "[--dry-run | -n] [--verbose] <refs>..."),
+	NULL
+};
+
 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 {
 	struct expire_reflog_policy_cb cb;
-	int i, status = 0;
+	int status = 0;
 	unsigned int flags = 0;
 
+	const struct option options[] = {
+		OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
+				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),
+		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_END()
+	};
+
 	memset(&cb, 0, sizeof(cb));
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
-			flags |= EXPIRE_REFLOGS_DRY_RUN;
-		else if (!strcmp(arg, "--rewrite"))
-			flags |= EXPIRE_REFLOGS_REWRITE;
-		else if (!strcmp(arg, "--updateref"))
-			flags |= EXPIRE_REFLOGS_UPDATE_REF;
-		else if (!strcmp(arg, "--verbose"))
-			flags |= EXPIRE_REFLOGS_VERBOSE;
-		else if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		else if (arg[0] == '-')
-			usage(_(reflog_delete_usage));
-		else
-			break;
-	}
+	argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
 
-	if (argc - i < 1)
+	if (argc < 1)
 		return error(_("no reflog specified to delete"));
 
-	for ( ; i < argc; i++) {
+	for (int i = 0; i < argc; i++) {
 		const char *spec = strstr(argv[i], "@{");
 		char *ep, *ref;
 		int recno;
-- 
gitgitgadget
Previous: René ScharfeNext: John Cai via GitGitGadget
Message 7 of 19 in “reflog.c: switch to use parse-options API”
  1. 0/2 reflog.c: switch to use parse-options APIJohn Cai via GitGitGadget, Dec 31, 2021
  2. 1/2 builtin/reflog.c: use parse-options for expire subcommandJohn Cai via GitGitGadget, Dec 31, 2021
  3. John CaiJan 1, 2022
  4. René ScharfeJan 1, 2022
  5. John CaiJan 1, 2022
  6. René ScharfeJan 2, 2022
  7. 2/2 builtin/reflog.c: switch to use parse-options API for delete subcommandJohn Cai via GitGitGadget, Dec 31, 2021
  8. 0/2 reflog.c: switch to use parse-options APIJohn Cai via GitGitGadget, Jan 3, 2022
  9. 1/2 parse-options.h: add parse_opt_expiry_date helperJohn Cai via GitGitGadget, Jan 3, 2022
  10. Junio C HamanoJan 4, 2022
  11. 2/2 builtin/reflog.c: switch to use parse-options API for delete subcommandJohn Cai via GitGitGadget, Jan 3, 2022
  12. Junio C HamanoJan 4, 2022
  13. builtin/reflog.c: use parse-options api for expire, delete subcommandsJohn Cai via GitGitGadget, Jan 4, 2022
  14. Junio C HamanoJan 4, 2022
  15. builtin/reflog.c: use parse-options api for expire, delete subcommandsJohn Cai via GitGitGadget, Jan 5, 2022
  16. Junio C HamanoJan 5, 2022
  17. builtin/reflog.c: use parse-options api for expire, delete subcommandsJohn Cai via GitGitGadget, Jan 6, 2022
  18. Junio C HamanoJan 6, 2022
  19. John CaiJan 6, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.