Skip to content

Commit 44cba9c

Browse files
committed
Merge branch 'jc/skip-prefix'
Code simplification. * jc/skip-prefix: C: use skip_prefix() to avoid hardcoded string length
2 parents 556ccd4 + 145136a commit 44cba9c

File tree

8 files changed

+18
-25
lines changed

8 files changed

+18
-25
lines changed

builtin/fast-export.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,7 @@ static void handle_tag(const char *name, struct tag *tag)
870870
printf("reset %s\nfrom %s\n\n",
871871
name, oid_to_hex(&null_oid));
872872
}
873-
if (starts_with(name, "refs/tags/"))
874-
name += 10;
873+
skip_prefix(name, "refs/tags/", &name);
875874
printf("tag %s\n", name);
876875
if (mark_tags) {
877876
mark_next_object(&tag->object);

builtin/reflog.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,16 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
560560

561561
for (i = 1; i < argc; i++) {
562562
const char *arg = argv[i];
563+
563564
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
564565
flags |= EXPIRE_REFLOGS_DRY_RUN;
565-
else if (starts_with(arg, "--expire=")) {
566-
if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
566+
else if (skip_prefix(arg, "--expire=", &arg)) {
567+
if (parse_expiry_date(arg, &cb.cmd.expire_total))
567568
die(_("'%s' is not a valid timestamp"), arg);
568569
explicit_expiry |= EXPIRE_TOTAL;
569570
}
570-
else if (starts_with(arg, "--expire-unreachable=")) {
571-
if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
571+
else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
572+
if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
572573
die(_("'%s' is not a valid timestamp"), arg);
573574
explicit_expiry |= EXPIRE_UNREACH;
574575
}

notes.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,10 +1279,8 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
12791279
if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
12801280
strbuf_addstr(sb, "\nNotes:\n");
12811281
} else {
1282-
if (starts_with(ref, "refs/"))
1283-
ref += 5;
1284-
if (starts_with(ref, "notes/"))
1285-
ref += 6;
1282+
skip_prefix(ref, "refs/", &ref);
1283+
skip_prefix(ref, "notes/", &ref);
12861284
strbuf_addf(sb, "\nNotes (%s):\n", ref);
12871285
}
12881286
}

parse-options.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ static enum parse_opt_result parse_long_opt(
357357
}
358358
/* negated? */
359359
if (!starts_with(arg, "no-")) {
360-
if (starts_with(long_name, "no-")) {
361-
long_name += 3;
360+
if (skip_prefix(long_name, "no-", &long_name)) {
362361
opt_flags |= OPT_UNSET;
363362
goto again;
364363
}

refs/files-backend.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
465465
close(fd);
466466
strbuf_rtrim(&sb_contents);
467467
buf = sb_contents.buf;
468-
if (starts_with(buf, "ref:")) {
469-
buf += 4;
468+
if (skip_prefix(buf, "ref:", &buf)) {
470469
while (isspace(*buf))
471470
buf++;
472471

remote-curl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,9 @@ static void parse_push(struct strbuf *buf)
12551255
int ret;
12561256

12571257
do {
1258-
if (starts_with(buf->buf, "push "))
1259-
argv_array_push(&specs, buf->buf + 5);
1258+
const char *arg;
1259+
if (skip_prefix(buf->buf, "push ", &arg))
1260+
argv_array_push(&specs, arg);
12601261
else
12611262
die(_("http transport does not support %s"), buf->buf);
12621263

sha1-name.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,9 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
908908
real_ref, flags, at_time, nth, oid, NULL,
909909
&co_time, &co_tz, &co_cnt)) {
910910
if (!len) {
911-
if (starts_with(real_ref, "refs/heads/")) {
912-
str = real_ref + 11;
913-
len = strlen(real_ref + 11);
914-
} else {
915-
/* detached HEAD */
911+
if (!skip_prefix(real_ref, "refs/heads/", &str))
916912
str = "HEAD";
917-
len = 4;
918-
}
913+
len = strlen(str);
919914
}
920915
if (at_time) {
921916
if (!(flags & GET_OID_QUIETLY)) {

transport-helper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,12 @@ static int fetch_with_fetch(struct transport *transport,
404404
sendline(data, &buf);
405405

406406
while (1) {
407+
const char *name;
408+
407409
if (recvline(data, &buf))
408410
exit(128);
409411

410-
if (starts_with(buf.buf, "lock ")) {
411-
const char *name = buf.buf + 5;
412+
if (skip_prefix(buf.buf, "lock ", &name)) {
412413
if (transport->pack_lockfile)
413414
warning(_("%s also locked %s"), data->name, name);
414415
else

0 commit comments

Comments
 (0)