use skip_prefix() to avoid more magic numbers

Continue where ae021d87 (use skip_prefix to avoid magic numbers) left off and use skip_prefix() in more places for determining the lengths of prefix strings to avoid using dependent constants and other indirect methods. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 4, 2014 at 20:54 UTC e3f1da982e4f14e7146964cb25a5011a3f41e84a
10 files changed +69 -75
builtin/apply.c
+1 -1
@@ -435,7 +435,7 @@ static unsigned long linelen(const char *buffer, unsigned long size)
435
436 static int is_dev_null(const char *str)
437 {
438 - return !memcmp("/dev/null", str, 9) && isspace(str[9]);
438 + return skip_prefix(str, "/dev/null", &str) && isspace(*str);
439 }
440
441 #define TERM_SPACE 1
builtin/branch.c
+13 -16
@@ -81,14 +81,16 @@ static int parse_branch_color_slot(const char *var, int ofs)
81
82 static int git_branch_config(const char *var, const char *value, void *cb)
83 {
84 + const char *slot_name;
85 +
86 if (starts_with(var, "column."))
87 return git_column_config(var, value, "branch", &colopts);
88 if (!strcmp(var, "color.branch")) {
89 branch_use_color = git_config_colorbool(var, value);
90 return 0;
91 }
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);
94 if (slot < 0)
95 return 0;
96 if (!value)
@@ -335,20 +337,18 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
337 static struct {
338 int kind;
339 const char *prefix;
338 - int pfxlen;
340 } 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/" },
343 };
344
345 /* Detect kind */
346 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
347 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 + }
352 }
353 if (ARRAY_SIZE(ref_kind) <= i)
354 return 0;
@@ -872,13 +872,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
872 head = resolve_refdup("HEAD", head_sha1, 0, NULL);
873 if (!head)
874 die(_("Failed to resolve HEAD as a valid ref."));
875 - if (!strcmp(head, "HEAD")) {
875 + if (!strcmp(head, "HEAD"))
876 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!"));
879 hashcpy(merge_filter_ref, head_sha1);
880
881
builtin/cat-file.c
+3 -2
@@ -82,8 +82,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
82 enum object_type type;
83 unsigned long size;
84 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))
88 die("%s not a valid tag", sha1_to_hex(sha1));
89 free(buffer);
90 } else
builtin/checkout.c
+2 -4
@@ -1150,10 +1150,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1150 const char *argv0 = argv[0];
1151 if (!argc || !strcmp(argv0, "--"))
1152 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);
1155 argv0 = strchr(argv0, '/');
1156 if (!argv0 || !argv0[1])
1157 die (_("Missing branch name; try -b"));
builtin/clean.c
+4 -3
@@ -100,6 +100,8 @@ static int parse_clean_color_slot(const char *var)
100
101 static int git_clean_config(const char *var, const char *value, void *cb)
102 {
103 + const char *slot_name;
104 +
105 if (starts_with(var, "column."))
106 return git_column_config(var, value, "clean", &colopts);
107
@@ -109,9 +111,8 @@ static int git_clean_config(const char *var, const char *value, void *cb)
111 clean_use_color = git_config_colorbool(var, value);
112 return 0;
113 }
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);
116 if (slot < 0)
117 return 0;
118 if (!value)
builtin/commit.c
+9 -9
@@ -1294,6 +1294,7 @@ static int parse_status_slot(const char *var, int offset)
1294 static int git_status_config(const char *k, const char *v, void *cb)
1295 {
1296 struct wt_status *s = cb;
1297 + const char *slot_name;
1298
1299 if (starts_with(k, "column."))
1300 return git_column_config(k, v, "status", &s->colopts);
@@ -1323,8 +1324,9 @@ static int git_status_config(const char *k, const char *v, void *cb)
1324 s->display_comment_prefix = git_config_bool(k, v);
1325 return 0;
1326 }
1326 - if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) {
1327 - int slot = parse_status_slot(k, 13);
1327 + if (skip_prefix(k, "status.color.", &slot_name) ||
1328 + skip_prefix(k, "color.status.", &slot_name)) {
1329 + int slot = parse_status_slot(k, slot_name - k);
1330 if (slot < 0)
1331 return 0;
1332 if (!v)
@@ -1513,13 +1515,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
1515 diff_setup_done(&rev.diffopt);
1516
1517 head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1516 - printf("[%s%s ",
1517 - starts_with(head, "refs/heads/") ?
1518 - head + 11 :
1519 - !strcmp(head, "HEAD") ?
1520 - _("detached HEAD") :
1521 - head,
1522 - initial_commit ? _(" (root-commit)") : "");
1518 + if (!strcmp(head, "HEAD"))
1519 + head = _("detached HEAD");
1520 + else
1521 + skip_prefix(head, "refs/heads/", &head);
1522 + printf("[%s%s ", head, initial_commit ? _(" (root-commit)") : "");
1523
1524 if (!log_tree_commit(&rev, commit)) {
1525 rev.always_show_header = 1;
builtin/get-tar-commit-id.c
+3 -2
@@ -19,6 +19,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
19 char buffer[HEADERSIZE];
20 struct ustar_header *header = (struct ustar_header *)buffer;
21 char *content = buffer + RECORDSIZE;
22 + const char *comment;
23 ssize_t n;
24
25 if (argc != 1)
@@ -29,10 +30,10 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
30 die("git get-tar-commit-id: read error");
31 if (header->typeflag[0] != 'g')
32 return 1;
32 - if (memcmp(content, "52 comment=", 11))
33 + if (!skip_prefix(content, "52 comment=", &comment))
34 return 1;
35
35 - n = write_in_full(1, content + 11, 41);
36 + n = write_in_full(1, comment, 41);
37 if (n < 41)
38 die_errno("git get-tar-commit-id: write error");
39
builtin/log.c
+4 -2
@@ -368,6 +368,8 @@ static int cmd_log_walk(struct rev_info *rev)
368
369 static int git_log_config(const char *var, const char *value, void *cb)
370 {
371 + const char *slot_name;
372 +
373 if (!strcmp(var, "format.pretty"))
374 return git_config_string(&fmt_pretty, var, value);
375 if (!strcmp(var, "format.subjectprefix"))
@@ -388,8 +390,8 @@ static int git_log_config(const char *var, const char *value, void *cb)
390 default_show_root = git_config_bool(var, value);
391 return 0;
392 }
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);
395 if (!strcmp(var, "log.mailmap")) {
396 use_mailmap_config = git_config_bool(var, value);
397 return 0;
builtin/remote-ext.c
+4 -6
@@ -30,16 +30,14 @@ static char *strip_escapes(const char *str, const char *service,
30 size_t rpos = 0;
31 int escape = 0;
32 char special = 0;
33 - size_t psoff = 0;
33 + const char *service_noprefix = service;
34 struct strbuf ret = STRBUF_INIT;
35
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);
37
38 /* Pass the service to command. */
39 setenv("GIT_EXT_SERVICE", service, 1);
42 - setenv("GIT_EXT_SERVICE_NOPREFIX", service + psoff, 1);
40 + setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1);
41
42 /* Scan the length of argument. */
43 while (str[rpos] && (escape || str[rpos] != ' ')) {
@@ -85,7 +83,7 @@ static char *strip_escapes(const char *str, const char *service,
83 strbuf_addch(&ret, str[rpos]);
84 break;
85 case 's':
88 - strbuf_addstr(&ret, service + psoff);
86 + strbuf_addstr(&ret, service_noprefix);
87 break;
88 case 'S':
89 strbuf_addstr(&ret, service);
pretty.c
+26 -30
@@ -73,10 +73,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
73 if (git_config_string(&fmt, var, value))
74 return -1;
75
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, '%'))
79 commit_format->is_tformat = 1;
80 else
81 commit_format->is_alias = 1;
@@ -157,12 +156,12 @@ void get_commit_format(const char *arg, struct rev_info *rev)
156 rev->commit_format = CMIT_FMT_DEFAULT;
157 return;
158 }
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);
161 return;
162 }
163
165 - if (!*arg || strchr(arg, '%')) {
164 + if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
165 save_user_format(rev, arg, 1);
166 return;
167 }
@@ -809,18 +808,19 @@ static void parse_commit_header(struct format_commit_context *context)
808 int i;
809
810 for (i = 0; msg[i]; i++) {
811 + const char *name;
812 int eol;
813 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
814 ; /* do nothing */
815
816 if (i == eol) {
817 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;
824 }
825 i = eol;
826 }
@@ -951,6 +951,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
951 const char *placeholder,
952 struct format_commit_context *c)
953 {
954 + const char *rest = placeholder;
955 +
956 if (placeholder[1] == '(') {
957 const char *begin = placeholder + 2;
958 const char *end = strchr(begin, ')');
@@ -958,10 +960,9 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
960
961 if (!end)
962 return 0;
961 - if (starts_with(begin, "auto,")) {
963 + if (skip_prefix(begin, "auto,", &begin)) {
964 if (!want_color(c->pretty_ctx->color))
965 return end - placeholder + 1;
964 - begin += 5;
966 }
967 color_parse_mem(begin,
968 end - begin,
@@ -969,20 +970,15 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
970 strbuf_addstr(sb, color);
971 return end - placeholder + 1;
972 }
972 - if (starts_with(placeholder + 1, "red")) {
973 + if (skip_prefix(placeholder + 1, "red", &rest))
974 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))
976 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))
978 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))
980 strbuf_addstr(sb, GIT_COLOR_RESET);
983 - return 6;
984 - } else
985 - return 0;
981 + return rest - placeholder;
982 }
983
984 static size_t parse_padding_placeholder(struct strbuf *sb,
@@ -1522,7 +1518,7 @@ static void pp_header(struct pretty_print_context *pp,
1518 int parents_shown = 0;
1519
1520 for (;;) {
1525 - const char *line = *msg_p;
1521 + const char *name, *line = *msg_p;
1522 int linelen = get_one_line(*msg_p);
1523
1524 if (!linelen)
@@ -1557,14 +1553,14 @@ static void pp_header(struct pretty_print_context *pp,
1553 * FULL shows both authors but not dates.
1554 * FULLER shows both authors and dates.
1555 */
1560 - if (starts_with(line, "author ")) {
1556 + if (skip_prefix(line, "author ", &name)) {
1557 strbuf_grow(sb, linelen + 80);
1562 - pp_user_info(pp, "Author", sb, line + 7, encoding);
1558 + pp_user_info(pp, "Author", sb, name, encoding);
1559 }
1564 - if (starts_with(line, "committer ") &&
1560 + if (skip_prefix(line, "committer ", &name) &&
1561 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1562 strbuf_grow(sb, linelen + 80);
1567 - pp_user_info(pp, "Commit", sb, line + 10, encoding);
1563 + pp_user_info(pp, "Commit", sb, name, encoding);
1564 }
1565 }
1566 }