Skip to content

Commit

Permalink
replace {pre,suf}fixcmp() with {starts,ends}_with()
Browse files Browse the repository at this point in the history
Leaving only the function definitions and declarations so that any
new topic in flight can still make use of the old functions, replace
existing uses of the prefixcmp() and suffixcmp() with new API
functions.

The change can be recreated by mechanically applying this:

    $ git grep -l -e prefixcmp -e suffixcmp -- \*.c |
      grep -v strbuf\\.c |
      xargs perl -pi -e '
        s|!prefixcmp\(|starts_with\(|g;
        s|prefixcmp\(|!starts_with\(|g;
        s|!suffixcmp\(|ends_with\(|g;
        s|suffixcmp\(|!ends_with\(|g;
      '

on the result of preparatory changes in this series.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
chriscool authored and gitster committed Dec 5, 2013
1 parent 9566231 commit 5955654
Show file tree
Hide file tree
Showing 88 changed files with 459 additions and 459 deletions.
2 changes: 1 addition & 1 deletion alias.c
Expand Up @@ -5,7 +5,7 @@ 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 (!prefixcmp(k, "alias.") && !strcmp(k + 6, alias_key)) { if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) {
if (!v) if (!v)
return config_error_nonbool(k); return config_error_nonbool(k);
alias_val = xstrdup(v); alias_val = xstrdup(v);
Expand Down
2 changes: 1 addition & 1 deletion attr.c
Expand Up @@ -211,7 +211,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
name = cp; name = cp;
namelen = strcspn(name, blank); namelen = strcspn(name, blank);
if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
!prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) { starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
if (!macro_ok) { if (!macro_ok) {
fprintf(stderr, "%s not allowed: %s:%d\n", fprintf(stderr, "%s not allowed: %s:%d\n",
name, src, lineno); name, src, lineno);
Expand Down
4 changes: 2 additions & 2 deletions bisect.c
Expand Up @@ -406,9 +406,9 @@ static int register_ref(const char *refname, const unsigned char *sha1,
if (!strcmp(refname, "bad")) { if (!strcmp(refname, "bad")) {
current_bad_sha1 = xmalloc(20); current_bad_sha1 = xmalloc(20);
hashcpy(current_bad_sha1, sha1); hashcpy(current_bad_sha1, sha1);
} else if (!prefixcmp(refname, "good-")) { } else if (starts_with(refname, "good-")) {
sha1_array_append(&good_revs, sha1); sha1_array_append(&good_revs, sha1);
} else if (!prefixcmp(refname, "skip-")) { } else if (starts_with(refname, "skip-")) {
sha1_array_append(&skipped_revs, sha1); sha1_array_append(&skipped_revs, sha1);
} }


Expand Down
4 changes: 2 additions & 2 deletions branch.c
Expand Up @@ -50,7 +50,7 @@ static int should_setup_rebase(const char *origin)
void install_branch_config(int flag, const char *local, const char *origin, const char *remote) void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
{ {
const char *shortname = remote + 11; const char *shortname = remote + 11;
int remote_is_branch = !prefixcmp(remote, "refs/heads/"); int remote_is_branch = starts_with(remote, "refs/heads/");
struct strbuf key = STRBUF_INIT; struct strbuf key = STRBUF_INIT;
int rebasing = should_setup_rebase(origin); int rebasing = should_setup_rebase(origin);


Expand Down Expand Up @@ -272,7 +272,7 @@ void create_branch(const char *head,
break; break;
case 1: case 1:
/* Unique completion -- good, only if it is a real branch */ /* Unique completion -- good, only if it is a real branch */
if (prefixcmp(real_ref, "refs/heads/") && if (!starts_with(real_ref, "refs/heads/") &&
validate_remote_tracking_branch(real_ref)) { validate_remote_tracking_branch(real_ref)) {
if (explicit_tracking) if (explicit_tracking)
die(_(upstream_not_branch), start_name); die(_(upstream_not_branch), start_name);
Expand Down
12 changes: 6 additions & 6 deletions builtin/apply.c
Expand Up @@ -1409,10 +1409,10 @@ static void recount_diff(const char *line, int size, struct fragment *fragment)
case '\\': case '\\':
continue; continue;
case '@': case '@':
ret = size < 3 || prefixcmp(line, "@@ "); ret = size < 3 || !starts_with(line, "@@ ");
break; break;
case 'd': case 'd':
ret = size < 5 || prefixcmp(line, "diff "); ret = size < 5 || !starts_with(line, "diff ");
break; break;
default: default:
ret = -1; ret = -1;
Expand Down Expand Up @@ -1798,11 +1798,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,


*status_p = 0; *status_p = 0;


if (!prefixcmp(buffer, "delta ")) { if (starts_with(buffer, "delta ")) {
patch_method = BINARY_DELTA_DEFLATED; patch_method = BINARY_DELTA_DEFLATED;
origlen = strtoul(buffer + 6, NULL, 10); origlen = strtoul(buffer + 6, NULL, 10);
} }
else if (!prefixcmp(buffer, "literal ")) { else if (starts_with(buffer, "literal ")) {
patch_method = BINARY_LITERAL_DEFLATED; patch_method = BINARY_LITERAL_DEFLATED;
origlen = strtoul(buffer + 8, NULL, 10); origlen = strtoul(buffer + 8, NULL, 10);
} }
Expand Down Expand Up @@ -3627,12 +3627,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
hunk->oldpos == 1 && hunk->oldlines == 1 && hunk->oldpos == 1 && hunk->oldlines == 1 &&
/* does preimage begin with the heading? */ /* does preimage begin with the heading? */
(preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL && (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
!prefixcmp(++preimage, heading) && starts_with(++preimage, heading) &&
/* does it record full SHA-1? */ /* does it record full SHA-1? */
!get_sha1_hex(preimage + sizeof(heading) - 1, sha1) && !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
preimage[sizeof(heading) + 40 - 1] == '\n' && preimage[sizeof(heading) + 40 - 1] == '\n' &&
/* does the abbreviated name on the index line agree with it? */ /* does the abbreviated name on the index line agree with it? */
!prefixcmp(preimage + sizeof(heading) - 1, p->old_sha1_prefix)) starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
return 0; /* it all looks fine */ return 0; /* it all looks fine */


/* we may have full object name on the index line */ /* we may have full object name on the index line */
Expand Down
4 changes: 2 additions & 2 deletions builtin/archive.c
Expand Up @@ -57,9 +57,9 @@ static int run_remote_archiver(int argc, const char **argv,
if (!buf) if (!buf)
die(_("git archive: expected ACK/NAK, got EOF")); die(_("git archive: expected ACK/NAK, got EOF"));
if (strcmp(buf, "ACK")) { if (strcmp(buf, "ACK")) {
if (!prefixcmp(buf, "NACK ")) if (starts_with(buf, "NACK "))
die(_("git archive: NACK %s"), buf + 5); die(_("git archive: NACK %s"), buf + 5);
if (!prefixcmp(buf, "ERR ")) if (starts_with(buf, "ERR "))
die(_("remote error: %s"), buf + 4); die(_("remote error: %s"), buf + 4);
die(_("git archive: protocol error")); die(_("git archive: protocol error"));
} }
Expand Down
6 changes: 3 additions & 3 deletions builtin/branch.c
Expand Up @@ -81,13 +81,13 @@ 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)
{ {
if (!prefixcmp(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 (!prefixcmp(var, "color.branch.")) { if (starts_with(var, "color.branch.")) {
int slot = parse_branch_color_slot(var, 13); int slot = parse_branch_color_slot(var, 13);
if (slot < 0) if (slot < 0)
return 0; return 0;
Expand Down Expand Up @@ -868,7 +868,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
if (!strcmp(head, "HEAD")) { if (!strcmp(head, "HEAD")) {
detached = 1; detached = 1;
} else { } else {
if (prefixcmp(head, "refs/heads/")) if (!starts_with(head, "refs/heads/"))
die(_("HEAD not found below refs/heads!")); die(_("HEAD not found below refs/heads!"));
head += 11; head += 11;
} }
Expand Down
8 changes: 4 additions & 4 deletions builtin/checkout.c
Expand Up @@ -781,7 +781,7 @@ 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 && !prefixcmp(old.path, "refs/heads/")) if (old.path && starts_with(old.path, "refs/heads/"))
old.name = old.path + strlen("refs/heads/"); old.name = old.path + strlen("refs/heads/");


if (!new->name) { if (!new->name) {
Expand Down Expand Up @@ -816,7 +816,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
return 0; return 0;
} }


if (!prefixcmp(var, "submodule.")) if (starts_with(var, "submodule."))
return parse_submodule_config_option(var, value); return parse_submodule_config_option(var, value);


return git_xmerge_config(var, value, NULL); return git_xmerge_config(var, value, NULL);
Expand Down Expand Up @@ -1151,9 +1151,9 @@ 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 (!prefixcmp(argv0, "refs/")) if (starts_with(argv0, "refs/"))
argv0 += 5; argv0 += 5;
if (!prefixcmp(argv0, "remotes/")) if (starts_with(argv0, "remotes/"))
argv0 += 8; argv0 += 8;
argv0 = strchr(argv0, '/'); argv0 = strchr(argv0, '/');
if (!argv0 || !argv0[1]) if (!argv0 || !argv0[1])
Expand Down
4 changes: 2 additions & 2 deletions builtin/clean.c
Expand Up @@ -100,7 +100,7 @@ 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)
{ {
if (!prefixcmp(var, "column.")) if (starts_with(var, "column."))
return git_column_config(var, value, "clean", &colopts); return git_column_config(var, value, "clean", &colopts);


/* honors the color.interactive* config variables which also /* honors the color.interactive* config variables which also
Expand All @@ -109,7 +109,7 @@ 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 (!prefixcmp(var, "color.interactive.")) { if (starts_with(var, "color.interactive.")) {
int slot = parse_clean_color_slot(var + int slot = parse_clean_color_slot(var +
strlen("color.interactive.")); strlen("color.interactive."));
if (slot < 0) if (slot < 0)
Expand Down
8 changes: 4 additions & 4 deletions builtin/clone.c
Expand Up @@ -508,9 +508,9 @@ static void write_followtags(const struct ref *refs, const char *msg)
{ {
const struct ref *ref; const struct ref *ref;
for (ref = refs; ref; ref = ref->next) { for (ref = refs; ref; ref = ref->next) {
if (prefixcmp(ref->name, "refs/tags/")) if (!starts_with(ref->name, "refs/tags/"))
continue; continue;
if (!suffixcmp(ref->name, "^{}")) if (ends_with(ref->name, "^{}"))
continue; continue;
if (!has_sha1_file(ref->old_sha1)) if (!has_sha1_file(ref->old_sha1))
continue; continue;
Expand Down Expand Up @@ -578,7 +578,7 @@ static void update_remote_refs(const struct ref *refs,
static void update_head(const struct ref *our, const struct ref *remote, static void update_head(const struct ref *our, const struct ref *remote,
const char *msg) const char *msg)
{ {
if (our && !prefixcmp(our->name, "refs/heads/")) { if (our && starts_with(our->name, "refs/heads/")) {
/* Local default branch link */ /* Local default branch link */
create_symref("HEAD", our->name, NULL); create_symref("HEAD", our->name, NULL);
if (!option_bare) { if (!option_bare) {
Expand Down Expand Up @@ -625,7 +625,7 @@ static int checkout(void)
if (advice_detached_head) if (advice_detached_head)
detach_advice(sha1_to_hex(sha1)); detach_advice(sha1_to_hex(sha1));
} else { } else {
if (prefixcmp(head, "refs/heads/")) if (!starts_with(head, "refs/heads/"))
die(_("HEAD not found below refs/heads!")); die(_("HEAD not found below refs/heads!"));
} }
free(head); free(head);
Expand Down
2 changes: 1 addition & 1 deletion builtin/column.c
Expand Up @@ -34,7 +34,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
}; };


/* This one is special and must be the first one */ /* This one is special and must be the first one */
if (argc > 1 && !prefixcmp(argv[1], "--command=")) { if (argc > 1 && starts_with(argv[1], "--command=")) {
command = argv[1] + 10; command = argv[1] + 10;
git_config(column_config, (void *)command); git_config(column_config, (void *)command);
} else } else
Expand Down
10 changes: 5 additions & 5 deletions builtin/commit.c
Expand Up @@ -733,7 +733,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
eol = nl - sb.buf; eol = nl - sb.buf;
else else
eol = sb.len; eol = sb.len;
if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) { if (starts_with(sb.buf + previous, "\nConflicts:\n")) {
ignore_footer = sb.len - previous; ignore_footer = sb.len - previous;
break; break;
} }
Expand Down Expand Up @@ -904,7 +904,7 @@ static int rest_is_empty(struct strbuf *sb, int start)
eol = sb->len; eol = sb->len;


if (strlen(sign_off_header) <= eol - i && if (strlen(sign_off_header) <= eol - i &&
!prefixcmp(sb->buf + i, sign_off_header)) { starts_with(sb->buf + i, sign_off_header)) {
i = eol; i = eol;
continue; continue;
} }
Expand Down Expand Up @@ -1183,7 +1183,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
{ {
struct wt_status *s = cb; struct wt_status *s = cb;


if (!prefixcmp(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);
if (!strcmp(k, "status.submodulesummary")) { if (!strcmp(k, "status.submodulesummary")) {
int is_bool; int is_bool;
Expand Down Expand Up @@ -1211,7 +1211,7 @@ 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 (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) {
int slot = parse_status_slot(k, 13); int slot = parse_status_slot(k, 13);
if (slot < 0) if (slot < 0)
return 0; return 0;
Expand Down Expand Up @@ -1377,7 +1377,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,


head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL); head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
printf("[%s%s ", printf("[%s%s ",
!prefixcmp(head, "refs/heads/") ? starts_with(head, "refs/heads/") ?
head + 11 : head + 11 :
!strcmp(head, "HEAD") ? !strcmp(head, "HEAD") ?
_("detached HEAD") : _("detached HEAD") :
Expand Down
2 changes: 1 addition & 1 deletion builtin/describe.c
Expand Up @@ -141,7 +141,7 @@ static void add_to_known_names(const char *path,


static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{ {
int is_tag = !prefixcmp(path, "refs/tags/"); int is_tag = starts_with(path, "refs/tags/");
unsigned char peeled[20]; unsigned char peeled[20];
int is_annotated, prio; int is_annotated, prio;


Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Expand Up @@ -476,7 +476,7 @@ static void handle_tag(const char *name, struct tag *tag)
} }
} }


if (!prefixcmp(name, "refs/tags/")) if (starts_with(name, "refs/tags/"))
name += 10; name += 10;
printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n", printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
name, tagged_mark, name, tagged_mark,
Expand Down
6 changes: 3 additions & 3 deletions builtin/fetch-pack.c
Expand Up @@ -48,11 +48,11 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
for (i = 1; i < argc && *argv[i] == '-'; i++) { for (i = 1; i < argc && *argv[i] == '-'; i++) {
const char *arg = argv[i]; const char *arg = argv[i];


if (!prefixcmp(arg, "--upload-pack=")) { if (starts_with(arg, "--upload-pack=")) {
args.uploadpack = arg + 14; args.uploadpack = arg + 14;
continue; continue;
} }
if (!prefixcmp(arg, "--exec=")) { if (starts_with(arg, "--exec=")) {
args.uploadpack = arg + 7; args.uploadpack = arg + 7;
continue; continue;
} }
Expand Down Expand Up @@ -85,7 +85,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
args.verbose = 1; args.verbose = 1;
continue; continue;
} }
if (!prefixcmp(arg, "--depth=")) { if (starts_with(arg, "--depth=")) {
args.depth = strtol(arg + 8, NULL, 0); args.depth = strtol(arg + 8, NULL, 0);
continue; continue;
} }
Expand Down
18 changes: 9 additions & 9 deletions builtin/fetch.c
Expand Up @@ -313,7 +313,7 @@ static int update_local_ref(struct ref *ref,
} }


if (!is_null_sha1(ref->old_sha1) && if (!is_null_sha1(ref->old_sha1) &&
!prefixcmp(ref->name, "refs/tags/")) { starts_with(ref->name, "refs/tags/")) {
int r; int r;
r = s_update_ref("updating tag", ref, 0); r = s_update_ref("updating tag", ref, 0);
strbuf_addf(display, "%c %-*s %-*s -> %s%s", strbuf_addf(display, "%c %-*s %-*s -> %s%s",
Expand All @@ -336,10 +336,10 @@ static int update_local_ref(struct ref *ref,
* more likely to follow a standard layout. * more likely to follow a standard layout.
*/ */
const char *name = remote_ref ? remote_ref->name : ""; const char *name = remote_ref ? remote_ref->name : "";
if (!prefixcmp(name, "refs/tags/")) { if (starts_with(name, "refs/tags/")) {
msg = "storing tag"; msg = "storing tag";
what = _("[new tag]"); what = _("[new tag]");
} else if (!prefixcmp(name, "refs/heads/")) { } else if (starts_with(name, "refs/heads/")) {
msg = "storing head"; msg = "storing head";
what = _("[new branch]"); what = _("[new branch]");
} else { } else {
Expand Down Expand Up @@ -471,15 +471,15 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
kind = ""; kind = "";
what = ""; what = "";
} }
else if (!prefixcmp(rm->name, "refs/heads/")) { else if (starts_with(rm->name, "refs/heads/")) {
kind = "branch"; kind = "branch";
what = rm->name + 11; what = rm->name + 11;
} }
else if (!prefixcmp(rm->name, "refs/tags/")) { else if (starts_with(rm->name, "refs/tags/")) {
kind = "tag"; kind = "tag";
what = rm->name + 10; what = rm->name + 10;
} }
else if (!prefixcmp(rm->name, "refs/remotes/")) { else if (starts_with(rm->name, "refs/remotes/")) {
kind = "remote-tracking branch"; kind = "remote-tracking branch";
what = rm->name + 13; what = rm->name + 13;
} }
Expand Down Expand Up @@ -644,7 +644,7 @@ static void find_non_local_tags(struct transport *transport,


for_each_ref(add_existing, &existing_refs); for_each_ref(add_existing, &existing_refs);
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
if (prefixcmp(ref->name, "refs/tags/")) if (!starts_with(ref->name, "refs/tags/"))
continue; continue;


/* /*
Expand All @@ -653,7 +653,7 @@ static void find_non_local_tags(struct transport *transport,
* to fetch then we can mark the ref entry in the list * to fetch then we can mark the ref entry in the list
* as one to ignore by setting util to NULL. * as one to ignore by setting util to NULL.
*/ */
if (!suffixcmp(ref->name, "^{}")) { if (ends_with(ref->name, "^{}")) {
if (item && !has_sha1_file(ref->old_sha1) && if (item && !has_sha1_file(ref->old_sha1) &&
!will_fetch(head, ref->old_sha1) && !will_fetch(head, ref->old_sha1) &&
!has_sha1_file(item->util) && !has_sha1_file(item->util) &&
Expand Down Expand Up @@ -892,7 +892,7 @@ static int get_remote_group(const char *key, const char *value, void *priv)
{ {
struct remote_group_data *g = priv; struct remote_group_data *g = priv;


if (!prefixcmp(key, "remotes.") && if (starts_with(key, "remotes.") &&
!strcmp(key + 8, g->name)) { !strcmp(key + 8, g->name)) {
/* split list by white space */ /* split list by white space */
int space = strcspn(value, " \t\n"); int space = strcspn(value, " \t\n");
Expand Down

0 comments on commit 5955654

Please sign in to comment.