Git development
 help / color / mirror / Atom feed
* [PATCH] create_directories: simplify and avoid repeated copying
@ 2007-04-12 20:29 Geert Bosch
  2007-04-14 14:11 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Geert Bosch @ 2007-04-12 20:29 UTC (permalink / raw)
  To: git

Signed-off-by: Geert Bosch <bosch@gnat.com>
---
I needed to change the logic of this function a bit for
doing some local hacks^Wmodifications and thought it might
be a slight improvement/simplification.
No change in functionality and passed 'make test'.

 entry.c |   23 +++++++++--------------
 1 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/entry.c b/entry.c
index d72f811..f966c59 100644
--- a/entry.c
+++ b/entry.c
@@ -3,24 +3,19 @@
 
 static void create_directories(const char *path, struct checkout *state)
 {
-	int len = strlen(path);
-	char *buf = xmalloc(len + 1);
-	const char *slash = path;
+	char *buf = xstrdup(path);
+	char *slash = buf;
 
 	while ((slash = strchr(slash+1, '/')) != NULL) {
-		len = slash - path;
-		memcpy(buf, path, len);
-		buf[len] = 0;
+		*slash = 0;
 		if (mkdir(buf, 0777)) {
-			if (errno == EEXIST) {
-				struct stat st;
-				if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
-					continue;
-				if (!stat(buf, &st) && S_ISDIR(st.st_mode))
-					continue; /* ok */
-			}
-			die("cannot create directory at %s", buf);
+			struct stat st;
+			int force = (slash - buf) > state->base_dir_len && state->force;
+			if (errno != EEXIST || ((!force || unlink(buf) || mkdir(buf, 0777)) &&
+			    (stat(buf, &st) || !S_ISDIR(st.st_mode))))
+				die("cannot create directory at %s", buf);
 		}
+		*slash='/';
 	}
 	free(buf);
 }
-- 
1.5.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] create_directories: simplify and avoid repeated copying
  2007-04-12 20:29 [PATCH] create_directories: simplify and avoid repeated copying Geert Bosch
@ 2007-04-14 14:11 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2007-04-14 14:11 UTC (permalink / raw)
  To: Geert Bosch; +Cc: git

Geert Bosch <bosch@gnat.com> writes:

> Signed-off-by: Geert Bosch <bosch@gnat.com>
> ---
> I needed to change the logic of this function a bit for
> doing some local hacks^Wmodifications and thought it might
> be a slight improvement/simplification.
> No change in functionality and passed 'make test'.

I like the removal of repeated copying, but I wonder if the
denser condition is really easier to read.

I tried to like it, but I needed to reindent it first, and then
I needed to read it three times to convince myself that they are
equivalent.

        if (errno != EEXIST
            || ((!force || unlink(buf) || mkdir(buf, 0777))
                && (stat(buf, &st) || !S_ISDIR(st.st_mode))) )
                die("cannot create directory at %s", buf);

In the end, I managed to convince myself that this denser
version is doing the same thing as the original, and it is nicer
to see it in only three lines, I am not sure if it is easier to
read anymore, judging from the time it took me to reach that
conclusion...

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-04-14 14:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-12 20:29 [PATCH] create_directories: simplify and avoid repeated copying Geert Bosch
2007-04-14 14:11 ` 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