[PATCH] Use parseopts in builtin-fetch

Subsystems: the rest

DORMANTno replies

6 messages, 3 authors, 2016-06-15 · open the first message on its own page

[PATCH] Use parseopts in builtin-fetch

From: Daniel Barkalow <hidden>
Date: 2016-06-15 22:43:47

Signed-off-by: Daniel Barkalow <redacted>
---
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

Re: [PATCH] Use parseopts in builtin-fetch

From: Pierre Habouzit <hidden>
Date: 2016-06-15 22:43:47

On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
Signed-off-by: Daniel Barkalow <redacted>
---
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.
  I believe the same patches (or very similar ones) are in pu but are
not in next yet because they conflict with the builtin-fetch recent
series.

  see http://git.madism.org/?p=git.git;a=blobdiff;f=builtin-fetch.c;h=12b1c4;hp=6b1750d;hb=7407915;hpb=61610e6
+		OPT_BOOLEAN('q', "quiet", &quiet, "fetch silently"),
  there is an OPT__QUIET(&quiet) for this one.
+	i = 1;
 	if (i < argc) {
 		int j = 0;
 		refs = xcalloc(argc - i + 1, sizeof(const char *));
  this is wrong, you meant i = 0, and frankly, it's better to just strip
i altogether.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

Re: [PATCH] Use parseopts in builtin-fetch

From: Pierre Habouzit <hidden>
Date: 2016-06-15 22:43:47

On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
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.
  About that: OPTION_BOOLEAN increments the associated variable, to
support this case specifically.

  The last thing that really miss in parse-options is a way to recurse
into a sub-array of struct option, to be able to port the generic diff
and revision arguments.

  Though, there is a difficulty here that I've not yet found how to
circumvent tastefully: right now options take an absolute pointer to
_the_ variable that will be filled with values. I need to be able to
relocate such a structure for sub-arrays for quite obvious reasons, and
that is quite hard to achieve without hazardous APIs. I currently lean
in the direction of simply memdup-ing the array and do fix-ups on
*values pointers. Though how to do that in a graceful way is not obvious
to me yet :)

  This is the issue that currently prevents me from migrating any log
and diff based, though git diff -wbBCM that doesn't work is irritating me
more and more every day, so I'm confident I'll find a solution soon :)

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

Re: [PATCH] Use parseopts in builtin-fetch

From: Kristian Høgsberg <hidden>
Date: 2016-06-15 22:43:47

On Mon, 2007-11-05 at 09:55 +0100, Pierre Habouzit wrote:
On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
quoted
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.
  About that: OPTION_BOOLEAN increments the associated variable, to
support this case specifically.

  The last thing that really miss in parse-options is a way to recurse
into a sub-array of struct option, to be able to port the generic diff
and revision arguments.

  Though, there is a difficulty here that I've not yet found how to
circumvent tastefully: right now options take an absolute pointer to
_the_ variable that will be filled with values. I need to be able to
relocate such a structure for sub-arrays for quite obvious reasons, and
that is quite hard to achieve without hazardous APIs. I currently lean
in the direction of simply memdup-ing the array and do fix-ups on
*values pointers. Though how to do that in a graceful way is not obvious
to me yet :)
What about requiring sub-arrays entries to take a pointer to a struct as
their 'value' and requiring that all value pointers in the sub-array be
relative to this struct, ie

  (void *) offsetof(struct diff_options, detect_rename)

Then you can have something like

   OPT__SUBARRAY(diff_options, &rev.diff_opt);

in your options array and it will fill out the specified my_diff_opts.

cheers,
Kristian

Re: [PATCH] Use parseopts in builtin-fetch

From: Daniel Barkalow <hidden>
Date: 2016-06-15 22:43:47

On Mon, 5 Nov 2007, Pierre Habouzit wrote:
On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
quoted
Signed-off-by: Daniel Barkalow <redacted>
---
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.
  I believe the same patches (or very similar ones) are in pu but are
not in next yet because they conflict with the builtin-fetch recent
series.

  see http://git.madism.org/?p=git.git;a=blobdiff;f=builtin-fetch.c;h=12b1c4;hp=6b1750d;hb=7407915;hpb=61610e6
Ah, okay, forgot to look there. In any case, I was mostly looking for what 
mistakes I shouldn't make in future conversions.
quoted
+		OPT_BOOLEAN('q', "quiet", &quiet, "fetch silently"),
  there is an OPT__QUIET(&quiet) for this one.
quoted
+	i = 1;
 	if (i < argc) {
 		int j = 0;
 		refs = xcalloc(argc - i + 1, sizeof(const char *));
  this is wrong, you meant i = 0, and frankly, it's better to just strip
i altogether.
I didn't consume the remote name's slot, and started at the next one. But 
you're right that it's probably nicest to do to *argv++, argc-- thing and 
be zero-based for the list afterwards. I think I need the 'i' in this 
case, because the names get somewhat converted from the exact list given.

	-Daniel
*This .sig left intentionally blank*

Re: [PATCH] Use parseopts in builtin-fetch

From: Daniel Barkalow <hidden>
Date: 2016-06-15 22:43:47

On Mon, 5 Nov 2007, Pierre Habouzit wrote:
On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
quoted
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.
  About that: OPTION_BOOLEAN increments the associated variable, to
support this case specifically.

  The last thing that really miss in parse-options is a way to recurse
into a sub-array of struct option, to be able to port the generic diff
and revision arguments.

  Though, there is a difficulty here that I've not yet found how to
circumvent tastefully: right now options take an absolute pointer to
_the_ variable that will be filled with values. I need to be able to
relocate such a structure for sub-arrays for quite obvious reasons, and
that is quite hard to achieve without hazardous APIs. I currently lean
in the direction of simply memdup-ing the array and do fix-ups on
*values pointers. Though how to do that in a graceful way is not obvious
to me yet :)
I'm not entirely clear as to why the "diff options" to an arbitrary 
command can't configure variables in the diff engine. The tricky thing 
would be supporting a single command that has two sets of the same 
options (if git log could show the same stuff in two different ways at the 
same time, for example).

But, in any case, it should be possible for an OPT_* macro to take a 
struct type and a member name, do the offsetof and store that, and the 
inclusion macro would take the absolute pointer to the struct.

Next time I get a chance, I'll see if I can come up with something 
suitable.

	-Daniel
*This .sig left intentionally blank*
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help