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);
55 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
56 if (1 <= llen && memchr(lhs, '*', llen)) {
57 if ((rhs && !is_glob) || (!rhs && fetch))
60 } else if (rhs && is_glob) {
64 item->pattern = is_glob;
65 item->src = xstrndup(lhs, llen);
66 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
69 struct object_id unused;
73 ; /* empty is ok; it means "HEAD" */
74 else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(item->src, &unused))
75 item->exact_sha1 = 1; /* ok */
76 else if (!check_refname_format(item->src, flags))
77 ; /* valid looking ref is ok */
82 ; /* missing is ok; it is the same as empty */
84 ; /* empty is ok; it means "do not store" */
85 else if (!check_refname_format(item->dst, flags))
86 ; /* valid looking ref is ok */
92 * - empty is allowed; it means delete.
93 * - when wildcarded, it must be a valid looking ref.
94 * - otherwise, it must be an extended SHA-1, but
95 * there is no existing way to validate this.
100 if (check_refname_format(item->src, flags))
104 ; /* anything goes, for now */
107 * - missing is allowed, but LHS then must be a
109 * - empty is not allowed.
110 * - otherwise it must be a valid looking ref.
113 if (check_refname_format(item->src, flags))
115 } else if (!*item->dst) {
118 if (check_refname_format(item->dst, flags))
126 void refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
128 memset(item, 0, sizeof(*item));
130 if (!parse_refspec(item, refspec, fetch))
131 die("Invalid refspec '%s'", refspec);
134 void refspec_item_clear(struct refspec_item *item)
136 FREE_AND_NULL(item->src);
137 FREE_AND_NULL(item->dst);
141 item->exact_sha1 = 0;
144 void refspec_init(struct refspec *rs, int fetch)
146 memset(rs, 0, sizeof(*rs));
150 void refspec_append(struct refspec *rs, const char *refspec)
152 struct refspec_item item;
154 refspec_item_init(&item, refspec, rs->fetch);
156 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
157 rs->items[rs->nr++] = item;
159 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
160 rs->raw[rs->raw_nr++] = xstrdup(refspec);
163 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
166 for (i = 0; i < nr; i++)
167 refspec_append(rs, refspecs[i]);
170 void refspec_clear(struct refspec *rs)
174 for (i = 0; i < rs->nr; i++)
175 refspec_item_clear(&rs->items[i]);
177 FREE_AND_NULL(rs->items);
181 for (i = 0; i < rs->raw_nr; i++)
182 free((char *)rs->raw[i]);
183 FREE_AND_NULL(rs->raw);
190 int valid_fetch_refspec(const char *fetch_refspec_str)
192 struct refspec_item refspec;
193 int ret = parse_refspec(&refspec, fetch_refspec_str, REFSPEC_FETCH);
194 refspec_item_clear(&refspec);