find_containing_dir(): use strbuf in implementation of this function
authorMichael Haggerty <mhagger@alum.mit.edu>
Thu, 26 Apr 2012 22:27:00 +0000 (00:27 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 3 May 2012 20:15:35 +0000 (13:15 -0700)
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c

diff --git a/refs.c b/refs.c
index 0d81b9e26712da2c0c5d8883ec55b87ff07cc255..623fb55da83773edd83149c6629a2ad46a037eec 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -309,20 +309,21 @@ static struct ref_entry *search_for_subdir(struct ref_dir *dir,
 static struct ref_dir *find_containing_dir(struct ref_dir *dir,
                                           const char *refname, int mkdir)
 {
-       char *refname_copy = xstrdup(refname);
-       char *slash;
-       struct ref_entry *entry;
-       for (slash = strchr(refname_copy, '/'); slash; slash = strchr(slash + 1, '/')) {
-               char tmp = slash[1];
-               slash[1] = '\0';
-               entry = search_for_subdir(dir, refname_copy, mkdir);
-               slash[1] = tmp;
+       struct strbuf dirname;
+       const char *slash;
+       strbuf_init(&dirname, PATH_MAX);
+       for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
+               struct ref_entry *entry;
+               strbuf_add(&dirname,
+                          refname + dirname.len,
+                          (slash + 1) - (refname + dirname.len));
+               entry = search_for_subdir(dir, dirname.buf, mkdir);
                if (!entry)
                        break;
                dir = &entry->u.subdir;
        }
 
-       free(refname_copy);
+       strbuf_release(&dirname);
        return dir;
 }