git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Michael Haggerty" <mhagger@alum.mit.edu>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 2/3] fetch-pack: use parse_options()
Date: Wed,  2 May 2012 21:38:09 +0700	[thread overview]
Message-ID: <1335969490-9181-3-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1335969490-9181-1-git-send-email-pclouds@gmail.com>


Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 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

  parent reply	other threads:[~2012-05-02 14:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-02 10:40 [PATCH 0/2] Fix some constness errors in fetch-pack mhagger
2012-05-02 10:40 ` [PATCH 1/2] cmd_fetch_pack(): declare dest to be const mhagger
2012-05-02 10:40 ` [PATCH 2/2] cmd_fetch_pack(): fix constness problem and memory leak mhagger
2012-05-02 11:14   ` Nguyen Thai Ngoc Duy
2012-05-02 13:35     ` Michael Haggerty
2012-05-02 14:38       ` [PATCH 0/3] Fix some constness errors in fetch-pack and parseopt conversion Nguyễn Thái Ngọc Duy
2012-05-02 14:38         ` [PATCH 1/3] cmd_fetch_pack(): declare dest to be const Nguyễn Thái Ngọc Duy
2012-05-02 14:38         ` Nguyễn Thái Ngọc Duy [this message]
2012-05-02 14:38         ` [PATCH 3/3] cmd_fetch_pack(): fix constness problem and memory leak Nguyễn Thái Ngọc Duy
2012-05-02 17:14     ` [PATCH 2/2] " Junio C Hamano
2012-05-21  1:47   ` Junio C Hamano
2012-05-21  8:13     ` Michael Haggerty
2012-05-19 14:05 ` [PATCH 0/2] Fix some constness errors in fetch-pack Michael Haggerty

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

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

  git send-email \
    --in-reply-to=1335969490-9181-3-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    /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.
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 NNTP newsgroup(s).