Skip to content

Commit dcb972a

Browse files
KarthikNayakgitster
authored andcommitted
refs: extract out refs_create_refdir_stubs()
For Git to recognize a directory as a Git directory, it requires the directory to contain: 1. 'HEAD' file 2. object/ directory 3. 'refs/' directory Here, #1 and #3 are part of the reference storage mechanism, specifically the files backend. Since then, newer backends such as the reftable backend have moved to using their own path ('reftable/') for storing references. But to ensure git still recognizes the directory as a Git directory, we create stubs. There are two locations we create stubs: - In 'refs/reftable-backend.c' when creating the reftable backend. - In 'clone.c' before spawning transport helpers. In a following commit, we'll add another instance. So instead of repeating the code, let's extract out this code to `refs_create_refdir_stubs()` and use it. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cdb89a8 commit dcb972a

4 files changed

Lines changed: 40 additions & 18 deletions

File tree

builtin/clone.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,12 +1225,7 @@ int cmd_clone(int argc,
12251225
initialize_repository_version(GIT_HASH_UNKNOWN,
12261226
the_repository->ref_storage_format, 1);
12271227

1228-
strbuf_addf(&buf, "%s/HEAD", git_dir);
1229-
write_file(buf.buf, "ref: refs/heads/.invalid");
1230-
1231-
strbuf_reset(&buf);
1232-
strbuf_addf(&buf, "%s/refs", git_dir);
1233-
safe_create_dir(the_repository, buf.buf, 1);
1228+
refs_create_refdir_stubs(the_repository, git_dir, NULL);
12341229

12351230
/*
12361231
* additional config can be injected with -c, make sure it's included

refs.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,3 +3402,27 @@ const char *ref_transaction_error_msg(enum ref_transaction_error err)
34023402
return "unknown failure";
34033403
}
34043404
}
3405+
3406+
void refs_create_refdir_stubs(struct repository *repo, const char *refdir,
3407+
const char *refs_heads_msg)
3408+
{
3409+
struct strbuf path = STRBUF_INIT;
3410+
3411+
3412+
strbuf_addf(&path, "%s/HEAD", refdir);
3413+
write_file(path.buf, "ref: refs/heads/.invalid");
3414+
adjust_shared_perm(repo, path.buf);
3415+
3416+
strbuf_reset(&path);
3417+
strbuf_addf(&path, "%s/refs", refdir);
3418+
safe_create_dir(repo, path.buf, 1);
3419+
3420+
if (refs_heads_msg) {
3421+
strbuf_reset(&path);
3422+
strbuf_addf(&path, "%s/refs/heads", refdir);
3423+
write_file(path.buf, "%s", refs_heads_msg);
3424+
adjust_shared_perm(repo, path.buf);
3425+
}
3426+
3427+
strbuf_release(&path);
3428+
}

refs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,4 +1427,17 @@ void ref_iterator_free(struct ref_iterator *ref_iterator);
14271427
int do_for_each_ref_iterator(struct ref_iterator *iter,
14281428
each_ref_fn fn, void *cb_data);
14291429

1430+
/*
1431+
* Git only recognizes a directory as a repository if it contains:
1432+
* - HEAD file
1433+
* - refs/ folder
1434+
* While it is necessary within the files backend, newer backends may not
1435+
* follow the same structure. To go around this, we create stubs as necessary.
1436+
*
1437+
* If provided with a 'refs_heads_msg', we create the 'refs/heads/head' file
1438+
* with the provided message.
1439+
*/
1440+
void refs_create_refdir_stubs(struct repository *repo, const char *refdir,
1441+
const char *refs_heads_msg);
1442+
14301443
#endif /* REFS_H */

refs/reftable-backend.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -491,18 +491,8 @@ static int reftable_be_create_on_disk(struct ref_store *ref_store,
491491
safe_create_dir(the_repository, sb.buf, 1);
492492
strbuf_reset(&sb);
493493

494-
strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
495-
write_file(sb.buf, "ref: refs/heads/.invalid");
496-
adjust_shared_perm(the_repository, sb.buf);
497-
strbuf_reset(&sb);
498-
499-
strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
500-
safe_create_dir(the_repository, sb.buf, 1);
501-
strbuf_reset(&sb);
502-
503-
strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
504-
write_file(sb.buf, "this repository uses the reftable format");
505-
adjust_shared_perm(the_repository, sb.buf);
494+
refs_create_refdir_stubs(the_repository, refs->base.gitdir,
495+
"this repository uses the reftable format");
506496

507497
strbuf_release(&sb);
508498
return 0;

0 commit comments

Comments
 (0)