refs: use strings directly in find_containing_dir()
Convert the parameter subdirname of search_for_subdir() to a length-limted string and then simply pass the interesting slice of the refname from find_containing_dir(), thereby avoiding to duplicate the string. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 22, 2012 at 20:50 UTC
dd02e72852280d73bfd845703476b230475b2776
1 file changed
+4
-9
refs.c
+4
-9
@@ -352,9 +352,9 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir,
352
* directory cannot be found. dir must already be complete.
353
*/
354
static struct ref_dir *search_for_subdir(struct ref_dir *dir,
355
- const char *subdirname, int mkdir)
355
+ const char *subdirname, size_t len,
356
+ int mkdir)
357
{
357
- size_t len = strlen(subdirname);
358
struct ref_entry *entry = search_ref_dir(dir, subdirname, len);
359
if (!entry) {
360
if (!mkdir)
@@ -383,15 +383,11 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
383
static struct ref_dir *find_containing_dir(struct ref_dir *dir,
384
const char *refname, int mkdir)
385
{
386
- struct strbuf dirname;
386
const char *slash;
388
- strbuf_init(&dirname, PATH_MAX);
387
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
388
+ size_t dirnamelen = slash - refname + 1;
389
struct ref_dir *subdir;
391
- strbuf_add(&dirname,
392
- refname + dirname.len,
393
- (slash + 1) - (refname + dirname.len));
394
- subdir = search_for_subdir(dir, dirname.buf, mkdir);
390
+ subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
391
if (!subdir) {
392
dir = NULL;
393
break;
@@ -399,7 +395,6 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
395
dir = subdir;
396
}
397
402
- strbuf_release(&dirname);
398
return dir;
399
}
400