lock_any_ref_for_update(): reject wildcard return from check_ref_format

Recent check_ref_format() returns -3 as well as -1 (general
error) and -2 (less than two levels).  The caller was explicitly
checking for -1, to allow "HEAD" but still needed to disallow
bogus refs.

This introduces symbolic constants for the return values from
check_ref_format() to make them read better and more
meaningful.  Normal ref creation codepath can still treat
non-zero return values as errors.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2008-01-01 22:33:20 -08:00
parent 49b9362fd3
commit 5f7b202a7f
2 changed files with 22 additions and 10 deletions

27
refs.c
View File

@ -613,32 +613,37 @@ int check_ref_format(const char *ref)
while ((ch = *cp++) == '/') while ((ch = *cp++) == '/')
; /* tolerate duplicated slashes */ ; /* tolerate duplicated slashes */
if (!ch) if (!ch)
return -1; /* should not end with slashes */ /* should not end with slashes */
return CHECK_REF_FORMAT_ERROR;
/* we are at the beginning of the path component */ /* we are at the beginning of the path component */
if (ch == '.') if (ch == '.')
return -1; return CHECK_REF_FORMAT_ERROR;
bad_type = bad_ref_char(ch); bad_type = bad_ref_char(ch);
if (bad_type) { if (bad_type) {
return (bad_type == 2 && !*cp) ? -3 : -1; return (bad_type == 2 && !*cp)
? CHECK_REF_FORMAT_WILDCARD
: CHECK_REF_FORMAT_ERROR;
} }
/* scan the rest of the path component */ /* scan the rest of the path component */
while ((ch = *cp++) != 0) { while ((ch = *cp++) != 0) {
bad_type = bad_ref_char(ch); bad_type = bad_ref_char(ch);
if (bad_type) { if (bad_type) {
return (bad_type == 2 && !*cp) ? -3 : -1; return (bad_type == 2 && !*cp)
? CHECK_REF_FORMAT_WILDCARD
: CHECK_REF_FORMAT_ERROR;
} }
if (ch == '/') if (ch == '/')
break; break;
if (ch == '.' && *cp == '.') if (ch == '.' && *cp == '.')
return -1; return CHECK_REF_FORMAT_ERROR;
} }
level++; level++;
if (!ch) { if (!ch) {
if (level < 2) if (level < 2)
return -2; /* at least of form "heads/blah" */ return CHECK_REF_FORMAT_ONELEVEL;
return 0; return CHECK_REF_FORMAT_OK;
} }
} }
} }
@ -816,9 +821,13 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags) struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags)
{ {
if (check_ref_format(ref) == -1) switch (check_ref_format(ref)) {
case CHECK_REF_FORMAT_ERROR:
case CHECK_REF_FORMAT_WILDCARD:
return NULL; return NULL;
return lock_ref_sha1_basic(ref, old_sha1, flags, NULL); default:
return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
}
} }
static struct lock_file packlock; static struct lock_file packlock;

5
refs.h
View File

@ -52,7 +52,10 @@ int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data);
*/ */
extern int for_each_reflog(each_ref_fn, void *); extern int for_each_reflog(each_ref_fn, void *);
/** Returns 0 if target has the right format for a ref. **/ #define CHECK_REF_FORMAT_OK 0
#define CHECK_REF_FORMAT_ERROR (-1)
#define CHECK_REF_FORMAT_ONELEVEL (-2)
#define CHECK_REF_FORMAT_WILDCARD (-3)
extern int check_ref_format(const char *target); extern int check_ref_format(const char *target);
/** rename ref, return 0 on success **/ /** rename ref, return 0 on success **/