git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Jacob Keller <jacob.e.keller@intel.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] tag: support configuring --sort via .gitconfig
Date: Thu, 10 Jul 2014 00:07:32 -0400	[thread overview]
Message-ID: <20140710040731.GD28401@sigill.intra.peff.net> (raw)
In-Reply-To: <1404945412-10197-1-git-send-email-jacob.e.keller@intel.com>

On Wed, Jul 09, 2014 at 03:36:51PM -0700, Jacob Keller wrote:

> +static int parse_sort_string(const char *arg)
> +{
> +	int sort = 0;
> +	int flags = 0;
> +
> +	if (*arg == '-') {
> +		flags |= REVERSE_SORT;
> +		arg++;
> +	}
> +	if (starts_with(arg, "version:")) {
> +		sort = VERCMP_SORT;
> +		arg += 8;
> +	} else if (starts_with(arg, "v:")) {
> +		sort = VERCMP_SORT;
> +		arg += 2;
> +	} else
> +		sort = STRCMP_SORT;
> +	if (strcmp(arg, "refname"))
> +		die(_("unsupported sort specification %s"), arg);
> +	sort |= flags;
> +
> +	return sort;
> +}

I know this is existing code you are moving, but I noticed it looks ripe
for using skip_prefix. Perhaps while we are in the area we should do the
following on top of your patch (I'd also be happy for it be squashed
in, but that may be too much in one patch).

-- >8 --
Subject: [PATCH] tag: use skip_prefix instead of magic numbers

We can make the parsing of the --sort parameter a bit more
readable by having skip_prefix keep our pointer up to date.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/tag.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/builtin/tag.c b/builtin/tag.c
index a679e32..016df98 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -353,18 +353,14 @@ static int parse_sort_string(const char *arg)
 	int sort = 0;
 	int flags = 0;
 
-	if (*arg == '-') {
+	if (skip_prefix(arg, "-", &arg))
 		flags |= REVERSE_SORT;
-		arg++;
-	}
-	if (starts_with(arg, "version:")) {
-		sort = VERCMP_SORT;
-		arg += 8;
-	} else if (starts_with(arg, "v:")) {
+
+	if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
 		sort = VERCMP_SORT;
-		arg += 2;
-	} else
+	else
 		sort = STRCMP_SORT;
+
 	if (strcmp(arg, "refname"))
 		die(_("unsupported sort specification %s"), arg);
 	sort |= flags;
-- 
2.0.0.566.gfe3e6b2

  parent reply	other threads:[~2014-07-10  4:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-09 22:36 [PATCH v2] tag: support configuring --sort via .gitconfig Jacob Keller
2014-07-10  4:00 ` Jeff King
2014-07-10  4:07 ` Jeff King [this message]
2014-07-10 17:59   ` Junio C Hamano
2014-07-10 19:34     ` Jeff King
2014-07-10 21:50       ` Keller, Jacob E
2014-07-10 22:46         ` Junio C Hamano
2014-07-10 20:38   ` 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=20140710040731.GD28401@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=jacob.e.keller@intel.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).