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>

Jeff King committed Jun 18, 2014 at 15:47 UTC ae021d87911da4328157273df24779892cb51277
11 files changed +149 -131
alias.c
+2 -1
@@ -5,7 +5,8 @@ static char *alias_val;
5
6 static int alias_lookup_cb(const char *k, const char *v, void *cb)
7 {
8 - if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) {
8 + const char *name;
9 + if (skip_prefix(k, "alias.", &name) && !strcmp(name, alias_key)) {
10 if (!v)
11 return config_error_nonbool(k);
12 alias_val = xstrdup(v);
connect.c
+6 -5
@@ -129,6 +129,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
129 char *name;
130 int len, name_len;
131 char *buffer = packet_buffer;
132 + const char *arg;
133
134 len = packet_read(in, &src_buf, &src_len,
135 packet_buffer, sizeof(packet_buffer),
@@ -140,12 +141,12 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
141 if (!len)
142 break;
143
143 - if (len > 4 && starts_with(buffer, "ERR "))
144 - die("remote error: %s", buffer + 4);
144 + if (len > 4 && skip_prefix(buffer, "ERR ", &arg))
145 + die("remote error: %s", arg);
146
146 - if (len == 48 && starts_with(buffer, "shallow ")) {
147 - if (get_sha1_hex(buffer + 8, old_sha1))
148 - die("protocol error: expected shallow sha-1, got '%s'", buffer + 8);
147 + if (len == 48 && skip_prefix(buffer, "shallow ", &arg)) {
148 + if (get_sha1_hex(arg, old_sha1))
149 + die("protocol error: expected shallow sha-1, got '%s'", arg);
150 if (!shallow_points)
151 die("repository on the other end cannot be shallow");
152 sha1_array_append(shallow_points, old_sha1);
convert.c
+2 -2
@@ -1121,9 +1121,9 @@ static int is_foreign_ident(const char *str)
1121 {
1122 int i;
1123
1124 - if (!starts_with(str, "$Id: "))
1124 + if (!skip_prefix(str, "$Id: ", &str))
1125 return 0;
1126 - for (i = 5; str[i]; i++) {
1126 + for (i = 0; str[i]; i++) {
1127 if (isspace(str[i]) && str[i+1] != '$')
1128 return 1;
1129 }
daemon.c
+38 -35
@@ -235,8 +235,10 @@ static int service_enabled;
235
236 static int git_daemon_config(const char *var, const char *value, void *cb)
237 {
238 - if (starts_with(var, "daemon.") &&
239 - !strcmp(var + 7, service_looking_at->config_name)) {
238 + const char *service;
239 +
240 + if (skip_prefix(var, "daemon.", &service) &&
241 + !strcmp(service, service_looking_at->config_name)) {
242 service_enabled = git_config_bool(var, value);
243 return 0;
244 }
@@ -1133,16 +1135,17 @@ int main(int argc, char **argv)
1135
1136 for (i = 1; i < argc; i++) {
1137 char *arg = argv[i];
1138 + const char *v;
1139
1137 - if (starts_with(arg, "--listen=")) {
1138 - string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
1140 + if (skip_prefix(arg, "--listen=", &v)) {
1141 + string_list_append(&listen_addr, xstrdup_tolower(v));
1142 continue;
1143 }
1141 - if (starts_with(arg, "--port=")) {
1144 + if (skip_prefix(arg, "--port=", &v)) {
1145 char *end;
1146 unsigned long n;
1144 - n = strtoul(arg+7, &end, 0);
1145 - if (arg[7] && !*end) {
1147 + n = strtoul(v, &end, 0);
1148 + if (*v && !*end) {
1149 listen_port = n;
1150 continue;
1151 }
@@ -1168,20 +1171,20 @@ int main(int argc, char **argv)
1171 export_all_trees = 1;
1172 continue;
1173 }
1171 - if (starts_with(arg, "--access-hook=")) {
1172 - access_hook = arg + 14;
1174 + if (skip_prefix(arg, "--access-hook=", &v)) {
1175 + access_hook = v;
1176 continue;
1177 }
1175 - if (starts_with(arg, "--timeout=")) {
1176 - timeout = atoi(arg+10);
1178 + if (skip_prefix(arg, "--timeout=", &v)) {
1179 + timeout = atoi(v);
1180 continue;
1181 }
1179 - if (starts_with(arg, "--init-timeout=")) {
1180 - init_timeout = atoi(arg+15);
1182 + if (skip_prefix(arg, "--init-timeout=", &v)) {
1183 + init_timeout = atoi(v);
1184 continue;
1185 }
1183 - if (starts_with(arg, "--max-connections=")) {
1184 - max_connections = atoi(arg+18);
1186 + if (skip_prefix(arg, "--max-connections=", &v)) {
1187 + max_connections = atoi(v);
1188 if (max_connections < 0)
1189 max_connections = 0; /* unlimited */
1190 continue;
@@ -1190,16 +1193,16 @@ int main(int argc, char **argv)
1193 strict_paths = 1;
1194 continue;
1195 }
1193 - if (starts_with(arg, "--base-path=")) {
1194 - base_path = arg+12;
1196 + if (skip_prefix(arg, "--base-path=", &v)) {
1197 + base_path = v;
1198 continue;
1199 }
1200 if (!strcmp(arg, "--base-path-relaxed")) {
1201 base_path_relaxed = 1;
1202 continue;
1203 }
1201 - if (starts_with(arg, "--interpolated-path=")) {
1202 - interpolated_path = arg+20;
1204 + if (skip_prefix(arg, "--interpolated-path=", &v)) {
1205 + interpolated_path = v;
1206 continue;
1207 }
1208 if (!strcmp(arg, "--reuseaddr")) {
@@ -1210,12 +1213,12 @@ int main(int argc, char **argv)
1213 user_path = "";
1214 continue;
1215 }
1213 - if (starts_with(arg, "--user-path=")) {
1214 - user_path = arg + 12;
1216 + if (skip_prefix(arg, "--user-path=", &v)) {
1217 + user_path = v;
1218 continue;
1219 }
1217 - if (starts_with(arg, "--pid-file=")) {
1218 - pid_file = arg + 11;
1220 + if (skip_prefix(arg, "--pid-file=", &v)) {
1221 + pid_file = v;
1222 continue;
1223 }
1224 if (!strcmp(arg, "--detach")) {
@@ -1223,28 +1226,28 @@ int main(int argc, char **argv)
1226 log_syslog = 1;
1227 continue;
1228 }
1226 - if (starts_with(arg, "--user=")) {
1227 - user_name = arg + 7;
1229 + if (skip_prefix(arg, "--user=", &v)) {
1230 + user_name = v;
1231 continue;
1232 }
1230 - if (starts_with(arg, "--group=")) {
1231 - group_name = arg + 8;
1233 + if (skip_prefix(arg, "--group=", &v)) {
1234 + group_name = v;
1235 continue;
1236 }
1234 - if (starts_with(arg, "--enable=")) {
1235 - enable_service(arg + 9, 1);
1237 + if (skip_prefix(arg, "--enable=", &v)) {
1238 + enable_service(v, 1);
1239 continue;
1240 }
1238 - if (starts_with(arg, "--disable=")) {
1239 - enable_service(arg + 10, 0);
1241 + if (skip_prefix(arg, "--disable=", &v)) {
1242 + enable_service(v, 0);
1243 continue;
1244 }
1242 - if (starts_with(arg, "--allow-override=")) {
1243 - make_service_overridable(arg + 17, 1);
1245 + if (skip_prefix(arg, "--allow-override=", &v)) {
1246 + make_service_overridable(v, 1);
1247 continue;
1248 }
1246 - if (starts_with(arg, "--forbid-override=")) {
1247 - make_service_overridable(arg + 18, 0);
1249 + if (skip_prefix(arg, "--forbid-override=", &v)) {
1250 + make_service_overridable(v, 0);
1251 continue;
1252 }
1253 if (!strcmp(arg, "--informative-errors")) {
diff.c
+34 -31
@@ -231,6 +231,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
231
232 int git_diff_basic_config(const char *var, const char *value, void *cb)
233 {
234 + const char *name;
235 +
236 if (!strcmp(var, "diff.renamelimit")) {
237 diff_rename_limit_default = git_config_int(var, value);
238 return 0;
@@ -239,8 +241,9 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
241 if (userdiff_config(var, value) < 0)
242 return -1;
243
242 - if (starts_with(var, "diff.color.") || starts_with(var, "color.diff.")) {
243 - int slot = parse_diff_color_slot(var + 11);
244 + if (skip_prefix(var, "diff.color.", &name) ||
245 + skip_prefix(var, "color.diff.", &name)) {
246 + int slot = parse_diff_color_slot(name);
247 if (slot < 0)
248 return 0;
249 if (!value)
@@ -2341,6 +2344,7 @@ static void builtin_diff(const char *name_a,
2344 } else {
2345 /* Crazy xdl interfaces.. */
2346 const char *diffopts = getenv("GIT_DIFF_OPTS");
2347 + const char *v;
2348 xpparam_t xpp;
2349 xdemitconf_t xecfg;
2350 struct emit_callback ecbdata;
@@ -2379,10 +2383,10 @@ static void builtin_diff(const char *name_a,
2383 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
2384 if (!diffopts)
2385 ;
2382 - else if (starts_with(diffopts, "--unified="))
2383 - xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
2384 - else if (starts_with(diffopts, "-u"))
2385 - xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
2386 + else if (skip_prefix(diffopts, "--unified=", &v))
2387 + xecfg.ctxlen = strtoul(v, NULL, 10);
2388 + else if (skip_prefix(diffopts, "-u", &v))
2389 + xecfg.ctxlen = strtoul(v, NULL, 10);
2390 if (o->word_diff)
2391 init_diff_words_data(&ecbdata, o, one, two);
2392 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)
3613 options->output_format |= DIFF_FORMAT_SHORTSTAT;
3614 else if (!strcmp(arg, "-X") || !strcmp(arg, "--dirstat"))
3615 return parse_dirstat_opt(options, "");
3612 - else if (starts_with(arg, "-X"))
3613 - return parse_dirstat_opt(options, arg + 2);
3614 - else if (starts_with(arg, "--dirstat="))
3615 - return parse_dirstat_opt(options, arg + 10);
3616 + else if (skip_prefix(arg, "-X", &arg))
3617 + return parse_dirstat_opt(options, arg);
3618 + else if (skip_prefix(arg, "--dirstat=", &arg))
3619 + return parse_dirstat_opt(options, arg);
3620 else if (!strcmp(arg, "--cumulative"))
3621 return parse_dirstat_opt(options, "cumulative");
3622 else if (!strcmp(arg, "--dirstat-by-file"))
3623 return parse_dirstat_opt(options, "files");
3620 - else if (starts_with(arg, "--dirstat-by-file=")) {
3624 + else if (skip_prefix(arg, "--dirstat-by-file=", &arg)) {
3625 parse_dirstat_opt(options, "files");
3622 - return parse_dirstat_opt(options, arg + 18);
3626 + return parse_dirstat_opt(options, arg);
3627 }
3628 else if (!strcmp(arg, "--check"))
3629 options->output_format |= DIFF_FORMAT_CHECKDIFF;
@@ -3669,9 +3673,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3673 DIFF_OPT_CLR(options, RENAME_EMPTY);
3674 else if (!strcmp(arg, "--relative"))
3675 DIFF_OPT_SET(options, RELATIVE_NAME);
3672 - else if (starts_with(arg, "--relative=")) {
3676 + else if (skip_prefix(arg, "--relative=", &arg)) {
3677 DIFF_OPT_SET(options, RELATIVE_NAME);
3674 - options->prefix = arg + 11;
3678 + options->prefix = arg;
3679 }
3680
3681 /* xdiff options */
@@ -3722,8 +3726,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3726 DIFF_OPT_CLR(options, FOLLOW_RENAMES);
3727 else if (!strcmp(arg, "--color"))
3728 options->use_color = 1;
3725 - else if (starts_with(arg, "--color=")) {
3726 - int value = git_config_colorbool(NULL, arg+8);
3729 + else if (skip_prefix(arg, "--color=", &arg)) {
3730 + int value = git_config_colorbool(NULL, arg);
3731 if (value < 0)
3732 return error("option `color' expects \"always\", \"auto\", or \"never\"");
3733 options->use_color = value;
@@ -3734,29 +3738,28 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3738 options->use_color = 1;
3739 options->word_diff = DIFF_WORDS_COLOR;
3740 }
3737 - else if (starts_with(arg, "--color-words=")) {
3741 + else if (skip_prefix(arg, "--color-words=", &arg)) {
3742 options->use_color = 1;
3743 options->word_diff = DIFF_WORDS_COLOR;
3740 - options->word_regex = arg + 14;
3744 + options->word_regex = arg;
3745 }
3746 else if (!strcmp(arg, "--word-diff")) {
3747 if (options->word_diff == DIFF_WORDS_NONE)
3748 options->word_diff = DIFF_WORDS_PLAIN;
3749 }
3746 - else if (starts_with(arg, "--word-diff=")) {
3747 - const char *type = arg + 12;
3748 - if (!strcmp(type, "plain"))
3750 + else if (skip_prefix(arg, "--word-diff=", &arg)) {
3751 + if (!strcmp(arg, "plain"))
3752 options->word_diff = DIFF_WORDS_PLAIN;
3750 - else if (!strcmp(type, "color")) {
3753 + else if (!strcmp(arg, "color")) {
3754 options->use_color = 1;
3755 options->word_diff = DIFF_WORDS_COLOR;
3756 }
3754 - else if (!strcmp(type, "porcelain"))
3757 + else if (!strcmp(arg, "porcelain"))
3758 options->word_diff = DIFF_WORDS_PORCELAIN;
3756 - else if (!strcmp(type, "none"))
3759 + else if (!strcmp(arg, "none"))
3760 options->word_diff = DIFF_WORDS_NONE;
3761 else
3759 - die("bad --word-diff argument: %s", type);
3762 + die("bad --word-diff argument: %s", arg);
3763 }
3764 else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
3765 if (options->word_diff == DIFF_WORDS_NONE)
@@ -3779,13 +3782,13 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3782 else if (!strcmp(arg, "--ignore-submodules")) {
3783 DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
3784 handle_ignore_submodules_arg(options, "all");
3782 - } else if (starts_with(arg, "--ignore-submodules=")) {
3785 + } else if (skip_prefix(arg, "--ignore-submodules=", &arg)) {
3786 DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
3784 - handle_ignore_submodules_arg(options, arg + 20);
3787 + handle_ignore_submodules_arg(options, arg);
3788 } else if (!strcmp(arg, "--submodule"))
3789 DIFF_OPT_SET(options, SUBMODULE_LOG);
3787 - else if (starts_with(arg, "--submodule="))
3788 - return parse_submodule_opt(options, arg + 12);
3790 + else if (skip_prefix(arg, "--submodule=", &arg))
3791 + return parse_submodule_opt(options, arg);
3792
3793 /* misc options */
3794 else if (!strcmp(arg, "-z"))
@@ -3820,8 +3823,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3823 }
3824 else if (!strcmp(arg, "--abbrev"))
3825 options->abbrev = DEFAULT_ABBREV;
3823 - else if (starts_with(arg, "--abbrev=")) {
3824 - options->abbrev = strtoul(arg + 9, NULL, 10);
3826 + else if (skip_prefix(arg, "--abbrev=", &arg)) {
3827 + options->abbrev = strtoul(arg, NULL, 10);
3828 if (options->abbrev < MINIMUM_ABBREV)
3829 options->abbrev = MINIMUM_ABBREV;
3830 else if (40 < options->abbrev)
fast-import.c
+37 -32
@@ -1912,8 +1912,9 @@ static void skip_optional_lf(void)
1912
1913 static void parse_mark(void)
1914 {
1915 - if (starts_with(command_buf.buf, "mark :")) {
1916 - next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1915 + const char *v;
1916 + if (skip_prefix(command_buf.buf, "mark :", &v)) {
1917 + next_mark = strtoumax(v, NULL, 10);
1918 read_next_command();
1919 }
1920 else
@@ -1922,14 +1923,15 @@ static void parse_mark(void)
1923
1924 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
1925 {
1926 + const char *data;
1927 strbuf_reset(sb);
1928
1927 - if (!starts_with(command_buf.buf, "data "))
1929 + if (!skip_prefix(command_buf.buf, "data ", &data))
1930 die("Expected 'data n' command, found: %s", command_buf.buf);
1931
1930 - if (starts_with(command_buf.buf + 5, "<<")) {
1931 - char *term = xstrdup(command_buf.buf + 5 + 2);
1932 - size_t term_len = command_buf.len - 5 - 2;
1932 + if (skip_prefix(data, "<<", &data)) {
1933 + char *term = xstrdup(data);
1934 + size_t term_len = command_buf.len - (data - command_buf.buf);
1935
1936 strbuf_detach(&command_buf, NULL);
1937 for (;;) {
@@ -1944,7 +1946,7 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
1946 free(term);
1947 }
1948 else {
1947 - uintmax_t len = strtoumax(command_buf.buf + 5, NULL, 10);
1949 + uintmax_t len = strtoumax(data, NULL, 10);
1950 size_t n = 0, length = (size_t)len;
1951
1952 if (limit && limit < len) {
@@ -2676,6 +2678,7 @@ static void parse_new_commit(void)
2678 struct hash_list *merge_list = NULL;
2679 unsigned int merge_count;
2680 unsigned char prev_fanout, new_fanout;
2681 + const char *v;
2682
2683 /* Obtain the branch name from the rest of our command */
2684 sp = strchr(command_buf.buf, ' ') + 1;
@@ -2685,12 +2688,12 @@ static void parse_new_commit(void)
2688
2689 read_next_command();
2690 parse_mark();
2688 - if (starts_with(command_buf.buf, "author ")) {
2689 - author = parse_ident(command_buf.buf + 7);
2691 + if (skip_prefix(command_buf.buf, "author ", &v)) {
2692 + author = parse_ident(v);
2693 read_next_command();
2694 }
2692 - if (starts_with(command_buf.buf, "committer ")) {
2693 - committer = parse_ident(command_buf.buf + 10);
2695 + if (skip_prefix(command_buf.buf, "committer ", &v)) {
2696 + committer = parse_ident(v);
2697 read_next_command();
2698 }
2699 if (!committer)
@@ -2777,6 +2780,7 @@ static void parse_new_tag(void)
2780 uintmax_t from_mark = 0;
2781 unsigned char sha1[20];
2782 enum object_type type;
2783 + const char *v;
2784
2785 /* Obtain the new tag name from the rest of our command */
2786 sp = strchr(command_buf.buf, ' ') + 1;
@@ -2819,8 +2823,8 @@ static void parse_new_tag(void)
2823 read_next_command();
2824
2825 /* tagger ... */
2822 - if (starts_with(command_buf.buf, "tagger ")) {
2823 - tagger = parse_ident(command_buf.buf + 7);
2826 + if (skip_prefix(command_buf.buf, "tagger ", &v)) {
2827 + tagger = parse_ident(v);
2828 read_next_command();
2829 } else
2830 tagger = NULL;
@@ -3207,9 +3211,9 @@ static void option_export_pack_edges(const char *edges)
3211
3212 static int parse_one_option(const char *option)
3213 {
3210 - if (starts_with(option, "max-pack-size=")) {
3214 + if (skip_prefix(option, "max-pack-size=", &option)) {
3215 unsigned long v;
3212 - if (!git_parse_ulong(option + 14, &v))
3216 + if (!git_parse_ulong(option, &v))
3217 return 0;
3218 if (v < 8192) {
3219 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)
3223 v = 1024 * 1024;
3224 }
3225 max_packsize = v;
3222 - } else if (starts_with(option, "big-file-threshold=")) {
3226 + } else if (skip_prefix(option, "big-file-threshold=", &option)) {
3227 unsigned long v;
3224 - if (!git_parse_ulong(option + 19, &v))
3228 + if (!git_parse_ulong(option, &v))
3229 return 0;
3230 big_file_threshold = v;
3227 - } else if (starts_with(option, "depth=")) {
3228 - option_depth(option + 6);
3229 - } else if (starts_with(option, "active-branches=")) {
3230 - option_active_branches(option + 16);
3231 - } else if (starts_with(option, "export-pack-edges=")) {
3232 - option_export_pack_edges(option + 18);
3231 + } else if (skip_prefix(option, "depth=", &option)) {
3232 + option_depth(option);
3233 + } else if (skip_prefix(option, "active-branches=", &option)) {
3234 + option_active_branches(option);
3235 + } else if (skip_prefix(option, "export-pack-edges=", &option)) {
3236 + option_export_pack_edges(option);
3237 } else if (starts_with(option, "quiet")) {
3238 show_stats = 0;
3239 } else if (starts_with(option, "stats")) {
@@ -3243,15 +3247,16 @@ static int parse_one_option(const char *option)
3247
3248 static int parse_one_feature(const char *feature, int from_stream)
3249 {
3246 - if (starts_with(feature, "date-format=")) {
3247 - option_date_format(feature + 12);
3248 - } else if (starts_with(feature, "import-marks=")) {
3249 - option_import_marks(feature + 13, from_stream, 0);
3250 - } else if (starts_with(feature, "import-marks-if-exists=")) {
3251 - option_import_marks(feature + strlen("import-marks-if-exists="),
3252 - from_stream, 1);
3253 - } else if (starts_with(feature, "export-marks=")) {
3254 - option_export_marks(feature + 13);
3250 + const char *arg;
3251 +
3252 + if (skip_prefix(feature, "date-format=", &arg)) {
3253 + option_date_format(arg);
3254 + } else if (skip_prefix(feature, "import-marks=", &arg)) {
3255 + option_import_marks(arg, from_stream, 0);
3256 + } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
3257 + option_import_marks(arg, from_stream, 1);
3258 + } else if (skip_prefix(feature, "export-marks=", &arg)) {
3259 + option_export_marks(arg);
3260 } else if (!strcmp(feature, "cat-blob")) {
3261 ; /* Don't die - this feature is supported */
3262 } else if (!strcmp(feature, "relative-marks")) {
fetch-pack.c
+5 -4
@@ -319,18 +319,19 @@ static int find_common(struct fetch_pack_args *args,
319
320 if (args->depth > 0) {
321 char *line;
322 + const char *arg;
323 unsigned char sha1[20];
324
325 send_request(args, fd[1], &req_buf);
326 while ((line = packet_read_line(fd[0], NULL))) {
326 - if (starts_with(line, "shallow ")) {
327 - if (get_sha1_hex(line + 8, sha1))
327 + if (skip_prefix(line, "shallow ", &arg)) {
328 + if (get_sha1_hex(arg, sha1))
329 die("invalid shallow line: %s", line);
330 register_shallow(sha1);
331 continue;
332 }
332 - if (starts_with(line, "unshallow ")) {
333 - if (get_sha1_hex(line + 10, sha1))
333 + if (skip_prefix(line, "unshallow ", &arg)) {
334 + if (get_sha1_hex(arg, sha1))
335 die("invalid unshallow line: %s", line);
336 if (!lookup_object(sha1))
337 die("object not found: %s", line);
git.c
+8 -10
@@ -54,8 +54,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
54 /*
55 * Check remaining flags.
56 */
57 - if (starts_with(cmd, "--exec-path")) {
58 - cmd += 11;
57 + if (skip_prefix(cmd, "--exec-path", &cmd)) {
58 if (*cmd == '=')
59 git_set_argv_exec_path(cmd + 1);
60 else {
@@ -92,8 +91,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
91 *envchanged = 1;
92 (*argv)++;
93 (*argc)--;
95 - } else if (starts_with(cmd, "--git-dir=")) {
96 - setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
94 + } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
95 + setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
96 if (envchanged)
97 *envchanged = 1;
98 } else if (!strcmp(cmd, "--namespace")) {
@@ -106,8 +105,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
105 *envchanged = 1;
106 (*argv)++;
107 (*argc)--;
109 - } else if (starts_with(cmd, "--namespace=")) {
110 - setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1);
108 + } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
109 + setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
110 if (envchanged)
111 *envchanged = 1;
112 } else if (!strcmp(cmd, "--work-tree")) {
@@ -120,8 +119,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
119 *envchanged = 1;
120 (*argv)++;
121 (*argc)--;
123 - } else if (starts_with(cmd, "--work-tree=")) {
124 - setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
122 + } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
123 + setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
124 if (envchanged)
125 *envchanged = 1;
126 } else if (!strcmp(cmd, "--bare")) {
@@ -578,8 +577,7 @@ int main(int argc, char **av)
577 * So we just directly call the builtin handler, and die if
578 * that one cannot handle it.
579 */
581 - if (starts_with(cmd, "git-")) {
582 - cmd += 4;
580 + if (skip_prefix(cmd, "git-", &cmd)) {
581 argv[0] = cmd;
582 handle_builtin(argc, argv);
583 die("cannot handle %s as a builtin", cmd);
help.c
+4 -2
@@ -251,11 +251,13 @@ static struct cmdnames aliases;
251
252 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
253 {
254 + const char *p;
255 +
256 if (!strcmp(var, "help.autocorrect"))
257 autocorrect = git_config_int(var,value);
258 /* Also use aliases for command lookup */
257 - if (starts_with(var, "alias."))
258 - add_cmdname(&aliases, var + 6, strlen(var + 6));
259 + if (skip_prefix(var, "alias.", &p))
260 + add_cmdname(&aliases, p, strlen(p));
261
262 return git_default_config(var, value, cb);
263 }
http-backend.c
+7 -4
@@ -221,17 +221,19 @@ static void get_idx_file(char *name)
221
222 static int http_config(const char *var, const char *value, void *cb)
223 {
224 + const char *p;
225 +
226 if (!strcmp(var, "http.getanyfile")) {
227 getanyfile = git_config_bool(var, value);
228 return 0;
229 }
230
229 - if (starts_with(var, "http.")) {
231 + if (skip_prefix(var, "http.", &p)) {
232 int i;
233
234 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
235 struct rpc_service *svc = &rpc_service[i];
234 - if (!strcmp(var + 5, svc->config_name)) {
236 + if (!strcmp(p, svc->config_name)) {
237 svc->enabled = git_config_bool(var, value);
238 return 0;
239 }
@@ -244,15 +246,16 @@ static int http_config(const char *var, const char *value, void *cb)
246
247 static struct rpc_service *select_service(const char *name)
248 {
249 + const char *svc_name;
250 struct rpc_service *svc = NULL;
251 int i;
252
250 - if (!starts_with(name, "git-"))
253 + if (!skip_prefix(name, "git-", &svc_name))
254 forbidden("Unsupported service: '%s'", name);
255
256 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
257 struct rpc_service *s = &rpc_service[i];
255 - if (!strcmp(s->name, name + 4)) {
258 + if (!strcmp(s->name, svc_name)) {
259 svc = s;
260 break;
261 }
http-push.c
+6 -5
@@ -770,9 +770,9 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
770 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
771 strcpy(lock->owner, ctx->cdata);
772 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
773 - if (starts_with(ctx->cdata, "Second-"))
774 - lock->timeout =
775 - strtol(ctx->cdata + 7, NULL, 10);
773 + const char *arg;
774 + if (skip_prefix(ctx->cdata, "Second-", &arg))
775 + lock->timeout = strtol(arg, NULL, 10);
776 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
777 lock->token = xmalloc(strlen(ctx->cdata) + 1);
778 strcpy(lock->token, ctx->cdata);
@@ -1561,6 +1561,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1561 {
1562 char *url;
1563 struct strbuf buffer = STRBUF_INIT;
1564 + const char *name;
1565
1566 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1567 sprintf(url, "%s%s", repo->url, path);
@@ -1578,8 +1579,8 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1579 return;
1580
1581 /* If it's a symref, set the refname; otherwise try for a sha1 */
1581 - if (starts_with((char *)buffer.buf, "ref: ")) {
1582 - *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1582 + if (skip_prefix(buffer.buf, "ref: ", &name)) {
1583 + *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
1584 } else {
1585 get_sha1_hex(buffer.buf, sha1);
1586 }