refs.c: extract function search_for_subdir()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 25, 2012 at 00:45 UTC f348ac923c9f834c3cdc434e6266872cf5710b71
1 file changed +24 -10
refs.c
+24 -10
@@ -276,6 +276,27 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
276 return *r;
277 }
278
279 +/*
280 + * Search for a directory entry directly within dir (without
281 + * recursing). Sort dir if necessary. subdirname must be a directory
282 + * name (i.e., end in '/'). If mkdir is set, then create the
283 + * directory if it is missing; otherwise, return NULL if the desired
284 + * directory cannot be found.
285 + */
286 +static struct ref_entry *search_for_subdir(struct ref_dir *dir,
287 + const char *subdirname, int mkdir)
288 +{
289 + struct ref_entry *entry = search_ref_dir(dir, subdirname);
290 + if (!entry) {
291 + if (!mkdir)
292 + return NULL;
293 + entry = create_dir_entry(subdirname);
294 + add_entry_to_dir(dir, entry);
295 + }
296 + assert(entry->flag & REF_DIR);
297 + return entry;
298 +}
299 +
300 /*
301 * If refname is a reference name, find the ref_dir within the dir
302 * tree that should hold refname. If refname is a directory name
@@ -294,17 +315,10 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
315 for (slash = strchr(refname_copy, '/'); slash; slash = strchr(slash + 1, '/')) {
316 char tmp = slash[1];
317 slash[1] = '\0';
297 - entry = search_ref_dir(dir, refname_copy);
298 - if (!entry) {
299 - if (!mkdir) {
300 - dir = NULL;
301 - break;
302 - }
303 - entry = create_dir_entry(refname_copy);
304 - add_entry_to_dir(dir, entry);
305 - }
318 + entry = search_for_subdir(dir, refname_copy, mkdir);
319 slash[1] = tmp;
307 - assert(entry->flag & REF_DIR);
320 + if (!entry)
321 + break;
322 dir = &entry->u.subdir;
323 }
324