Commit b9d283d1 by Vicent Martí

Merge pull request #897 from nulltoken/topic/git_reference_check_format

refs: expose git_reference_normalize_name()
parents 05752700 2e0c8816
...@@ -376,6 +376,54 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref); ...@@ -376,6 +376,54 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
*/ */
GIT_EXTERN(int) git_reference_is_branch(git_reference *ref); GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
enum {
GIT_REF_FORMAT_NORMAL = 0,
/**
* Control whether one-level refnames are accepted
* (i.e., refnames that do not contain multiple /-separated
* components)
*/
GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
/**
* Interpret the provided name as a reference pattern for a
* refspec (as used with remote repositories). If this option
* is enabled, the name is allowed to contain a single * (<star>)
* in place of a one full pathname component
* (e.g., foo/<star>/bar but not foo/bar<star>).
*/
GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
};
/**
* Normalize the reference name by removing any leading
* slash (/) characters and collapsing runs of adjacent slashes
* between name components into a single slash.
*
* Once normalized, if the reference name is valid, it will be
* returned in the user allocated buffer.
*
* TODO: Implement handling of GIT_REF_FORMAT_REFSPEC_PATTERN
*
* @param buffer_out The user allocated buffer where the
* normalized name will be stored.
*
* @param buffer_size buffer_out size
*
* @param name name to be checked.
*
* @param flags Flags to determine the options to be applied while
* checking the validatity of the name.
*
* @return 0 or an error code.
*/
GIT_EXTERN(int) git_reference_normalize_name(
char *buffer_out,
size_t buffer_size,
const char *name,
unsigned int flags);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -68,11 +68,6 @@ static int reference_path_available(git_repository *repo, ...@@ -68,11 +68,6 @@ static int reference_path_available(git_repository *repo,
static int reference_delete(git_reference *ref); static int reference_delete(git_reference *ref);
static int reference_lookup(git_reference *ref); static int reference_lookup(git_reference *ref);
/* name normalization */
static int normalize_name(char *buffer_out, size_t out_size,
const char *name, int is_oid_ref);
void git_reference_free(git_reference *reference) void git_reference_free(git_reference *reference)
{ {
if (reference == NULL) if (reference == NULL)
...@@ -1099,9 +1094,12 @@ int git_reference_lookup_resolved( ...@@ -1099,9 +1094,12 @@ int git_reference_lookup_resolved(
scan->name = git__calloc(GIT_REFNAME_MAX + 1, sizeof(char)); scan->name = git__calloc(GIT_REFNAME_MAX + 1, sizeof(char));
GITERR_CHECK_ALLOC(scan->name); GITERR_CHECK_ALLOC(scan->name);
if ((result = normalize_name(scan->name, GIT_REFNAME_MAX, name, 0)) < 0) { if ((result = git_reference__normalize_name(
git_reference_free(scan); scan->name,
return result; GIT_REFNAME_MAX,
name)) < 0) {
git_reference_free(scan);
return result;
} }
scan->target.symbolic = git__strdup(scan->name); scan->target.symbolic = git__strdup(scan->name);
...@@ -1198,8 +1196,11 @@ int git_reference_create_symbolic( ...@@ -1198,8 +1196,11 @@ int git_reference_create_symbolic(
char normalized[GIT_REFNAME_MAX]; char normalized[GIT_REFNAME_MAX];
git_reference *ref = NULL; git_reference *ref = NULL;
if (normalize_name(normalized, sizeof(normalized), name, 0) < 0) if (git_reference__normalize_name(
return -1; normalized,
sizeof(normalized),
name) < 0)
return -1;
if (reference_can_write(repo, normalized, NULL, force) < 0) if (reference_can_write(repo, normalized, NULL, force) < 0)
return -1; return -1;
...@@ -1234,8 +1235,11 @@ int git_reference_create_oid( ...@@ -1234,8 +1235,11 @@ int git_reference_create_oid(
git_reference *ref = NULL; git_reference *ref = NULL;
char normalized[GIT_REFNAME_MAX]; char normalized[GIT_REFNAME_MAX];
if (normalize_name(normalized, sizeof(normalized), name, 1) < 0) if (git_reference__normalize_name_oid(
return -1; normalized,
sizeof(normalized),
name) < 0)
return -1;
if (reference_can_write(repo, normalized, NULL, force) < 0) if (reference_can_write(repo, normalized, NULL, force) < 0)
return -1; return -1;
...@@ -1314,8 +1318,11 @@ int git_reference_set_target(git_reference *ref, const char *target) ...@@ -1314,8 +1318,11 @@ int git_reference_set_target(git_reference *ref, const char *target)
return -1; return -1;
} }
if (normalize_name(normalized, sizeof(normalized), target, 0)) if (git_reference__normalize_name(
return -1; normalized,
sizeof(normalized),
target))
return -1;
git__free(ref->target.symbolic); git__free(ref->target.symbolic);
ref->target.symbolic = git__strdup(normalized); ref->target.symbolic = git__strdup(normalized);
...@@ -1327,15 +1334,23 @@ int git_reference_set_target(git_reference *ref, const char *target) ...@@ -1327,15 +1334,23 @@ int git_reference_set_target(git_reference *ref, const char *target)
int git_reference_rename(git_reference *ref, const char *new_name, int force) int git_reference_rename(git_reference *ref, const char *new_name, int force)
{ {
int result; int result;
unsigned int normalization_flags;
git_buf aux_path = GIT_BUF_INIT; git_buf aux_path = GIT_BUF_INIT;
char normalized[GIT_REFNAME_MAX]; char normalized[GIT_REFNAME_MAX];
const char *head_target = NULL; const char *head_target = NULL;
git_reference *head = NULL; git_reference *head = NULL;
if (normalize_name(normalized, sizeof(normalized), normalization_flags = ref->flags & GIT_REF_SYMBOLIC ?
new_name, ref->flags & GIT_REF_OID) < 0) GIT_REF_FORMAT_ALLOW_ONELEVEL
return -1; : GIT_REF_FORMAT_NORMAL;
if (git_reference_normalize_name(
normalized,
sizeof(normalized),
new_name,
normalization_flags) < 0)
return -1;
if (reference_can_write(ref->owner, normalized, ref->name, force) < 0) if (reference_can_write(ref->owner, normalized, ref->name, force) < 0)
return -1; return -1;
...@@ -1565,11 +1580,11 @@ static int is_valid_ref_char(char ch) ...@@ -1565,11 +1580,11 @@ static int is_valid_ref_char(char ch)
} }
} }
static int normalize_name( int git_reference_normalize_name(
char *buffer_out, char *buffer_out,
size_t out_size, size_t buffer_size,
const char *name, const char *name,
int is_oid_ref) unsigned int flags)
{ {
const char *name_end, *buffer_out_start; const char *name_end, *buffer_out_start;
const char *current; const char *current;
...@@ -1577,12 +1592,17 @@ static int normalize_name( ...@@ -1577,12 +1592,17 @@ static int normalize_name(
assert(name && buffer_out); assert(name && buffer_out);
if (flags & GIT_REF_FORMAT_REFSPEC_PATTERN) {
giterr_set(GITERR_INVALID, "Unimplemented");
return -1;
}
buffer_out_start = buffer_out; buffer_out_start = buffer_out;
current = name; current = name;
name_end = name + strlen(name); name_end = name + strlen(name);
/* Terminating null byte */ /* Terminating null byte */
out_size--; buffer_size--;
/* A refname can not be empty */ /* A refname can not be empty */
if (name_end == name) if (name_end == name)
...@@ -1592,7 +1612,7 @@ static int normalize_name( ...@@ -1592,7 +1612,7 @@ static int normalize_name(
if (*(name_end - 1) == '.' || *(name_end - 1) == '/') if (*(name_end - 1) == '.' || *(name_end - 1) == '/')
goto invalid_name; goto invalid_name;
while (current < name_end && out_size) { while (current < name_end && buffer_size > 0) {
if (!is_valid_ref_char(*current)) if (!is_valid_ref_char(*current))
goto invalid_name; goto invalid_name;
...@@ -1615,19 +1635,29 @@ static int normalize_name( ...@@ -1615,19 +1635,29 @@ static int normalize_name(
} }
if (*current == '/') if (*current == '/')
contains_a_slash = 1; if (buffer_out > buffer_out_start)
contains_a_slash = 1;
else {
current++;
continue;
}
*buffer_out++ = *current++; *buffer_out++ = *current++;
out_size--; buffer_size--;
} }
if (!out_size) if (current < name_end) {
goto invalid_name; giterr_set(
GITERR_REFERENCE,
"The provided buffer is too short to hold the normalization of '%s'", name);
return GIT_EBUFS;
}
/* Object id refname have to contain at least one slash, except /* Object id refname have to contain at least one slash, except
* for HEAD in a detached state or MERGE_HEAD if we're in the * for HEAD in a detached state or MERGE_HEAD if we're in the
* middle of a merge */ * middle of a merge */
if (is_oid_ref && if (!(flags & GIT_REF_FORMAT_ALLOW_ONELEVEL) &&
!contains_a_slash && !contains_a_slash &&
strcmp(name, GIT_HEAD_FILE) != 0 && strcmp(name, GIT_HEAD_FILE) != 0 &&
strcmp(name, GIT_MERGE_HEAD_FILE) != 0 && strcmp(name, GIT_MERGE_HEAD_FILE) != 0 &&
...@@ -1640,18 +1670,12 @@ static int normalize_name( ...@@ -1640,18 +1670,12 @@ static int normalize_name(
*buffer_out = '\0'; *buffer_out = '\0';
/*
* For object id references, name has to start with refs/. Again,
* we need to allow HEAD to be in a detached state.
*/
if (is_oid_ref && !(git__prefixcmp(buffer_out_start, GIT_REFS_DIR) ||
strcmp(buffer_out_start, GIT_HEAD_FILE)))
goto invalid_name;
return 0; return 0;
invalid_name: invalid_name:
giterr_set(GITERR_REFERENCE, "The given reference name is not valid"); giterr_set(
GITERR_REFERENCE,
"The given reference name '%s' is not valid", name);
return -1; return -1;
} }
...@@ -1660,7 +1684,11 @@ int git_reference__normalize_name( ...@@ -1660,7 +1684,11 @@ int git_reference__normalize_name(
size_t out_size, size_t out_size,
const char *name) const char *name)
{ {
return normalize_name(buffer_out, out_size, name, 0); return git_reference_normalize_name(
buffer_out,
out_size,
name,
GIT_REF_FORMAT_ALLOW_ONELEVEL);
} }
int git_reference__normalize_name_oid( int git_reference__normalize_name_oid(
...@@ -1668,7 +1696,11 @@ int git_reference__normalize_name_oid( ...@@ -1668,7 +1696,11 @@ int git_reference__normalize_name_oid(
size_t out_size, size_t out_size,
const char *name) const char *name)
{ {
return normalize_name(buffer_out, out_size, name, 1); return git_reference_normalize_name(
buffer_out,
out_size,
name,
GIT_REF_FORMAT_NORMAL);
} }
#define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC) #define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment