Skip to content

Commit fe9bfec

Browse files
author
yuangli
committed
tag: refactor tag name validity checks
1 parent 7560ac4 commit fe9bfec

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/tag.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ static int write_tag_annotation(
244244
return -1;
245245
}
246246

247+
static bool tag_name_follows_pattern(const char *tag_name)
248+
{
249+
/*
250+
* Discourage tag name starting with dash,
251+
* https://github.com/git/git/commit/4f0accd638b8d2
252+
*/
253+
return tag_name[0] != '-';
254+
}
255+
247256
static int git_tag_create__internal(
248257
git_oid *oid,
249258
git_repository *repo,
@@ -269,6 +278,11 @@ static int git_tag_create__internal(
269278
return -1;
270279
}
271280

281+
if (!tag_name_follows_pattern(tag_name)) {
282+
git_error_set(GIT_ERROR_TAG, "'%s' is not a valid tag name", tag_name);
283+
return -1;
284+
}
285+
272286
error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name);
273287
if (error < 0 && error != GIT_ENOTFOUND)
274288
goto cleanup;
@@ -540,11 +554,7 @@ int git_tag_name_is_valid(int *valid, const char *name)
540554

541555
GIT_ASSERT(valid);
542556

543-
/*
544-
* Discourage tag name starting with dash,
545-
* https://github.com/git/git/commit/4f0accd638b8d2
546-
*/
547-
if (!name || name[0] == '-')
557+
if (!name || !tag_name_follows_pattern(name))
548558
goto done;
549559

550560
if ((error = git_buf_puts(&ref_name, GIT_REFS_TAGS_DIR)) < 0 ||

0 commit comments

Comments
 (0)