update-ref.c: extract a new function, parse_refname()

There is no reason to obscure the fact that parse_first_arg() always parses refnames. Form the new function by combining parse_first_arg() and update_store_ref_name(). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 7, 2014 at 15:48 UTC ed410e611dc39dc7e845f27a660b76b7d0ecbab6
1 file changed +41 -49
builtin/update-ref.c
+41 -49
@@ -35,14 +35,6 @@ static struct ref_update *update_alloc(void)
35 return update;
36 }
37
38 -static void update_store_ref_name(struct ref_update *update,
39 - const char *ref_name)
40 -{
41 - if (check_refname_format(ref_name, REFNAME_ALLOW_ONELEVEL))
42 - die("invalid ref format: %s", ref_name);
43 - update->ref_name = xstrdup(ref_name);
44 -}
45 -
38 static void update_store_new_sha1(struct ref_update *update,
39 const char *newvalue)
40 {
@@ -86,23 +78,35 @@ static const char *parse_arg(const char *next, struct strbuf *arg)
78 }
79
80 /*
89 - * Parse the argument immediately after "command SP". If not -z, then
90 - * handle C-quoting. Write the argument to arg. Set *next to point
91 - * at the character that terminates the argument. Die if C-quoting is
92 - * malformed.
81 + * Parse the reference name immediately after "command SP". If not
82 + * -z, then handle C-quoting. Return a pointer to a newly allocated
83 + * string containing the name of the reference, or NULL if there was
84 + * an error. Update *next to point at the character that terminates
85 + * the argument. Die if C-quoting is malformed or the reference name
86 + * is invalid.
87 */
94 -static void parse_first_arg(struct strbuf *input, const char **next,
95 - struct strbuf *arg)
88 +static char *parse_refname(struct strbuf *input, const char **next)
89 {
97 - strbuf_reset(arg);
90 + struct strbuf ref = STRBUF_INIT;
91 +
92 if (line_termination) {
93 /* Without -z, use the next argument */
100 - *next = parse_arg(*next, arg);
94 + *next = parse_arg(*next, &ref);
95 } else {
96 /* With -z, use everything up to the next NUL */
103 - strbuf_addstr(arg, *next);
104 - *next += arg->len;
97 + strbuf_addstr(&ref, *next);
98 + *next += ref.len;
99 + }
100 +
101 + if (!ref.len) {
102 + strbuf_release(&ref);
103 + return NULL;
104 }
105 +
106 + if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
107 + die("invalid ref format: %s", ref.buf);
108 +
109 + return strbuf_detach(&ref, NULL);
110 }
111
112 /*
@@ -150,111 +154,99 @@ static int parse_next_arg(struct strbuf *input, const char **next,
154
155 static const char *parse_cmd_update(struct strbuf *input, const char *next)
156 {
153 - struct strbuf ref = STRBUF_INIT;
157 struct strbuf newvalue = STRBUF_INIT;
158 struct strbuf oldvalue = STRBUF_INIT;
159 struct ref_update *update;
160
161 update = update_alloc();
162
160 - parse_first_arg(input, &next, &ref);
161 - if (ref.buf[0])
162 - update_store_ref_name(update, ref.buf);
163 - else
163 + update->ref_name = parse_refname(input, &next);
164 + if (!update->ref_name)
165 die("update line missing <ref>");
166
167 if (!parse_next_arg(input, &next, &newvalue))
168 update_store_new_sha1(update, newvalue.buf);
169 else
169 - die("update %s missing <newvalue>", ref.buf);
170 + die("update %s missing <newvalue>", update->ref_name);
171
172 if (!parse_next_arg(input, &next, &oldvalue)) {
173 update_store_old_sha1(update, oldvalue.buf);
174 if (*next != line_termination)
174 - die("update %s has extra input: %s", ref.buf, next);
175 + die("update %s has extra input: %s", update->ref_name, next);
176 } else if (!line_termination)
176 - die("update %s missing [<oldvalue>] NUL", ref.buf);
177 + die("update %s missing [<oldvalue>] NUL", update->ref_name);
178
179 return next;
180 }
181
182 static const char *parse_cmd_create(struct strbuf *input, const char *next)
183 {
183 - struct strbuf ref = STRBUF_INIT;
184 struct strbuf newvalue = STRBUF_INIT;
185 struct ref_update *update;
186
187 update = update_alloc();
188
189 - parse_first_arg(input, &next, &ref);
190 - if (ref.buf[0])
191 - update_store_ref_name(update, ref.buf);
192 - else
189 + update->ref_name = parse_refname(input, &next);
190 + if (!update->ref_name)
191 die("create line missing <ref>");
192
193 if (!parse_next_arg(input, &next, &newvalue))
194 update_store_new_sha1(update, newvalue.buf);
195 else
198 - die("create %s missing <newvalue>", ref.buf);
196 + die("create %s missing <newvalue>", update->ref_name);
197
198 if (is_null_sha1(update->new_sha1))
201 - die("create %s given zero new value", ref.buf);
199 + die("create %s given zero new value", update->ref_name);
200
201 if (*next != line_termination)
204 - die("create %s has extra input: %s", ref.buf, next);
202 + die("create %s has extra input: %s", update->ref_name, next);
203
204 return next;
205 }
206
207 static const char *parse_cmd_delete(struct strbuf *input, const char *next)
208 {
211 - struct strbuf ref = STRBUF_INIT;
209 struct strbuf oldvalue = STRBUF_INIT;
210 struct ref_update *update;
211
212 update = update_alloc();
213
217 - parse_first_arg(input, &next, &ref);
218 - if (ref.buf[0])
219 - update_store_ref_name(update, ref.buf);
220 - else
214 + update->ref_name = parse_refname(input, &next);
215 + if (!update->ref_name)
216 die("delete line missing <ref>");
217
218 if (!parse_next_arg(input, &next, &oldvalue)) {
219 update_store_old_sha1(update, oldvalue.buf);
220 if (update->have_old && is_null_sha1(update->old_sha1))
226 - die("delete %s given zero old value", ref.buf);
221 + die("delete %s given zero old value", update->ref_name);
222 } else if (!line_termination)
228 - die("delete %s missing [<oldvalue>] NUL", ref.buf);
223 + die("delete %s missing [<oldvalue>] NUL", update->ref_name);
224
225 if (*next != line_termination)
231 - die("delete %s has extra input: %s", ref.buf, next);
226 + die("delete %s has extra input: %s", update->ref_name, next);
227
228 return next;
229 }
230
231 static const char *parse_cmd_verify(struct strbuf *input, const char *next)
232 {
238 - struct strbuf ref = STRBUF_INIT;
233 struct strbuf value = STRBUF_INIT;
234 struct ref_update *update;
235
236 update = update_alloc();
237
244 - parse_first_arg(input, &next, &ref);
245 - if (ref.buf[0])
246 - update_store_ref_name(update, ref.buf);
247 - else
238 + update->ref_name = parse_refname(input, &next);
239 + if (!update->ref_name)
240 die("verify line missing <ref>");
241
242 if (!parse_next_arg(input, &next, &value)) {
243 update_store_old_sha1(update, value.buf);
244 hashcpy(update->new_sha1, update->old_sha1);
245 } else if (!line_termination)
254 - die("verify %s missing [<oldvalue>] NUL", ref.buf);
246 + die("verify %s missing [<oldvalue>] NUL", update->ref_name);
247
248 if (*next != line_termination)
257 - die("verify %s has extra input: %s", ref.buf, next);
249 + die("verify %s has extra input: %s", update->ref_name, next);
250
251 return next;
252 }