files-backend: add and use files_packed_refs_path()

Keep repo-related path handling in one place. This will make it easier to add submodule/multiworktree support later. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC 33dfb9f3f2aa6b7cc3fcba84ad8ac1485272e003
1 file changed +18 -11
refs/files-backend.c
+18 -11
@@ -923,6 +923,8 @@ struct files_ref_store {
923 */
924 const char *submodule;
925
926 + char *packed_refs_path;
927 +
928 struct ref_entry *loose;
929 struct packed_ref_cache *packed;
930 };
@@ -985,7 +987,14 @@ static struct ref_store *files_ref_store_create(const char *submodule)
987
988 base_ref_store_init(ref_store, &refs_be_files);
989
988 - refs->submodule = xstrdup_or_null(submodule);
990 + if (submodule) {
991 + refs->submodule = xstrdup(submodule);
992 + refs->packed_refs_path = git_pathdup_submodule(
993 + refs->submodule, "packed-refs");
994 + return ref_store;
995 + }
996 +
997 + refs->packed_refs_path = git_pathdup("packed-refs");
998
999 return ref_store;
1000 }
@@ -1153,19 +1162,18 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
1162 strbuf_release(&line);
1163 }
1164
1165 +static const char *files_packed_refs_path(struct files_ref_store *refs)
1166 +{
1167 + return refs->packed_refs_path;
1168 +}
1169 +
1170 /*
1171 * Get the packed_ref_cache for the specified files_ref_store,
1172 * creating it if necessary.
1173 */
1174 static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
1175 {
1162 - char *packed_refs_file;
1163 -
1164 - if (refs->submodule)
1165 - packed_refs_file = git_pathdup_submodule(refs->submodule,
1166 - "packed-refs");
1167 - else
1168 - packed_refs_file = git_pathdup("packed-refs");
1176 + const char *packed_refs_file = files_packed_refs_path(refs);
1177
1178 if (refs->packed &&
1179 !stat_validity_check(&refs->packed->validity, packed_refs_file))
@@ -1184,7 +1192,6 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
1192 fclose(f);
1193 }
1194 }
1187 - free(packed_refs_file);
1195 return refs->packed;
1196 }
1197
@@ -2157,7 +2164,7 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
2164 }
2165
2166 if (hold_lock_file_for_update_timeout(
2160 - &packlock, git_path("packed-refs"),
2167 + &packlock, files_packed_refs_path(refs),
2168 flags, timeout_value) < 0)
2169 return -1;
2170 /*
@@ -2423,7 +2430,7 @@ static int repack_without_refs(struct files_ref_store *refs,
2430 return 0; /* no refname exists in packed refs */
2431
2432 if (lock_packed_refs(refs, 0)) {
2426 - unable_to_lock_message(git_path("packed-refs"), errno, err);
2433 + unable_to_lock_message(files_packed_refs_path(refs), errno, err);
2434 return -1;
2435 }
2436 packed = get_packed_refs(refs);