Skip to content

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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);

0 commit comments

Comments
 (0)