* [PATCH] archive: simplify refname handling
@ 2012-05-18 5:15 René Scharfe
2012-05-18 15:59 ` Jeff King
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: René Scharfe @ 2012-05-18 5:15 UTC (permalink / raw)
To: git discussion list; +Cc: Jeff King, Junio C Hamano
There is no need to build a copy of the relevant part of the string just
to make sure we have a NUL-terminated string. We can simply pass the
length of the interesting part to dwim_ref() instead.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
archive.c | 17 +++++------------
1 file changed, 5 insertions(+), 12 deletions(-)
diff --git a/archive.c b/archive.c
index cd083ea..ecc1ff0 100644
--- a/archive.c
+++ b/archive.c
@@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
/* Remotes are only allowed to fetch actual refs */
if (remote) {
char *ref = NULL;
- const char *refname, *colon = NULL;
-
- colon = strchr(name, ':');
- if (colon)
- refname = xstrndup(name, colon - name);
- else
- refname = name;
-
- if (!dwim_ref(refname, strlen(refname), sha1, &ref))
- die("no such ref: %s", refname);
- if (refname != name)
- free((void *)refname);
+ const char *colon = strchr(name, ':');
+ size_t refnamelen = colon ? colon - name : strlen(name);
+
+ if (!dwim_ref(name, refnamelen, sha1, &ref))
+ die("no such ref: %.*s", refnamelen, name);
free(ref);
}
--
1.7.10.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] archive: simplify refname handling
2012-05-18 5:15 [PATCH] archive: simplify refname handling René Scharfe
@ 2012-05-18 15:59 ` Jeff King
2012-05-18 16:44 ` Carlos Martín Nieto
2012-05-18 18:22 ` Junio C Hamano
2012-05-18 18:27 ` Junio C Hamano
2 siblings, 1 reply; 5+ messages in thread
From: Jeff King @ 2012-05-18 15:59 UTC (permalink / raw)
To: René Scharfe
Cc: Carlos Martín Nieto, git discussion list, Junio C Hamano
[+cc Carlos, as this is his code]
On Fri, May 18, 2012 at 07:15:17AM +0200, René Scharfe wrote:
> There is no need to build a copy of the relevant part of the string just
> to make sure we have a NUL-terminated string. We can simply pass the
> length of the interesting part to dwim_ref() instead.
>
> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
> ---
> archive.c | 17 +++++------------
> 1 file changed, 5 insertions(+), 12 deletions(-)
>
> diff --git a/archive.c b/archive.c
> index cd083ea..ecc1ff0 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
> /* Remotes are only allowed to fetch actual refs */
> if (remote) {
> char *ref = NULL;
> - const char *refname, *colon = NULL;
> -
> - colon = strchr(name, ':');
> - if (colon)
> - refname = xstrndup(name, colon - name);
> - else
> - refname = name;
> -
> - if (!dwim_ref(refname, strlen(refname), sha1, &ref))
> - die("no such ref: %s", refname);
> - if (refname != name)
> - free((void *)refname);
> + const char *colon = strchr(name, ':');
> + size_t refnamelen = colon ? colon - name : strlen(name);
> +
> + if (!dwim_ref(name, refnamelen, sha1, &ref))
> + die("no such ref: %.*s", refnamelen, name);
> free(ref);
Looks obviously correct to me.
-Peff
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] archive: simplify refname handling
2012-05-18 15:59 ` Jeff King
@ 2012-05-18 16:44 ` Carlos Martín Nieto
0 siblings, 0 replies; 5+ messages in thread
From: Carlos Martín Nieto @ 2012-05-18 16:44 UTC (permalink / raw)
To: Jeff King; +Cc: René Scharfe, git discussion list, Junio C Hamano
On Fri, 2012-05-18 at 11:59 -0400, Jeff King wrote:
> [+cc Carlos, as this is his code]
>
> On Fri, May 18, 2012 at 07:15:17AM +0200, René Scharfe wrote:
>
> > There is no need to build a copy of the relevant part of the string just
> > to make sure we have a NUL-terminated string. We can simply pass the
> > length of the interesting part to dwim_ref() instead.
> >
> > Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
> > ---
> > archive.c | 17 +++++------------
> > 1 file changed, 5 insertions(+), 12 deletions(-)
> >
> > diff --git a/archive.c b/archive.c
> > index cd083ea..ecc1ff0 100644
> > --- a/archive.c
> > +++ b/archive.c
> > @@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
> > /* Remotes are only allowed to fetch actual refs */
> > if (remote) {
> > char *ref = NULL;
> > - const char *refname, *colon = NULL;
> > -
> > - colon = strchr(name, ':');
> > - if (colon)
> > - refname = xstrndup(name, colon - name);
> > - else
> > - refname = name;
> > -
> > - if (!dwim_ref(refname, strlen(refname), sha1, &ref))
> > - die("no such ref: %s", refname);
> > - if (refname != name)
> > - free((void *)refname);
> > + const char *colon = strchr(name, ':');
> > + size_t refnamelen = colon ? colon - name : strlen(name);
> > +
> > + if (!dwim_ref(name, refnamelen, sha1, &ref))
> > + die("no such ref: %.*s", refnamelen, name);
> > free(ref);
>
> Looks obviously correct to me.
Yep, that's clearly a much better way of doing it.
cmn
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] archive: simplify refname handling
2012-05-18 5:15 [PATCH] archive: simplify refname handling René Scharfe
2012-05-18 15:59 ` Jeff King
@ 2012-05-18 18:22 ` Junio C Hamano
2012-05-18 18:27 ` Junio C Hamano
2 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2012-05-18 18:22 UTC (permalink / raw)
To: René Scharfe; +Cc: git discussion list, Jeff King
Looks good to me. Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] archive: simplify refname handling
2012-05-18 5:15 [PATCH] archive: simplify refname handling René Scharfe
2012-05-18 15:59 ` Jeff King
2012-05-18 18:22 ` Junio C Hamano
@ 2012-05-18 18:27 ` Junio C Hamano
2 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2012-05-18 18:27 UTC (permalink / raw)
To: René Scharfe; +Cc: git discussion list, Jeff King, Junio C Hamano
René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:
> + const char *colon = strchr(name, ':');
> + size_t refnamelen = colon ? colon - name : strlen(name);
> +
> + if (!dwim_ref(name, refnamelen, sha1, &ref))
> + die("no such ref: %.*s", refnamelen, name);
> free(ref);
> }
Both dwim_ref()'s second parameter the vararg that corresponds to the
length part of "%.*s" in die(), expect "int", not "size_t", so I needed
this to get it compile.
archive.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/archive.c b/archive.c
index 081ecb5..1e7156d 100644
--- a/archive.c
+++ b/archive.c
@@ -261,7 +261,7 @@ static void parse_treeish_arg(const char **argv,
if (remote) {
char *ref = NULL;
const char *colon = strchr(name, ':');
- size_t refnamelen = colon ? colon - name : strlen(name);
+ int refnamelen = colon ? colon - name : strlen(name);
if (!dwim_ref(name, refnamelen, sha1, &ref))
die("no such ref: %.*s", refnamelen, name);
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-05-18 18:27 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-18 5:15 [PATCH] archive: simplify refname handling René Scharfe
2012-05-18 15:59 ` Jeff King
2012-05-18 16:44 ` Carlos Martín Nieto
2012-05-18 18:22 ` Junio C Hamano
2012-05-18 18:27 ` Junio C Hamano
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).