Skip to content

Commit 145c590

Browse files
committed
Merge branch 'rs/more-uses-of-skip-prefix'
* rs/more-uses-of-skip-prefix: use skip_prefix() to avoid more magic numbers
2 parents 63434da + e3f1da9 commit 145c590

File tree

10 files changed

+69
-75
lines changed

10 files changed

+69
-75
lines changed

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ static unsigned long linelen(const char *buffer, unsigned long size)
436436

437437
static int is_dev_null(const char *str)
438438
{
439-
return !memcmp("/dev/null", str, 9) && isspace(str[9]);
439+
return skip_prefix(str, "/dev/null", &str) && isspace(*str);
440440
}
441441

442442
#define TERM_SPACE 1

builtin/branch.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ static int parse_branch_color_slot(const char *var, int ofs)
8181

8282
static int git_branch_config(const char *var, const char *value, void *cb)
8383
{
84+
const char *slot_name;
85+
8486
if (starts_with(var, "column."))
8587
return git_column_config(var, value, "branch", &colopts);
8688
if (!strcmp(var, "color.branch")) {
8789
branch_use_color = git_config_colorbool(var, value);
8890
return 0;
8991
}
90-
if (starts_with(var, "color.branch.")) {
91-
int slot = parse_branch_color_slot(var, 13);
92+
if (skip_prefix(var, "color.branch.", &slot_name)) {
93+
int slot = parse_branch_color_slot(var, slot_name - var);
9294
if (slot < 0)
9395
return 0;
9496
if (!value)
@@ -335,20 +337,18 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
335337
static struct {
336338
int kind;
337339
const char *prefix;
338-
int pfxlen;
339340
} ref_kind[] = {
340-
{ REF_LOCAL_BRANCH, "refs/heads/", 11 },
341-
{ REF_REMOTE_BRANCH, "refs/remotes/", 13 },
341+
{ REF_LOCAL_BRANCH, "refs/heads/" },
342+
{ REF_REMOTE_BRANCH, "refs/remotes/" },
342343
};
343344

344345
/* Detect kind */
345346
for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
346347
prefix = ref_kind[i].prefix;
347-
if (strncmp(refname, prefix, ref_kind[i].pfxlen))
348-
continue;
349-
kind = ref_kind[i].kind;
350-
refname += ref_kind[i].pfxlen;
351-
break;
348+
if (skip_prefix(refname, prefix, &refname)) {
349+
kind = ref_kind[i].kind;
350+
break;
351+
}
352352
}
353353
if (ARRAY_SIZE(ref_kind) <= i)
354354
return 0;
@@ -872,13 +872,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
872872
head = resolve_refdup("HEAD", head_sha1, 0, NULL);
873873
if (!head)
874874
die(_("Failed to resolve HEAD as a valid ref."));
875-
if (!strcmp(head, "HEAD")) {
875+
if (!strcmp(head, "HEAD"))
876876
detached = 1;
877-
} else {
878-
if (!starts_with(head, "refs/heads/"))
879-
die(_("HEAD not found below refs/heads!"));
880-
head += 11;
881-
}
877+
else if (!skip_prefix(head, "refs/heads/", &head))
878+
die(_("HEAD not found below refs/heads!"));
882879
hashcpy(merge_filter_ref, head_sha1);
883880

884881

builtin/cat-file.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
8282
enum object_type type;
8383
unsigned long size;
8484
char *buffer = read_sha1_file(sha1, &type, &size);
85-
if (memcmp(buffer, "object ", 7) ||
86-
get_sha1_hex(buffer + 7, blob_sha1))
85+
const char *target;
86+
if (!skip_prefix(buffer, "object ", &target) ||
87+
get_sha1_hex(target, blob_sha1))
8788
die("%s not a valid tag", sha1_to_hex(sha1));
8889
free(buffer);
8990
} else

builtin/checkout.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,10 +1150,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
11501150
const char *argv0 = argv[0];
11511151
if (!argc || !strcmp(argv0, "--"))
11521152
die (_("--track needs a branch name"));
1153-
if (starts_with(argv0, "refs/"))
1154-
argv0 += 5;
1155-
if (starts_with(argv0, "remotes/"))
1156-
argv0 += 8;
1153+
skip_prefix(argv0, "refs/", &argv0);
1154+
skip_prefix(argv0, "remotes/", &argv0);
11571155
argv0 = strchr(argv0, '/');
11581156
if (!argv0 || !argv0[1])
11591157
die (_("Missing branch name; try -b"));

builtin/clean.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ static int parse_clean_color_slot(const char *var)
100100

101101
static int git_clean_config(const char *var, const char *value, void *cb)
102102
{
103+
const char *slot_name;
104+
103105
if (starts_with(var, "column."))
104106
return git_column_config(var, value, "clean", &colopts);
105107

@@ -109,9 +111,8 @@ static int git_clean_config(const char *var, const char *value, void *cb)
109111
clean_use_color = git_config_colorbool(var, value);
110112
return 0;
111113
}
112-
if (starts_with(var, "color.interactive.")) {
113-
int slot = parse_clean_color_slot(var +
114-
strlen("color.interactive."));
114+
if (skip_prefix(var, "color.interactive.", &slot_name)) {
115+
int slot = parse_clean_color_slot(slot_name);
115116
if (slot < 0)
116117
return 0;
117118
if (!value)

builtin/commit.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,7 @@ static int parse_status_slot(const char *var, int offset)
12951295
static int git_status_config(const char *k, const char *v, void *cb)
12961296
{
12971297
struct wt_status *s = cb;
1298+
const char *slot_name;
12981299

12991300
if (starts_with(k, "column."))
13001301
return git_column_config(k, v, "status", &s->colopts);
@@ -1324,8 +1325,9 @@ static int git_status_config(const char *k, const char *v, void *cb)
13241325
s->display_comment_prefix = git_config_bool(k, v);
13251326
return 0;
13261327
}
1327-
if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) {
1328-
int slot = parse_status_slot(k, 13);
1328+
if (skip_prefix(k, "status.color.", &slot_name) ||
1329+
skip_prefix(k, "color.status.", &slot_name)) {
1330+
int slot = parse_status_slot(k, slot_name - k);
13291331
if (slot < 0)
13301332
return 0;
13311333
if (!v)
@@ -1514,13 +1516,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
15141516
diff_setup_done(&rev.diffopt);
15151517

15161518
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1517-
printf("[%s%s ",
1518-
starts_with(head, "refs/heads/") ?
1519-
head + 11 :
1520-
!strcmp(head, "HEAD") ?
1521-
_("detached HEAD") :
1522-
head,
1523-
initial_commit ? _(" (root-commit)") : "");
1519+
if (!strcmp(head, "HEAD"))
1520+
head = _("detached HEAD");
1521+
else
1522+
skip_prefix(head, "refs/heads/", &head);
1523+
printf("[%s%s ", head, initial_commit ? _(" (root-commit)") : "");
15241524

15251525
if (!log_tree_commit(&rev, commit)) {
15261526
rev.always_show_header = 1;

builtin/get-tar-commit-id.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
1919
char buffer[HEADERSIZE];
2020
struct ustar_header *header = (struct ustar_header *)buffer;
2121
char *content = buffer + RECORDSIZE;
22+
const char *comment;
2223
ssize_t n;
2324

2425
if (argc != 1)
@@ -29,10 +30,10 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
2930
die("git get-tar-commit-id: read error");
3031
if (header->typeflag[0] != 'g')
3132
return 1;
32-
if (memcmp(content, "52 comment=", 11))
33+
if (!skip_prefix(content, "52 comment=", &comment))
3334
return 1;
3435

35-
n = write_in_full(1, content + 11, 41);
36+
n = write_in_full(1, comment, 41);
3637
if (n < 41)
3738
die_errno("git get-tar-commit-id: write error");
3839

builtin/log.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ static int cmd_log_walk(struct rev_info *rev)
368368

369369
static int git_log_config(const char *var, const char *value, void *cb)
370370
{
371+
const char *slot_name;
372+
371373
if (!strcmp(var, "format.pretty"))
372374
return git_config_string(&fmt_pretty, var, value);
373375
if (!strcmp(var, "format.subjectprefix"))
@@ -388,8 +390,8 @@ static int git_log_config(const char *var, const char *value, void *cb)
388390
default_show_root = git_config_bool(var, value);
389391
return 0;
390392
}
391-
if (starts_with(var, "color.decorate."))
392-
return parse_decorate_color_config(var, 15, value);
393+
if (skip_prefix(var, "color.decorate.", &slot_name))
394+
return parse_decorate_color_config(var, slot_name - var, value);
393395
if (!strcmp(var, "log.mailmap")) {
394396
use_mailmap_config = git_config_bool(var, value);
395397
return 0;

builtin/remote-ext.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ static char *strip_escapes(const char *str, const char *service,
3030
size_t rpos = 0;
3131
int escape = 0;
3232
char special = 0;
33-
size_t psoff = 0;
33+
const char *service_noprefix = service;
3434
struct strbuf ret = STRBUF_INIT;
3535

36-
/* Calculate prefix length for \s and lengths for \s and \S */
37-
if (!strncmp(service, "git-", 4))
38-
psoff = 4;
36+
skip_prefix(service_noprefix, "git-", &service_noprefix);
3937

4038
/* Pass the service to command. */
4139
setenv("GIT_EXT_SERVICE", service, 1);
42-
setenv("GIT_EXT_SERVICE_NOPREFIX", service + psoff, 1);
40+
setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1);
4341

4442
/* Scan the length of argument. */
4543
while (str[rpos] && (escape || str[rpos] != ' ')) {
@@ -85,7 +83,7 @@ static char *strip_escapes(const char *str, const char *service,
8583
strbuf_addch(&ret, str[rpos]);
8684
break;
8785
case 's':
88-
strbuf_addstr(&ret, service + psoff);
86+
strbuf_addstr(&ret, service_noprefix);
8987
break;
9088
case 'S':
9189
strbuf_addstr(&ret, service);

pretty.c

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
7373
if (git_config_string(&fmt, var, value))
7474
return -1;
7575

76-
if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) {
77-
commit_format->is_tformat = fmt[0] == 't';
78-
fmt = strchr(fmt, ':') + 1;
79-
} else if (strchr(fmt, '%'))
76+
if (skip_prefix(fmt, "format:", &fmt))
77+
commit_format->is_tformat = 0;
78+
else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
8079
commit_format->is_tformat = 1;
8180
else
8281
commit_format->is_alias = 1;
@@ -157,12 +156,12 @@ void get_commit_format(const char *arg, struct rev_info *rev)
157156
rev->commit_format = CMIT_FMT_DEFAULT;
158157
return;
159158
}
160-
if (starts_with(arg, "format:") || starts_with(arg, "tformat:")) {
161-
save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
159+
if (skip_prefix(arg, "format:", &arg)) {
160+
save_user_format(rev, arg, 0);
162161
return;
163162
}
164163

165-
if (!*arg || strchr(arg, '%')) {
164+
if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
166165
save_user_format(rev, arg, 1);
167166
return;
168167
}
@@ -809,18 +808,19 @@ static void parse_commit_header(struct format_commit_context *context)
809808
int i;
810809

811810
for (i = 0; msg[i]; i++) {
811+
const char *name;
812812
int eol;
813813
for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
814814
; /* do nothing */
815815

816816
if (i == eol) {
817817
break;
818-
} else if (starts_with(msg + i, "author ")) {
819-
context->author.off = i + 7;
820-
context->author.len = eol - i - 7;
821-
} else if (starts_with(msg + i, "committer ")) {
822-
context->committer.off = i + 10;
823-
context->committer.len = eol - i - 10;
818+
} else if (skip_prefix(msg + i, "author ", &name)) {
819+
context->author.off = name - msg;
820+
context->author.len = msg + eol - name;
821+
} else if (skip_prefix(msg + i, "committer ", &name)) {
822+
context->committer.off = name - msg;
823+
context->committer.len = msg + eol - name;
824824
}
825825
i = eol;
826826
}
@@ -951,38 +951,34 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
951951
const char *placeholder,
952952
struct format_commit_context *c)
953953
{
954+
const char *rest = placeholder;
955+
954956
if (placeholder[1] == '(') {
955957
const char *begin = placeholder + 2;
956958
const char *end = strchr(begin, ')');
957959
char color[COLOR_MAXLEN];
958960

959961
if (!end)
960962
return 0;
961-
if (starts_with(begin, "auto,")) {
963+
if (skip_prefix(begin, "auto,", &begin)) {
962964
if (!want_color(c->pretty_ctx->color))
963965
return end - placeholder + 1;
964-
begin += 5;
965966
}
966967
color_parse_mem(begin,
967968
end - begin,
968969
"--pretty format", color);
969970
strbuf_addstr(sb, color);
970971
return end - placeholder + 1;
971972
}
972-
if (starts_with(placeholder + 1, "red")) {
973+
if (skip_prefix(placeholder + 1, "red", &rest))
973974
strbuf_addstr(sb, GIT_COLOR_RED);
974-
return 4;
975-
} else if (starts_with(placeholder + 1, "green")) {
975+
else if (skip_prefix(placeholder + 1, "green", &rest))
976976
strbuf_addstr(sb, GIT_COLOR_GREEN);
977-
return 6;
978-
} else if (starts_with(placeholder + 1, "blue")) {
977+
else if (skip_prefix(placeholder + 1, "blue", &rest))
979978
strbuf_addstr(sb, GIT_COLOR_BLUE);
980-
return 5;
981-
} else if (starts_with(placeholder + 1, "reset")) {
979+
else if (skip_prefix(placeholder + 1, "reset", &rest))
982980
strbuf_addstr(sb, GIT_COLOR_RESET);
983-
return 6;
984-
} else
985-
return 0;
981+
return rest - placeholder;
986982
}
987983

988984
static size_t parse_padding_placeholder(struct strbuf *sb,
@@ -1522,7 +1518,7 @@ static void pp_header(struct pretty_print_context *pp,
15221518
int parents_shown = 0;
15231519

15241520
for (;;) {
1525-
const char *line = *msg_p;
1521+
const char *name, *line = *msg_p;
15261522
int linelen = get_one_line(*msg_p);
15271523

15281524
if (!linelen)
@@ -1557,14 +1553,14 @@ static void pp_header(struct pretty_print_context *pp,
15571553
* FULL shows both authors but not dates.
15581554
* FULLER shows both authors and dates.
15591555
*/
1560-
if (starts_with(line, "author ")) {
1556+
if (skip_prefix(line, "author ", &name)) {
15611557
strbuf_grow(sb, linelen + 80);
1562-
pp_user_info(pp, "Author", sb, line + 7, encoding);
1558+
pp_user_info(pp, "Author", sb, name, encoding);
15631559
}
1564-
if (starts_with(line, "committer ") &&
1560+
if (skip_prefix(line, "committer ", &name) &&
15651561
(pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
15661562
strbuf_grow(sb, linelen + 80);
1567-
pp_user_info(pp, "Commit", sb, line + 10, encoding);
1563+
pp_user_info(pp, "Commit", sb, name, encoding);
15681564
}
15691565
}
15701566
}

0 commit comments

Comments
 (0)