refs: wrap the packed refs cache in a level of indirection

As we know, we can solve any problem in this manner. In this case, the problem is to avoid freeing a packed refs cache while somebody is using it. So add a level of indirection as a prelude to reference-counting the packed refs cache. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 20, 2013 at 10:37 UTC 2fff7812902abd0afe05ae1e9ef334fcd26f0389
1 file changed +26 -6
refs.c
+26 -6
@@ -806,6 +806,10 @@ static int is_refname_available(const char *refname, const char *oldrefname,
806 return 1;
807 }
808
809 +struct packed_ref_cache {
810 + struct ref_entry *root;
811 +};
812 +
813 /*
814 * Future: need to be in "struct repository"
815 * when doing a full libification.
@@ -813,7 +817,7 @@ static int is_refname_available(const char *refname, const char *oldrefname,
817 static struct ref_cache {
818 struct ref_cache *next;
819 struct ref_entry *loose;
816 - struct ref_entry *packed;
820 + struct packed_ref_cache *packed;
821 /*
822 * The submodule name, or "" for the main repo. We allocate
823 * length 1 rather than FLEX_ARRAY so that the main ref_cache
@@ -825,7 +829,8 @@ static struct ref_cache {
829 static void clear_packed_ref_cache(struct ref_cache *refs)
830 {
831 if (refs->packed) {
828 - free_ref_entry(refs->packed);
832 + free_ref_entry(refs->packed->root);
833 + free(refs->packed);
834 refs->packed = NULL;
835 }
836 }
@@ -996,24 +1001,39 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
1001 }
1002 }
1003
999 -static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1004 +/*
1005 + * Get the packed_ref_cache for the specified ref_cache, creating it
1006 + * if necessary.
1007 + */
1008 +static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs)
1009 {
1010 if (!refs->packed) {
1011 const char *packed_refs_file;
1012 FILE *f;
1013
1005 - refs->packed = create_dir_entry(refs, "", 0, 0);
1014 + refs->packed = xcalloc(1, sizeof(*refs->packed));
1015 + refs->packed->root = create_dir_entry(refs, "", 0, 0);
1016 if (*refs->name)
1017 packed_refs_file = git_path_submodule(refs->name, "packed-refs");
1018 else
1019 packed_refs_file = git_path("packed-refs");
1020 f = fopen(packed_refs_file, "r");
1021 if (f) {
1012 - read_packed_refs(f, get_ref_dir(refs->packed));
1022 + read_packed_refs(f, get_ref_dir(refs->packed->root));
1023 fclose(f);
1024 }
1025 }
1016 - return get_ref_dir(refs->packed);
1026 + return refs->packed;
1027 +}
1028 +
1029 +static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
1030 +{
1031 + return get_ref_dir(packed_ref_cache->root);
1032 +}
1033 +
1034 +static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1035 +{
1036 + return get_packed_ref_dir(get_packed_ref_cache(refs));
1037 }
1038
1039 void add_packed_ref(const char *refname, const unsigned char *sha1)