Subject: [PATCH 2/3] fetch-pack: use parse_options()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 2 May 2012 21:38:09 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 I _think_ this also fixes a case when --keep is passed twice, then
 lock_pack is set to 1, but pack_lockfile_ptr is not set while it is
 set when --lock-pack is given.

 builtin/fetch-pack.c |  110 ++++++++++++++++++--------------------------------
 fetch-pack.h         |   20 +++++-----
 2 files changed, 49 insertions(+), 81 deletions(-)

diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index 7e9d62f..65ac111 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -10,6 +10,7 @@
 #include "remote.h"
 #include "run-command.h"
 #include "transport.h"
+#include "parse-options.h"
 
 static int transfer_unpack_limit = -1;
 static int fetch_unpack_limit = -1;
@@ -22,10 +23,12 @@ static struct fetch_pack_args args = {
 	/* .uploadpack = */ "git-upload-pack",
 };
 
-static const char fetch_pack_usage[] =
-"git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
-"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
-"[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
+static const char* fetch_pack_usage[] = {
+	"git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
+	"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
+	"[--no-progress] [-v] [<host>:]<directory> [<refs>...]",
+	NULL
+};
 
 #define COMPLETE	(1U << 0)
 #define COMMON		(1U << 1)
@@ -906,79 +909,44 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 	int fd[2];
 	char *pack_lockfile = NULL;
 	char **pack_lockfile_ptr = NULL;
+	int progress = 1;
 	struct child_process *conn;
+	struct option opts[] = {
+		OPT_STRING(0, "upload-pack", &args.uploadpack, "path",
+			   "path to upload pack on remote end"),
+		OPT_STRING( 0 , "exec", &args.uploadpack, "upload-pack",
+			    "path to upload pack on remote end"),
+		OPT__QUIET(&args.quiet, "do not print results to stdout"),
+		OPT_COUNTUP('k', "keep", &args.keep_pack, "keep downloaded pack"),
+		OPT_BOOL(0, "thin", &args.use_thin_pack, "create thin packs"),
+		OPT_BOOL(0, "include-tag", &args.include_tag,
+			 "include tag objects that refer to objects to be packed"),
+		OPT_BOOL(0, "all", &args.fetch_all, "fetch from all remotes" ),
+		OPT_BOOL(0, "stdin", &args.stdin_refs, "read refs from stdin" ),
+		OPT__VERBOSE(&args.verbose, "be more verbose"),
+		OPT_INTEGER(0, "depth", &args.depth,
+			    "deepen history of shallow clone"),
+		OPT_BOOL(0, "progress", &progress, "show progress"),
+		OPT_BOOL(0, "stateless-rpc", &args.stateless_rpc, "use stateless RPC"),
+		OPT_BOOL(0, "lock-pack", &args.lock_pack, "lock the downloaded pack"),
+		OPT_END()
+	};
 
 	packet_trace_identity("fetch-pack");
 
 	nr_heads = 0;
 	heads = NULL;
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (*arg == '-') {
-			if (!prefixcmp(arg, "--upload-pack=")) {
-				args.uploadpack = arg + 14;
-				continue;
-			}
-			if (!prefixcmp(arg, "--exec=")) {
-				args.uploadpack = arg + 7;
-				continue;
-			}
-			if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
-				args.quiet = 1;
-				continue;
-			}
-			if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
-				args.lock_pack = args.keep_pack;
-				args.keep_pack = 1;
-				continue;
-			}
-			if (!strcmp("--thin", arg)) {
-				args.use_thin_pack = 1;
-				continue;
-			}
-			if (!strcmp("--include-tag", arg)) {
-				args.include_tag = 1;
-				continue;
-			}
-			if (!strcmp("--all", arg)) {
-				args.fetch_all = 1;
-				continue;
-			}
-			if (!strcmp("--stdin", arg)) {
-				args.stdin_refs = 1;
-				continue;
-			}
-			if (!strcmp("-v", arg)) {
-				args.verbose = 1;
-				continue;
-			}
-			if (!prefixcmp(arg, "--depth=")) {
-				args.depth = strtol(arg + 8, NULL, 0);
-				continue;
-			}
-			if (!strcmp("--no-progress", arg)) {
-				args.no_progress = 1;
-				continue;
-			}
-			if (!strcmp("--stateless-rpc", arg)) {
-				args.stateless_rpc = 1;
-				continue;
-			}
-			if (!strcmp("--lock-pack", arg)) {
-				args.lock_pack = 1;
-				pack_lockfile_ptr = &pack_lockfile;
-				continue;
-			}
-			usage(fetch_pack_usage);
-		}
-		dest = arg;
-		heads = (char **)(argv + i + 1);
-		nr_heads = argc - i - 1;
-		break;
-	}
-	if (!dest)
-		usage(fetch_pack_usage);
+	argc = parse_options(argc, argv, prefix, opts, fetch_pack_usage, 0);
+	args.no_progress = !progress;
+	if (args.keep_pack > 1)
+		args.lock_pack = 1;
+	if (args.lock_pack)
+		pack_lockfile_ptr = &pack_lockfile;
+	dest = argv[0];
+	if (!argc || !dest)
+		usage_with_options(fetch_pack_usage, opts);
+	heads = (char **)(argv + 1);
+	nr_heads = argc - 1;
 
 	if (args.stdin_refs) {
 		/*
diff --git a/fetch-pack.h b/fetch-pack.h
index 7c2069c..d440162 100644
--- a/fetch-pack.h
+++ b/fetch-pack.h
@@ -5,16 +5,16 @@ struct fetch_pack_args {
 	const char *uploadpack;
 	int unpacklimit;
 	int depth;
-	unsigned quiet:1,
-		keep_pack:1,
-		lock_pack:1,
-		use_thin_pack:1,
-		fetch_all:1,
-		stdin_refs:1,
-		verbose:1,
-		no_progress:1,
-		include_tag:1,
-		stateless_rpc:1;
+	int quiet;
+	int keep_pack;
+	int lock_pack;
+	int use_thin_pack;
+	int fetch_all;
+	int stdin_refs;
+	int verbose;
+	int no_progress;
+	int include_tag;
+	int stateless_rpc;
 };
 
 struct ref *fetch_pack(struct fetch_pack_args *args,
-- 
1.7.8.36.g69ee2