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>
This commit is contained in:
Michael Haggerty 2012-04-25 00:45:11 +02:00 коммит произвёл Junio C Hamano
Родитель abc390989f
Коммит f348ac923c
1 изменённых файлов: 24 добавлений и 10 удалений

34
refs.c
Просмотреть файл

@ -276,6 +276,27 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
return *r; return *r;
} }
/*
* Search for a directory entry directly within dir (without
* recursing). Sort dir if necessary. subdirname must be a directory
* name (i.e., end in '/'). If mkdir is set, then create the
* directory if it is missing; otherwise, return NULL if the desired
* directory cannot be found.
*/
static struct ref_entry *search_for_subdir(struct ref_dir *dir,
const char *subdirname, int mkdir)
{
struct ref_entry *entry = search_ref_dir(dir, subdirname);
if (!entry) {
if (!mkdir)
return NULL;
entry = create_dir_entry(subdirname);
add_entry_to_dir(dir, entry);
}
assert(entry->flag & REF_DIR);
return entry;
}
/* /*
* If refname is a reference name, find the ref_dir within the dir * If refname is a reference name, find the ref_dir within the dir
* tree that should hold refname. If refname is a directory name * 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,
for (slash = strchr(refname_copy, '/'); slash; slash = strchr(slash + 1, '/')) { for (slash = strchr(refname_copy, '/'); slash; slash = strchr(slash + 1, '/')) {
char tmp = slash[1]; char tmp = slash[1];
slash[1] = '\0'; slash[1] = '\0';
entry = search_ref_dir(dir, refname_copy); entry = search_for_subdir(dir, refname_copy, mkdir);
if (!entry) {
if (!mkdir) {
dir = NULL;
break;
}
entry = create_dir_entry(refname_copy);
add_entry_to_dir(dir, entry);
}
slash[1] = tmp; slash[1] = tmp;
assert(entry->flag & REF_DIR); if (!entry)
break;
dir = &entry->u.subdir; dir = &entry->u.subdir;
} }