Skip to content
This repository was archived by the owner on Jul 2, 2019. It is now read-only.

Commit 5955654

Browse files
chriscoolgitster
authored andcommitted
replace {pre,suf}fixcmp() with {starts,ends}_with()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9566231 commit 5955654

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+459
-459
lines changed

alias.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ static char *alias_val;
55

66
static int alias_lookup_cb(const char *k, const char *v, void *cb)
77
{
8-
if (!prefixcmp(k, "alias.") && !strcmp(k + 6, alias_key)) {
8+
if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) {
99
if (!v)
1010
return config_error_nonbool(k);
1111
alias_val = xstrdup(v);

attr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
211211
name = cp;
212212
namelen = strcspn(name, blank);
213213
if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
214-
!prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
214+
starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
215215
if (!macro_ok) {
216216
fprintf(stderr, "%s not allowed: %s:%d\n",
217217
name, src, lineno);

bisect.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ static int register_ref(const char *refname, const unsigned char *sha1,
406406
if (!strcmp(refname, "bad")) {
407407
current_bad_sha1 = xmalloc(20);
408408
hashcpy(current_bad_sha1, sha1);
409-
} else if (!prefixcmp(refname, "good-")) {
409+
} else if (starts_with(refname, "good-")) {
410410
sha1_array_append(&good_revs, sha1);
411-
} else if (!prefixcmp(refname, "skip-")) {
411+
} else if (starts_with(refname, "skip-")) {
412412
sha1_array_append(&skipped_revs, sha1);
413413
}
414414

branch.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int should_setup_rebase(const char *origin)
5050
void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
5151
{
5252
const char *shortname = remote + 11;
53-
int remote_is_branch = !prefixcmp(remote, "refs/heads/");
53+
int remote_is_branch = starts_with(remote, "refs/heads/");
5454
struct strbuf key = STRBUF_INIT;
5555
int rebasing = should_setup_rebase(origin);
5656

@@ -272,7 +272,7 @@ void create_branch(const char *head,
272272
break;
273273
case 1:
274274
/* Unique completion -- good, only if it is a real branch */
275-
if (prefixcmp(real_ref, "refs/heads/") &&
275+
if (!starts_with(real_ref, "refs/heads/") &&
276276
validate_remote_tracking_branch(real_ref)) {
277277
if (explicit_tracking)
278278
die(_(upstream_not_branch), start_name);

builtin/apply.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1409,10 +1409,10 @@ static void recount_diff(const char *line, int size, struct fragment *fragment)
14091409
case '\\':
14101410
continue;
14111411
case '@':
1412-
ret = size < 3 || prefixcmp(line, "@@ ");
1412+
ret = size < 3 || !starts_with(line, "@@ ");
14131413
break;
14141414
case 'd':
1415-
ret = size < 5 || prefixcmp(line, "diff ");
1415+
ret = size < 5 || !starts_with(line, "diff ");
14161416
break;
14171417
default:
14181418
ret = -1;
@@ -1798,11 +1798,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,
17981798

17991799
*status_p = 0;
18001800

1801-
if (!prefixcmp(buffer, "delta ")) {
1801+
if (starts_with(buffer, "delta ")) {
18021802
patch_method = BINARY_DELTA_DEFLATED;
18031803
origlen = strtoul(buffer + 6, NULL, 10);
18041804
}
1805-
else if (!prefixcmp(buffer, "literal ")) {
1805+
else if (starts_with(buffer, "literal ")) {
18061806
patch_method = BINARY_LITERAL_DEFLATED;
18071807
origlen = strtoul(buffer + 8, NULL, 10);
18081808
}
@@ -3627,12 +3627,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
36273627
hunk->oldpos == 1 && hunk->oldlines == 1 &&
36283628
/* does preimage begin with the heading? */
36293629
(preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3630-
!prefixcmp(++preimage, heading) &&
3630+
starts_with(++preimage, heading) &&
36313631
/* does it record full SHA-1? */
36323632
!get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
36333633
preimage[sizeof(heading) + 40 - 1] == '\n' &&
36343634
/* does the abbreviated name on the index line agree with it? */
3635-
!prefixcmp(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3635+
starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
36363636
return 0; /* it all looks fine */
36373637

36383638
/* we may have full object name on the index line */

builtin/archive.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ static int run_remote_archiver(int argc, const char **argv,
5757
if (!buf)
5858
die(_("git archive: expected ACK/NAK, got EOF"));
5959
if (strcmp(buf, "ACK")) {
60-
if (!prefixcmp(buf, "NACK "))
60+
if (starts_with(buf, "NACK "))
6161
die(_("git archive: NACK %s"), buf + 5);
62-
if (!prefixcmp(buf, "ERR "))
62+
if (starts_with(buf, "ERR "))
6363
die(_("remote error: %s"), buf + 4);
6464
die(_("git archive: protocol error"));
6565
}

builtin/branch.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ static int parse_branch_color_slot(const char *var, int ofs)
8181

8282
static int git_branch_config(const char *var, const char *value, void *cb)
8383
{
84-
if (!prefixcmp(var, "column."))
84+
if (starts_with(var, "column."))
8585
return git_column_config(var, value, "branch", &colopts);
8686
if (!strcmp(var, "color.branch")) {
8787
branch_use_color = git_config_colorbool(var, value);
8888
return 0;
8989
}
90-
if (!prefixcmp(var, "color.branch.")) {
90+
if (starts_with(var, "color.branch.")) {
9191
int slot = parse_branch_color_slot(var, 13);
9292
if (slot < 0)
9393
return 0;
@@ -868,7 +868,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
868868
if (!strcmp(head, "HEAD")) {
869869
detached = 1;
870870
} else {
871-
if (prefixcmp(head, "refs/heads/"))
871+
if (!starts_with(head, "refs/heads/"))
872872
die(_("HEAD not found below refs/heads!"));
873873
head += 11;
874874
}

builtin/checkout.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ static int switch_branches(const struct checkout_opts *opts,
781781
if (!(flag & REF_ISSYMREF))
782782
old.path = NULL;
783783

784-
if (old.path && !prefixcmp(old.path, "refs/heads/"))
784+
if (old.path && starts_with(old.path, "refs/heads/"))
785785
old.name = old.path + strlen("refs/heads/");
786786

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

819-
if (!prefixcmp(var, "submodule."))
819+
if (starts_with(var, "submodule."))
820820
return parse_submodule_config_option(var, value);
821821

822822
return git_xmerge_config(var, value, NULL);
@@ -1151,9 +1151,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
11511151
const char *argv0 = argv[0];
11521152
if (!argc || !strcmp(argv0, "--"))
11531153
die (_("--track needs a branch name"));
1154-
if (!prefixcmp(argv0, "refs/"))
1154+
if (starts_with(argv0, "refs/"))
11551155
argv0 += 5;
1156-
if (!prefixcmp(argv0, "remotes/"))
1156+
if (starts_with(argv0, "remotes/"))
11571157
argv0 += 8;
11581158
argv0 = strchr(argv0, '/');
11591159
if (!argv0 || !argv0[1])

builtin/clean.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int parse_clean_color_slot(const char *var)
100100

101101
static int git_clean_config(const char *var, const char *value, void *cb)
102102
{
103-
if (!prefixcmp(var, "column."))
103+
if (starts_with(var, "column."))
104104
return git_column_config(var, value, "clean", &colopts);
105105

106106
/* honors the color.interactive* config variables which also
@@ -109,7 +109,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
109109
clean_use_color = git_config_colorbool(var, value);
110110
return 0;
111111
}
112-
if (!prefixcmp(var, "color.interactive.")) {
112+
if (starts_with(var, "color.interactive.")) {
113113
int slot = parse_clean_color_slot(var +
114114
strlen("color.interactive."));
115115
if (slot < 0)

builtin/clone.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ static void write_followtags(const struct ref *refs, const char *msg)
508508
{
509509
const struct ref *ref;
510510
for (ref = refs; ref; ref = ref->next) {
511-
if (prefixcmp(ref->name, "refs/tags/"))
511+
if (!starts_with(ref->name, "refs/tags/"))
512512
continue;
513-
if (!suffixcmp(ref->name, "^{}"))
513+
if (ends_with(ref->name, "^{}"))
514514
continue;
515515
if (!has_sha1_file(ref->old_sha1))
516516
continue;
@@ -578,7 +578,7 @@ static void update_remote_refs(const struct ref *refs,
578578
static void update_head(const struct ref *our, const struct ref *remote,
579579
const char *msg)
580580
{
581-
if (our && !prefixcmp(our->name, "refs/heads/")) {
581+
if (our && starts_with(our->name, "refs/heads/")) {
582582
/* Local default branch link */
583583
create_symref("HEAD", our->name, NULL);
584584
if (!option_bare) {
@@ -625,7 +625,7 @@ static int checkout(void)
625625
if (advice_detached_head)
626626
detach_advice(sha1_to_hex(sha1));
627627
} else {
628-
if (prefixcmp(head, "refs/heads/"))
628+
if (!starts_with(head, "refs/heads/"))
629629
die(_("HEAD not found below refs/heads!"));
630630
}
631631
free(head);

builtin/column.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
3434
};
3535

3636
/* This one is special and must be the first one */
37-
if (argc > 1 && !prefixcmp(argv[1], "--command=")) {
37+
if (argc > 1 && starts_with(argv[1], "--command=")) {
3838
command = argv[1] + 10;
3939
git_config(column_config, (void *)command);
4040
} else

builtin/commit.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
733733
eol = nl - sb.buf;
734734
else
735735
eol = sb.len;
736-
if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
736+
if (starts_with(sb.buf + previous, "\nConflicts:\n")) {
737737
ignore_footer = sb.len - previous;
738738
break;
739739
}
@@ -904,7 +904,7 @@ static int rest_is_empty(struct strbuf *sb, int start)
904904
eol = sb->len;
905905

906906
if (strlen(sign_off_header) <= eol - i &&
907-
!prefixcmp(sb->buf + i, sign_off_header)) {
907+
starts_with(sb->buf + i, sign_off_header)) {
908908
i = eol;
909909
continue;
910910
}
@@ -1183,7 +1183,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
11831183
{
11841184
struct wt_status *s = cb;
11851185

1186-
if (!prefixcmp(k, "column."))
1186+
if (starts_with(k, "column."))
11871187
return git_column_config(k, v, "status", &s->colopts);
11881188
if (!strcmp(k, "status.submodulesummary")) {
11891189
int is_bool;
@@ -1211,7 +1211,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
12111211
s->display_comment_prefix = git_config_bool(k, v);
12121212
return 0;
12131213
}
1214-
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1214+
if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) {
12151215
int slot = parse_status_slot(k, 13);
12161216
if (slot < 0)
12171217
return 0;
@@ -1377,7 +1377,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13771377

13781378
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
13791379
printf("[%s%s ",
1380-
!prefixcmp(head, "refs/heads/") ?
1380+
starts_with(head, "refs/heads/") ?
13811381
head + 11 :
13821382
!strcmp(head, "HEAD") ?
13831383
_("detached HEAD") :

builtin/describe.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void add_to_known_names(const char *path,
141141

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

builtin/fast-export.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static void handle_tag(const char *name, struct tag *tag)
476476
}
477477
}
478478

479-
if (!prefixcmp(name, "refs/tags/"))
479+
if (starts_with(name, "refs/tags/"))
480480
name += 10;
481481
printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
482482
name, tagged_mark,

builtin/fetch-pack.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
4848
for (i = 1; i < argc && *argv[i] == '-'; i++) {
4949
const char *arg = argv[i];
5050

51-
if (!prefixcmp(arg, "--upload-pack=")) {
51+
if (starts_with(arg, "--upload-pack=")) {
5252
args.uploadpack = arg + 14;
5353
continue;
5454
}
55-
if (!prefixcmp(arg, "--exec=")) {
55+
if (starts_with(arg, "--exec=")) {
5656
args.uploadpack = arg + 7;
5757
continue;
5858
}
@@ -85,7 +85,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
8585
args.verbose = 1;
8686
continue;
8787
}
88-
if (!prefixcmp(arg, "--depth=")) {
88+
if (starts_with(arg, "--depth=")) {
8989
args.depth = strtol(arg + 8, NULL, 0);
9090
continue;
9191
}

builtin/fetch.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static int update_local_ref(struct ref *ref,
313313
}
314314

315315
if (!is_null_sha1(ref->old_sha1) &&
316-
!prefixcmp(ref->name, "refs/tags/")) {
316+
starts_with(ref->name, "refs/tags/")) {
317317
int r;
318318
r = s_update_ref("updating tag", ref, 0);
319319
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
@@ -336,10 +336,10 @@ static int update_local_ref(struct ref *ref,
336336
* more likely to follow a standard layout.
337337
*/
338338
const char *name = remote_ref ? remote_ref->name : "";
339-
if (!prefixcmp(name, "refs/tags/")) {
339+
if (starts_with(name, "refs/tags/")) {
340340
msg = "storing tag";
341341
what = _("[new tag]");
342-
} else if (!prefixcmp(name, "refs/heads/")) {
342+
} else if (starts_with(name, "refs/heads/")) {
343343
msg = "storing head";
344344
what = _("[new branch]");
345345
} else {
@@ -471,15 +471,15 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
471471
kind = "";
472472
what = "";
473473
}
474-
else if (!prefixcmp(rm->name, "refs/heads/")) {
474+
else if (starts_with(rm->name, "refs/heads/")) {
475475
kind = "branch";
476476
what = rm->name + 11;
477477
}
478-
else if (!prefixcmp(rm->name, "refs/tags/")) {
478+
else if (starts_with(rm->name, "refs/tags/")) {
479479
kind = "tag";
480480
what = rm->name + 10;
481481
}
482-
else if (!prefixcmp(rm->name, "refs/remotes/")) {
482+
else if (starts_with(rm->name, "refs/remotes/")) {
483483
kind = "remote-tracking branch";
484484
what = rm->name + 13;
485485
}
@@ -644,7 +644,7 @@ static void find_non_local_tags(struct transport *transport,
644644

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

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

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

0 commit comments

Comments
 (0)