get_ref_dir(): add function for getting a ref_dir from a ref_entry

Convert all accesses of a ref_dir within a ref_entry to use this function. This function will later be responsible for reading loose references from disk on demand. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 27, 2012 at 00:27 UTC d7826d5427724d878e52bce49139f96187527fc3
1 file changed +25 -15
refs.c
+25 -15
@@ -171,6 +171,12 @@ struct ref_entry {
171 char name[FLEX_ARRAY];
172 };
173
174 +static struct ref_dir *get_ref_dir(struct ref_entry *entry)
175 +{
176 + assert(entry->flag & REF_DIR);
177 + return &entry->u.subdir;
178 +}
179 +
180 static struct ref_entry *create_ref_entry(const char *refname,
181 const unsigned char *sha1, int flag,
182 int check_name)
@@ -195,7 +201,7 @@ static void clear_ref_dir(struct ref_dir *dir);
201 static void free_ref_entry(struct ref_entry *entry)
202 {
203 if (entry->flag & REF_DIR)
198 - clear_ref_dir(&entry->u.subdir);
204 + clear_ref_dir(get_ref_dir(entry));
205 free(entry);
206 }
207
@@ -320,7 +326,7 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
326 entry = search_for_subdir(dir, dirname.buf, mkdir);
327 if (!entry)
328 break;
323 - dir = &entry->u.subdir;
329 + dir = get_ref_dir(entry);
330 }
331
332 strbuf_release(&dirname);
@@ -449,8 +455,9 @@ static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
455 struct ref_entry *entry = dir->entries[i];
456 int retval;
457 if (entry->flag & REF_DIR) {
452 - sort_ref_dir(&entry->u.subdir);
453 - retval = do_for_each_ref_in_dir(&entry->u.subdir, 0,
458 + struct ref_dir *subdir = get_ref_dir(entry);
459 + sort_ref_dir(subdir);
460 + retval = do_for_each_ref_in_dir(subdir, 0,
461 base, fn, trim, flags, cb_data);
462 } else {
463 retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
@@ -495,10 +502,12 @@ static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
502 if (cmp == 0) {
503 if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
504 /* Both are directories; descend them in parallel. */
498 - sort_ref_dir(&e1->u.subdir);
499 - sort_ref_dir(&e2->u.subdir);
505 + struct ref_dir *subdir1 = get_ref_dir(e1);
506 + struct ref_dir *subdir2 = get_ref_dir(e2);
507 + sort_ref_dir(subdir1);
508 + sort_ref_dir(subdir2);
509 retval = do_for_each_ref_in_dirs(
501 - &e1->u.subdir, &e2->u.subdir,
510 + subdir1, subdir2,
511 base, fn, trim, flags, cb_data);
512 i1++;
513 i2++;
@@ -521,9 +530,10 @@ static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
530 i2++;
531 }
532 if (e->flag & REF_DIR) {
524 - sort_ref_dir(&e->u.subdir);
533 + struct ref_dir *subdir = get_ref_dir(e);
534 + sort_ref_dir(subdir);
535 retval = do_for_each_ref_in_dir(
526 - &e->u.subdir, 0,
536 + subdir, 0,
537 base, fn, trim, flags, cb_data);
538 } else {
539 retval = do_one_ref(base, fn, trim, flags, cb_data, e);
@@ -751,11 +761,11 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
761 packed_refs_file = git_path("packed-refs");
762 f = fopen(packed_refs_file, "r");
763 if (f) {
754 - read_packed_refs(f, &refs->packed->u.subdir);
764 + read_packed_refs(f, get_ref_dir(refs->packed));
765 fclose(f);
766 }
767 }
758 - return &refs->packed->u.subdir;
768 + return get_ref_dir(refs->packed);
769 }
770
771 void add_packed_ref(const char *refname, const unsigned char *sha1)
@@ -809,7 +819,7 @@ static void read_loose_refs(struct ref_cache *refs, const char *dirname,
819 } else if (S_ISDIR(st.st_mode)) {
820 strbuf_addch(&refname, '/');
821 read_loose_refs(refs, refname.buf,
812 - &search_for_subdir(dir, refname.buf, 1)->u.subdir);
822 + get_ref_dir(search_for_subdir(dir, refname.buf, 1)));
823 } else {
824 if (*refs->name) {
825 hashclr(sha1);
@@ -836,10 +846,10 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
846 if (!refs->loose) {
847 refs->loose = create_dir_entry("");
848 read_loose_refs(refs, "refs/",
839 - &search_for_subdir(&refs->loose->u.subdir,
840 - "refs/", 1)->u.subdir);
849 + get_ref_dir(search_for_subdir(get_ref_dir(refs->loose),
850 + "refs/", 1)));
851 }
842 - return &refs->loose->u.subdir;
852 + return get_ref_dir(refs->loose);
853 }
854
855 /* We allow "recursive" symbolic refs. Only within reason, though */