get_ref_dir(): take the containing directory as argument
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index 113739f14f72ce8c472e812bd7af22cf5572a712..afd0cf75bc1292a046bad3524532f4ddcb0fe08d 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -276,6 +276,27 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
        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
  * 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, '/')) {
                char tmp = slash[1];
                slash[1] = '\0';
-               entry = search_ref_dir(dir, refname_copy);
-               if (!entry) {
-                       if (!mkdir) {
-                               dir = NULL;
-                               break;
-                       }
-                       entry = create_dir_entry(refname_copy);
-                       add_entry_to_dir(dir, entry);
-               }
+               entry = search_for_subdir(dir, refname_copy, mkdir);
                slash[1] = tmp;
-               assert(entry->flag & REF_DIR);
+               if (!entry)
+                       break;
                dir = &entry->u.subdir;
        }
 
@@ -751,7 +765,8 @@ void add_packed_ref(const char *refname, const unsigned char *sha1)
 
 /*
  * Read the loose references for refs from the namespace dirname.
- * dirname must end with '/'.
+ * dirname must end with '/'.  dir must be the directory entry
+ * corresponding to dirname.
  */
 static void get_ref_dir(struct ref_cache *refs, const char *dirname,
                        struct ref_dir *dir)
@@ -792,7 +807,8 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
                        ; /* silently ignore */
                } else if (S_ISDIR(st.st_mode)) {
                        strbuf_addch(&refname, '/');
-                       get_ref_dir(refs, refname.buf, dir);
+                       get_ref_dir(refs, refname.buf,
+                                   &search_for_subdir(dir, refname.buf, 1)->u.subdir);
                } else {
                        if (*refs->name) {
                                hashclr(sha1);
@@ -805,7 +821,8 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
                                hashclr(sha1);
                                flag |= REF_ISBROKEN;
                        }
-                       add_ref(dir, create_ref_entry(refname.buf, sha1, flag, 1));
+                       add_entry_to_dir(dir,
+                                        create_ref_entry(refname.buf, sha1, flag, 1));
                }
                strbuf_setlen(&refname, dirnamelen);
        }
@@ -816,7 +833,8 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
 static struct ref_dir *get_loose_refs(struct ref_cache *refs)
 {
        if (!refs->did_loose) {
-               get_ref_dir(refs, "refs/", &refs->loose);
+               get_ref_dir(refs, "refs/",
+                           &search_for_subdir(&refs->loose, "refs/", 1)->u.subdir);
                refs->did_loose = 1;
        }
        return &refs->loose;