2 #include "argv-array.h"
6 static struct refspec_item s_tag_refspec
= {
15 /* See TAG_REFSPEC for the string version */
16 const struct refspec_item
*tag_refspec
= &s_tag_refspec
;
19 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
20 * Returns 1 if successful and 0 if the refspec is invalid.
22 static int parse_refspec(struct refspec_item
*item
, const char *refspec
, int fetch
)
26 const char *lhs
, *rhs
;
37 rhs
= strrchr(lhs
, ':');
40 * Before going on, special case ":" (or "+:") as a refspec
41 * for pushing matching refs.
43 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
49 size_t rlen
= strlen(++rhs
);
50 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
51 item
->dst
= xstrndup(rhs
, rlen
);
54 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
55 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
56 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
59 } else if (rhs
&& is_glob
) {
63 item
->pattern
= is_glob
;
64 item
->src
= xstrndup(lhs
, llen
);
65 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
68 struct object_id unused
;
72 ; /* empty is ok; it means "HEAD" */
73 else if (llen
== GIT_SHA1_HEXSZ
&& !get_oid_hex(item
->src
, &unused
))
74 item
->exact_sha1
= 1; /* ok */
75 else if (!check_refname_format(item
->src
, flags
))
76 ; /* valid looking ref is ok */
81 ; /* missing is ok; it is the same as empty */
83 ; /* empty is ok; it means "do not store" */
84 else if (!check_refname_format(item
->dst
, flags
))
85 ; /* valid looking ref is ok */
91 * - empty is allowed; it means delete.
92 * - when wildcarded, it must be a valid looking ref.
93 * - otherwise, it must be an extended SHA-1, but
94 * there is no existing way to validate this.
99 if (check_refname_format(item
->src
, flags
))
103 ; /* anything goes, for now */
106 * - missing is allowed, but LHS then must be a
108 * - empty is not allowed.
109 * - otherwise it must be a valid looking ref.
112 if (check_refname_format(item
->src
, flags
))
114 } else if (!*item
->dst
) {
117 if (check_refname_format(item
->dst
, flags
))
125 void refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
127 memset(item
, 0, sizeof(*item
));
129 if (!parse_refspec(item
, refspec
, fetch
))
130 die("Invalid refspec '%s'", refspec
);
133 void refspec_item_clear(struct refspec_item
*item
)
135 FREE_AND_NULL(item
->src
);
136 FREE_AND_NULL(item
->dst
);
140 item
->exact_sha1
= 0;
143 void refspec_init(struct refspec
*rs
, int fetch
)
145 memset(rs
, 0, sizeof(*rs
));
149 void refspec_append(struct refspec
*rs
, const char *refspec
)
151 struct refspec_item item
;
153 refspec_item_init(&item
, refspec
, rs
->fetch
);
155 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
156 rs
->items
[rs
->nr
++] = item
;
158 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
159 rs
->raw
[rs
->raw_nr
++] = xstrdup(refspec
);
162 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
165 for (i
= 0; i
< nr
; i
++)
166 refspec_append(rs
, refspecs
[i
]);
169 void refspec_clear(struct refspec
*rs
)
173 for (i
= 0; i
< rs
->nr
; i
++)
174 refspec_item_clear(&rs
->items
[i
]);
176 FREE_AND_NULL(rs
->items
);
180 for (i
= 0; i
< rs
->raw_nr
; i
++)
181 free((char *)rs
->raw
[i
]);
182 FREE_AND_NULL(rs
->raw
);
189 int valid_fetch_refspec(const char *fetch_refspec_str
)
191 struct refspec_item refspec
;
192 int ret
= parse_refspec(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
193 refspec_item_clear(&refspec
);
197 void refspec_ref_prefixes(const struct refspec
*rs
,
198 struct argv_array
*ref_prefixes
)
201 for (i
= 0; i
< rs
->nr
; i
++) {
202 const struct refspec_item
*item
= &rs
->items
[i
];
203 const char *prefix
= NULL
;
205 if (item
->exact_sha1
)
207 if (rs
->fetch
== REFSPEC_FETCH
)
211 else if (item
->src
&& !item
->exact_sha1
)
216 const char *glob
= strchr(prefix
, '*');
217 argv_array_pushf(ref_prefixes
, "%.*s",
218 (int)(glob
- prefix
),
221 expand_ref_prefix(ref_prefixes
, prefix
);