files-backend: add and use files_ref_path()

Keep repo-related path handling in one place. This will make it easier to add submodule/multiworktree support later. This automatically adds the "if submodule then use the submodule version of git_path" to other call sites too. But it does not mean those operations are submodule-ready. Not yet. 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 19e02f4f46c46bcd2544e5811953a4ee324f2f7c
1 file changed +23 -24
refs/files-backend.c
+23 -24
@@ -1180,6 +1180,18 @@ static void files_reflog_path(struct files_ref_store *refs,
1180 strbuf_git_path(sb, "logs/%s", refname);
1181 }
1182
1183 +static void files_ref_path(struct files_ref_store *refs,
1184 + struct strbuf *sb,
1185 + const char *refname)
1186 +{
1187 + if (refs->submodule) {
1188 + strbuf_git_path_submodule(sb, refs->submodule, "%s", refname);
1189 + return;
1190 + }
1191 +
1192 + strbuf_git_path(sb, "%s", refname);
1193 +}
1194 +
1195 /*
1196 * Get the packed_ref_cache for the specified files_ref_store,
1197 * creating it if necessary.
@@ -1249,19 +1261,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1261 struct strbuf refname;
1262 struct strbuf path = STRBUF_INIT;
1263 size_t path_baselen;
1252 - int err = 0;
1264
1254 - if (refs->submodule)
1255 - err = strbuf_git_path_submodule(&path, refs->submodule, "%s", dirname);
1256 - else
1257 - strbuf_git_path(&path, "%s", dirname);
1265 + files_ref_path(refs, &path, dirname);
1266 path_baselen = path.len;
1267
1260 - if (err) {
1261 - strbuf_release(&path);
1262 - return;
1263 - }
1264 -
1268 d = opendir(path.buf);
1269 if (!d) {
1270 strbuf_release(&path);
@@ -1396,10 +1399,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
1399 *type = 0;
1400 strbuf_reset(&sb_path);
1401
1399 - if (refs->submodule)
1400 - strbuf_git_path_submodule(&sb_path, refs->submodule, "%s", refname);
1401 - else
1402 - strbuf_git_path(&sb_path, "%s", refname);
1402 + files_ref_path(refs, &sb_path, refname);
1403
1404 path = sb_path.buf;
1405
@@ -1587,7 +1587,7 @@ static int lock_raw_ref(struct files_ref_store *refs,
1587 *lock_p = lock = xcalloc(1, sizeof(*lock));
1588
1589 lock->ref_name = xstrdup(refname);
1590 - strbuf_git_path(&ref_file, "%s", refname);
1590 + files_ref_path(refs, &ref_file, refname);
1591
1592 retry:
1593 switch (safe_create_leading_directories(ref_file.buf)) {
@@ -2056,7 +2056,7 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
2056 if (flags & REF_DELETING)
2057 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
2058
2059 - strbuf_git_path(&ref_file, "%s", refname);
2059 + files_ref_path(refs, &ref_file, refname);
2060 resolved = !!resolve_ref_unsafe(refname, resolve_flags,
2061 lock->old_oid.hash, type);
2062 if (!resolved && errno == EISDIR) {
@@ -2355,7 +2355,7 @@ static void try_remove_empty_parents(struct files_ref_store *refs,
2355 strbuf_setlen(&buf, q - buf.buf);
2356
2357 strbuf_reset(&sb);
2358 - strbuf_git_path(&sb, "%s", buf.buf);
2358 + files_ref_path(refs, &sb, buf.buf);
2359 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
2360 flags &= ~REMOVE_EMPTY_PARENTS_REF;
2361
@@ -2672,7 +2672,7 @@ static int files_rename_ref(struct ref_store *ref_store,
2672 struct strbuf path = STRBUF_INIT;
2673 int result;
2674
2675 - strbuf_git_path(&path, "%s", newrefname);
2675 + files_ref_path(refs, &path, newrefname);
2676 result = remove_empty_directories(&path);
2677 strbuf_release(&path);
2678
@@ -3906,7 +3906,7 @@ static int files_transaction_commit(struct ref_store *ref_store,
3906 update->type & REF_ISSYMREF) {
3907 /* It is a loose reference. */
3908 strbuf_reset(&sb);
3909 - strbuf_git_path(&sb, "%s", lock->ref_name);
3909 + files_ref_path(refs, &sb, lock->ref_name);
3910 if (unlink_or_msg(sb.buf, err)) {
3911 ret = TRANSACTION_GENERIC_ERROR;
3912 goto cleanup;
@@ -4206,19 +4206,18 @@ static int files_reflog_expire(struct ref_store *ref_store,
4206
4207 static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
4208 {
4209 + struct files_ref_store *refs =
4210 + files_downcast(ref_store, 0, "init_db");
4211 struct strbuf sb = STRBUF_INIT;
4212
4211 - /* Check validity (but we don't need the result): */
4212 - files_downcast(ref_store, 0, "init_db");
4213 -
4213 /*
4214 * Create .git/refs/{heads,tags}
4215 */
4217 - strbuf_git_path(&sb, "refs/heads");
4216 + files_ref_path(refs, &sb, "refs/heads");
4217 safe_create_dir(sb.buf, 1);
4218
4219 strbuf_reset(&sb);
4221 - strbuf_git_path(&sb, "refs/tags");
4220 + files_ref_path(refs, &sb, "refs/tags");
4221 safe_create_dir(sb.buf, 1);
4222
4223 strbuf_release(&sb);