5 static struct refspec_item s_tag_refspec = {
14 /* See TAG_REFSPEC for the string version */
15 const struct refspec_item *tag_refspec = &s_tag_refspec;
18 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
19 * Returns 1 if successful and 0 if the refspec is invalid.
21 static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
25 const char *lhs, *rhs;
36 rhs = strrchr(lhs, ':');
39 * Before going on, special case ":" (or "+:") as a refspec
40 * for pushing matching refs.
42 if (!fetch && rhs == lhs && rhs[1] == '\0') {
48 size_t rlen = strlen(++rhs);
49 is_glob = (1 <= rlen && strchr(rhs, '*'));
50 item->dst = xstrndup(rhs, rlen);
53 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
54 if (1 <= llen && memchr(lhs, '*', llen)) {
55 if ((rhs && !is_glob) || (!rhs && fetch))
58 } else if (rhs && is_glob) {
62 item->pattern = is_glob;
63 item->src = xstrndup(lhs, llen);
64 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
67 struct object_id unused;
71 ; /* empty is ok; it means "HEAD" */
72 else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(item->src, &unused))
73 item->exact_sha1 = 1; /* ok */
74 else if (!check_refname_format(item->src, flags))
75 ; /* valid looking ref is ok */
80 ; /* missing is ok; it is the same as empty */
82 ; /* empty is ok; it means "do not store" */
83 else if (!check_refname_format(item->dst, flags))
84 ; /* valid looking ref is ok */
90 * - empty is allowed; it means delete.
91 * - when wildcarded, it must be a valid looking ref.
92 * - otherwise, it must be an extended SHA-1, but
93 * there is no existing way to validate this.
98 if (check_refname_format(item->src, flags))
102 ; /* anything goes, for now */
105 * - missing is allowed, but LHS then must be a
107 * - empty is not allowed.
108 * - otherwise it must be a valid looking ref.
111 if (check_refname_format(item->src, flags))
113 } else if (!*item->dst) {
116 if (check_refname_format(item->dst, flags))
124 void refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
126 memset(item, 0, sizeof(*item));
128 if (!parse_refspec(item, refspec, fetch))
129 die("Invalid refspec '%s'", refspec);
132 void refspec_item_clear(struct refspec_item *item)
134 FREE_AND_NULL(item->src);
135 FREE_AND_NULL(item->dst);
139 item->exact_sha1 = 0;
142 void refspec_init(struct refspec *rs, int fetch)
144 memset(rs, 0, sizeof(*rs));
148 void refspec_append(struct refspec *rs, const char *refspec)
150 struct refspec_item item;
152 refspec_item_init(&item, refspec, rs->fetch);
154 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
155 rs->items[rs->nr++] = item;
157 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
158 rs->raw[rs->raw_nr++] = xstrdup(refspec);
161 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
164 for (i = 0; i < nr; i++)
165 refspec_append(rs, refspecs[i]);
168 void refspec_clear(struct refspec *rs)
172 for (i = 0; i < rs->nr; i++)
173 refspec_item_clear(&rs->items[i]);
175 FREE_AND_NULL(rs->items);
179 for (i = 0; i < rs->raw_nr; i++)
180 free((char *)rs->raw[i]);
181 FREE_AND_NULL(rs->raw);
188 int valid_fetch_refspec(const char *fetch_refspec_str)
190 struct refspec_item refspec;
191 int ret = parse_refspec(&refspec, fetch_refspec_str, REFSPEC_FETCH);
192 refspec_item_clear(&refspec);