use skip_prefix to avoid magic numbers

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

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

This is easy to get wrong, since you have to count the
prefix string yourself, and there's no compiler check if the
string changes.  We can use skip_prefix to avoid the magic
numbers here.

Note that some of these conversions could be much shorter.
For example:

  if (starts_with(arg, "--foo=")) {
	  bar = arg + 6;
	  continue;
  }

could become:

  if (skip_prefix(arg, "--foo=", &bar))
	  continue;

However, I have left it as:

  if (skip_prefix(arg, "--foo=", &v)) {
	  bar = v;
	  continue;
  }

to visually match nearby cases which need to actually
process the string. Like:

  if (skip_prefix(arg, "--foo=", &v)) {
	  bar = atoi(v);
	  continue;
  }

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2014-06-18 15:47:50 -04:00 committed by Junio C Hamano
parent 21a2d4ada5
commit ae021d8791
11 changed files with 149 additions and 131 deletions

View file

@ -5,7 +5,8 @@ static char *alias_val;
static int alias_lookup_cb(const char *k, const char *v, void *cb) static int alias_lookup_cb(const char *k, const char *v, void *cb)
{ {
if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) { const char *name;
if (skip_prefix(k, "alias.", &name) && !strcmp(name, alias_key)) {
if (!v) if (!v)
return config_error_nonbool(k); return config_error_nonbool(k);
alias_val = xstrdup(v); alias_val = xstrdup(v);

View file

@ -129,6 +129,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
char *name; char *name;
int len, name_len; int len, name_len;
char *buffer = packet_buffer; char *buffer = packet_buffer;
const char *arg;
len = packet_read(in, &src_buf, &src_len, len = packet_read(in, &src_buf, &src_len,
packet_buffer, sizeof(packet_buffer), packet_buffer, sizeof(packet_buffer),
@ -140,12 +141,12 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
if (!len) if (!len)
break; break;
if (len > 4 && starts_with(buffer, "ERR ")) if (len > 4 && skip_prefix(buffer, "ERR ", &arg))
die("remote error: %s", buffer + 4); die("remote error: %s", arg);
if (len == 48 && starts_with(buffer, "shallow ")) { if (len == 48 && skip_prefix(buffer, "shallow ", &arg)) {
if (get_sha1_hex(buffer + 8, old_sha1)) if (get_sha1_hex(arg, old_sha1))
die("protocol error: expected shallow sha-1, got '%s'", buffer + 8); die("protocol error: expected shallow sha-1, got '%s'", arg);
if (!shallow_points) if (!shallow_points)
die("repository on the other end cannot be shallow"); die("repository on the other end cannot be shallow");
sha1_array_append(shallow_points, old_sha1); sha1_array_append(shallow_points, old_sha1);

View file

@ -1121,9 +1121,9 @@ static int is_foreign_ident(const char *str)
{ {
int i; int i;
if (!starts_with(str, "$Id: ")) if (!skip_prefix(str, "$Id: ", &str))
return 0; return 0;
for (i = 5; str[i]; i++) { for (i = 0; str[i]; i++) {
if (isspace(str[i]) && str[i+1] != '$') if (isspace(str[i]) && str[i+1] != '$')
return 1; return 1;
} }

View file

@ -235,8 +235,10 @@ static int service_enabled;
static int git_daemon_config(const char *var, const char *value, void *cb) static int git_daemon_config(const char *var, const char *value, void *cb)
{ {
if (starts_with(var, "daemon.") && const char *service;
!strcmp(var + 7, service_looking_at->config_name)) {
if (skip_prefix(var, "daemon.", &service) &&
!strcmp(service, service_looking_at->config_name)) {
service_enabled = git_config_bool(var, value); service_enabled = git_config_bool(var, value);
return 0; return 0;
} }
@ -1133,16 +1135,17 @@ int main(int argc, char **argv)
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
char *arg = argv[i]; char *arg = argv[i];
const char *v;
if (starts_with(arg, "--listen=")) { if (skip_prefix(arg, "--listen=", &v)) {
string_list_append(&listen_addr, xstrdup_tolower(arg + 9)); string_list_append(&listen_addr, xstrdup_tolower(v));
continue; continue;
} }
if (starts_with(arg, "--port=")) { if (skip_prefix(arg, "--port=", &v)) {
char *end; char *end;
unsigned long n; unsigned long n;
n = strtoul(arg+7, &end, 0); n = strtoul(v, &end, 0);
if (arg[7] && !*end) { if (*v && !*end) {
listen_port = n; listen_port = n;
continue; continue;
} }
@ -1168,20 +1171,20 @@ int main(int argc, char **argv)
export_all_trees = 1; export_all_trees = 1;
continue; continue;
} }
if (starts_with(arg, "--access-hook=")) { if (skip_prefix(arg, "--access-hook=", &v)) {
access_hook = arg + 14; access_hook = v;
continue; continue;
} }
if (starts_with(arg, "--timeout=")) { if (skip_prefix(arg, "--timeout=", &v)) {
timeout = atoi(arg+10); timeout = atoi(v);
continue; continue;
} }
if (starts_with(arg, "--init-timeout=")) { if (skip_prefix(arg, "--init-timeout=", &v)) {
init_timeout = atoi(arg+15); init_timeout = atoi(v);
continue; continue;
} }
if (starts_with(arg, "--max-connections=")) { if (skip_prefix(arg, "--max-connections=", &v)) {
max_connections = atoi(arg+18); max_connections = atoi(v);
if (max_connections < 0) if (max_connections < 0)
max_connections = 0; /* unlimited */ max_connections = 0; /* unlimited */
continue; continue;
@ -1190,16 +1193,16 @@ int main(int argc, char **argv)
strict_paths = 1; strict_paths = 1;
continue; continue;
} }
if (starts_with(arg, "--base-path=")) { if (skip_prefix(arg, "--base-path=", &v)) {
base_path = arg+12; base_path = v;
continue; continue;
} }
if (!strcmp(arg, "--base-path-relaxed")) { if (!strcmp(arg, "--base-path-relaxed")) {
base_path_relaxed = 1; base_path_relaxed = 1;
continue; continue;
} }
if (starts_with(arg, "--interpolated-path=")) { if (skip_prefix(arg, "--interpolated-path=", &v)) {
interpolated_path = arg+20; interpolated_path = v;
continue; continue;
} }
if (!strcmp(arg, "--reuseaddr")) { if (!strcmp(arg, "--reuseaddr")) {
@ -1210,12 +1213,12 @@ int main(int argc, char **argv)
user_path = ""; user_path = "";
continue; continue;
} }
if (starts_with(arg, "--user-path=")) { if (skip_prefix(arg, "--user-path=", &v)) {
user_path = arg + 12; user_path = v;
continue; continue;
} }
if (starts_with(arg, "--pid-file=")) { if (skip_prefix(arg, "--pid-file=", &v)) {
pid_file = arg + 11; pid_file = v;
continue; continue;
} }
if (!strcmp(arg, "--detach")) { if (!strcmp(arg, "--detach")) {
@ -1223,28 +1226,28 @@ int main(int argc, char **argv)
log_syslog = 1; log_syslog = 1;
continue; continue;
} }
if (starts_with(arg, "--user=")) { if (skip_prefix(arg, "--user=", &v)) {
user_name = arg + 7; user_name = v;
continue; continue;
} }
if (starts_with(arg, "--group=")) { if (skip_prefix(arg, "--group=", &v)) {
group_name = arg + 8; group_name = v;
continue; continue;
} }
if (starts_with(arg, "--enable=")) { if (skip_prefix(arg, "--enable=", &v)) {
enable_service(arg + 9, 1); enable_service(v, 1);
continue; continue;
} }
if (starts_with(arg, "--disable=")) { if (skip_prefix(arg, "--disable=", &v)) {
enable_service(arg + 10, 0); enable_service(v, 0);
continue; continue;
} }
if (starts_with(arg, "--allow-override=")) { if (skip_prefix(arg, "--allow-override=", &v)) {
make_service_overridable(arg + 17, 1); make_service_overridable(v, 1);
continue; continue;
} }
if (starts_with(arg, "--forbid-override=")) { if (skip_prefix(arg, "--forbid-override=", &v)) {
make_service_overridable(arg + 18, 0); make_service_overridable(v, 0);
continue; continue;
} }
if (!strcmp(arg, "--informative-errors")) { if (!strcmp(arg, "--informative-errors")) {

65
diff.c
View file

@ -231,6 +231,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
int git_diff_basic_config(const char *var, const char *value, void *cb) int git_diff_basic_config(const char *var, const char *value, void *cb)
{ {
const char *name;
if (!strcmp(var, "diff.renamelimit")) { if (!strcmp(var, "diff.renamelimit")) {
diff_rename_limit_default = git_config_int(var, value); diff_rename_limit_default = git_config_int(var, value);
return 0; return 0;
@ -239,8 +241,9 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
if (userdiff_config(var, value) < 0) if (userdiff_config(var, value) < 0)
return -1; return -1;
if (starts_with(var, "diff.color.") || starts_with(var, "color.diff.")) { if (skip_prefix(var, "diff.color.", &name) ||
int slot = parse_diff_color_slot(var + 11); skip_prefix(var, "color.diff.", &name)) {
int slot = parse_diff_color_slot(name);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!value) if (!value)
@ -2341,6 +2344,7 @@ static void builtin_diff(const char *name_a,
} else { } else {
/* Crazy xdl interfaces.. */ /* Crazy xdl interfaces.. */
const char *diffopts = getenv("GIT_DIFF_OPTS"); const char *diffopts = getenv("GIT_DIFF_OPTS");
const char *v;
xpparam_t xpp; xpparam_t xpp;
xdemitconf_t xecfg; xdemitconf_t xecfg;
struct emit_callback ecbdata; struct emit_callback ecbdata;
@ -2379,10 +2383,10 @@ static void builtin_diff(const char *name_a,
xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
if (!diffopts) if (!diffopts)
; ;
else if (starts_with(diffopts, "--unified=")) else if (skip_prefix(diffopts, "--unified=", &v))
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); xecfg.ctxlen = strtoul(v, NULL, 10);
else if (starts_with(diffopts, "-u")) else if (skip_prefix(diffopts, "-u", &v))
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); xecfg.ctxlen = strtoul(v, NULL, 10);
if (o->word_diff) if (o->word_diff)
init_diff_words_data(&ecbdata, o, one, two); init_diff_words_data(&ecbdata, o, one, two);
xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata, xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
@ -3609,17 +3613,17 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
options->output_format |= DIFF_FORMAT_SHORTSTAT; options->output_format |= DIFF_FORMAT_SHORTSTAT;
else if (!strcmp(arg, "-X") || !strcmp(arg, "--dirstat")) else if (!strcmp(arg, "-X") || !strcmp(arg, "--dirstat"))
return parse_dirstat_opt(options, ""); return parse_dirstat_opt(options, "");
else if (starts_with(arg, "-X")) else if (skip_prefix(arg, "-X", &arg))
return parse_dirstat_opt(options, arg + 2); return parse_dirstat_opt(options, arg);
else if (starts_with(arg, "--dirstat=")) else if (skip_prefix(arg, "--dirstat=", &arg))
return parse_dirstat_opt(options, arg + 10); return parse_dirstat_opt(options, arg);
else if (!strcmp(arg, "--cumulative")) else if (!strcmp(arg, "--cumulative"))
return parse_dirstat_opt(options, "cumulative"); return parse_dirstat_opt(options, "cumulative");
else if (!strcmp(arg, "--dirstat-by-file")) else if (!strcmp(arg, "--dirstat-by-file"))
return parse_dirstat_opt(options, "files"); return parse_dirstat_opt(options, "files");
else if (starts_with(arg, "--dirstat-by-file=")) { else if (skip_prefix(arg, "--dirstat-by-file=", &arg)) {
parse_dirstat_opt(options, "files"); parse_dirstat_opt(options, "files");
return parse_dirstat_opt(options, arg + 18); return parse_dirstat_opt(options, arg);
} }
else if (!strcmp(arg, "--check")) else if (!strcmp(arg, "--check"))
options->output_format |= DIFF_FORMAT_CHECKDIFF; options->output_format |= DIFF_FORMAT_CHECKDIFF;
@ -3669,9 +3673,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
DIFF_OPT_CLR(options, RENAME_EMPTY); DIFF_OPT_CLR(options, RENAME_EMPTY);
else if (!strcmp(arg, "--relative")) else if (!strcmp(arg, "--relative"))
DIFF_OPT_SET(options, RELATIVE_NAME); DIFF_OPT_SET(options, RELATIVE_NAME);
else if (starts_with(arg, "--relative=")) { else if (skip_prefix(arg, "--relative=", &arg)) {
DIFF_OPT_SET(options, RELATIVE_NAME); DIFF_OPT_SET(options, RELATIVE_NAME);
options->prefix = arg + 11; options->prefix = arg;
} }
/* xdiff options */ /* xdiff options */
@ -3722,8 +3726,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
DIFF_OPT_CLR(options, FOLLOW_RENAMES); DIFF_OPT_CLR(options, FOLLOW_RENAMES);
else if (!strcmp(arg, "--color")) else if (!strcmp(arg, "--color"))
options->use_color = 1; options->use_color = 1;
else if (starts_with(arg, "--color=")) { else if (skip_prefix(arg, "--color=", &arg)) {
int value = git_config_colorbool(NULL, arg+8); int value = git_config_colorbool(NULL, arg);
if (value < 0) if (value < 0)
return error("option `color' expects \"always\", \"auto\", or \"never\""); return error("option `color' expects \"always\", \"auto\", or \"never\"");
options->use_color = value; options->use_color = value;
@ -3734,29 +3738,28 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
options->use_color = 1; options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR; options->word_diff = DIFF_WORDS_COLOR;
} }
else if (starts_with(arg, "--color-words=")) { else if (skip_prefix(arg, "--color-words=", &arg)) {
options->use_color = 1; options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR; options->word_diff = DIFF_WORDS_COLOR;
options->word_regex = arg + 14; options->word_regex = arg;
} }
else if (!strcmp(arg, "--word-diff")) { else if (!strcmp(arg, "--word-diff")) {
if (options->word_diff == DIFF_WORDS_NONE) if (options->word_diff == DIFF_WORDS_NONE)
options->word_diff = DIFF_WORDS_PLAIN; options->word_diff = DIFF_WORDS_PLAIN;
} }
else if (starts_with(arg, "--word-diff=")) { else if (skip_prefix(arg, "--word-diff=", &arg)) {
const char *type = arg + 12; if (!strcmp(arg, "plain"))
if (!strcmp(type, "plain"))
options->word_diff = DIFF_WORDS_PLAIN; options->word_diff = DIFF_WORDS_PLAIN;
else if (!strcmp(type, "color")) { else if (!strcmp(arg, "color")) {
options->use_color = 1; options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR; options->word_diff = DIFF_WORDS_COLOR;
} }
else if (!strcmp(type, "porcelain")) else if (!strcmp(arg, "porcelain"))
options->word_diff = DIFF_WORDS_PORCELAIN; options->word_diff = DIFF_WORDS_PORCELAIN;
else if (!strcmp(type, "none")) else if (!strcmp(arg, "none"))
options->word_diff = DIFF_WORDS_NONE; options->word_diff = DIFF_WORDS_NONE;
else else
die("bad --word-diff argument: %s", type); die("bad --word-diff argument: %s", arg);
} }
else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) { else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
if (options->word_diff == DIFF_WORDS_NONE) if (options->word_diff == DIFF_WORDS_NONE)
@ -3779,13 +3782,13 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
else if (!strcmp(arg, "--ignore-submodules")) { else if (!strcmp(arg, "--ignore-submodules")) {
DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG); DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
handle_ignore_submodules_arg(options, "all"); handle_ignore_submodules_arg(options, "all");
} else if (starts_with(arg, "--ignore-submodules=")) { } else if (skip_prefix(arg, "--ignore-submodules=", &arg)) {
DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG); DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
handle_ignore_submodules_arg(options, arg + 20); handle_ignore_submodules_arg(options, arg);
} else if (!strcmp(arg, "--submodule")) } else if (!strcmp(arg, "--submodule"))
DIFF_OPT_SET(options, SUBMODULE_LOG); DIFF_OPT_SET(options, SUBMODULE_LOG);
else if (starts_with(arg, "--submodule=")) else if (skip_prefix(arg, "--submodule=", &arg))
return parse_submodule_opt(options, arg + 12); return parse_submodule_opt(options, arg);
/* misc options */ /* misc options */
else if (!strcmp(arg, "-z")) else if (!strcmp(arg, "-z"))
@ -3820,8 +3823,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
} }
else if (!strcmp(arg, "--abbrev")) else if (!strcmp(arg, "--abbrev"))
options->abbrev = DEFAULT_ABBREV; options->abbrev = DEFAULT_ABBREV;
else if (starts_with(arg, "--abbrev=")) { else if (skip_prefix(arg, "--abbrev=", &arg)) {
options->abbrev = strtoul(arg + 9, NULL, 10); options->abbrev = strtoul(arg, NULL, 10);
if (options->abbrev < MINIMUM_ABBREV) if (options->abbrev < MINIMUM_ABBREV)
options->abbrev = MINIMUM_ABBREV; options->abbrev = MINIMUM_ABBREV;
else if (40 < options->abbrev) else if (40 < options->abbrev)

View file

@ -1912,8 +1912,9 @@ static void skip_optional_lf(void)
static void parse_mark(void) static void parse_mark(void)
{ {
if (starts_with(command_buf.buf, "mark :")) { const char *v;
next_mark = strtoumax(command_buf.buf + 6, NULL, 10); if (skip_prefix(command_buf.buf, "mark :", &v)) {
next_mark = strtoumax(v, NULL, 10);
read_next_command(); read_next_command();
} }
else else
@ -1922,14 +1923,15 @@ static void parse_mark(void)
static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res) static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
{ {
const char *data;
strbuf_reset(sb); strbuf_reset(sb);
if (!starts_with(command_buf.buf, "data ")) if (!skip_prefix(command_buf.buf, "data ", &data))
die("Expected 'data n' command, found: %s", command_buf.buf); die("Expected 'data n' command, found: %s", command_buf.buf);
if (starts_with(command_buf.buf + 5, "<<")) { if (skip_prefix(data, "<<", &data)) {
char *term = xstrdup(command_buf.buf + 5 + 2); char *term = xstrdup(data);
size_t term_len = command_buf.len - 5 - 2; size_t term_len = command_buf.len - (data - command_buf.buf);
strbuf_detach(&command_buf, NULL); strbuf_detach(&command_buf, NULL);
for (;;) { for (;;) {
@ -1944,7 +1946,7 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
free(term); free(term);
} }
else { else {
uintmax_t len = strtoumax(command_buf.buf + 5, NULL, 10); uintmax_t len = strtoumax(data, NULL, 10);
size_t n = 0, length = (size_t)len; size_t n = 0, length = (size_t)len;
if (limit && limit < len) { if (limit && limit < len) {
@ -2676,6 +2678,7 @@ static void parse_new_commit(void)
struct hash_list *merge_list = NULL; struct hash_list *merge_list = NULL;
unsigned int merge_count; unsigned int merge_count;
unsigned char prev_fanout, new_fanout; unsigned char prev_fanout, new_fanout;
const char *v;
/* Obtain the branch name from the rest of our command */ /* Obtain the branch name from the rest of our command */
sp = strchr(command_buf.buf, ' ') + 1; sp = strchr(command_buf.buf, ' ') + 1;
@ -2685,12 +2688,12 @@ static void parse_new_commit(void)
read_next_command(); read_next_command();
parse_mark(); parse_mark();
if (starts_with(command_buf.buf, "author ")) { if (skip_prefix(command_buf.buf, "author ", &v)) {
author = parse_ident(command_buf.buf + 7); author = parse_ident(v);
read_next_command(); read_next_command();
} }
if (starts_with(command_buf.buf, "committer ")) { if (skip_prefix(command_buf.buf, "committer ", &v)) {
committer = parse_ident(command_buf.buf + 10); committer = parse_ident(v);
read_next_command(); read_next_command();
} }
if (!committer) if (!committer)
@ -2777,6 +2780,7 @@ static void parse_new_tag(void)
uintmax_t from_mark = 0; uintmax_t from_mark = 0;
unsigned char sha1[20]; unsigned char sha1[20];
enum object_type type; enum object_type type;
const char *v;
/* Obtain the new tag name from the rest of our command */ /* Obtain the new tag name from the rest of our command */
sp = strchr(command_buf.buf, ' ') + 1; sp = strchr(command_buf.buf, ' ') + 1;
@ -2819,8 +2823,8 @@ static void parse_new_tag(void)
read_next_command(); read_next_command();
/* tagger ... */ /* tagger ... */
if (starts_with(command_buf.buf, "tagger ")) { if (skip_prefix(command_buf.buf, "tagger ", &v)) {
tagger = parse_ident(command_buf.buf + 7); tagger = parse_ident(v);
read_next_command(); read_next_command();
} else } else
tagger = NULL; tagger = NULL;
@ -3207,9 +3211,9 @@ static void option_export_pack_edges(const char *edges)
static int parse_one_option(const char *option) static int parse_one_option(const char *option)
{ {
if (starts_with(option, "max-pack-size=")) { if (skip_prefix(option, "max-pack-size=", &option)) {
unsigned long v; unsigned long v;
if (!git_parse_ulong(option + 14, &v)) if (!git_parse_ulong(option, &v))
return 0; return 0;
if (v < 8192) { if (v < 8192) {
warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v); warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
@ -3219,17 +3223,17 @@ static int parse_one_option(const char *option)
v = 1024 * 1024; v = 1024 * 1024;
} }
max_packsize = v; max_packsize = v;
} else if (starts_with(option, "big-file-threshold=")) { } else if (skip_prefix(option, "big-file-threshold=", &option)) {
unsigned long v; unsigned long v;
if (!git_parse_ulong(option + 19, &v)) if (!git_parse_ulong(option, &v))
return 0; return 0;
big_file_threshold = v; big_file_threshold = v;
} else if (starts_with(option, "depth=")) { } else if (skip_prefix(option, "depth=", &option)) {
option_depth(option + 6); option_depth(option);
} else if (starts_with(option, "active-branches=")) { } else if (skip_prefix(option, "active-branches=", &option)) {
option_active_branches(option + 16); option_active_branches(option);
} else if (starts_with(option, "export-pack-edges=")) { } else if (skip_prefix(option, "export-pack-edges=", &option)) {
option_export_pack_edges(option + 18); option_export_pack_edges(option);
} else if (starts_with(option, "quiet")) { } else if (starts_with(option, "quiet")) {
show_stats = 0; show_stats = 0;
} else if (starts_with(option, "stats")) { } else if (starts_with(option, "stats")) {
@ -3243,15 +3247,16 @@ static int parse_one_option(const char *option)
static int parse_one_feature(const char *feature, int from_stream) static int parse_one_feature(const char *feature, int from_stream)
{ {
if (starts_with(feature, "date-format=")) { const char *arg;
option_date_format(feature + 12);
} else if (starts_with(feature, "import-marks=")) { if (skip_prefix(feature, "date-format=", &arg)) {
option_import_marks(feature + 13, from_stream, 0); option_date_format(arg);
} else if (starts_with(feature, "import-marks-if-exists=")) { } else if (skip_prefix(feature, "import-marks=", &arg)) {
option_import_marks(feature + strlen("import-marks-if-exists="), option_import_marks(arg, from_stream, 0);
from_stream, 1); } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
} else if (starts_with(feature, "export-marks=")) { option_import_marks(arg, from_stream, 1);
option_export_marks(feature + 13); } else if (skip_prefix(feature, "export-marks=", &arg)) {
option_export_marks(arg);
} else if (!strcmp(feature, "cat-blob")) { } else if (!strcmp(feature, "cat-blob")) {
; /* Don't die - this feature is supported */ ; /* Don't die - this feature is supported */
} else if (!strcmp(feature, "relative-marks")) { } else if (!strcmp(feature, "relative-marks")) {

View file

@ -319,18 +319,19 @@ static int find_common(struct fetch_pack_args *args,
if (args->depth > 0) { if (args->depth > 0) {
char *line; char *line;
const char *arg;
unsigned char sha1[20]; unsigned char sha1[20];
send_request(args, fd[1], &req_buf); send_request(args, fd[1], &req_buf);
while ((line = packet_read_line(fd[0], NULL))) { while ((line = packet_read_line(fd[0], NULL))) {
if (starts_with(line, "shallow ")) { if (skip_prefix(line, "shallow ", &arg)) {
if (get_sha1_hex(line + 8, sha1)) if (get_sha1_hex(arg, sha1))
die("invalid shallow line: %s", line); die("invalid shallow line: %s", line);
register_shallow(sha1); register_shallow(sha1);
continue; continue;
} }
if (starts_with(line, "unshallow ")) { if (skip_prefix(line, "unshallow ", &arg)) {
if (get_sha1_hex(line + 10, sha1)) if (get_sha1_hex(arg, sha1))
die("invalid unshallow line: %s", line); die("invalid unshallow line: %s", line);
if (!lookup_object(sha1)) if (!lookup_object(sha1))
die("object not found: %s", line); die("object not found: %s", line);

18
git.c
View file

@ -54,8 +54,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
/* /*
* Check remaining flags. * Check remaining flags.
*/ */
if (starts_with(cmd, "--exec-path")) { if (skip_prefix(cmd, "--exec-path", &cmd)) {
cmd += 11;
if (*cmd == '=') if (*cmd == '=')
git_set_argv_exec_path(cmd + 1); git_set_argv_exec_path(cmd + 1);
else { else {
@ -92,8 +91,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1; *envchanged = 1;
(*argv)++; (*argv)++;
(*argc)--; (*argc)--;
} else if (starts_with(cmd, "--git-dir=")) { } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
if (envchanged) if (envchanged)
*envchanged = 1; *envchanged = 1;
} else if (!strcmp(cmd, "--namespace")) { } else if (!strcmp(cmd, "--namespace")) {
@ -106,8 +105,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1; *envchanged = 1;
(*argv)++; (*argv)++;
(*argc)--; (*argc)--;
} else if (starts_with(cmd, "--namespace=")) { } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1); setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
if (envchanged) if (envchanged)
*envchanged = 1; *envchanged = 1;
} else if (!strcmp(cmd, "--work-tree")) { } else if (!strcmp(cmd, "--work-tree")) {
@ -120,8 +119,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1; *envchanged = 1;
(*argv)++; (*argv)++;
(*argc)--; (*argc)--;
} else if (starts_with(cmd, "--work-tree=")) { } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1); setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
if (envchanged) if (envchanged)
*envchanged = 1; *envchanged = 1;
} else if (!strcmp(cmd, "--bare")) { } else if (!strcmp(cmd, "--bare")) {
@ -578,8 +577,7 @@ int main(int argc, char **av)
* So we just directly call the builtin handler, and die if * So we just directly call the builtin handler, and die if
* that one cannot handle it. * that one cannot handle it.
*/ */
if (starts_with(cmd, "git-")) { if (skip_prefix(cmd, "git-", &cmd)) {
cmd += 4;
argv[0] = cmd; argv[0] = cmd;
handle_builtin(argc, argv); handle_builtin(argc, argv);
die("cannot handle %s as a builtin", cmd); die("cannot handle %s as a builtin", cmd);

6
help.c
View file

@ -251,11 +251,13 @@ static struct cmdnames aliases;
static int git_unknown_cmd_config(const char *var, const char *value, void *cb) static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
{ {
const char *p;
if (!strcmp(var, "help.autocorrect")) if (!strcmp(var, "help.autocorrect"))
autocorrect = git_config_int(var,value); autocorrect = git_config_int(var,value);
/* Also use aliases for command lookup */ /* Also use aliases for command lookup */
if (starts_with(var, "alias.")) if (skip_prefix(var, "alias.", &p))
add_cmdname(&aliases, var + 6, strlen(var + 6)); add_cmdname(&aliases, p, strlen(p));
return git_default_config(var, value, cb); return git_default_config(var, value, cb);
} }

View file

@ -221,17 +221,19 @@ static void get_idx_file(char *name)
static int http_config(const char *var, const char *value, void *cb) static int http_config(const char *var, const char *value, void *cb)
{ {
const char *p;
if (!strcmp(var, "http.getanyfile")) { if (!strcmp(var, "http.getanyfile")) {
getanyfile = git_config_bool(var, value); getanyfile = git_config_bool(var, value);
return 0; return 0;
} }
if (starts_with(var, "http.")) { if (skip_prefix(var, "http.", &p)) {
int i; int i;
for (i = 0; i < ARRAY_SIZE(rpc_service); i++) { for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
struct rpc_service *svc = &rpc_service[i]; struct rpc_service *svc = &rpc_service[i];
if (!strcmp(var + 5, svc->config_name)) { if (!strcmp(p, svc->config_name)) {
svc->enabled = git_config_bool(var, value); svc->enabled = git_config_bool(var, value);
return 0; return 0;
} }
@ -244,15 +246,16 @@ static int http_config(const char *var, const char *value, void *cb)
static struct rpc_service *select_service(const char *name) static struct rpc_service *select_service(const char *name)
{ {
const char *svc_name;
struct rpc_service *svc = NULL; struct rpc_service *svc = NULL;
int i; int i;
if (!starts_with(name, "git-")) if (!skip_prefix(name, "git-", &svc_name))
forbidden("Unsupported service: '%s'", name); forbidden("Unsupported service: '%s'", name);
for (i = 0; i < ARRAY_SIZE(rpc_service); i++) { for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
struct rpc_service *s = &rpc_service[i]; struct rpc_service *s = &rpc_service[i];
if (!strcmp(s->name, name + 4)) { if (!strcmp(s->name, svc_name)) {
svc = s; svc = s;
break; break;
} }

View file

@ -770,9 +770,9 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
lock->owner = xmalloc(strlen(ctx->cdata) + 1); lock->owner = xmalloc(strlen(ctx->cdata) + 1);
strcpy(lock->owner, ctx->cdata); strcpy(lock->owner, ctx->cdata);
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) { } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
if (starts_with(ctx->cdata, "Second-")) const char *arg;
lock->timeout = if (skip_prefix(ctx->cdata, "Second-", &arg))
strtol(ctx->cdata + 7, NULL, 10); lock->timeout = strtol(arg, NULL, 10);
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) { } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
lock->token = xmalloc(strlen(ctx->cdata) + 1); lock->token = xmalloc(strlen(ctx->cdata) + 1);
strcpy(lock->token, ctx->cdata); strcpy(lock->token, ctx->cdata);
@ -1561,6 +1561,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
{ {
char *url; char *url;
struct strbuf buffer = STRBUF_INIT; struct strbuf buffer = STRBUF_INIT;
const char *name;
url = xmalloc(strlen(repo->url) + strlen(path) + 1); url = xmalloc(strlen(repo->url) + strlen(path) + 1);
sprintf(url, "%s%s", repo->url, path); sprintf(url, "%s%s", repo->url, path);
@ -1578,8 +1579,8 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
return; return;
/* If it's a symref, set the refname; otherwise try for a sha1 */ /* If it's a symref, set the refname; otherwise try for a sha1 */
if (starts_with((char *)buffer.buf, "ref: ")) { if (skip_prefix(buffer.buf, "ref: ", &name)) {
*symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6); *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
} else { } else {
get_sha1_hex(buffer.buf, sha1); get_sha1_hex(buffer.buf, sha1);
} }