From: Jacob Keller <jacob.e.keller@intel.com>
To: git@vger.kernel.org
Cc: Jacob Keller <jacob.e.keller@intel.com>, Jeff King <peff@peff.net>
Subject: [PATCH v9 3/4] tag: update parsing to be more precise regarding errors
Date: Tue, 15 Jul 2014 16:32:58 -0700 [thread overview]
Message-ID: <1405467179-16064-3-git-send-email-jacob.e.keller@intel.com> (raw)
In-Reply-To: <1405467179-16064-1-git-send-email-jacob.e.keller@intel.com>
Update the parsing of sort string specifications so that it is able to
properly detect errors in the function type and allowed atoms.
Cc: Jeff King <peff@peff.net>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
---
builtin/tag.c | 55 +++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 41 insertions(+), 14 deletions(-)
diff --git a/builtin/tag.c b/builtin/tag.c
index ef765563388c..7d82526e76be 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -522,24 +522,51 @@ static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
{
int *sort = opt->value;
- int flags = 0;
+ char *value, *separator, *type, *atom;
+ int flags = 0, function = 0, err = 0;
- if (*arg == '-') {
+ /* skip the '-' prefix for reverse sort order first */
+ if (skip_prefix(arg, "-", &arg))
flags |= REVERSE_SORT;
- arg++;
+
+ /* duplicate string so we can modify it in place */
+ value = xstrdup(arg);
+
+ /* determine the sort function and the sorting atom */
+ separator = strchr(value, ':');
+ if (separator) {
+ /* split the string at the separator with a NULL byte */
+ *separator = '\0';
+ type = value;
+ atom = separator + 1;
+ } else {
+ /* we have no separator, so assume the whole string is the * atom */
+ type = NULL;
+ atom = value;
}
- if (starts_with(arg, "version:")) {
- *sort = VERCMP_SORT;
- arg += 8;
- } else if (starts_with(arg, "v:")) {
- *sort = VERCMP_SORT;
- arg += 2;
+
+ if (type) {
+ if (!strcmp(type, "version") || !strcmp(type, "v"))
+ function = VERCMP_SORT;
+ else {
+ err = error(_("unsupported sort function '%s'"), type);
+ goto abort;
+ }
+
} else
- *sort = STRCMP_SORT;
- if (strcmp(arg, "refname"))
- die(_("unsupported sort specification %s"), arg);
- *sort |= flags;
- return 0;
+ function = STRCMP_SORT;
+
+ /* for now, only the refname is a valid atom */
+ if (atom && strcmp(atom, "refname")) {
+ err = error(_("unsupported sort specification '%s'"), atom);
+ goto abort;
+ }
+
+ *sort = (flags | function);
+
+abort:
+ free(value);
+ return err;
}
int cmd_tag(int argc, const char **argv, const char *prefix)
--
2.0.1.475.g9b8d714
next prev parent reply other threads:[~2014-07-15 23:33 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-15 23:32 [PATCH v9 1/4] usage: make error functions a stack Jacob Keller
2014-07-15 23:32 ` [PATCH v9 2/4] tag: fix --sort tests to use cat<<-\EOF format Jacob Keller
2014-07-15 23:32 ` Jacob Keller [this message]
2014-07-15 23:32 ` [PATCH v9 4/4] tag: support configuring --sort via .gitconfig Jacob Keller
2014-07-15 23:42 ` Jeff King
2014-07-15 23:46 ` Keller, Jacob E
2014-07-16 17:59 ` Junio C Hamano
2014-07-16 19:51 ` Keller, Jacob E
2014-07-16 21:13 ` Keller, Jacob E
2014-07-16 21:40 ` Junio C Hamano
2014-07-16 21:42 ` Keller, Jacob E
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=1405467179-16064-3-git-send-email-jacob.e.keller@intel.com \
--to=jacob.e.keller@intel.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/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).