Subject: [PATCH 4/6] revisions: split handle_revision_args from parse_revisions.

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

This new function is meant to only parse non option revision arguments in
a row. IOW it's meant to parse what remains from a parse-opt filtering of
the argument list, with the knowledge of the "--" position (0 means none).

Signed-off-by: Pierre Habouzit <[email-masked]>
---
 revision.c |   97 +++++++++++++++++++++++++++++++----------------------------
 revision.h |    1 +
 2 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/revision.c b/revision.c
index 87f89a2..c7de30f 100644
--- a/revision.c
+++ b/revision.c
@@ -1241,49 +1241,11 @@ int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
 	return 1;
 }
 
-/*
- * Parse revision information, filling in the "rev_info" structure,
- * and removing the used arguments from the argument list.
- *
- * Returns the number of arguments left that weren't recognized
- * (which are also moved to the head of the argument list)
- */
-int parse_revisions(int argc, const char **argv, struct rev_info *revs)
+int handle_revision_args(struct rev_info *revs,
+                         int argc, const char **argv, int dashdash_pos)
 {
-	int i, left, flags;
-	int seen_dashdash = 0;
-
-	/* First, filter out revision options and look for "--" */
-	for (left = i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		int opts;
-
-		if (arg[0] != '-') {
-			argv[left++] = arg;
-			continue;
-		}
-
-		if (!strcmp(arg, "--")) {
-			seen_dashdash = left;
-			memcpy(argv + left, argv + i, sizeof(*argv) * (argc - i));
-			left += argc - i;
-			break;
-		}
+	int i, left, flags = 0;
 
-		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 (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 
@@ -1320,7 +1282,7 @@ int parse_revisions(int argc, const char **argv, struct rev_info *revs)
 				revs->no_walk = 0;
 				continue;
 			}
-			if (i == seen_dashdash) {
+			if (i == dashdash_pos) {
 				argv[i] = NULL;
 				argc = i;
 				if (argv[i + 1])
@@ -1332,10 +1294,10 @@ int parse_revisions(int argc, const char **argv, struct rev_info *revs)
 			continue;
 		}
 
-		if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
+		if (handle_revision_arg(arg, revs, flags, dashdash_pos)) {
 			int j;
-			if (seen_dashdash || *arg == '^')
-				die("bad revision '%s'", arg);
+			if (dashdash_pos || *arg == '^')
+				return error("bad revision '%s'", arg);
 
 			/* If we didn't have a "--":
 			 * (1) all filenames must exist;
@@ -1351,7 +1313,50 @@ int parse_revisions(int argc, const char **argv, struct rev_info *revs)
 			break;
 		}
 	}
-	return left;
+
+        return left;
+}
+
+/*
+ * Parse revision information, filling in the "rev_info" structure,
+ * and removing the used arguments from the argument list.
+ *
+ * Returns the number of arguments left that weren't recognized
+ * (which are also moved to the head of the argument list)
+ */
+int parse_revisions(int argc, const char **argv, struct rev_info *revs)
+{
+	int i, left, dashdash_pos = 0;
+
+	/* First, filter out revision options and look for "--" */
+	for (left = i = 1; i < argc; i++) {
+		const char *arg = argv[i];
+		int opts;
+
+		if (arg[0] != '-') {
+			argv[left++] = arg;
+			continue;
+		}
+
+		if (!strcmp(arg, "--")) {
+			dashdash_pos = 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;
+
+        return handle_revision_args(revs, left, argv, dashdash_pos);
 }
 
 void setup_revisions(struct rev_info *revs, const char *def)
diff --git a/revision.h b/revision.h
index 46ab713..c0f5df0 100644
--- a/revision.h
+++ b/revision.h
@@ -125,6 +125,7 @@ extern int parse_revisions(int argc, const char **argv, struct rev_info *revs);
 extern void setup_revisions(struct rev_info *revs, const char *def);
 extern int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
 			       int *unkc, const char **unkv);
+extern int handle_revision_args(struct rev_info *revs, int argc, const char **argv, int dashdash_pos);
 extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
 
 extern int prepare_revision_walk(struct rev_info *revs);
-- 
1.5.6.2.352.g416a6