git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Michele Ballabio <barra_cuda@katamail.com>
To: rene.scharfe@lsrfire.ath.cx
Cc: sverre@rabbelier.nl, Johannes.Schindelin@gmx.de,
	git@vger.kernel.org, gitster@pobox.com
Subject: [PATCH 7/9 - v2] builtin-checkout-index.c: use parse_options()
Date: Fri, 25 Jul 2008 18:25:55 +0200	[thread overview]
Message-ID: <1217003155-12026-1-git-send-email-barra_cuda@katamail.com> (raw)
In-Reply-To: <48899657.5090209@lsrfire.ath.cx>

This changes "struct checkout" (now it uses ints and not bitfields) to
simplify the parsing code.

Signed-off-by: Michele Ballabio <barra_cuda@katamail.com>
---
On Friday 25 July 2008, René Scharfe wrote:
> In the case of struct checkout, though, we could simply make the
> bitfield members full ints, because there are only a few instances of
> this structure in memory at any given time. Wasting a few bytes of RAM
> in order to gain much simpler code is OK in this case, I think.
> OPT_BOOLEAN looks a lot nicer than a callback.

Yes. I only wanted the changes to be minimal, and only affect the option
parsing. In this sense, I still think the old patch is better. Here it is
the one you suggested (maybe Johannes suggested the same, but I didn't
understand :).

 builtin-checkout-index.c |  113 +++++++++++++++++++---------------------------
 cache.h                  |    8 ++--
 2 files changed, 50 insertions(+), 71 deletions(-)

diff --git a/builtin-checkout-index.c b/builtin-checkout-index.c
index 71ebabf..135348e 100644
--- a/builtin-checkout-index.c
+++ b/builtin-checkout-index.c
@@ -40,6 +40,7 @@
 #include "cache.h"
 #include "quote.h"
 #include "cache-tree.h"
+#include "parse-options.h"
 
 #define CHECKOUT_ALL 4
 static int line_termination = '\n';
@@ -153,18 +154,43 @@ static void checkout_all(const char *prefix, int prefix_length)
 		exit(128);
 }
 
-static const char checkout_cache_usage[] =
-"git checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
+static const char * const checkout_cache_usage[] = {
+	"git checkout-index [options] [--] <file>...",
+	NULL
+};
 
 static struct lock_file lock_file;
 
 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 {
-	int i;
 	int newfd = -1;
 	int all = 0;
 	int read_from_stdin = 0;
 	int prefix_length;
+	char *stage = NULL;
+
+	const struct option options[] = {
+		OPT_BOOLEAN('a', "all", &all,
+			    "checks out all files in the index"),
+		OPT_BOOLEAN('f', "force", &state.force,
+			    "force overwrite of existing files"),
+		OPT__QUIET(&state.quiet),
+		OPT_SET_INT('n', "no-create", &state.not_new,
+			"do not checkout new files, refresh existing ones", 1),
+		OPT_BOOLEAN('u', "index", &state.refresh_cache,
+			    "update stat information in the index"),
+		OPT_SET_INT('z', NULL, &line_termination,
+			    "separate paths with NUL", 0),
+		OPT_BOOLEAN(0, "stdin", &read_from_stdin,
+			    "read paths from stdin"),
+		OPT_BOOLEAN(0, "temp", &to_tempfile,
+			    "write content to temporary files"),
+		OPT_STRING(0, "prefix", &state.base_dir, "string",
+			   "prepend <string> when creating files"),
+		OPT_STRING(0, "stage", &stage, "1|2|3|all",
+			   "copy out files from the named stage"),
+		OPT_END()
+	};
 
 	git_config(git_default_config, NULL);
 	state.base_dir = "";
@@ -174,71 +200,24 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 		die("invalid cache");
 	}
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
+	argc = parse_options(argc, argv, options, checkout_cache_usage, 0);
 
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
-			all = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
-			state.force = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
-			state.quiet = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
-			state.not_new = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
-			state.refresh_cache = 1;
-			if (newfd < 0)
-				newfd = hold_locked_index(&lock_file, 1);
-			continue;
-		}
-		if (!strcmp(arg, "-z")) {
-			line_termination = 0;
-			continue;
-		}
-		if (!strcmp(arg, "--stdin")) {
-			if (i != argc - 1)
-				die("--stdin must be at the end");
-			read_from_stdin = 1;
-			i++; /* do not consider arg as a file name */
-			break;
-		}
-		if (!strcmp(arg, "--temp")) {
+	if ((state.refresh_cache) && (newfd < 0))
+		newfd = hold_locked_index(&lock_file, 1);
+	if (state.base_dir)
+		state.base_dir_len = strlen(state.base_dir);
+
+	if (stage) {
+		if (!strcmp(stage, "all")) {
 			to_tempfile = 1;
-			continue;
-		}
-		if (!prefixcmp(arg, "--prefix=")) {
-			state.base_dir = arg+9;
-			state.base_dir_len = strlen(state.base_dir);
-			continue;
-		}
-		if (!prefixcmp(arg, "--stage=")) {
-			if (!strcmp(arg + 8, "all")) {
-				to_tempfile = 1;
-				checkout_stage = CHECKOUT_ALL;
-			} else {
-				int ch = arg[8];
-				if ('1' <= ch && ch <= '3')
-					checkout_stage = arg[8] - '0';
-				else
-					die("stage should be between 1 and 3 or all");
-			}
-			continue;
+			checkout_stage = CHECKOUT_ALL;
+		} else {
+			int ch = stage[0];
+			if ('1' <= ch && ch <= '3')
+				checkout_stage = stage[0] - '0';
+			else
+				die("stage should be between 1 and 3 or all");
 		}
-		if (arg[0] == '-')
-			usage(checkout_cache_usage);
-		break;
 	}
 
 	if (state.base_dir_len || to_tempfile) {
@@ -253,8 +232,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	}
 
 	/* Check out named files first */
-	for ( ; i < argc; i++) {
-		const char *arg = argv[i];
+	while (argc-- > 0) {
+		const char *arg = *argv++;
 		const char *p;
 
 		if (all)
diff --git a/cache.h b/cache.h
index 38985aa..0bbe33b 100644
--- a/cache.h
+++ b/cache.h
@@ -618,10 +618,10 @@ extern const char *fmt_name(const char *name, const char *email);
 struct checkout {
 	const char *base_dir;
 	int base_dir_len;
-	unsigned force:1,
-		 quiet:1,
-		 not_new:1,
-		 refresh_cache:1;
+	int force;
+	int quiet;
+	int not_new;
+	int refresh_cache;
 };
 
 extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
-- 
1.5.6.3

  reply	other threads:[~2008-07-25 16:20 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-23 21:42 [PATCH 0/9] Extend use of parse_options() Michele Ballabio
2008-07-23 21:42 ` [PATCH 1/9] builtin-verify-tag.c: use parse_options() Michele Ballabio
2008-07-24 16:59   ` Olivier Marin
2008-07-24 17:08     ` Johannes Schindelin
2008-07-25 15:20       ` Olivier Marin
2008-07-26  0:53         ` Johannes Schindelin
2008-07-28 10:48         ` [PATCH] builtin-verify-tag: fix -v option parsing Olivier Marin
2008-07-28 11:06           ` Johannes Schindelin
2008-07-28 11:42             ` Olivier Marin
2008-07-28 12:10               ` Johannes Schindelin
2008-07-23 21:42 ` [PATCH 2/9] builtin-write-tree.c: use parse_options() Michele Ballabio
2008-07-23 21:42 ` [PATCH 3/9] builtin-prune-packed.c: " Michele Ballabio
2008-07-23 21:42 ` [PATCH 4/9] builtin-ls-tree.c: " Michele Ballabio
2008-07-23 21:42 ` [PATCH 5/9] builtin-rev-list.c: " Michele Ballabio
2008-07-23 21:42 ` [PATCH 6/9] builtin-init-db.c: " Michele Ballabio
2008-07-24 16:15   ` Olivier Marin
2008-07-24 17:07     ` Johannes Schindelin
2008-07-25 15:20       ` Olivier Marin
2008-07-26  0:55         ` Johannes Schindelin
2008-07-24 20:07     ` Michele Ballabio
2008-07-25  8:15       ` [PATCH 6/9 - v2] " Michele Ballabio
2008-07-25 15:20       ` [PATCH 6/9] " Olivier Marin
2008-07-25 19:53         ` [PATCH 6/9 - v3] " Michele Ballabio
2008-07-23 21:42 ` [PATCH 7/9] builtin-checkout-index.c: " Michele Ballabio
2008-07-24 14:44   ` Johannes Schindelin
2008-07-24 20:08     ` Michele Ballabio
2008-07-24 20:35       ` Sverre Rabbelier
2008-07-25  9:01         ` René Scharfe
2008-07-25 16:25           ` Michele Ballabio [this message]
2008-07-23 21:42 ` [PATCH 8/9] builtin-fetch-pack.c: " Michele Ballabio
2008-07-23 21:42 ` [PATCH 9/9] builtin-mailinfo.c: " Michele Ballabio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1217003155-12026-1-git-send-email-barra_cuda@katamail.com \
    --to=barra_cuda@katamail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rene.scharfe@lsrfire.ath.cx \
    --cc=sverre@rabbelier.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).