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>
This commit is contained in:
René Scharfe 2014-10-04 20:54:50 +02:00 committed by Junio C Hamano
parent 565301e416
commit e3f1da982e
10 changed files with 69 additions and 75 deletions

View File

@ -435,7 +435,7 @@ static unsigned long linelen(const char *buffer, unsigned long size)
static int is_dev_null(const char *str) static int is_dev_null(const char *str)
{ {
return !memcmp("/dev/null", str, 9) && isspace(str[9]); return skip_prefix(str, "/dev/null", &str) && isspace(*str);
} }
#define TERM_SPACE 1 #define TERM_SPACE 1

View File

@ -81,14 +81,16 @@ static int parse_branch_color_slot(const char *var, int ofs)
static int git_branch_config(const char *var, const char *value, void *cb) static int git_branch_config(const char *var, const char *value, void *cb)
{ {
const char *slot_name;
if (starts_with(var, "column.")) if (starts_with(var, "column."))
return git_column_config(var, value, "branch", &colopts); return git_column_config(var, value, "branch", &colopts);
if (!strcmp(var, "color.branch")) { if (!strcmp(var, "color.branch")) {
branch_use_color = git_config_colorbool(var, value); branch_use_color = git_config_colorbool(var, value);
return 0; return 0;
} }
if (starts_with(var, "color.branch.")) { if (skip_prefix(var, "color.branch.", &slot_name)) {
int slot = parse_branch_color_slot(var, 13); int slot = parse_branch_color_slot(var, slot_name - var);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!value) if (!value)
@ -335,20 +337,18 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
static struct { static struct {
int kind; int kind;
const char *prefix; const char *prefix;
int pfxlen;
} ref_kind[] = { } ref_kind[] = {
{ REF_LOCAL_BRANCH, "refs/heads/", 11 }, { REF_LOCAL_BRANCH, "refs/heads/" },
{ REF_REMOTE_BRANCH, "refs/remotes/", 13 }, { REF_REMOTE_BRANCH, "refs/remotes/" },
}; };
/* Detect kind */ /* Detect kind */
for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
prefix = ref_kind[i].prefix; prefix = ref_kind[i].prefix;
if (strncmp(refname, prefix, ref_kind[i].pfxlen)) if (skip_prefix(refname, prefix, &refname)) {
continue; kind = ref_kind[i].kind;
kind = ref_kind[i].kind; break;
refname += ref_kind[i].pfxlen; }
break;
} }
if (ARRAY_SIZE(ref_kind) <= i) if (ARRAY_SIZE(ref_kind) <= i)
return 0; return 0;
@ -872,13 +872,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
head = resolve_refdup("HEAD", head_sha1, 0, NULL); head = resolve_refdup("HEAD", head_sha1, 0, NULL);
if (!head) if (!head)
die(_("Failed to resolve HEAD as a valid ref.")); die(_("Failed to resolve HEAD as a valid ref."));
if (!strcmp(head, "HEAD")) { if (!strcmp(head, "HEAD"))
detached = 1; detached = 1;
} else { else if (!skip_prefix(head, "refs/heads/", &head))
if (!starts_with(head, "refs/heads/")) die(_("HEAD not found below refs/heads!"));
die(_("HEAD not found below refs/heads!"));
head += 11;
}
hashcpy(merge_filter_ref, head_sha1); hashcpy(merge_filter_ref, head_sha1);

View File

@ -82,8 +82,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
enum object_type type; enum object_type type;
unsigned long size; unsigned long size;
char *buffer = read_sha1_file(sha1, &type, &size); char *buffer = read_sha1_file(sha1, &type, &size);
if (memcmp(buffer, "object ", 7) || const char *target;
get_sha1_hex(buffer + 7, blob_sha1)) if (!skip_prefix(buffer, "object ", &target) ||
get_sha1_hex(target, blob_sha1))
die("%s not a valid tag", sha1_to_hex(sha1)); die("%s not a valid tag", sha1_to_hex(sha1));
free(buffer); free(buffer);
} else } else

View File

@ -1150,10 +1150,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
const char *argv0 = argv[0]; const char *argv0 = argv[0];
if (!argc || !strcmp(argv0, "--")) if (!argc || !strcmp(argv0, "--"))
die (_("--track needs a branch name")); die (_("--track needs a branch name"));
if (starts_with(argv0, "refs/")) skip_prefix(argv0, "refs/", &argv0);
argv0 += 5; skip_prefix(argv0, "remotes/", &argv0);
if (starts_with(argv0, "remotes/"))
argv0 += 8;
argv0 = strchr(argv0, '/'); argv0 = strchr(argv0, '/');
if (!argv0 || !argv0[1]) if (!argv0 || !argv0[1])
die (_("Missing branch name; try -b")); die (_("Missing branch name; try -b"));

View File

@ -100,6 +100,8 @@ static int parse_clean_color_slot(const char *var)
static int git_clean_config(const char *var, const char *value, void *cb) static int git_clean_config(const char *var, const char *value, void *cb)
{ {
const char *slot_name;
if (starts_with(var, "column.")) if (starts_with(var, "column."))
return git_column_config(var, value, "clean", &colopts); return git_column_config(var, value, "clean", &colopts);
@ -109,9 +111,8 @@ static int git_clean_config(const char *var, const char *value, void *cb)
clean_use_color = git_config_colorbool(var, value); clean_use_color = git_config_colorbool(var, value);
return 0; return 0;
} }
if (starts_with(var, "color.interactive.")) { if (skip_prefix(var, "color.interactive.", &slot_name)) {
int slot = parse_clean_color_slot(var + int slot = parse_clean_color_slot(slot_name);
strlen("color.interactive."));
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!value) if (!value)

View File

@ -1294,6 +1294,7 @@ static int parse_status_slot(const char *var, int offset)
static int git_status_config(const char *k, const char *v, void *cb) static int git_status_config(const char *k, const char *v, void *cb)
{ {
struct wt_status *s = cb; struct wt_status *s = cb;
const char *slot_name;
if (starts_with(k, "column.")) if (starts_with(k, "column."))
return git_column_config(k, v, "status", &s->colopts); 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)
s->display_comment_prefix = git_config_bool(k, v); s->display_comment_prefix = git_config_bool(k, v);
return 0; return 0;
} }
if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) { if (skip_prefix(k, "status.color.", &slot_name) ||
int slot = parse_status_slot(k, 13); skip_prefix(k, "color.status.", &slot_name)) {
int slot = parse_status_slot(k, slot_name - k);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!v) if (!v)
@ -1513,13 +1515,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
diff_setup_done(&rev.diffopt); diff_setup_done(&rev.diffopt);
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL); head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
printf("[%s%s ", if (!strcmp(head, "HEAD"))
starts_with(head, "refs/heads/") ? head = _("detached HEAD");
head + 11 : else
!strcmp(head, "HEAD") ? skip_prefix(head, "refs/heads/", &head);
_("detached HEAD") : printf("[%s%s ", head, initial_commit ? _(" (root-commit)") : "");
head,
initial_commit ? _(" (root-commit)") : "");
if (!log_tree_commit(&rev, commit)) { if (!log_tree_commit(&rev, commit)) {
rev.always_show_header = 1; rev.always_show_header = 1;

View File

@ -19,6 +19,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
char buffer[HEADERSIZE]; char buffer[HEADERSIZE];
struct ustar_header *header = (struct ustar_header *)buffer; struct ustar_header *header = (struct ustar_header *)buffer;
char *content = buffer + RECORDSIZE; char *content = buffer + RECORDSIZE;
const char *comment;
ssize_t n; ssize_t n;
if (argc != 1) if (argc != 1)
@ -29,10 +30,10 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
die("git get-tar-commit-id: read error"); die("git get-tar-commit-id: read error");
if (header->typeflag[0] != 'g') if (header->typeflag[0] != 'g')
return 1; return 1;
if (memcmp(content, "52 comment=", 11)) if (!skip_prefix(content, "52 comment=", &comment))
return 1; return 1;
n = write_in_full(1, content + 11, 41); n = write_in_full(1, comment, 41);
if (n < 41) if (n < 41)
die_errno("git get-tar-commit-id: write error"); die_errno("git get-tar-commit-id: write error");

View File

@ -368,6 +368,8 @@ static int cmd_log_walk(struct rev_info *rev)
static int git_log_config(const char *var, const char *value, void *cb) static int git_log_config(const char *var, const char *value, void *cb)
{ {
const char *slot_name;
if (!strcmp(var, "format.pretty")) if (!strcmp(var, "format.pretty"))
return git_config_string(&fmt_pretty, var, value); return git_config_string(&fmt_pretty, var, value);
if (!strcmp(var, "format.subjectprefix")) if (!strcmp(var, "format.subjectprefix"))
@ -388,8 +390,8 @@ static int git_log_config(const char *var, const char *value, void *cb)
default_show_root = git_config_bool(var, value); default_show_root = git_config_bool(var, value);
return 0; return 0;
} }
if (starts_with(var, "color.decorate.")) if (skip_prefix(var, "color.decorate.", &slot_name))
return parse_decorate_color_config(var, 15, value); return parse_decorate_color_config(var, slot_name - var, value);
if (!strcmp(var, "log.mailmap")) { if (!strcmp(var, "log.mailmap")) {
use_mailmap_config = git_config_bool(var, value); use_mailmap_config = git_config_bool(var, value);
return 0; return 0;

View File

@ -30,16 +30,14 @@ static char *strip_escapes(const char *str, const char *service,
size_t rpos = 0; size_t rpos = 0;
int escape = 0; int escape = 0;
char special = 0; char special = 0;
size_t psoff = 0; const char *service_noprefix = service;
struct strbuf ret = STRBUF_INIT; struct strbuf ret = STRBUF_INIT;
/* Calculate prefix length for \s and lengths for \s and \S */ skip_prefix(service_noprefix, "git-", &service_noprefix);
if (!strncmp(service, "git-", 4))
psoff = 4;
/* Pass the service to command. */ /* Pass the service to command. */
setenv("GIT_EXT_SERVICE", service, 1); setenv("GIT_EXT_SERVICE", service, 1);
setenv("GIT_EXT_SERVICE_NOPREFIX", service + psoff, 1); setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1);
/* Scan the length of argument. */ /* Scan the length of argument. */
while (str[rpos] && (escape || str[rpos] != ' ')) { while (str[rpos] && (escape || str[rpos] != ' ')) {
@ -85,7 +83,7 @@ static char *strip_escapes(const char *str, const char *service,
strbuf_addch(&ret, str[rpos]); strbuf_addch(&ret, str[rpos]);
break; break;
case 's': case 's':
strbuf_addstr(&ret, service + psoff); strbuf_addstr(&ret, service_noprefix);
break; break;
case 'S': case 'S':
strbuf_addstr(&ret, service); strbuf_addstr(&ret, service);

View File

@ -73,10 +73,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
if (git_config_string(&fmt, var, value)) if (git_config_string(&fmt, var, value))
return -1; return -1;
if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) { if (skip_prefix(fmt, "format:", &fmt))
commit_format->is_tformat = fmt[0] == 't'; commit_format->is_tformat = 0;
fmt = strchr(fmt, ':') + 1; else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
} else if (strchr(fmt, '%'))
commit_format->is_tformat = 1; commit_format->is_tformat = 1;
else else
commit_format->is_alias = 1; commit_format->is_alias = 1;
@ -157,12 +156,12 @@ void get_commit_format(const char *arg, struct rev_info *rev)
rev->commit_format = CMIT_FMT_DEFAULT; rev->commit_format = CMIT_FMT_DEFAULT;
return; return;
} }
if (starts_with(arg, "format:") || starts_with(arg, "tformat:")) { if (skip_prefix(arg, "format:", &arg)) {
save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't'); save_user_format(rev, arg, 0);
return; return;
} }
if (!*arg || strchr(arg, '%')) { if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
save_user_format(rev, arg, 1); save_user_format(rev, arg, 1);
return; return;
} }
@ -809,18 +808,19 @@ static void parse_commit_header(struct format_commit_context *context)
int i; int i;
for (i = 0; msg[i]; i++) { for (i = 0; msg[i]; i++) {
const char *name;
int eol; int eol;
for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
; /* do nothing */ ; /* do nothing */
if (i == eol) { if (i == eol) {
break; break;
} else if (starts_with(msg + i, "author ")) { } else if (skip_prefix(msg + i, "author ", &name)) {
context->author.off = i + 7; context->author.off = name - msg;
context->author.len = eol - i - 7; context->author.len = msg + eol - name;
} else if (starts_with(msg + i, "committer ")) { } else if (skip_prefix(msg + i, "committer ", &name)) {
context->committer.off = i + 10; context->committer.off = name - msg;
context->committer.len = eol - i - 10; context->committer.len = msg + eol - name;
} }
i = eol; i = eol;
} }
@ -951,6 +951,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
const char *placeholder, const char *placeholder,
struct format_commit_context *c) struct format_commit_context *c)
{ {
const char *rest = placeholder;
if (placeholder[1] == '(') { if (placeholder[1] == '(') {
const char *begin = placeholder + 2; const char *begin = placeholder + 2;
const char *end = strchr(begin, ')'); const char *end = strchr(begin, ')');
@ -958,10 +960,9 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
if (!end) if (!end)
return 0; return 0;
if (starts_with(begin, "auto,")) { if (skip_prefix(begin, "auto,", &begin)) {
if (!want_color(c->pretty_ctx->color)) if (!want_color(c->pretty_ctx->color))
return end - placeholder + 1; return end - placeholder + 1;
begin += 5;
} }
color_parse_mem(begin, color_parse_mem(begin,
end - begin, end - begin,
@ -969,20 +970,15 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
strbuf_addstr(sb, color); strbuf_addstr(sb, color);
return end - placeholder + 1; return end - placeholder + 1;
} }
if (starts_with(placeholder + 1, "red")) { if (skip_prefix(placeholder + 1, "red", &rest))
strbuf_addstr(sb, GIT_COLOR_RED); strbuf_addstr(sb, GIT_COLOR_RED);
return 4; else if (skip_prefix(placeholder + 1, "green", &rest))
} else if (starts_with(placeholder + 1, "green")) {
strbuf_addstr(sb, GIT_COLOR_GREEN); strbuf_addstr(sb, GIT_COLOR_GREEN);
return 6; else if (skip_prefix(placeholder + 1, "blue", &rest))
} else if (starts_with(placeholder + 1, "blue")) {
strbuf_addstr(sb, GIT_COLOR_BLUE); strbuf_addstr(sb, GIT_COLOR_BLUE);
return 5; else if (skip_prefix(placeholder + 1, "reset", &rest))
} else if (starts_with(placeholder + 1, "reset")) {
strbuf_addstr(sb, GIT_COLOR_RESET); strbuf_addstr(sb, GIT_COLOR_RESET);
return 6; return rest - placeholder;
} else
return 0;
} }
static size_t parse_padding_placeholder(struct strbuf *sb, static size_t parse_padding_placeholder(struct strbuf *sb,
@ -1522,7 +1518,7 @@ static void pp_header(struct pretty_print_context *pp,
int parents_shown = 0; int parents_shown = 0;
for (;;) { for (;;) {
const char *line = *msg_p; const char *name, *line = *msg_p;
int linelen = get_one_line(*msg_p); int linelen = get_one_line(*msg_p);
if (!linelen) if (!linelen)
@ -1557,14 +1553,14 @@ static void pp_header(struct pretty_print_context *pp,
* FULL shows both authors but not dates. * FULL shows both authors but not dates.
* FULLER shows both authors and dates. * FULLER shows both authors and dates.
*/ */
if (starts_with(line, "author ")) { if (skip_prefix(line, "author ", &name)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info(pp, "Author", sb, line + 7, encoding); pp_user_info(pp, "Author", sb, name, encoding);
} }
if (starts_with(line, "committer ") && if (skip_prefix(line, "committer ", &name) &&
(pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info(pp, "Commit", sb, line + 10, encoding); pp_user_info(pp, "Commit", sb, name, encoding);
} }
} }
} }