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

[PATCH 8/9] builtin-fetch-pack.c: use parse_options()

From
Michele Ballabio <barra_cuda@katamail.com>
Date
Jul 23, 2008, 21:42 UTC
Message-ID
<1216849332-26813-9-git-send-email-barra_cuda@katamail.com>
In-Reply-To
<1216849332-26813-1-git-send-email-barra_cuda@katamail.com>
Signed-off-by: Michele Ballabio <barra_cuda@katamail.com>
---
 builtin-fetch-pack.c |  144 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 90 insertions(+), 54 deletions(-)
diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c
index 273239a..701be41 100644
--- a/builtin-fetch-pack.c
+++ b/builtin-fetch-pack.c
@@ -9,6 +9,7 @@
 #include "fetch-pack.h"
 #include "remote.h"
 #include "run-command.h"
+#include "parse-options.h"
 
 static int transfer_unpack_limit = -1;
 static int fetch_unpack_limit = -1;
@@ -17,8 +18,10 @@ static struct fetch_pack_args args = {
 	/* .uploadpack = */ "git-upload-pack",
 };
 
-static const char fetch_pack_usage[] =
-"git fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
+static const char * const fetch_pack_usage[] = {
+	"git fetch-pack [options] [<host>:]<directory> [<refs>...]",
+	NULL
+};
 
 #define COMPLETE	(1U << 0)
 #define COMMON		(1U << 1)
@@ -667,6 +670,56 @@ static void fetch_pack_setup(void)
 	did_setup = 1;
 }
 
+static int parse_opt_keep_pack_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->lock_pack = t_args->keep_pack;
+	t_args->keep_pack = 1;
+	return 0;
+}
+
+static int parse_opt_thin_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->use_thin_pack = unset ? 0 : 1;
+	return 0;
+}
+
+static int parse_opt_include_tag_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->include_tag = unset ? 0 : 1;
+	return 0;
+}
+
+static int parse_opt_no_progress_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->no_progress = 1;
+	return 0;
+}
+
+static int parse_opt_fetch_all_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->fetch_all = unset ? 0 : 1;
+	return 0;
+}
+
+static int parse_opt_quiet_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->quiet = unset ? 0 : 1;
+	return 0;
+}
+
+static int parse_opt_verbose_cb(const struct option *opt, const char *arg, int unset)
+{
+	struct fetch_pack_args *t_args = opt->value;
+	t_args->verbose = unset ? 0 : 1;
+	return 0;
+}
+
 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 {
 	int i, ret, nr_heads;
@@ -677,60 +730,43 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 
 	nr_heads = 0;
 	heads = NULL;
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
 
-		if (*arg == '-') {
-			if (!prefixcmp(arg, "--upload-pack=")) {
-				args.uploadpack = arg + 14;
-				continue;
-			}
-			if (!prefixcmp(arg, "--exec=")) {
-				args.uploadpack = arg + 7;
-				continue;
-			}
-			if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
-				args.quiet = 1;
-				continue;
-			}
-			if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
-				args.lock_pack = args.keep_pack;
-				args.keep_pack = 1;
-				continue;
-			}
-			if (!strcmp("--thin", arg)) {
-				args.use_thin_pack = 1;
-				continue;
-			}
-			if (!strcmp("--include-tag", arg)) {
-				args.include_tag = 1;
-				continue;
-			}
-			if (!strcmp("--all", arg)) {
-				args.fetch_all = 1;
-				continue;
-			}
-			if (!strcmp("-v", arg)) {
-				args.verbose = 1;
-				continue;
-			}
-			if (!prefixcmp(arg, "--depth=")) {
-				args.depth = strtol(arg + 8, NULL, 0);
-				continue;
-			}
-			if (!strcmp("--no-progress", arg)) {
-				args.no_progress = 1;
-				continue;
-			}
-			usage(fetch_pack_usage);
-		}
-		dest = (char *)arg;
-		heads = (char **)(argv + i + 1);
-		nr_heads = argc - i - 1;
-		break;
-	}
+	const struct option options[] = {
+		{ OPTION_CALLBACK, 0, "all", &args, NULL,
+		 "fetch all remote refs", PARSE_OPT_NOARG,
+		 parse_opt_fetch_all_cb },
+		OPT_STRING(0, "upload-pack", &args.uploadpack, "git-upload-pack",
+			   "specify path to git-upload-pack on remote"),
+		OPT_STRING(0, "exec", &args.uploadpack, "git-upload-pack",
+			   "same as --upload-pack <git-upload-pack>."),
+		{ OPTION_CALLBACK, 0, "no-progress", &args, NULL,
+		 "do not show the progress", PARSE_OPT_NOARG | PARSE_OPT_NONEG,
+		 parse_opt_no_progress_cb },
+		{ OPTION_CALLBACK, 'q', "quiet", &args, NULL,
+		 "be quiet", PARSE_OPT_NOARG, parse_opt_quiet_cb },
+		{ OPTION_CALLBACK, 'v', "verbose", &args, NULL,
+		 "be verbose", PARSE_OPT_NOARG, parse_opt_verbose_cb },
+		OPT_INTEGER(0, "depth", &args.depth, "fetch chains not longer than <n>"),
+		{ OPTION_CALLBACK, 'k', "keep", &args, NULL,
+		 "create a single packfile of received data",
+		 PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_keep_pack_cb },
+		{ OPTION_CALLBACK, 0, "include-tag", &args, NULL,
+		 "download annotated tags too", PARSE_OPT_NOARG,
+		 parse_opt_include_tag_cb },
+		{ OPTION_CALLBACK, 0, "thin", &args, NULL,
+		 "minimize number of objects to be sent",
+		 PARSE_OPT_NOARG, parse_opt_thin_cb },
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, options, fetch_pack_usage, 0);
+
+	dest = (char *)argv[0];
+	heads = (char **)(argv + 1);
+	nr_heads = argc - 1;
+
 	if (!dest)
-		usage(fetch_pack_usage);
+		usage_with_options(fetch_pack_usage, options);
 
 	conn = git_connect(fd, (char *)dest, args.uploadpack,
 			   args.verbose ? CONNECT_VERBOSE : 0);
-- 
1.5.6.3
Previous: Michele BallabioNext: Michele Ballabio
Message 30 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.