From: Jonathan Nieder <jrnieder@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Junio C Hamano <gitster@pobox.com>,
Duy Nguyen <pclouds@gmail.com>,
Git Mailing List <git@vger.kernel.org>
Subject: Re: [PATCH v2 2/3] rev-parse: be more careful with munging arguments
Date: Fri, 6 Dec 2013 16:04:37 -0800 [thread overview]
Message-ID: <20131207000437.GP29959@google.com> (raw)
In-Reply-To: <20131206220751.GB25620@sigill.intra.peff.net>
Jeff King wrote:
> As an aside, this is one of those places where C's string functions do
> gross things with const.
Yes, yuck.
The fundamental grossness is that argv is semantically char **
(assuming this doesn't segfault) but passed around as const char **.
I wonder why we don't use the same trick in git_extract_argv0_path and
when handling STRIP_EXTENSION in git.c.
Alternatively, try_difference could create a new buffer for its own
use. Originally (v0.99~273, 2005-06-13) this dotdot handling code was
pretty simple:
for (i = 1; i < argc; i++) {
char *arg = argv[i];
...
dotdot = strstr(arg, "..");
if (dotdot) {
unsigned char end[20];
char *n = dotdot+2;
*dotdot = 0;
if (!get_sha1(arg, sha1)) {
if (!*n)
n = "HEAD";
if (!get_sha1(n, end)) {
printf("%s\n", sha1_to_hex(end));
printf("^%s\n", sha1_to_hex(sha1));
continue;
}
}
*dotdot = '.';
}
printf("%s\n", arg);
}
The 'continue' without making the argument right again was justified
because we were done with the argument.
Now it is still not much more complicated --- the function is
logically just trying to parse the argument into a "this" and "next"
part and print them.
Something like the following (untested). Or if the allocation churn
is too much, it could use a static strbuf.
diff --git i/builtin/rev-parse.c w/builtin/rev-parse.c
index c76b89d..98262b3 100644
--- i/builtin/rev-parse.c
+++ w/builtin/rev-parse.c
@@ -234,37 +234,35 @@ static int try_difference(const char *arg)
unsigned char sha1[20];
unsigned char end[20];
const char *next;
- const char *this;
+ struct strbuf this = STRBUF_INIT;
int symmetric;
- static const char head_by_default[] = "HEAD";
if (!(dotdot = strstr(arg, "..")))
return 0;
next = dotdot + 2;
- this = arg;
symmetric = (*next == '.');
-
- *dotdot = 0;
next += symmetric;
- if (!*next)
- next = head_by_default;
- if (dotdot == arg)
- this = head_by_default;
-
- if (this == head_by_default && next == head_by_default &&
- !symmetric) {
+ if (!*next && dotdot == arg && !symmetric)
/*
* Just ".."? That is not a range but the
* pathspec for the parent directory.
*/
- *dotdot = '.';
return 0;
+
+ strbuf_add(&this, arg, dotdot - arg);
+
+ if (!*next)
+ next = "HEAD";
+ if (dotdot == arg) {
+ strbuf_reset(&this);
+ strbuf_addstr(&this, "HEAD");
}
- if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
+ if (!get_sha1_committish(this.buf, sha1)
+ && !get_sha1_committish(next, end)) {
show_rev(NORMAL, end, next);
- show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
+ show_rev(symmetric ? NORMAL : REVERSED, sha1, this.buf);
if (symmetric) {
struct commit_list *exclude;
struct commit *a, *b;
@@ -279,9 +277,10 @@ static int try_difference(const char *arg)
exclude = n;
}
}
+ strbuf_release(&this);
return 1;
}
- *dotdot = '.';
+ strbuf_release(&this);
return 0;
}
next prev parent reply other threads:[~2013-12-07 0:04 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-05 10:07 [BUG] redundant error message Duy Nguyen
2013-12-05 19:15 ` Jeff King
2013-12-05 20:00 ` Junio C Hamano
2013-12-05 20:03 ` Jeff King
2013-12-05 20:15 ` Junio C Hamano
2013-12-05 21:00 ` Jeff King
2013-12-05 21:28 ` Jeff King
2013-12-05 21:44 ` Junio C Hamano
2013-12-06 21:12 ` [PATCH 0/2] rev-parse and "--" Jeff King
2013-12-06 21:13 ` [PATCH 1/2] rev-parse: correctly diagnose revision errors before "--" Jeff King
2013-12-06 21:15 ` [PATCH 2/2] rev-parse: diagnose ambiguous revision/filename arguments Jeff King
2013-12-06 22:05 ` [PATCH v2 0/3] rev-parse and "--" Jeff King
2013-12-06 22:05 ` [PATCH v2 1/3] rev-parse: correctly diagnose revision errors before "--" Jeff King
2013-12-06 23:34 ` Jonathan Nieder
2013-12-06 22:07 ` [PATCH v2 2/3] rev-parse: be more careful with munging arguments Jeff King
2013-12-07 0:04 ` Jonathan Nieder [this message]
2013-12-09 21:33 ` Eric Sunshine
2013-12-06 22:08 ` [PATCH v2 3/3] rev-parse: diagnose ambiguous revision/filename arguments Jeff King
2013-12-06 23:25 ` [PATCH v2 0/3] rev-parse and "--" Jonathan Nieder
2013-12-06 23:30 ` Jeff King
2013-12-09 19:05 ` Junio C Hamano
2013-12-09 19:12 ` Jonathan Nieder
2013-12-09 19:23 ` Jonathan Nieder
2013-12-09 20:48 ` Junio C Hamano
2013-12-09 20:56 ` Jonathan Nieder
2013-12-09 21:10 ` Junio C Hamano
2013-12-06 1:15 ` [BUG] redundant error message Duy Nguyen
2013-12-06 22:13 ` Jeff King
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=20131207000437.GP29959@google.com \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--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 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.