@matheusbsbs / matheus-bagliano / commits / 804dba54f5
Fork 0

update-ref: pass end pointer instead of strbuf

We currently pass both an `strbuf` containing the current command line as well as the `next` pointer pointing to the first argument to commands. This is both confusing and makes code more intertwined. Convert this to use a simple pointer as well as a pointer pointing to the end of the input as a preparatory step to line-wise reading of stdin. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 2, 2020 at 09:09 UTC 804dba54f5abdb35b5d15eed63625581c82697fb
1 file changed +15 -15
builtin/update-ref.c
+15 -15
@@ -95,7 +95,7 @@ static char *parse_refname(const char **next)
95 * provided but cannot be converted to a SHA-1, die. flags can
96 * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
97 */
98 -static int parse_next_oid(struct strbuf *input, const char **next,
98 +static int parse_next_oid(const char **next, const char *end,
99 struct object_id *oid,
100 const char *command, const char *refname,
101 int flags)
@@ -103,7 +103,7 @@ static int parse_next_oid(struct strbuf *input, const char **next,
103 struct strbuf arg = STRBUF_INIT;
104 int ret = 0;
105
106 - if (*next == input->buf + input->len)
106 + if (*next == end)
107 goto eof;
108
109 if (line_termination) {
@@ -128,7 +128,7 @@ static int parse_next_oid(struct strbuf *input, const char **next,
128 die("%s %s: expected NUL but got: %s",
129 command, refname, *next);
130 (*next)++;
131 - if (*next == input->buf + input->len)
131 + if (*next == end)
132 goto eof;
133 strbuf_addstr(&arg, *next);
134 *next += arg.len;
@@ -179,7 +179,7 @@ static int parse_next_oid(struct strbuf *input, const char **next,
179 */
180
181 static const char *parse_cmd_update(struct ref_transaction *transaction,
182 - struct strbuf *input, const char *next)
182 + const char *next, const char *end)
183 {
184 struct strbuf err = STRBUF_INIT;
185 char *refname;
@@ -190,11 +190,11 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
190 if (!refname)
191 die("update: missing <ref>");
192
193 - if (parse_next_oid(input, &next, &new_oid, "update", refname,
193 + if (parse_next_oid(&next, end, &new_oid, "update", refname,
194 PARSE_SHA1_ALLOW_EMPTY))
195 die("update %s: missing <newvalue>", refname);
196
197 - have_old = !parse_next_oid(input, &next, &old_oid, "update", refname,
197 + have_old = !parse_next_oid(&next, end, &old_oid, "update", refname,
198 PARSE_SHA1_OLD);
199
200 if (*next != line_termination)
@@ -214,7 +214,7 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
214 }
215
216 static const char *parse_cmd_create(struct ref_transaction *transaction,
217 - struct strbuf *input, const char *next)
217 + const char *next, const char *end)
218 {
219 struct strbuf err = STRBUF_INIT;
220 char *refname;
@@ -224,7 +224,7 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
224 if (!refname)
225 die("create: missing <ref>");
226
227 - if (parse_next_oid(input, &next, &new_oid, "create", refname, 0))
227 + if (parse_next_oid(&next, end, &new_oid, "create", refname, 0))
228 die("create %s: missing <newvalue>", refname);
229
230 if (is_null_oid(&new_oid))
@@ -246,7 +246,7 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
246 }
247
248 static const char *parse_cmd_delete(struct ref_transaction *transaction,
249 - struct strbuf *input, const char *next)
249 + const char *next, const char *end)
250 {
251 struct strbuf err = STRBUF_INIT;
252 char *refname;
@@ -257,7 +257,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
257 if (!refname)
258 die("delete: missing <ref>");
259
260 - if (parse_next_oid(input, &next, &old_oid, "delete", refname,
260 + if (parse_next_oid(&next, end, &old_oid, "delete", refname,
261 PARSE_SHA1_OLD)) {
262 have_old = 0;
263 } else {
@@ -282,7 +282,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
282 }
283
284 static const char *parse_cmd_verify(struct ref_transaction *transaction,
285 - struct strbuf *input, const char *next)
285 + const char *next, const char *end)
286 {
287 struct strbuf err = STRBUF_INIT;
288 char *refname;
@@ -292,7 +292,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
292 if (!refname)
293 die("verify: missing <ref>");
294
295 - if (parse_next_oid(input, &next, &old_oid, "verify", refname,
295 + if (parse_next_oid(&next, end, &old_oid, "verify", refname,
296 PARSE_SHA1_OLD))
297 oidclr(&old_oid);
298
@@ -311,7 +311,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
311 }
312
313 static const char *parse_cmd_option(struct ref_transaction *transaction,
314 - struct strbuf *input, const char *next)
314 + const char *next, const char *end)
315 {
316 const char *rest;
317 if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
@@ -323,7 +323,7 @@ static const char *parse_cmd_option(struct ref_transaction *transaction,
323
324 static const struct parse_cmd {
325 const char *prefix;
326 - const char *(*fn)(struct ref_transaction *, struct strbuf *, const char *);
326 + const char *(*fn)(struct ref_transaction *, const char *, const char *);
327 } command[] = {
328 { "update", parse_cmd_update },
329 { "create", parse_cmd_create },
@@ -363,7 +363,7 @@ static void update_refs_stdin(struct ref_transaction *transaction)
363 if (!cmd)
364 die("unknown command: %s", next);
365
366 - next = cmd->fn(transaction, &input, next);
366 + next = cmd->fn(transaction, next, input.buf + input.len);
367 next++;
368 }
369