[PATCH] archive: simplify refname handling

Subsystems: the rest

DORMANTno replies

3 messages, 3 authors, 2016-06-15 · open the first message on its own page

[PATCH] archive: simplify refname handling

From: René Scharfe <hidden>
Date: 2016-06-15 22:53:50

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 <redacted>
---
 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

Re: [PATCH] archive: simplify refname handling

From: Jeff King <hidden>
Date: 2016-06-15 22:53:50

[+cc Carlos, as this is his code]

On Fri, May 18, 2012 at 07:15:17AM +0200, René Scharfe wrote:
quoted hunk
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 <redacted>
---
 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

Re: [PATCH] archive: simplify refname handling

From: Carlos Martín Nieto <hidden>
Date: 2016-06-15 22:53:50

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:
quoted
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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help