Skip to content

Commit

Permalink
Change check_ref_format() to take a flags argument
Browse files Browse the repository at this point in the history
Change check_ref_format() to take a flags argument that indicates what
is acceptable in the reference name (analogous to "git
check-ref-format"'s "--allow-onelevel" and "--refspec-pattern").  This
is more convenient for callers and also fixes a failure in the test
suite (and likely elsewhere in the code) by enabling "onelevel" and
"refspec-pattern" to be allowed independently of each other.

Also rename check_ref_format() to check_refname_format() to make it
obvious that it deals with refnames rather than references themselves.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
mhagger authored and gitster committed Oct 5, 2011
1 parent 9224b73 commit 8d9c501
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 126 deletions.
21 changes: 1 addition & 20 deletions builtin/check-ref-format.c
Expand Up @@ -53,9 +53,6 @@ static void refname_format_print(const char *arg)
printf("%s\n", refname); printf("%s\n", refname);
} }


#define REFNAME_ALLOW_ONELEVEL 1
#define REFNAME_REFSPEC_PATTERN 2

int cmd_check_ref_format(int argc, const char **argv, const char *prefix) int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
{ {
int i; int i;
Expand Down Expand Up @@ -83,24 +80,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
if (! (i == argc - 1)) if (! (i == argc - 1))
usage(builtin_check_ref_format_usage); usage(builtin_check_ref_format_usage);


switch (check_ref_format(argv[i])) { if (check_refname_format(argv[i], flags))
case CHECK_REF_FORMAT_OK:
break;
case CHECK_REF_FORMAT_ERROR:
return 1; return 1;
case CHECK_REF_FORMAT_ONELEVEL:
if (!(flags & REFNAME_ALLOW_ONELEVEL))
return 1;
else
break;
case CHECK_REF_FORMAT_WILDCARD:
if (!(flags & REFNAME_REFSPEC_PATTERN))
return 1;
else
break;
default:
die("internal error: unexpected value from check_ref_format()");
}


if (print) if (print)
refname_format_print(argv[i]); refname_format_print(argv[i]);
Expand Down
2 changes: 1 addition & 1 deletion builtin/checkout.c
Expand Up @@ -882,7 +882,7 @@ static int parse_branchname_arg(int argc, const char **argv,
new->name = arg; new->name = arg;
setup_branch_path(new); setup_branch_path(new);


if (check_ref_format(new->path) == CHECK_REF_FORMAT_OK && if (!check_refname_format(new->path, 0) &&
resolve_ref(new->path, branch_rev, 1, NULL)) resolve_ref(new->path, branch_rev, 1, NULL))
hashcpy(rev, branch_rev); hashcpy(rev, branch_rev);
else else
Expand Down
2 changes: 1 addition & 1 deletion builtin/fetch-pack.c
Expand Up @@ -544,7 +544,7 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
for (ref = *refs; ref; ref = next) { for (ref = *refs; ref; ref = next) {
next = ref->next; next = ref->next;
if (!memcmp(ref->name, "refs/", 5) && if (!memcmp(ref->name, "refs/", 5) &&
check_ref_format(ref->name + 5)) check_refname_format(ref->name + 5, 0))
; /* trash */ ; /* trash */
else if (args.fetch_all && else if (args.fetch_all &&
(!args.depth || prefixcmp(ref->name, "refs/tags/") )) { (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/receive-pack.c
Expand Up @@ -356,7 +356,7 @@ static const char *update(struct command *cmd)
struct ref_lock *lock; struct ref_lock *lock;


/* only refs/... are allowed */ /* only refs/... are allowed */
if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) { if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) {
rp_error("refusing to create funny ref '%s' remotely", name); rp_error("refusing to create funny ref '%s' remotely", name);
return "funny refname"; return "funny refname";
} }
Expand Down
2 changes: 1 addition & 1 deletion builtin/replace.c
Expand Up @@ -94,7 +94,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
"refs/replace/%s", "refs/replace/%s",
sha1_to_hex(object)) > sizeof(ref) - 1) sha1_to_hex(object)) > sizeof(ref) - 1)
die("replace ref name too long: %.*s...", 50, ref); die("replace ref name too long: %.*s...", 50, ref);
if (check_ref_format(ref)) if (check_refname_format(ref, 0))
die("'%s' is not a valid ref name.", ref); die("'%s' is not a valid ref name.", ref);


if (!resolve_ref(ref, prev, 1, NULL)) if (!resolve_ref(ref, prev, 1, NULL))
Expand Down
2 changes: 1 addition & 1 deletion builtin/show-ref.c
Expand Up @@ -145,7 +145,7 @@ static int exclude_existing(const char *match)
if (strncmp(ref, match, matchlen)) if (strncmp(ref, match, matchlen))
continue; continue;
} }
if (check_ref_format(ref)) { if (check_refname_format(ref, 0)) {
warning("ref '%s' ignored", ref); warning("ref '%s' ignored", ref);
continue; continue;
} }
Expand Down
4 changes: 2 additions & 2 deletions builtin/tag.c
Expand Up @@ -407,12 +407,12 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
{ {
if (name[0] == '-') if (name[0] == '-')
return CHECK_REF_FORMAT_ERROR; return -1;


strbuf_reset(sb); strbuf_reset(sb);
strbuf_addf(sb, "refs/tags/%s", name); strbuf_addf(sb, "refs/tags/%s", name);


return check_ref_format(sb->buf); return check_refname_format(sb->buf, 0);
} }


int cmd_tag(int argc, const char **argv, const char *prefix) int cmd_tag(int argc, const char **argv, const char *prefix)
Expand Down
2 changes: 1 addition & 1 deletion connect.c
Expand Up @@ -22,7 +22,7 @@ static int check_ref(const char *name, int len, unsigned int flags)
len -= 5; len -= 5;


/* REF_NORMAL means that we don't want the magic fake tag refs */ /* REF_NORMAL means that we don't want the magic fake tag refs */
if ((flags & REF_NORMAL) && check_ref_format(name) < 0) if ((flags & REF_NORMAL) && check_refname_format(name, 0))
return 0; return 0;


/* REF_HEADS means that we want regular branch heads */ /* REF_HEADS means that we want regular branch heads */
Expand Down
2 changes: 1 addition & 1 deletion environment.c
Expand Up @@ -106,7 +106,7 @@ static char *expand_namespace(const char *raw_namespace)
if (strcmp((*c)->buf, "/") != 0) if (strcmp((*c)->buf, "/") != 0)
strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf); strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
strbuf_list_free(components); strbuf_list_free(components);
if (check_ref_format(buf.buf) != CHECK_REF_FORMAT_OK) if (check_refname_format(buf.buf, 0))
die("bad git namespace path \"%s\"", raw_namespace); die("bad git namespace path \"%s\"", raw_namespace);
strbuf_addch(&buf, '/'); strbuf_addch(&buf, '/');
return strbuf_detach(&buf, NULL); return strbuf_detach(&buf, NULL);
Expand Down
7 changes: 1 addition & 6 deletions fast-import.c
Expand Up @@ -722,13 +722,8 @@ static struct branch *new_branch(const char *name)


if (b) if (b)
die("Invalid attempt to create duplicate branch: %s", name); die("Invalid attempt to create duplicate branch: %s", name);
switch (check_ref_format(name)) { if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
case 0: break; /* its valid */
case CHECK_REF_FORMAT_ONELEVEL:
break; /* valid, but too few '/', allow anyway */
default:
die("Branch name doesn't conform to GIT standards: %s", name); die("Branch name doesn't conform to GIT standards: %s", name);
}


b = pool_calloc(1, sizeof(struct branch)); b = pool_calloc(1, sizeof(struct branch));
b->name = pool_strdup(name); b->name = pool_strdup(name);
Expand Down
2 changes: 1 addition & 1 deletion git_remote_helpers/git/git.py
Expand Up @@ -54,7 +54,7 @@ def valid_git_ref (ref_name):
# The following is a reimplementation of the git check-ref-format # The following is a reimplementation of the git check-ref-format
# command. The rules were derived from the git check-ref-format(1) # command. The rules were derived from the git check-ref-format(1)
# manual page. This code should be replaced by a call to # manual page. This code should be replaced by a call to
# check_ref_format() in the git library, when such is available. # check_refname_format() in the git library, when such is available.
if ref_name.endswith('/') or \ if ref_name.endswith('/') or \
ref_name.startswith('.') or \ ref_name.startswith('.') or \
ref_name.count('/.') or \ ref_name.count('/.') or \
Expand Down
5 changes: 3 additions & 2 deletions notes-merge.c
Expand Up @@ -570,7 +570,8 @@ int notes_merge(struct notes_merge_options *o,
/* Dereference o->local_ref into local_sha1 */ /* Dereference o->local_ref into local_sha1 */
if (!resolve_ref(o->local_ref, local_sha1, 0, NULL)) if (!resolve_ref(o->local_ref, local_sha1, 0, NULL))
die("Failed to resolve local notes ref '%s'", o->local_ref); die("Failed to resolve local notes ref '%s'", o->local_ref);
else if (!check_ref_format(o->local_ref) && is_null_sha1(local_sha1)) else if (!check_refname_format(o->local_ref, 0) &&
is_null_sha1(local_sha1))
local = NULL; /* local_sha1 == null_sha1 indicates unborn ref */ local = NULL; /* local_sha1 == null_sha1 indicates unborn ref */
else if (!(local = lookup_commit_reference(local_sha1))) else if (!(local = lookup_commit_reference(local_sha1)))
die("Could not parse local commit %s (%s)", die("Could not parse local commit %s (%s)",
Expand All @@ -583,7 +584,7 @@ int notes_merge(struct notes_merge_options *o,
* Failed to get remote_sha1. If o->remote_ref looks like an * Failed to get remote_sha1. If o->remote_ref looks like an
* unborn ref, perform the merge using an empty notes tree. * unborn ref, perform the merge using an empty notes tree.
*/ */
if (!check_ref_format(o->remote_ref)) { if (!check_refname_format(o->remote_ref, 0)) {
hashclr(remote_sha1); hashclr(remote_sha1);
remote = NULL; remote = NULL;
} else { } else {
Expand Down
2 changes: 1 addition & 1 deletion pack-refs.c
Expand Up @@ -72,7 +72,7 @@ static void try_remove_empty_parents(char *name)
for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
while (*p && *p != '/') while (*p && *p != '/')
p++; p++;
/* tolerate duplicate slashes; see check_ref_format() */ /* tolerate duplicate slashes; see check_refname_format() */
while (*p == '/') while (*p == '/')
p++; p++;
} }
Expand Down
42 changes: 19 additions & 23 deletions refs.c
Expand Up @@ -872,10 +872,9 @@ static inline int bad_ref_char(int ch)
return 0; return 0;
} }


int check_ref_format(const char *ref) int check_refname_format(const char *ref, int flags)
{ {
int ch, level, last; int ch, level, last;
int ret = CHECK_REF_FORMAT_OK;
const char *cp = ref; const char *cp = ref;


level = 0; level = 0;
Expand All @@ -884,41 +883,42 @@ int check_ref_format(const char *ref)
; /* tolerate duplicated slashes */ ; /* tolerate duplicated slashes */
if (!ch) if (!ch)
/* should not end with slashes */ /* should not end with slashes */
return CHECK_REF_FORMAT_ERROR; return -1;


/* we are at the beginning of the path component */ /* we are at the beginning of the path component */
if (ch == '.') if (ch == '.')
return CHECK_REF_FORMAT_ERROR; return -1;
if (bad_ref_char(ch)) { if (bad_ref_char(ch)) {
if (ch == '*' && (!*cp || *cp == '/') && if ((flags & REFNAME_REFSPEC_PATTERN) && ch == '*' &&
ret == CHECK_REF_FORMAT_OK) (!*cp || *cp == '/'))
ret = CHECK_REF_FORMAT_WILDCARD; /* Accept one wildcard as a full refname component. */
flags &= ~REFNAME_REFSPEC_PATTERN;
else else
return CHECK_REF_FORMAT_ERROR; return -1;
} }


last = ch; last = ch;
/* scan the rest of the path component */ /* scan the rest of the path component */
while ((ch = *cp++) != 0) { while ((ch = *cp++) != 0) {
if (bad_ref_char(ch)) if (bad_ref_char(ch))
return CHECK_REF_FORMAT_ERROR; return -1;
if (ch == '/') if (ch == '/')
break; break;
if (last == '.' && ch == '.') if (last == '.' && ch == '.')
return CHECK_REF_FORMAT_ERROR; return -1;
if (last == '@' && ch == '{') if (last == '@' && ch == '{')
return CHECK_REF_FORMAT_ERROR; return -1;
last = ch; last = ch;
} }
level++; level++;
if (!ch) { if (!ch) {
if (ref <= cp - 2 && cp[-2] == '.') if (ref <= cp - 2 && cp[-2] == '.')
return CHECK_REF_FORMAT_ERROR; return -1;
if (level < 2) if (level < 2 && !(flags & REFNAME_ALLOW_ONELEVEL))
return CHECK_REF_FORMAT_ONELEVEL; return -1;
if (has_extension(ref, ".lock")) if (has_extension(ref, ".lock"))
return CHECK_REF_FORMAT_ERROR; return -1;
return ret; return 0;
} }
} }
} }
Expand Down Expand Up @@ -1103,21 +1103,17 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1) struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
{ {
char refpath[PATH_MAX]; char refpath[PATH_MAX];
if (check_ref_format(ref)) if (check_refname_format(ref, 0))
return NULL; return NULL;
strcpy(refpath, mkpath("refs/%s", ref)); strcpy(refpath, mkpath("refs/%s", ref));
return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL); return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
} }


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)
{ {
switch (check_ref_format(ref)) { if (check_refname_format(ref, REFNAME_ALLOW_ONELEVEL))
default:
return NULL; return NULL;
case 0: return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
case CHECK_REF_FORMAT_ONELEVEL:
return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
}
} }


static struct lock_file packlock; static struct lock_file packlock;
Expand Down
17 changes: 12 additions & 5 deletions refs.h
Expand Up @@ -97,11 +97,18 @@ int for_each_recent_reflog_ent(const char *ref, each_reflog_ent_fn fn, long, voi
*/ */
extern int for_each_reflog(each_ref_fn, void *); extern int for_each_reflog(each_ref_fn, void *);


#define CHECK_REF_FORMAT_OK 0 #define REFNAME_ALLOW_ONELEVEL 1
#define CHECK_REF_FORMAT_ERROR (-1) #define REFNAME_REFSPEC_PATTERN 2
#define CHECK_REF_FORMAT_ONELEVEL (-2)
#define CHECK_REF_FORMAT_WILDCARD (-3) /*
extern int check_ref_format(const char *target); * Return 0 iff ref has the correct format for a refname according to
* the rules described in Documentation/git-check-ref-format.txt. If
* REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
* reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
* allow a "*" wildcard character in place of one of the name
* components.
*/
extern int check_refname_format(const char *ref, int flags);


extern const char *prettify_refname(const char *refname); extern const char *prettify_refname(const char *refname);
extern char *shorten_unambiguous_ref(const char *ref, int strict); extern char *shorten_unambiguous_ref(const char *ref, int strict);
Expand Down

0 comments on commit 8d9c501

Please sign in to comment.