]> git.ipfire.org Git - thirdparty/git.git/commitdiff
C: use skip_prefix() to avoid hardcoded string length
authorJunio C Hamano <gitster@pobox.com>
Thu, 30 Jan 2020 19:35:46 +0000 (11:35 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jan 2020 21:03:45 +0000 (13:03 -0800)
We often skip an optional prefix in a string with a hardcoded
constant, e.g.

if (starts_with(string, "prefix"))
string += 6;

which is less error prone when written

skip_prefix(string, "prefix", &string);

Note that this changes a few error messages from "git reflog expire
--expire=nonsense.timestamp", which used to complain by saying

    '--expire=nonsense.timestamp' is not a valid timestamp

but with this change, we say

    'nonsense.timestamp' is not a valid timestamp

which is more technically correct (the string with --expire= as
a prefix obviously cannot be a valid timestamp, but the error is
about the part of the input without that prefix).

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fast-export.c
builtin/reflog.c
notes.c
parse-options.c
refs/files-backend.c
remote-curl.c
sha1-name.c
transport-helper.c

index dbec4df92bd7229b1a46f56f27b4e29c706fc214..164406fdd4b64e59629a3a7eae0f43a0901bb321 100644 (file)
@@ -870,8 +870,7 @@ static void handle_tag(const char *name, struct tag *tag)
                printf("reset %s\nfrom %s\n\n",
                       name, oid_to_hex(&null_oid));
        }
-       if (starts_with(name, "refs/tags/"))
-               name += 10;
+       skip_prefix(name, "refs/tags/", &name);
        printf("tag %s\n", name);
        if (mark_tags) {
                mark_next_object(&tag->object);
index 4d3430900d06d82e8e06441f8fdfea00f7aecf78..81dfd563c0d937eb8943f84493d2d48e8af15f1b 100644 (file)
@@ -560,15 +560,16 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
+
                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
                        flags |= EXPIRE_REFLOGS_DRY_RUN;
-               else if (starts_with(arg, "--expire=")) {
-                       if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
+               else if (skip_prefix(arg, "--expire=", &arg)) {
+                       if (parse_expiry_date(arg, &cb.cmd.expire_total))
                                die(_("'%s' is not a valid timestamp"), arg);
                        explicit_expiry |= EXPIRE_TOTAL;
                }
-               else if (starts_with(arg, "--expire-unreachable=")) {
-                       if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
+               else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
+                       if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
                                die(_("'%s' is not a valid timestamp"), arg);
                        explicit_expiry |= EXPIRE_UNREACH;
                }
diff --git a/notes.c b/notes.c
index 0c79964c26a71d6843c43c94fe260131334749f2..a0349fa77831bcb79525e3f3a06132297857df87 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1279,10 +1279,8 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
                if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
                        strbuf_addstr(sb, "\nNotes:\n");
                } else {
-                       if (starts_with(ref, "refs/"))
-                               ref += 5;
-                       if (starts_with(ref, "notes/"))
-                               ref += 6;
+                       skip_prefix(ref, "refs/", &ref);
+                       skip_prefix(ref, "notes/", &ref);
                        strbuf_addf(sb, "\nNotes (%s):\n", ref);
                }
        }
index 60fae3ad213782dffa82e3c7dd246b47c566bf56..e8c04109ba1d4a1cffa9551c29152e922ad6c18a 100644 (file)
@@ -357,8 +357,7 @@ is_abbreviated:
                        }
                        /* negated? */
                        if (!starts_with(arg, "no-")) {
-                               if (starts_with(long_name, "no-")) {
-                                       long_name += 3;
+                               if (skip_prefix(long_name, "no-", &long_name)) {
                                        opt_flags |= OPT_UNSET;
                                        goto again;
                                }
index 0ea66a28b6c9b951041c050c5489c8110905549c..561c33ac8a97a8e0a3de9ba27ca9bd51937c6c03 100644 (file)
@@ -465,8 +465,7 @@ stat_ref:
        close(fd);
        strbuf_rtrim(&sb_contents);
        buf = sb_contents.buf;
-       if (starts_with(buf, "ref:")) {
-               buf += 4;
+       if (skip_prefix(buf, "ref:", &buf)) {
                while (isspace(*buf))
                        buf++;
 
index 350d92a074ed82a99060cebb1a4a3fb0a98cfd8e..8eb96152f51b641477c3b01bd62801d2bd2075eb 100644 (file)
@@ -1255,8 +1255,9 @@ static void parse_push(struct strbuf *buf)
        int ret;
 
        do {
-               if (starts_with(buf->buf, "push "))
-                       argv_array_push(&specs, buf->buf + 5);
+               const char *arg;
+               if (skip_prefix(buf->buf, "push ", &arg))
+                       argv_array_push(&specs, arg);
                else
                        die(_("http transport does not support %s"), buf->buf);
 
index 200eb373ad80fbcc395c9adab0d6b7f72008b99e..75d1c326063d3571c12eb67679468f1bb1fb2c63 100644 (file)
@@ -908,14 +908,9 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
                                real_ref, flags, at_time, nth, oid, NULL,
                                &co_time, &co_tz, &co_cnt)) {
                        if (!len) {
-                               if (starts_with(real_ref, "refs/heads/")) {
-                                       str = real_ref + 11;
-                                       len = strlen(real_ref + 11);
-                               } else {
-                                       /* detached HEAD */
+                               if (!skip_prefix(real_ref, "refs/heads/", &str))
                                        str = "HEAD";
-                                       len = 4;
-                               }
+                               len = strlen(str);
                        }
                        if (at_time) {
                                if (!(flags & GET_OID_QUIETLY)) {
index 413d9d873e8623ac40ee3c5912d80d5fb2534722..20a7185ec40e1cf4612c259019f325208632373b 100644 (file)
@@ -404,11 +404,12 @@ static int fetch_with_fetch(struct transport *transport,
        sendline(data, &buf);
 
        while (1) {
+               const char *name;
+
                if (recvline(data, &buf))
                        exit(128);
 
-               if (starts_with(buf.buf, "lock ")) {
-                       const char *name = buf.buf + 5;
+               if (skip_prefix(buf.buf, "lock ", &name)) {
                        if (transport->pack_lockfile)
                                warning(_("%s also locked %s"), data->name, name);
                        else