Skip to content

Commit 19e02f4

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 802de3d commit 19e02f4

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

refs/files-backend.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,18 @@ static void files_reflog_path(struct files_ref_store *refs,
11801180
strbuf_git_path(sb, "logs/%s", refname);
11811181
}
11821182

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+
11831195
/*
11841196
* Get the packed_ref_cache for the specified files_ref_store,
11851197
* creating it if necessary.
@@ -1249,19 +1261,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
12491261
struct strbuf refname;
12501262
struct strbuf path = STRBUF_INIT;
12511263
size_t path_baselen;
1252-
int err = 0;
12531264

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);
12581266
path_baselen = path.len;
12591267

1260-
if (err) {
1261-
strbuf_release(&path);
1262-
return;
1263-
}
1264-
12651268
d = opendir(path.buf);
12661269
if (!d) {
12671270
strbuf_release(&path);
@@ -1396,10 +1399,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
13961399
*type = 0;
13971400
strbuf_reset(&sb_path);
13981401

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);
14031403

14041404
path = sb_path.buf;
14051405

@@ -1587,7 +1587,7 @@ static int lock_raw_ref(struct files_ref_store *refs,
15871587
*lock_p = lock = xcalloc(1, sizeof(*lock));
15881588

15891589
lock->ref_name = xstrdup(refname);
1590-
strbuf_git_path(&ref_file, "%s", refname);
1590+
files_ref_path(refs, &ref_file, refname);
15911591

15921592
retry:
15931593
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,
20562056
if (flags & REF_DELETING)
20572057
resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
20582058

2059-
strbuf_git_path(&ref_file, "%s", refname);
2059+
files_ref_path(refs, &ref_file, refname);
20602060
resolved = !!resolve_ref_unsafe(refname, resolve_flags,
20612061
lock->old_oid.hash, type);
20622062
if (!resolved && errno == EISDIR) {
@@ -2355,7 +2355,7 @@ static void try_remove_empty_parents(struct files_ref_store *refs,
23552355
strbuf_setlen(&buf, q - buf.buf);
23562356

23572357
strbuf_reset(&sb);
2358-
strbuf_git_path(&sb, "%s", buf.buf);
2358+
files_ref_path(refs, &sb, buf.buf);
23592359
if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
23602360
flags &= ~REMOVE_EMPTY_PARENTS_REF;
23612361

@@ -2672,7 +2672,7 @@ static int files_rename_ref(struct ref_store *ref_store,
26722672
struct strbuf path = STRBUF_INIT;
26732673
int result;
26742674

2675-
strbuf_git_path(&path, "%s", newrefname);
2675+
files_ref_path(refs, &path, newrefname);
26762676
result = remove_empty_directories(&path);
26772677
strbuf_release(&path);
26782678

@@ -3906,7 +3906,7 @@ static int files_transaction_commit(struct ref_store *ref_store,
39063906
update->type & REF_ISSYMREF) {
39073907
/* It is a loose reference. */
39083908
strbuf_reset(&sb);
3909-
strbuf_git_path(&sb, "%s", lock->ref_name);
3909+
files_ref_path(refs, &sb, lock->ref_name);
39103910
if (unlink_or_msg(sb.buf, err)) {
39113911
ret = TRANSACTION_GENERIC_ERROR;
39123912
goto cleanup;
@@ -4206,19 +4206,18 @@ static int files_reflog_expire(struct ref_store *ref_store,
42064206

42074207
static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
42084208
{
4209+
struct files_ref_store *refs =
4210+
files_downcast(ref_store, 0, "init_db");
42094211
struct strbuf sb = STRBUF_INIT;
42104212

4211-
/* Check validity (but we don't need the result): */
4212-
files_downcast(ref_store, 0, "init_db");
4213-
42144213
/*
42154214
* Create .git/refs/{heads,tags}
42164215
*/
4217-
strbuf_git_path(&sb, "refs/heads");
4216+
files_ref_path(refs, &sb, "refs/heads");
42184217
safe_create_dir(sb.buf, 1);
42194218

42204219
strbuf_reset(&sb);
4221-
strbuf_git_path(&sb, "refs/tags");
4220+
files_ref_path(refs, &sb, "refs/tags");
42224221
safe_create_dir(sb.buf, 1);
42234222

42244223
strbuf_release(&sb);

0 commit comments

Comments
 (0)