Browse Source

use skip_prefix to avoid repeating strings

It's a common idiom to match a prefix and then skip past it
with strlen, like:

  if (starts_with(foo, "bar"))
	  foo += strlen("bar");

This avoids magic numbers, but means we have to repeat the
string (and there is no compiler check that we didn't make a
typo in one of the strings).

We can use skip_prefix to handle this case without repeating
ourselves.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Jeff King 11 years ago committed by Junio C Hamano
parent
commit
95b567c7c3
  1. 4
      builtin/checkout.c
  2. 6
      builtin/fmt-merge-msg.c
  3. 12
      builtin/log.c
  4. 27
      diff.c
  5. 7
      help.c
  6. 15
      merge-recursive.c
  7. 3
      pretty.c
  8. 15
      remote-curl.c
  9. 5
      remote.c
  10. 4
      sha1_name.c

4
builtin/checkout.c

@ -776,8 +776,8 @@ static int switch_branches(const struct checkout_opts *opts,
if (!(flag & REF_ISSYMREF)) if (!(flag & REF_ISSYMREF))
old.path = NULL; old.path = NULL;


if (old.path && starts_with(old.path, "refs/heads/")) if (old.path)
old.name = old.path + strlen("refs/heads/"); skip_prefix(old.path, "refs/heads/", &old.name);


if (!new->name) { if (!new->name) {
new->name = "HEAD"; new->name = "HEAD";

6
builtin/fmt-merge-msg.c

@ -100,7 +100,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
{ {
int i, len = strlen(line); int i, len = strlen(line);
struct origin_data *origin_data; struct origin_data *origin_data;
char *src, *origin; char *src;
const char *origin;
struct src_data *src_data; struct src_data *src_data;
struct string_list_item *item; struct string_list_item *item;
int pulling_head = 0; int pulling_head = 0;
@ -164,8 +165,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
origin = line; origin = line;
string_list_append(&src_data->tag, origin + 4); string_list_append(&src_data->tag, origin + 4);
src_data->head_status |= 2; src_data->head_status |= 2;
} else if (starts_with(line, "remote-tracking branch ")) { } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
origin = line + strlen("remote-tracking branch ");
string_list_append(&src_data->r_branch, origin); string_list_append(&src_data->r_branch, origin);
src_data->head_status |= 2; src_data->head_status |= 2;
} else { } else {

12
builtin/log.c

@ -872,7 +872,7 @@ static char *find_branch_name(struct rev_info *rev)
int i, positive = -1; int i, positive = -1;
unsigned char branch_sha1[20]; unsigned char branch_sha1[20];
const unsigned char *tip_sha1; const unsigned char *tip_sha1;
const char *ref; const char *ref, *v;
char *full_ref, *branch = NULL; char *full_ref, *branch = NULL;


for (i = 0; i < rev->cmdline.nr; i++) { for (i = 0; i < rev->cmdline.nr; i++) {
@ -888,9 +888,9 @@ static char *find_branch_name(struct rev_info *rev)
ref = rev->cmdline.rev[positive].name; ref = rev->cmdline.rev[positive].name;
tip_sha1 = rev->cmdline.rev[positive].item->sha1; tip_sha1 = rev->cmdline.rev[positive].item->sha1;
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) && if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
starts_with(full_ref, "refs/heads/") && skip_prefix(full_ref, "refs/heads/", &v) &&
!hashcmp(tip_sha1, branch_sha1)) !hashcmp(tip_sha1, branch_sha1))
branch = xstrdup(full_ref + strlen("refs/heads/")); branch = xstrdup(v);
free(full_ref); free(full_ref);
return branch; return branch;
} }
@ -1394,10 +1394,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)


if (check_head) { if (check_head) {
unsigned char sha1[20]; unsigned char sha1[20];
const char *ref; const char *ref, *v;
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL); ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
if (ref && starts_with(ref, "refs/heads/")) if (ref && skip_prefix(ref, "refs/heads/", &v))
branch_name = xstrdup(ref + strlen("refs/heads/")); branch_name = xstrdup(v);
else else
branch_name = xstrdup(""); /* no branch */ branch_name = xstrdup(""); /* no branch */
} }

27
diff.c

@ -3395,12 +3395,10 @@ int parse_long_opt(const char *opt, const char **argv,
const char **optarg) const char **optarg)
{ {
const char *arg = argv[0]; const char *arg = argv[0];
if (arg[0] != '-' || arg[1] != '-') if (!skip_prefix(arg, "--", &arg))
return 0; return 0;
arg += strlen("--"); if (!skip_prefix(arg, opt, &arg))
if (!starts_with(arg, opt))
return 0; return 0;
arg += strlen(opt);
if (*arg == '=') { /* stuck form: --option=value */ if (*arg == '=') { /* stuck form: --option=value */
*optarg = arg + 1; *optarg = arg + 1;
return 1; return 1;
@ -3429,8 +3427,7 @@ static int stat_opt(struct diff_options *options, const char **av)


switch (*arg) { switch (*arg) {
case '-': case '-':
if (starts_with(arg, "-width")) { if (skip_prefix(arg, "-width", &arg)) {
arg += strlen("-width");
if (*arg == '=') if (*arg == '=')
width = strtoul(arg + 1, &end, 10); width = strtoul(arg + 1, &end, 10);
else if (!*arg && !av[1]) else if (!*arg && !av[1])
@ -3439,8 +3436,7 @@ static int stat_opt(struct diff_options *options, const char **av)
width = strtoul(av[1], &end, 10); width = strtoul(av[1], &end, 10);
argcount = 2; argcount = 2;
} }
} else if (starts_with(arg, "-name-width")) { } else if (skip_prefix(arg, "-name-width", &arg)) {
arg += strlen("-name-width");
if (*arg == '=') if (*arg == '=')
name_width = strtoul(arg + 1, &end, 10); name_width = strtoul(arg + 1, &end, 10);
else if (!*arg && !av[1]) else if (!*arg && !av[1])
@ -3449,8 +3445,7 @@ static int stat_opt(struct diff_options *options, const char **av)
name_width = strtoul(av[1], &end, 10); name_width = strtoul(av[1], &end, 10);
argcount = 2; argcount = 2;
} }
} else if (starts_with(arg, "-graph-width")) { } else if (skip_prefix(arg, "-graph-width", &arg)) {
arg += strlen("-graph-width");
if (*arg == '=') if (*arg == '=')
graph_width = strtoul(arg + 1, &end, 10); graph_width = strtoul(arg + 1, &end, 10);
else if (!*arg && !av[1]) else if (!*arg && !av[1])
@ -3459,8 +3454,7 @@ static int stat_opt(struct diff_options *options, const char **av)
graph_width = strtoul(av[1], &end, 10); graph_width = strtoul(av[1], &end, 10);
argcount = 2; argcount = 2;
} }
} else if (starts_with(arg, "-count")) { } else if (skip_prefix(arg, "-count", &arg)) {
arg += strlen("-count");
if (*arg == '=') if (*arg == '=')
count = strtoul(arg + 1, &end, 10); count = strtoul(arg + 1, &end, 10);
else if (!*arg && !av[1]) else if (!*arg && !av[1])
@ -3905,16 +3899,13 @@ static int diff_scoreopt_parse(const char *opt)
cmd = *opt++; cmd = *opt++;
if (cmd == '-') { if (cmd == '-') {
/* convert the long-form arguments into short-form versions */ /* convert the long-form arguments into short-form versions */
if (starts_with(opt, "break-rewrites")) { if (skip_prefix(opt, "break-rewrites", &opt)) {
opt += strlen("break-rewrites");
if (*opt == 0 || *opt++ == '=') if (*opt == 0 || *opt++ == '=')
cmd = 'B'; cmd = 'B';
} else if (starts_with(opt, "find-copies")) { } else if (skip_prefix(opt, "find-copies", &opt)) {
opt += strlen("find-copies");
if (*opt == 0 || *opt++ == '=') if (*opt == 0 || *opt++ == '=')
cmd = 'C'; cmd = 'C';
} else if (starts_with(opt, "find-renames")) { } else if (skip_prefix(opt, "find-renames", &opt)) {
opt += strlen("find-renames");
if (*opt == 0 || *opt++ == '=') if (*opt == 0 || *opt++ == '=')
cmd = 'M'; cmd = 'M';
} }

7
help.c

@ -414,11 +414,12 @@ static int append_similar_ref(const char *refname, const unsigned char *sha1,
{ {
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
char *branch = strrchr(refname, '/') + 1; char *branch = strrchr(refname, '/') + 1;
const char *remote;

/* A remote branch of the same name is deemed similar */ /* A remote branch of the same name is deemed similar */
if (starts_with(refname, "refs/remotes/") && if (skip_prefix(refname, "refs/remotes/", &remote) &&
!strcmp(branch, cb->base_ref)) !strcmp(branch, cb->base_ref))
string_list_append(cb->similar_refs, string_list_append(cb->similar_refs, remote);
refname + strlen("refs/remotes/"));
return 0; return 0;
} }



15
merge-recursive.c

@ -2063,6 +2063,8 @@ void init_merge_options(struct merge_options *o)


int parse_merge_opt(struct merge_options *o, const char *s) int parse_merge_opt(struct merge_options *o, const char *s)
{ {
const char *arg;

if (!s || !*s) if (!s || !*s)
return -1; return -1;
if (!strcmp(s, "ours")) if (!strcmp(s, "ours"))
@ -2071,14 +2073,14 @@ int parse_merge_opt(struct merge_options *o, const char *s)
o->recursive_variant = MERGE_RECURSIVE_THEIRS; o->recursive_variant = MERGE_RECURSIVE_THEIRS;
else if (!strcmp(s, "subtree")) else if (!strcmp(s, "subtree"))
o->subtree_shift = ""; o->subtree_shift = "";
else if (starts_with(s, "subtree=")) else if (skip_prefix(s, "subtree=", &arg))
o->subtree_shift = s + strlen("subtree="); o->subtree_shift = arg;
else if (!strcmp(s, "patience")) else if (!strcmp(s, "patience"))
o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF); o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
else if (!strcmp(s, "histogram")) else if (!strcmp(s, "histogram"))
o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF); o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
else if (starts_with(s, "diff-algorithm=")) { else if (skip_prefix(s, "diff-algorithm=", &arg)) {
long value = parse_algorithm_value(s + strlen("diff-algorithm=")); long value = parse_algorithm_value(arg);
if (value < 0) if (value < 0)
return -1; return -1;
/* clear out previous settings */ /* clear out previous settings */
@ -2096,9 +2098,8 @@ int parse_merge_opt(struct merge_options *o, const char *s)
o->renormalize = 1; o->renormalize = 1;
else if (!strcmp(s, "no-renormalize")) else if (!strcmp(s, "no-renormalize"))
o->renormalize = 0; o->renormalize = 0;
else if (starts_with(s, "rename-threshold=")) { else if (skip_prefix(s, "rename-threshold=", &arg)) {
const char *score = s + strlen("rename-threshold="); if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
if ((o->rename_score = parse_rename_score(&score)) == -1 || *score != 0)
return -1; return -1;
} }
else else

3
pretty.c

@ -40,10 +40,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
const char *fmt; const char *fmt;
int i; int i;


if (!starts_with(var, "pretty.")) if (!skip_prefix(var, "pretty.", &name))
return 0; return 0;


name = var + strlen("pretty.");
for (i = 0; i < builtin_formats_len; i++) { for (i = 0; i < builtin_formats_len; i++) {
if (!strcmp(commit_formats[i].name, name)) if (!strcmp(commit_formats[i].name, name))
return 0; return 0;

15
remote-curl.c

@ -791,9 +791,9 @@ static void parse_fetch(struct strbuf *buf)
int alloc_heads = 0, nr_heads = 0; int alloc_heads = 0, nr_heads = 0;


do { do {
if (starts_with(buf->buf, "fetch ")) { const char *p;
char *p = buf->buf + strlen("fetch "); if (skip_prefix(buf->buf, "fetch ", &p)) {
char *name; const char *name;
struct ref *ref; struct ref *ref;
unsigned char old_sha1[20]; unsigned char old_sha1[20];


@ -968,6 +968,8 @@ int main(int argc, const char **argv)
http_init(remote, url.buf, 0); http_init(remote, url.buf, 0);


do { do {
const char *arg;

if (strbuf_getline(&buf, stdin, '\n') == EOF) { if (strbuf_getline(&buf, stdin, '\n') == EOF) {
if (ferror(stdin)) if (ferror(stdin))
fprintf(stderr, "Error reading command stream\n"); fprintf(stderr, "Error reading command stream\n");
@ -989,9 +991,8 @@ int main(int argc, const char **argv)
} else if (starts_with(buf.buf, "push ")) { } else if (starts_with(buf.buf, "push ")) {
parse_push(&buf); parse_push(&buf);


} else if (starts_with(buf.buf, "option ")) { } else if (skip_prefix(buf.buf, "option ", &arg)) {
char *name = buf.buf + strlen("option "); char *value = strchr(arg, ' ');
char *value = strchr(name, ' ');
int result; int result;


if (value) if (value)
@ -999,7 +1000,7 @@ int main(int argc, const char **argv)
else else
value = "true"; value = "true";


result = set_option(name, value); result = set_option(arg, value);
if (!result) if (!result)
printf("ok\n"); printf("ok\n");
else if (result < 0) else if (result < 0)

5
remote.c

@ -488,9 +488,8 @@ static void read_config(void)
current_branch = NULL; current_branch = NULL;
head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag); head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
if (head_ref && (flag & REF_ISSYMREF) && if (head_ref && (flag & REF_ISSYMREF) &&
starts_with(head_ref, "refs/heads/")) { skip_prefix(head_ref, "refs/heads/", &head_ref)) {
current_branch = current_branch = make_branch(head_ref, 0);
make_branch(head_ref + strlen("refs/heads/"), 0);
} }
git_config(handle_config, NULL); git_config(handle_config, NULL);
if (branch_pushremote_name) { if (branch_pushremote_name) {

4
sha1_name.c

@ -911,10 +911,8 @@ static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
const char *match = NULL, *target = NULL; const char *match = NULL, *target = NULL;
size_t len; size_t len;


if (starts_with(message, "checkout: moving from ")) { if (skip_prefix(message, "checkout: moving from ", &match))
match = message + strlen("checkout: moving from ");
target = strstr(match, " to "); target = strstr(match, " to ");
}


if (!match || !target) if (!match || !target)
return 0; return 0;

Loading…
Cancel
Save