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

[PATCH 6/9 - v3] builtin-init-db.c: use parse_options()

From
Michele Ballabio <barra_cuda@katamail.com>
Date
Jul 25, 2008, 19:53 UTC
Message-ID
<1217015608-26205-1-git-send-email-barra_cuda@katamail.com>
In-Reply-To
<4889EF45.1040603@free.fr>
Signed-off-by: Michele Ballabio <barra_cuda@katamail.com>
---
On Friday 25 July 2008, Olivier Marin wrote:
Show 10 quoted lines
> Michele Ballabio a écrit :
> > 
> >>> +		  PARSE_OPT_OPTARG, parse_opt_shared_cb, PERM_GROUP },
> >> Are you sure the default value is really used here?
> > 
> > Yes. Perhaps I don't understand your question. Can you explain what you mean?
> 
> If I read the code correctly in parse-options.c, with OPTION_CALLBACK, the
> default value is not "automatically" used. You can use it in your callback
> if you want, but because you don't, I think it's never used.

Oh, you're right, but git_config_perm() handles NULL just fine, so I can remove it. Done in this patch, thanks.

Show 23 quoted lines
> what I suggested is more something like:
> 
> static int parse_opt_shared_cb(const struct option *opt, const char *arg,
> 			       int unset)
> {
> 	*(int *)(opt->value) = unset ? -1 : git_config_perm("arg", arg);
> 	return 0;
> }
> 
> int shared = -1;
> 
> { OPTION_CALLBACK, 0, "shared", &shared,
> 	"permissions", "setup as shared repository",
> 	PARSE_OPT_OPTARG, parse_perm_callback },
> 
> if (shared >= 0)
> 	shared_repository = shared;
> 
> This way we do not change shared_repository during parsing, so we do not
> loose the initial value.
> 
> But it seems nobody care about this kind of details, so perhaps, you can
> just ignore this suggestion.

I might be wrong, but shared_repository is initialized to PERM_UMASK and does not change before parse_options() is called, so this is not much useful.

 builtin-init-db.c |   57 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 36 insertions(+), 21 deletions(-)
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 38b4fcb..01b84a9 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -6,6 +6,7 @@
 #include "cache.h"
 #include "builtin.h"
 #include "exec_cmd.h"
+#include "parse-options.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -353,8 +354,18 @@ static int guess_repository_type(const char *git_dir)
 	return 1;
 }
 
-static const char init_db_usage[] =
-"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
+static const char * const init_db_usage[] = {
+	"git init [-q | --quiet] [--bare] [--template=<dir>] [--shared[=<type>]]",
+	NULL
+};
+
+static int parse_opt_shared_cb(const struct option *opt, const char *arg,
+			       int unset)
+{
+	*(int *)(opt->value) = unset ? PERM_UMASK :
+				       git_config_perm("arg", arg);
+	return 0;
+}
 
 /*
  * If you want to, you can share the DB area with any number of branches.
@@ -367,25 +378,29 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 	const char *git_dir;
 	const char *template_dir = NULL;
 	unsigned int flags = 0;
-	int i;
-
-	for (i = 1; i < argc; i++, argv++) {
-		const char *arg = argv[1];
-		if (!prefixcmp(arg, "--template="))
-			template_dir = arg+11;
-		else if (!strcmp(arg, "--bare")) {
-			static char git_dir[PATH_MAX+1];
-			is_bare_repository_cfg = 1;
-			setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
-						sizeof(git_dir)), 0);
-		} else if (!strcmp(arg, "--shared"))
-			shared_repository = PERM_GROUP;
-		else if (!prefixcmp(arg, "--shared="))
-			shared_repository = git_config_perm("arg", arg+9);
-		else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
-			flags |= INIT_DB_QUIET;
-		else
-			usage(init_db_usage);
+	int bare = 0;
+
+	const struct option options[] = {
+		OPT_STRING(0, "template", &template_dir, "path",
+			   "path to the template directory"),
+		OPT_BOOLEAN(0, "bare", &bare, "set up a bare repository"),
+		{ OPTION_CALLBACK, 0, "shared", &shared_repository,
+		  "permissions", "set up a shared repository",
+		  PARSE_OPT_OPTARG, parse_opt_shared_cb },
+		OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, options, init_db_usage, 0);
+
+	if (argc > 0)
+		usage_with_options(init_db_usage, options);
+
+	if (bare) {
+		static char git_dir[PATH_MAX+1];
+		is_bare_repository_cfg = 1;
+		setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
+					sizeof(git_dir)), 0);
 	}
 
 	/*
-- 
1.5.6.3
Previous: Olivier MarinNext: Michele Ballabio
Message 23 of 31 in “Extend use of parse_options()”
  1. 0/9 Extend use of parse_options()Michele Ballabio, Jul 23, 2008
  2. 1/9 builtin-verify-tag.c: use parse_options()Michele Ballabio, Jul 23, 2008
  3. Olivier MarinJul 24, 2008
  4. Johannes SchindelinJul 24, 2008
  5. Olivier MarinJul 25, 2008
  6. Johannes SchindelinJul 26, 2008
  7. builtin-verify-tag: fix -v option parsingOlivier Marin, Jul 28, 2008
  8. Johannes SchindelinJul 28, 2008
  9. Olivier MarinJul 28, 2008
  10. Johannes SchindelinJul 28, 2008
  11. 2/9 builtin-write-tree.c: use parse_options()Michele Ballabio, Jul 23, 2008
  12. 3/9 builtin-prune-packed.c: use parse_options()Michele Ballabio, Jul 23, 2008
  13. 4/9 builtin-ls-tree.c: use parse_options()Michele Ballabio, Jul 23, 2008
  14. 5/9 builtin-rev-list.c: use parse_options()Michele Ballabio, Jul 23, 2008
  15. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 23, 2008
  16. Olivier MarinJul 24, 2008
  17. Johannes SchindelinJul 24, 2008
  18. Olivier MarinJul 25, 2008
  19. Johannes SchindelinJul 26, 2008
  20. Michele BallabioJul 24, 2008
  21. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 25, 2008
  22. Olivier MarinJul 25, 2008
  23. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 25, 2008
  24. 7/9 builtin-checkout-index.c: use parse_options()Michele Ballabio, Jul 23, 2008
  25. Johannes SchindelinJul 24, 2008
  26. Michele BallabioJul 24, 2008
  27. Sverre RabbelierJul 24, 2008
  28. René ScharfeJul 25, 2008
  29. 7/9 builtin-checkout-index.c: use parse_options()Michele Ballabio, Jul 25, 2008
  30. 8/9 builtin-fetch-pack.c: use parse_options()Michele Ballabio, Jul 23, 2008
  31. 9/9 builtin-mailinfo.c: use parse_options()Michele Ballabio, Jul 23, 2008

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.