From: Junio C Hamano <gitster@pobox.com>
To: Stefan Beller <sbeller@google.com>
Cc: git@vger.kernel.org, pclouds@gmail.com, ramsay@ramsayjones.plus.com
Subject: Re: [PATCHv2] pathspec: allow escaped query values
Date: Thu, 02 Jun 2016 14:54:10 -0700 [thread overview]
Message-ID: <xmqqr3cfs1dp.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <20160602213015.21712-1-sbeller@google.com> (Stefan Beller's message of "Thu, 2 Jun 2016 14:30:15 -0700")
Stefan Beller <sbeller@google.com> writes:
> In our own .gitattributes file we have attributes such as:
>
> *.[ch] whitespace=indent,trail,space
>
> When querying for attributes we want to be able to ask for the exact
> value, i.e.
>
> git ls-files :(attr:whitespace=indent,trail,space)
>
> should work, but the commas are used in the attr magic to introduce
> the next attr, ...
> ...
> So here is the "escaping only, but escaping done right" version.
> (It goes on top of sb/pathspec-label)
The phrase "should work" is probably a bit too strong (I'd have said
"it would be nice if this worked"), as we do not have to even
support comma for our immediately expected use cases. Allowing it
merely makes a casual test using our own .gitattributes easier.
> +static size_t strcspn_escaped(const char *s, const char *reject)
Perhaps s/reject/stop/?
> +{
> + const char *i, *j;
> +
> + for (i = s; *i; i++) {
> + /* skip escaped the character */
> + if (i[0] == '\\' && i[1]) {
> + i++;
> + continue;
> + }
> + /* see if any of the chars matches the current character */
> + for (j = reject; *j; j++)
> + if (!*i || *i == *j)
> + return i - s;
I somehow doubt that *i can be NUL here. In any case, this looks
more like
/* is *i is one of the stop codon? */
if (strchr(stop, *i))
break;
> + }
> + return i - s;
> +}
> +static char *attr_value_unescape(const char *value)
> +{
> + char *i, *ret = xstrdup(value);
> +
> + for (i = ret; *i; i++) {
> + if (i[0] == '\\') {
> + if (!i[1])
> + die(_("Escape character '\\' not allowed as "
> + "last character in attr value"));
> +
> + /* remove the backslash */
> + memmove(i, i + 1, strlen(i));
> + /* and ignore the character after that */
> + i++;
> + }
> + }
> + return ret;
> +}
> +
Repeated memmove() and strlen() somehow bothers me. Would there be
a more efficient and straight-forward way to do this, perhaps along
the lines of this instead?
const char *src;
char *dst, *ret;
ret = xmalloc(strlen(value));
for (src = value, dst = ret; *src; src++, dst++) {
if (*src == '\\') {
if (!src[1])
die();
src++;
}
if (*src && invalid_value_char(*src))
die("cannot use '%c' for value matching", *src)
*dst = *src;
}
*dst = '\0'
return ret;
Even though I earlier said "Now we have an unquote mechanism, we can
open the floodgate by lifting the "no backslash in value" check, I
now realize that we do want to keep some escape hatch for us to
extend the quoting syntax even more later, so perhaps with something
like this:
static inline int invalid_value_char(const char ch)
{
if (isalnum(ch) || strchr(",-_", ch))
return 0;
return -1;
}
Thanks.
next prev parent reply other threads:[~2016-06-02 21:54 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-02 21:30 [PATCHv2] pathspec: allow escaped query values Stefan Beller
2016-06-02 21:54 ` Junio C Hamano [this message]
2016-06-02 22:10 ` Stefan Beller
2016-06-02 22:53 ` Stefan Beller
2016-06-02 23:01 ` Junio C Hamano
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=xmqqr3cfs1dp.fsf@gitster.mtv.corp.google.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=pclouds@gmail.com \
--cc=ramsay@ramsayjones.plus.com \
--cc=sbeller@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.