From 19e02f4f46c46bcd2544e5811953a4ee324f2f7c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 26 Mar 2017 09:42:23 +0700 Subject: [PATCH] files-backend: add and use files_ref_path() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- refs/files-backend.c | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index b0e0df9f66..741e52d143 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1180,6 +1180,18 @@ static void files_reflog_path(struct files_ref_store *refs, strbuf_git_path(sb, "logs/%s", refname); } +static void files_ref_path(struct files_ref_store *refs, + struct strbuf *sb, + const char *refname) +{ + if (refs->submodule) { + strbuf_git_path_submodule(sb, refs->submodule, "%s", refname); + return; + } + + strbuf_git_path(sb, "%s", refname); +} + /* * Get the packed_ref_cache for the specified files_ref_store, * creating it if necessary. @@ -1249,19 +1261,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir) struct strbuf refname; struct strbuf path = STRBUF_INIT; size_t path_baselen; - int err = 0; - if (refs->submodule) - err = strbuf_git_path_submodule(&path, refs->submodule, "%s", dirname); - else - strbuf_git_path(&path, "%s", dirname); + files_ref_path(refs, &path, dirname); path_baselen = path.len; - if (err) { - strbuf_release(&path); - return; - } - d = opendir(path.buf); if (!d) { strbuf_release(&path); @@ -1396,10 +1399,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, *type = 0; strbuf_reset(&sb_path); - if (refs->submodule) - strbuf_git_path_submodule(&sb_path, refs->submodule, "%s", refname); - else - strbuf_git_path(&sb_path, "%s", refname); + files_ref_path(refs, &sb_path, refname); path = sb_path.buf; @@ -1587,7 +1587,7 @@ static int lock_raw_ref(struct files_ref_store *refs, *lock_p = lock = xcalloc(1, sizeof(*lock)); lock->ref_name = xstrdup(refname); - strbuf_git_path(&ref_file, "%s", refname); + files_ref_path(refs, &ref_file, refname); retry: 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, if (flags & REF_DELETING) resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; - strbuf_git_path(&ref_file, "%s", refname); + files_ref_path(refs, &ref_file, refname); resolved = !!resolve_ref_unsafe(refname, resolve_flags, lock->old_oid.hash, type); if (!resolved && errno == EISDIR) { @@ -2355,7 +2355,7 @@ static void try_remove_empty_parents(struct files_ref_store *refs, strbuf_setlen(&buf, q - buf.buf); strbuf_reset(&sb); - strbuf_git_path(&sb, "%s", buf.buf); + files_ref_path(refs, &sb, buf.buf); if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf)) flags &= ~REMOVE_EMPTY_PARENTS_REF; @@ -2672,7 +2672,7 @@ static int files_rename_ref(struct ref_store *ref_store, struct strbuf path = STRBUF_INIT; int result; - strbuf_git_path(&path, "%s", newrefname); + files_ref_path(refs, &path, newrefname); result = remove_empty_directories(&path); strbuf_release(&path); @@ -3906,7 +3906,7 @@ static int files_transaction_commit(struct ref_store *ref_store, update->type & REF_ISSYMREF) { /* It is a loose reference. */ strbuf_reset(&sb); - strbuf_git_path(&sb, "%s", lock->ref_name); + files_ref_path(refs, &sb, lock->ref_name); if (unlink_or_msg(sb.buf, err)) { ret = TRANSACTION_GENERIC_ERROR; goto cleanup; @@ -4206,19 +4206,18 @@ static int files_reflog_expire(struct ref_store *ref_store, static int files_init_db(struct ref_store *ref_store, struct strbuf *err) { + struct files_ref_store *refs = + files_downcast(ref_store, 0, "init_db"); struct strbuf sb = STRBUF_INIT; - /* Check validity (but we don't need the result): */ - files_downcast(ref_store, 0, "init_db"); - /* * Create .git/refs/{heads,tags} */ - strbuf_git_path(&sb, "refs/heads"); + files_ref_path(refs, &sb, "refs/heads"); safe_create_dir(sb.buf, 1); strbuf_reset(&sb); - strbuf_git_path(&sb, "refs/tags"); + files_ref_path(refs, &sb, "refs/tags"); safe_create_dir(sb.buf, 1); strbuf_release(&sb); -- 2.11.4.GIT