fsck: add ref name check for files backend
The git-fsck(1) only implicitly checks the reference, it does not fully check refs with bad format name such as standalone "@". However, a file ending with ".lock" should not be marked as having a bad ref name. It is expected that concurrent writers may have such lock files. We currently ignore this situation. But for bare ".lock" file, we will report it as error. In order to provide such checks, add a new fsck message id "badRefName" with default ERROR type. Use existing "check_refname_format" to explicit check the ref name. And add a new unit test to verify the functionality. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
a7600b8481
commit
1c31be45b3
@@ -3419,6 +3419,36 @@ typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
|
||||
const char *refs_check_dir,
|
||||
struct dir_iterator *iter);
|
||||
|
||||
static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
|
||||
struct fsck_options *o,
|
||||
const char *refs_check_dir,
|
||||
struct dir_iterator *iter)
|
||||
{
|
||||
struct strbuf sb = STRBUF_INIT;
|
||||
int ret = 0;
|
||||
|
||||
/*
|
||||
* Ignore the files ending with ".lock" as they may be lock files
|
||||
* However, do not allow bare ".lock" files.
|
||||
*/
|
||||
if (iter->basename[0] != '.' && ends_with(iter->basename, ".lock"))
|
||||
goto cleanup;
|
||||
|
||||
if (check_refname_format(iter->basename, REFNAME_ALLOW_ONELEVEL)) {
|
||||
struct fsck_ref_report report = { .path = NULL };
|
||||
|
||||
strbuf_addf(&sb, "%s/%s", refs_check_dir, iter->relative_path);
|
||||
report.path = sb.buf;
|
||||
ret = fsck_report_ref(o, &report,
|
||||
FSCK_MSG_BAD_REF_NAME,
|
||||
"invalid refname format");
|
||||
}
|
||||
|
||||
cleanup:
|
||||
strbuf_release(&sb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int files_fsck_refs_dir(struct ref_store *ref_store,
|
||||
struct fsck_options *o,
|
||||
const char *refs_check_dir,
|
||||
@@ -3470,6 +3500,7 @@ static int files_fsck_refs(struct ref_store *ref_store,
|
||||
struct fsck_options *o)
|
||||
{
|
||||
files_fsck_refs_fn fsck_refs_fn[]= {
|
||||
files_fsck_refs_name,
|
||||
NULL,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user