Outright forbid reference names with a leading '-'. Matches behaviour documented in git-repository(5).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
diff --git a/got/got.c b/got/got.c
index ef77dfc..5541936 100644
--- a/got/got.c
+++ b/got/got.c
@@ -655,12 +655,12 @@ cmd_import(int argc, char *argv[])
return error;
/*
- * Don't let the user create a branch named '-'.
+ * Don't let the user create a branch name with a leading '-'.
* While technically a valid reference name, this case is usually
* an unintended typo.
*/
- if (branch_name[0] == '-' && branch_name[1] == '\0')
- return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
+ if (branch_name[0] == '-')
+ return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
error = got_error_from_errno("asprintf");
@@ -3096,12 +3096,12 @@ add_ref(struct got_repository *repo, const char *refname, const char *target)
struct got_reference *ref = NULL;
/*
- * Don't let the user create a reference named '-'.
+ * Don't let the user create a reference name with a leading '-'.
* While technically a valid reference name, this case is usually
* an unintended typo.
*/
- if (refname[0] == '-' && refname[1] == '\0')
- return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
+ if (refname[0] == '-')
+ return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
repo);
@@ -3139,12 +3139,12 @@ add_symref(struct got_repository *repo, const char *refname, const char *target)
struct got_reference *target_ref = NULL;
/*
- * Don't let the user create a reference named '-'.
+ * Don't let the user create a reference name with a leading '-'.
* While technically a valid reference name, this case is usually
* an unintended typo.
*/
- if (refname[0] == '-' && refname[1] == '\0')
- return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
+ if (refname[0] == '-')
+ return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
err = got_ref_open(&target_ref, repo, target, 0);
if (err)
@@ -3430,12 +3430,12 @@ add_branch(struct got_repository *repo, const char *branch_name,
char *base_refname = NULL, *refname = NULL;
/*
- * Don't let the user create a branch named '-'.
+ * Don't let the user create a branch name with a leading '-'.
* While technically a valid reference name, this case is usually
* an unintended typo.
*/
- if (branch_name[0] == '-' && branch_name[1] == '\0')
- return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
+ if (branch_name[0] == '-')
+ return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
err = got_error_from_errno("asprintf");
@@ -3849,12 +3849,12 @@ add_tag(struct got_repository *repo, const char *tag_name,
int preserve_tagmsg = 0;
/*
- * Don't let the user create a tag named '-'.
+ * Don't let the user create a tag name with a leading '-'.
* While technically a valid reference name, this case is usually
* an unintended typo.
*/
- if (tag_name[0] == '-' && tag_name[1] == '\0')
- return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
+ if (tag_name[0] == '-')
+ return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
err = get_author(&tagger, repo);
if (err)
diff --git a/include/got_error.h b/include/got_error.h
index 4e252ff..e2e96e4 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -126,6 +126,7 @@
#define GOT_ERR_GIT_REPO_FORMAT 110
#define GOT_ERR_REBASE_REQUIRED 111
#define GOT_ERR_REGEX 112
+#define GOT_ERR_REF_NAME_MINUS 113
static const struct got_error {
int code;
@@ -258,6 +259,7 @@ static const struct got_error {
{ GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
{ GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
{ GOT_ERR_REGEX, "regular expression error" },
+ { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
};
/*