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

[PATCH] Use parseopts in builtin-fetch

From
Daniel Barkalow <barkalow@iabervon.org>
Date
Nov 5, 2007, 03:35 UTC
Message-ID
<Pine.LNX.4.64.0711042233590.7357@iabervon.org>
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
I mostly did this and the next one for practice with the API. I'm 
impressed that "git fetch -vv" is even handled correctly without anything 
special. Now that I've done it, assuming I did it right, it might as well 
get added to the series.
 builtin-fetch.c |   95 +++++++++++++++---------------------------------------
 1 files changed, 27 insertions(+), 68 deletions(-)
diff --git a/builtin-fetch.c b/builtin-fetch.c
index 6b1750d..a079cb0 100644
--- a/builtin-fetch.c
+++ b/builtin-fetch.c
@@ -8,8 +8,12 @@
 #include "path-list.h"
 #include "remote.h"
 #include "transport.h"
+#include "parse-options.h"
 
-static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]";
+static const char * const fetch_usage[] = {
+	"git-fetch [option] [<repository> <refspec>...]",
+	NULL
+};
 
 static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
 static char *default_rla = NULL;
@@ -470,71 +474,21 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 	int cmd_len = 0;
 	const char *depth = NULL, *upload_pack = NULL;
 	int keep = 0;
-
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		cmd_len += strlen(arg);
-
-		if (arg[0] != '-')
-			break;
-		if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
-			append = 1;
-			continue;
-		}
-		if (!prefixcmp(arg, "--upload-pack=")) {
-			upload_pack = arg + 14;
-			continue;
-		}
-		if (!strcmp(arg, "--upload-pack")) {
-			i++;
-			if (i == argc)
-				usage(fetch_usage);
-			upload_pack = argv[i];
-			continue;
-		}
-		if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
-			force = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--no-tags")) {
-			no_tags = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
-			tags = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
-			keep = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
-			update_head_ok = 1;
-			continue;
-		}
-		if (!prefixcmp(arg, "--depth=")) {
-			depth = arg + 8;
-			continue;
-		}
-		if (!strcmp(arg, "--depth")) {
-			i++;
-			if (i == argc)
-				usage(fetch_usage);
-			depth = argv[i];
-			continue;
-		}
-		if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
-			quiet = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
-			verbose++;
-			continue;
-		}
-		usage(fetch_usage);
-	}
-
-	for (j = i; j < argc; j++)
+	struct option options[] = {
+		OPT__VERBOSE(&verbose),
+		OPT_BOOLEAN('a', "append", &append, "append fetched data to exisitng FETCH_HEAD"),
+		OPT_BOOLEAN('f', "force", &force, "force"),
+		OPT_BOOLEAN( 0 , "no-tags", &no_tags, "don't fetch tags"),
+		OPT_BOOLEAN('t', "tags", &tags, "fetch all tags"),
+		OPT_BOOLEAN('k', "keep", &keep, "keep pack file"),
+		OPT_STRING( 0, "depth", &depth, "depth", "deepen the repository by <depth> commits"),
+		OPT_BOOLEAN('u', "update-head-ok", &update_head_ok, "allow updating head"),
+		OPT_BOOLEAN('q', "quiet", &quiet, "fetch silently"),
+		OPT_STRING( 0 , "upload-pack", &upload_pack, "upload-pack", "remote executable for git-upload-pack"),
+		OPT_END(),
+	};
+
+	for (j = 1; j < argc; j++)
 		cmd_len += strlen(argv[j]);
 
 	default_rla = xmalloc(cmd_len + 5 + argc + 1);
@@ -545,12 +499,16 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 		rla_offset += strlen(argv[j]) + 1;
 	}
 
-	if (i == argc)
+	argc = parse_options(argc, argv, options, fetch_usage, 0);
+
+	if (argc == 0)
 		remote = remote_get(NULL);
 	else
-		remote = remote_get(argv[i++]);
+		remote = remote_get(argv[0]);
 
 	transport = transport_get(remote, remote->url[0]);
+	if (!transport)
+		die("couldn't get transport");
 	if (verbose >= 2)
 		transport->verbose = 1;
 	if (quiet)
@@ -565,6 +523,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 	if (!transport->url)
 		die("Where do you want to fetch from today?");
 
+	i = 1;
 	if (i < argc) {
 		int j = 0;
 		refs = xcalloc(argc - i + 1, sizeof(const char *));
-- 
1.5.3.5.1528.gb6568-dirty
Next: Pierre Habouzit
Message 1 of 8 in “Use parseopts in builtin-fetch”
  1. Use parseopts in builtin-fetchDaniel Barkalow, Nov 5, 2007
  2. Pierre HabouzitNov 5, 2007
  3. Daniel BarkalowNov 5, 2007
  4. Pierre HabouzitNov 5, 2007
  5. Kristian HøgsbergNov 5, 2007
  6. Daniel BarkalowNov 5, 2007
  7. Junio C HamanoNov 5, 2007
  8. Pierre HabouzitNov 5, 2007

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.