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
);
56 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
57 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
58 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
61 } else if (rhs
&& is_glob
) {
65 item
->pattern
= is_glob
;
66 item
->src
= xstrndup(lhs
, llen
);
67 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
70 struct object_id unused
;
74 ; /* empty is ok; it means "HEAD" */
75 else if (llen
== GIT_SHA1_HEXSZ
&& !get_oid_hex(item
->src
, &unused
))
76 item
->exact_sha1
= 1; /* ok */
77 else if (!check_refname_format(item
->src
, flags
))
78 ; /* valid looking ref is ok */
83 ; /* missing is ok; it is the same as empty */
85 ; /* empty is ok; it means "do not store" */
86 else if (!check_refname_format(item
->dst
, flags
))
87 ; /* valid looking ref is ok */
93 * - empty is allowed; it means delete.
94 * - when wildcarded, it must be a valid looking ref.
95 * - otherwise, it must be an extended SHA-1, but
96 * there is no existing way to validate this.
101 if (check_refname_format(item
->src
, flags
))
105 ; /* anything goes, for now */
108 * - missing is allowed, but LHS then must be a
110 * - empty is not allowed.
111 * - otherwise it must be a valid looking ref.
114 if (check_refname_format(item
->src
, flags
))
116 } else if (!*item
->dst
) {
119 if (check_refname_format(item
->dst
, flags
))
127 int refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
129 memset(item
, 0, sizeof(*item
));
130 return parse_refspec(item
, refspec
, fetch
);
133 void refspec_item_init_or_die(struct refspec_item
*item
, const char *refspec
,
136 if (!refspec_item_init(item
, refspec
, fetch
))
137 die("Invalid refspec '%s'", refspec
);
140 void refspec_item_clear(struct refspec_item
*item
)
142 FREE_AND_NULL(item
->src
);
143 FREE_AND_NULL(item
->dst
);
147 item
->exact_sha1
= 0;
150 void refspec_init(struct refspec
*rs
, int fetch
)
152 memset(rs
, 0, sizeof(*rs
));
156 void refspec_append(struct refspec
*rs
, const char *refspec
)
158 struct refspec_item item
;
160 refspec_item_init_or_die(&item
, refspec
, rs
->fetch
);
162 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
163 rs
->items
[rs
->nr
++] = item
;
165 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
166 rs
->raw
[rs
->raw_nr
++] = xstrdup(refspec
);
169 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
172 for (i
= 0; i
< nr
; i
++)
173 refspec_append(rs
, refspecs
[i
]);
176 void refspec_clear(struct refspec
*rs
)
180 for (i
= 0; i
< rs
->nr
; i
++)
181 refspec_item_clear(&rs
->items
[i
]);
183 FREE_AND_NULL(rs
->items
);
187 for (i
= 0; i
< rs
->raw_nr
; i
++)
188 free((char *)rs
->raw
[i
]);
189 FREE_AND_NULL(rs
->raw
);
196 int valid_fetch_refspec(const char *fetch_refspec_str
)
198 struct refspec_item refspec
;
199 int ret
= refspec_item_init(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
200 refspec_item_clear(&refspec
);
204 void refspec_ref_prefixes(const struct refspec
*rs
,
205 struct argv_array
*ref_prefixes
)
208 for (i
= 0; i
< rs
->nr
; i
++) {
209 const struct refspec_item
*item
= &rs
->items
[i
];
210 const char *prefix
= NULL
;
212 if (item
->exact_sha1
)
214 if (rs
->fetch
== REFSPEC_FETCH
)
218 else if (item
->src
&& !item
->exact_sha1
)
223 const char *glob
= strchr(prefix
, '*');
224 argv_array_pushf(ref_prefixes
, "%.*s",
225 (int)(glob
- prefix
),
228 expand_ref_prefix(ref_prefixes
, prefix
);