threads / patch / 10662

patchUse parseopts in builtin-fetch

Subject: [PATCH] Use parseopts in builtin-fetch

## tl;dr

8 messages between Nov 5, 2007 and Nov 5, 2007. Diffs are folded; open one to read it.

replies: 7people: 4as markdown or json

Daniel Barkalow· Nov 5, 2007, 03:35 UTC · lore
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(-)
Show changes to builtin-fetch.c +27 −68
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
Pierre Habouzit· Nov 5, 2007, 08:43 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Use parseopts in builtin-fetch

On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
Show 6 quoted lines
> 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.
  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
Daniel Barkalow· Nov 5, 2007, 17:14 UTC · re: Pierre Habouzit · lore

Re: [PATCH] Use parseopts in builtin-fetch

On Mon, 5 Nov 2007, Pierre Habouzit wrote:
Show 13 quoted lines
> On Mon, Nov 05, 2007 at 03:35:34AM +0000, Daniel Barkalow wrote:
> > 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.
> 
>   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.

Show 11 quoted lines
> > +		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.

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*
Pierre Habouzit· Nov 5, 2007, 08:55 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Use parseopts in builtin-fetch

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
Kristian Høgsberg· Nov 5, 2007, 17:02 UTC · re: Pierre Habouzit · lore

Re: [PATCH] Use parseopts in builtin-fetch

On Mon, 2007-11-05 at 09:55 +0100, Pierre Habouzit wrote:
Show 20 quoted lines
> 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 :)

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

Daniel Barkalow· Nov 5, 2007, 17:41 UTC · re: Pierre Habouzit · lore

Re: [PATCH] Use parseopts in builtin-fetch

On Mon, 5 Nov 2007, Pierre Habouzit wrote:
Show 20 quoted lines
> 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 :)

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*
Junio C Hamano· Nov 5, 2007, 19:13 UTC · re: Pierre Habouzit · lore

Re: [PATCH] Use parseopts in builtin-fetch

Pierre Habouzit <madcoder@debian.org> writes:
Show 11 quoted lines
> 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.

Another micronit is I found lacking is that it is a bit too cumbersome to accept only a subset of integer as a value (e.g. "this option takes a positive integer, not zero nor negative"). The caller can set up a callback to handle that, though.

Pierre Habouzit· Nov 5, 2007, 19:48 UTC · re: Junio C Hamano · lore

Re: [PATCH] Use parseopts in builtin-fetch

On Mon, Nov 05, 2007 at 07:13:33PM +0000, Junio C Hamano wrote:
Show 19 quoted lines
> Pierre Habouzit <madcoder@debian.org> writes:
> 
> > 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.
> 
> Another micronit is I found lacking is that it is a bit too
> cumbersome to accept only a subset of integer as a value
> (e.g. "this option takes a positive integer, not zero nor
> negative").  The caller can set up a callback to handle that,
> though.

As a general rule we may want to have some kind of "ranged" integers. but like you said the callback is always here for that, and if we begin to have dozens of those we may consider creating a new type for those if needed.

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

← back to recent threads