refs: move out stub modification to generic layer

When creating the reftable reference backend on disk, we create stubs to
ensure that the directory can be recognized as a Git repository. This is
done by calling `refs_create_refdir_stubs()`. Move this to the generic
layer as this is needed for all backends excluding from the files
backends. In an upcoming commit where we introduce alternate reference
backend locations, we'll have to also create stubs in the $GIT_DIR
irrespective of the backend being used. This commit builds the base to
add that logic.

Similarly, move the logic for deletion of stubs to the generic layer.
The files backend recursively calls the remove function of the
'packed-backend', here skip calling the generic function since that
would try to delete stubs.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Karthik Nayak
2026-02-25 10:40:43 +01:00
committed by Junio C Hamano
parent 4ffbb02ee4
commit 2a32ac429e
3 changed files with 50 additions and 30 deletions

47
refs.c
View File

@@ -2189,12 +2189,55 @@ void refs_create_refdir_stubs(struct repository *repo, const char *refdir,
/* backend functions */
int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err)
{
return refs->be->create_on_disk(refs, flags, err);
int ret = refs->be->create_on_disk(refs, flags, err);
if (!ret &&
ref_storage_format_by_name(refs->be->name) != REF_STORAGE_FORMAT_FILES) {
struct strbuf msg = STRBUF_INIT;
strbuf_addf(&msg, "this repository uses the %s format", refs->be->name);
refs_create_refdir_stubs(refs->repo, refs->gitdir, msg.buf);
strbuf_release(&msg);
}
return ret;
}
int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err)
{
return refs->be->remove_on_disk(refs, err);
int ret = refs->be->remove_on_disk(refs, err);
if (!ret &&
ref_storage_format_by_name(refs->be->name) != REF_STORAGE_FORMAT_FILES) {
struct strbuf sb = STRBUF_INIT;
strbuf_addf(&sb, "%s/HEAD", refs->gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub HEAD: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs/heads", refs->gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub heads: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs", refs->gitdir);
if (rmdir(sb.buf) < 0) {
strbuf_addf(err, "could not delete refs directory: %s",
strerror(errno));
ret = -1;
}
strbuf_release(&sb);
}
return ret;
}
int repo_resolve_gitlink_ref(struct repository *r,

View File

@@ -3700,7 +3700,11 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
if (for_each_root_ref(refs, remove_one_root_ref, &data) < 0)
ret = -1;
if (ref_store_remove_on_disk(refs->packed_ref_store, err) < 0)
/*
* Directly access the cleanup functions for packed-refs as the generic function
* would try to clear stubs which isn't required for the files backend.
*/
if (refs->packed_ref_store->be->remove_on_disk(refs->packed_ref_store, err) < 0)
ret = -1;
strbuf_release(&sb);

View File

@@ -491,9 +491,6 @@ static int reftable_be_create_on_disk(struct ref_store *ref_store,
safe_create_dir(the_repository, sb.buf, 1);
strbuf_reset(&sb);
refs_create_refdir_stubs(the_repository, refs->base.gitdir,
"this repository uses the reftable format");
strbuf_release(&sb);
return 0;
}
@@ -519,30 +516,6 @@ static int reftable_be_remove_on_disk(struct ref_store *ref_store,
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub HEAD: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub heads: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
if (rmdir(sb.buf) < 0) {
strbuf_addf(err, "could not delete refs directory: %s",
strerror(errno));
ret = -1;
}
strbuf_release(&sb);
return ret;