From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Jonathan Nieder <jrnieder@gmail.com>,
Junio C Hamano <gitster@pobox.com>, Joey Hess <joey@kitenet.net>
Subject: [PATCH 2/2] bundle: rewrite builtin to use parse-options
Date: Thu, 15 Dec 2011 22:15:28 +0530 [thread overview]
Message-ID: <1323967528-10537-3-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1323967528-10537-1-git-send-email-artagnon@gmail.com>
The git-bundle builtin currently parses command-line options by hand;
this is fragile, and reports cryptic errors on failure. Use the
parse-options library to do the parsing instead.
Encouraged-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
builtin/bundle.c | 91 +++++++++++++++++++++++++++++++----------------------
t/t5704-bundle.sh | 2 +-
2 files changed, 54 insertions(+), 39 deletions(-)
diff --git a/builtin/bundle.c b/builtin/bundle.c
index 92a8a60..13ed770 100644
--- a/builtin/bundle.c
+++ b/builtin/bundle.c
@@ -1,5 +1,6 @@
#include "builtin.h"
#include "cache.h"
+#include "parse-options.h"
#include "bundle.h"
/*
@@ -9,57 +10,71 @@
* bundle supporting "fetch", "pull", and "ls-remote".
*/
-static const char builtin_bundle_usage[] =
- "git bundle create <file> <git-rev-list args>\n"
- " or: git bundle verify <file>\n"
- " or: git bundle list-heads <file> [<refname>...]\n"
- " or: git bundle unbundle <file> [<refname>...]";
+static const char * builtin_bundle_usage[] = {
+ "git bundle create <file> <git-rev-list args>",
+ "git bundle verify <file>",
+ "git bundle list-heads <file> [<refname>...]",
+ "git bundle unbundle <file> [<refname>...]",
+ NULL
+};
int cmd_bundle(int argc, const char **argv, const char *prefix)
{
- struct bundle_header header;
- const char *cmd, *bundle_file;
+ int prefix_length;
int bundle_fd = -1;
- char buffer[PATH_MAX];
+ const char *subcommand, *bundle_file;
+ struct bundle_header header;
+ struct option options[] = { OPT_END() };
- if (argc < 3)
- usage(builtin_bundle_usage);
+ argc = parse_options(argc, argv, prefix, options,
+ builtin_bundle_usage, PARSE_OPT_STOP_AT_NON_OPTION);
- cmd = argv[1];
- bundle_file = argv[2];
- argc -= 2;
- argv += 2;
+ if (argc < 2)
+ usage_with_options(builtin_bundle_usage, options);
+ subcommand = argv[0];
- if (prefix && bundle_file[0] != '/') {
- snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
- bundle_file = buffer;
- }
+ argc = parse_options(argc, argv, prefix, options,
+ builtin_bundle_usage, PARSE_OPT_STOP_AT_NON_OPTION);
+
+ /* Disallow stray arguments */
+ if ((strcmp(subcommand, "create") && argc > 2) ||
+ (!strcmp(subcommand, "verify") && argc > 1))
+ usage_with_options(builtin_bundle_usage, options);
- memset(&header, 0, sizeof(header));
- if (strcmp(cmd, "create") && (bundle_fd =
- read_bundle_header(bundle_file, &header)) < 0)
- return 1;
+ prefix_length = prefix ? strlen(prefix) : 0;
+ bundle_file = prefix_filename(prefix, prefix_length, argv[0]);
- if (!strcmp(cmd, "verify")) {
+ /* Read out bundle header, except in the "create" case */
+ if (strcmp(subcommand, "create")) {
+ memset(&header, 0, sizeof(header));
+ bundle_fd = read_bundle_header(bundle_file, &header);
+ if (bundle_fd < 0)
+ die_errno(_("Failed to open bundle file '%s'"), bundle_file);
+ }
+
+ if (!strcmp(subcommand, "create")) {
+ if (!startup_info->have_repository)
+ die(_("Need a repository to create a bundle."));
+ return create_bundle(&header, bundle_file, argc, argv);
+ } else if (!strcmp(subcommand, "verify")) {
close(bundle_fd);
if (verify_bundle(&header, 1))
- return 1;
+ return -1; /* Error already reported */
fprintf(stderr, _("%s is okay\n"), bundle_file);
- return 0;
- }
- if (!strcmp(cmd, "list-heads")) {
+ } else if (!strcmp(subcommand, "list-heads")) {
close(bundle_fd);
- return !!list_bundle_refs(&header, argc, argv);
- }
- if (!strcmp(cmd, "create")) {
- if (!startup_info->have_repository)
- die(_("Need a repository to create a bundle."));
- return !!create_bundle(&header, bundle_file, argc, argv);
- } else if (!strcmp(cmd, "unbundle")) {
- if (!startup_info->have_repository)
+ return list_bundle_refs(&header, argc, argv);
+ } else if (!strcmp(subcommand, "unbundle")) {
+ if (!startup_info->have_repository) {
+ close(bundle_fd);
die(_("Need a repository to unbundle."));
- return !!unbundle(&header, bundle_fd, 0) ||
+ }
+ return unbundle(&header, bundle_fd, 0) ||
list_bundle_refs(&header, argc, argv);
- } else
- usage(builtin_bundle_usage);
+ } else {
+ close(bundle_fd);
+ usage_with_options(builtin_bundle_usage, options);
+ }
+
+ return 0;
}
diff --git a/t/t5704-bundle.sh b/t/t5704-bundle.sh
index 09ff4f1..8e3f677 100755
--- a/t/t5704-bundle.sh
+++ b/t/t5704-bundle.sh
@@ -53,7 +53,7 @@ test_expect_success 'disallow stray command-line options' '
test_must_fail git bundle create --junk bundle second third
'
-test_expect_failure 'disallow stray command-line arguments' '
+test_expect_success 'disallow stray command-line arguments' '
git bundle create bundle second third &&
test_must_fail git bundle verify bundle junk
'
--
1.7.4.1
next prev parent reply other threads:[~2011-12-15 16:45 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-08 13:10 [PATCH v2 0/6] Fix '&&' chaining in tests Ramkumar Ramachandra
2011-12-08 13:10 ` [PATCH 1/2] parse-options: introduce OPT_SUBCOMMAND Ramkumar Ramachandra
2011-12-08 16:30 ` Jonathan Nieder
2011-12-08 16:43 ` Ramkumar Ramachandra
2011-12-08 13:10 ` [PATCH 1/6] t3040 (subprojects-basic): modernize style Ramkumar Ramachandra
2011-12-08 16:34 ` Jonathan Nieder
2011-12-09 7:56 ` Ramkumar Ramachandra
2011-12-09 18:26 ` Junio C Hamano
2011-12-09 18:32 ` Ramkumar Ramachandra
2011-12-08 13:10 ` [PATCH 2/2] bundle: rewrite builtin to use parse-options Ramkumar Ramachandra
2011-12-08 16:39 ` Jonathan Nieder
2011-12-08 16:47 ` Ramkumar Ramachandra
2011-12-08 17:03 ` Jonathan Nieder
2011-12-08 17:38 ` Ramkumar Ramachandra
2011-12-08 17:59 ` Jonathan Nieder
2011-12-08 19:39 ` Ramkumar Ramachandra
2011-12-08 23:48 ` Junio C Hamano
2011-12-09 13:33 ` Jakub Narebski
2011-12-09 18:24 ` Junio C Hamano
2011-12-15 16:45 ` [PATCH 0/2] Improve git-bundle builtin Ramkumar Ramachandra
2011-12-15 16:45 ` [PATCH 1/2] t5704 (bundle): rewrite for larger coverage Ramkumar Ramachandra
2011-12-15 21:16 ` Jonathan Nieder
2011-12-15 16:45 ` Ramkumar Ramachandra [this message]
2011-12-15 21:29 ` [PATCH 2/2] bundle: rewrite builtin to use parse-options Jonathan Nieder
2011-12-15 20:54 ` [PATCH 0/2] Improve git-bundle builtin Jonathan Nieder
2011-12-15 21:30 ` Junio C Hamano
2011-12-08 13:10 ` [PATCH 2/6] t3030 (merge-recursive): use test_expect_code Ramkumar Ramachandra
2011-12-08 13:10 ` [PATCH 3/6] t1006 (cat-file): use test_cmp Ramkumar Ramachandra
2011-12-08 13:28 ` Matthieu Moy
2011-12-08 13:33 ` Ramkumar Ramachandra
2011-12-08 13:41 ` Matthieu Moy
2011-12-08 16:55 ` Jonathan Nieder
2011-12-08 13:10 ` [PATCH 4/6] t3200 (branch): fix '&&' chaining Ramkumar Ramachandra
2011-12-08 17:07 ` Jonathan Nieder
2011-12-08 13:10 ` [PATCH 5/6] t1510 (worktree): " Ramkumar Ramachandra
2011-12-08 17:12 ` Jonathan Nieder
2011-12-09 0:00 ` Junio C Hamano
2011-12-08 13:10 ` [PATCH 6/6] test: " Ramkumar Ramachandra
2011-12-08 17:18 ` Jonathan Nieder
2011-12-08 19:55 ` [PATCH v2 0/6] Fix '&&' chaining in tests Junio C Hamano
2011-12-09 5:23 ` Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH v3 " Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 1/6] t3040 (subprojects-basic): fix '&&' chaining, modernize style Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 2/6] t3030 (merge-recursive): use test_expect_code Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 3/6] t1006 (cat-file): use test_cmp Ramkumar Ramachandra
2011-12-09 18:43 ` Junio C Hamano
2011-12-09 18:47 ` Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 4/6] t3200 (branch): fix '&&' chaining Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 5/6] t1510 (worktree): " Ramkumar Ramachandra
2011-12-09 11:29 ` [PATCH 6/6] tests: " Ramkumar Ramachandra
-- strict thread matches above, loose matches on Subject: below --
2011-12-08 12:07 [PATCH 0/2] Parsing a subcommand using parse-options Ramkumar Ramachandra
2011-12-08 12:07 ` [PATCH 2/2] bundle: rewrite builtin to use parse-options Ramkumar Ramachandra
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=1323967528-10537-3-git-send-email-artagnon@gmail.com \
--to=artagnon@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=joey@kitenet.net \
--cc=jrnieder@gmail.com \
/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).