refspec: s/refspec_item_init/&_or_die/g
[git/git.git] / refspec.c
CommitLineData
ec0cb496 1#include "cache.h"
6373cb59 2#include "argv-array.h"
ec0cb496
BW
3#include "refs.h"
4#include "refspec.h"
5
0ad4a5ff 6static struct refspec_item s_tag_refspec = {
ec0cb496
BW
7 0,
8 1,
9 0,
10 0,
11 "refs/tags/*",
12 "refs/tags/*"
13};
14
15/* See TAG_REFSPEC for the string version */
0ad4a5ff 16const struct refspec_item *tag_refspec = &s_tag_refspec;
ec0cb496 17
3eec3700
BW
18/*
19 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
20 * Returns 1 if successful and 0 if the refspec is invalid.
21 */
22static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
ec0cb496 23{
3eec3700
BW
24 size_t llen;
25 int is_glob;
26 const char *lhs, *rhs;
27 int flags;
ec0cb496 28
3eec3700 29 is_glob = 0;
ec0cb496 30
3eec3700
BW
31 lhs = refspec;
32 if (*lhs == '+') {
33 item->force = 1;
34 lhs++;
35 }
ec0cb496 36
3eec3700 37 rhs = strrchr(lhs, ':');
ec0cb496 38
3eec3700
BW
39 /*
40 * Before going on, special case ":" (or "+:") as a refspec
41 * for pushing matching refs.
42 */
43 if (!fetch && rhs == lhs && rhs[1] == '\0') {
44 item->matching = 1;
45 return 1;
46 }
ec0cb496 47
3eec3700
BW
48 if (rhs) {
49 size_t rlen = strlen(++rhs);
50 is_glob = (1 <= rlen && strchr(rhs, '*'));
51 item->dst = xstrndup(rhs, rlen);
52 }
53
54 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
55 if (1 <= llen && memchr(lhs, '*', llen)) {
56 if ((rhs && !is_glob) || (!rhs && fetch))
57 return 0;
58 is_glob = 1;
59 } else if (rhs && is_glob) {
60 return 0;
61 }
62
63 item->pattern = is_glob;
64 item->src = xstrndup(lhs, llen);
65 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
66
67 if (fetch) {
68 struct object_id unused;
69
70 /* LHS */
71 if (!*item->src)
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 */
77 else
78 return 0;
79 /* RHS */
80 if (!item->dst)
81 ; /* missing is ok; it is the same as empty */
82 else if (!*item->dst)
83 ; /* empty is ok; it means "do not store" */
84 else if (!check_refname_format(item->dst, flags))
85 ; /* valid looking ref is ok */
86 else
87 return 0;
88 } else {
ec0cb496 89 /*
3eec3700
BW
90 * LHS
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.
ec0cb496 95 */
3eec3700
BW
96 if (!*item->src)
97 ; /* empty is ok */
98 else if (is_glob) {
99 if (check_refname_format(item->src, flags))
100 return 0;
ec0cb496 101 }
3eec3700
BW
102 else
103 ; /* anything goes, for now */
104 /*
105 * RHS
106 * - missing is allowed, but LHS then must be a
107 * valid looking ref.
108 * - empty is not allowed.
109 * - otherwise it must be a valid looking ref.
110 */
111 if (!item->dst) {
112 if (check_refname_format(item->src, flags))
113 return 0;
114 } else if (!*item->dst) {
115 return 0;
116 } else {
117 if (check_refname_format(item->dst, flags))
118 return 0;
ec0cb496 119 }
3eec3700 120 }
ec0cb496 121
3eec3700
BW
122 return 1;
123}
ec0cb496 124
dc064221
ÆAB
125void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
126 int fetch)
6d4c0578
BW
127{
128 memset(item, 0, sizeof(*item));
129
130 if (!parse_refspec(item, refspec, fetch))
131 die("Invalid refspec '%s'", refspec);
132}
133
134void refspec_item_clear(struct refspec_item *item)
135{
136 FREE_AND_NULL(item->src);
137 FREE_AND_NULL(item->dst);
138 item->force = 0;
139 item->pattern = 0;
140 item->matching = 0;
141 item->exact_sha1 = 0;
142}
143
144void refspec_init(struct refspec *rs, int fetch)
145{
146 memset(rs, 0, sizeof(*rs));
147 rs->fetch = fetch;
148}
149
150void refspec_append(struct refspec *rs, const char *refspec)
151{
152 struct refspec_item item;
153
dc064221 154 refspec_item_init_or_die(&item, refspec, rs->fetch);
6d4c0578
BW
155
156 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
157 rs->items[rs->nr++] = item;
158
159 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
160 rs->raw[rs->raw_nr++] = xstrdup(refspec);
161}
162
163void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
164{
165 int i;
166 for (i = 0; i < nr; i++)
167 refspec_append(rs, refspecs[i]);
168}
169
170void refspec_clear(struct refspec *rs)
171{
172 int i;
173
174 for (i = 0; i < rs->nr; i++)
175 refspec_item_clear(&rs->items[i]);
176
177 FREE_AND_NULL(rs->items);
178 rs->alloc = 0;
179 rs->nr = 0;
180
181 for (i = 0; i < rs->raw_nr; i++)
182 free((char *)rs->raw[i]);
183 FREE_AND_NULL(rs->raw);
184 rs->raw_alloc = 0;
185 rs->raw_nr = 0;
186
187 rs->fetch = 0;
188}
c8fa9efe
BW
189
190int valid_fetch_refspec(const char *fetch_refspec_str)
191{
192 struct refspec_item refspec;
193 int ret = parse_refspec(&refspec, fetch_refspec_str, REFSPEC_FETCH);
194 refspec_item_clear(&refspec);
195 return ret;
196}
6373cb59
BW
197
198void refspec_ref_prefixes(const struct refspec *rs,
199 struct argv_array *ref_prefixes)
200{
201 int i;
202 for (i = 0; i < rs->nr; i++) {
203 const struct refspec_item *item = &rs->items[i];
204 const char *prefix = NULL;
205
6c301adb
JN
206 if (item->exact_sha1)
207 continue;
6373cb59
BW
208 if (rs->fetch == REFSPEC_FETCH)
209 prefix = item->src;
210 else if (item->dst)
211 prefix = item->dst;
212 else if (item->src && !item->exact_sha1)
213 prefix = item->src;
214
215 if (prefix) {
216 if (item->pattern) {
217 const char *glob = strchr(prefix, '*');
218 argv_array_pushf(ref_prefixes, "%.*s",
219 (int)(glob - prefix),
220 prefix);
221 } else {
222 expand_ref_prefix(ref_prefixes, prefix);
223 }
224 }
225 }
226}