Subject: [PATCH 3/6] revisions: parse_revisions refactor.

From: Pierre Habouzit <[email-masked]>
Date: Tue, 8 Jul 2008 11:56:01 +0200

Reorder parse_revisions so that it works more like a parse-opt based
parser would: use two pass, the first filtering out options that the
revision parser understands, and remember if we saw a "--", and then
parse revision arguments.

Once everyone is using a parse-opt parser, it will be possible to simplify
parse_revisions further. The first pass is a NOOP for incremental
parse-opt based parsers.

Signed-off-by: Pierre Habouzit <[email-masked]>
---
 revision.c |   63 ++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/revision.c b/revision.c
index a3792c9..87f89a2 100644
--- a/revision.c
+++ b/revision.c
@@ -1250,30 +1250,44 @@ int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
  */
 int parse_revisions(int argc, const char **argv, struct rev_info *revs)
 {
-	int i, flags, seen_dashdash;
-	const char **unrecognized = argv + 1;
-	int left = 1;
+	int i, left, flags;
+	int seen_dashdash = 0;
 
-	/* First, search for "--" */
-	seen_dashdash = 0;
-	for (i = 1; i < argc; i++) {
+	/* First, filter out revision options and look for "--" */
+	for (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
-		if (strcmp(arg, "--"))
+		int opts;
+
+		if (arg[0] != '-') {
+			argv[left++] = arg;
 			continue;
-		argv[i] = NULL;
-		argc = i;
-		if (argv[i + 1])
-			revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
-		seen_dashdash = 1;
-		break;
+		}
+
+		if (!strcmp(arg, "--")) {
+			seen_dashdash = left;
+			memcpy(argv + left, argv + i, sizeof(*argv) * (argc - i));
+			left += argc - i;
+			break;
+		}
+
+		opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
+		if (opts > 0) {
+			i += opts - 1;
+			continue;
+		}
+		if (opts < 0)
+			exit(128);
+		argv[left++] = arg;
 	}
+	argv[left] = NULL;
+	argc = left;
 
+	/* Second, parse revisions arguments */
 	flags = 0;
-	for (i = 1; i < argc; i++) {
+	for (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
-		if (*arg == '-') {
-			int opts;
 
+		if (*arg == '-') {
 			if (!strcmp(arg, "--all")) {
 				handle_refs(revs, flags, for_each_ref);
 				continue;
@@ -1306,16 +1320,15 @@ int parse_revisions(int argc, const char **argv, struct rev_info *revs)
 				revs->no_walk = 0;
 				continue;
 			}
-
-			opts = handle_revision_opt(revs, argc - i, argv + i, NULL, NULL);
-			if (opts > 0) {
-				i += opts - 1;
-				continue;
+			if (i == seen_dashdash) {
+				argv[i] = NULL;
+				argc = i;
+				if (argv[i + 1])
+					revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
+				break;
 			}
-			if (opts < 0)
-				exit(128);
-			*unrecognized++ = arg;
-			left++;
+
+			argv[left++] = arg;
 			continue;
 		}
 
-- 
1.5.6.2.352.g416a6