Skip to content

Commit

Permalink
Merge branch 'cc/starts-n-ends-with'
Browse files Browse the repository at this point in the history
Remove a few duplicate implementations of prefix/suffix comparison
functions, and rename them to starts_with and ends_with.

* cc/starts-n-ends-with:
  replace {pre,suf}fixcmp() with {starts,ends}_with()
  strbuf: introduce starts_with() and ends_with()
  builtin/remote: remove postfixcmp() and use suffixcmp() instead
  environment: normalize use of prefixcmp() by removing " != 0"
  • Loading branch information
Junio C Hamano committed Dec 17, 2013
2 parents 14a9c5f + 5955654 commit ad70448
Show file tree
Hide file tree
Showing 91 changed files with 484 additions and 481 deletions.
2 changes: 1 addition & 1 deletion alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ static char *alias_val;

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)
return config_error_nonbool(k);
alias_val = xstrdup(v);
Expand Down
2 changes: 1 addition & 1 deletion attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
name = cp;
namelen = strcspn(name, blank);
if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
!prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
if (!macro_ok) {
fprintf(stderr, "%s not allowed: %s:%d\n",
name, src, lineno);
Expand Down
4 changes: 2 additions & 2 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ static int register_ref(const char *refname, const unsigned char *sha1,
if (!strcmp(refname, "bad")) {
current_bad_sha1 = xmalloc(20);
hashcpy(current_bad_sha1, sha1);
} else if (!prefixcmp(refname, "good-")) {
} else if (starts_with(refname, "good-")) {
sha1_array_append(&good_revs, sha1);
} else if (!prefixcmp(refname, "skip-")) {
} else if (starts_with(refname, "skip-")) {
sha1_array_append(&skipped_revs, sha1);
}

Expand Down
4 changes: 2 additions & 2 deletions branch.c
Original file line number Diff line number Diff line change
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)
{
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;
int rebasing = should_setup_rebase(origin);

Expand Down Expand Up @@ -272,7 +272,7 @@ void create_branch(const char *head,
break;
case 1:
/* 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)) {
if (explicit_tracking)
die(_(upstream_not_branch), start_name);
Expand Down
12 changes: 6 additions & 6 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,10 +1409,10 @@ static void recount_diff(const char *line, int size, struct fragment *fragment)
case '\\':
continue;
case '@':
ret = size < 3 || prefixcmp(line, "@@ ");
ret = size < 3 || !starts_with(line, "@@ ");
break;
case 'd':
ret = size < 5 || prefixcmp(line, "diff ");
ret = size < 5 || !starts_with(line, "diff ");
break;
default:
ret = -1;
Expand Down Expand Up @@ -1798,11 +1798,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,

*status_p = 0;

if (!prefixcmp(buffer, "delta ")) {
if (starts_with(buffer, "delta ")) {
patch_method = BINARY_DELTA_DEFLATED;
origlen = strtoul(buffer + 6, NULL, 10);
}
else if (!prefixcmp(buffer, "literal ")) {
else if (starts_with(buffer, "literal ")) {
patch_method = BINARY_LITERAL_DEFLATED;
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 &&
/* does preimage begin with the heading? */
(preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
!prefixcmp(++preimage, heading) &&
starts_with(++preimage, heading) &&
/* does it record full SHA-1? */
!get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
preimage[sizeof(heading) + 40 - 1] == '\n' &&
/* 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 */

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

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;
}

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

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];
if (!argc || !strcmp(argv0, "--"))
die (_("--track needs a branch name"));
if (!prefixcmp(argv0, "refs/"))
if (starts_with(argv0, "refs/"))
argv0 += 5;
if (!prefixcmp(argv0, "remotes/"))
if (starts_with(argv0, "remotes/"))
argv0 += 8;
argv0 = strchr(argv0, '/');
if (!argv0 || !argv0[1])
Expand Down
4 changes: 2 additions & 2 deletions builtin/clean.c
Original file line number Diff line number Diff line change
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)
{
if (!prefixcmp(var, "column."))
if (starts_with(var, "column."))
return git_column_config(var, value, "clean", &colopts);

/* 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);
return 0;
}
if (!prefixcmp(var, "color.interactive.")) {
if (starts_with(var, "color.interactive.")) {
int slot = parse_clean_color_slot(var +
strlen("color.interactive."));
if (slot < 0)
Expand Down
8 changes: 4 additions & 4 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ static void write_followtags(const struct ref *refs, const char *msg)
{
const struct ref *ref;
for (ref = refs; ref; ref = ref->next) {
if (prefixcmp(ref->name, "refs/tags/"))
if (!starts_with(ref->name, "refs/tags/"))
continue;
if (!suffixcmp(ref->name, "^{}"))
if (ends_with(ref->name, "^{}"))
continue;
if (!has_sha1_file(ref->old_sha1))
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,
const char *msg)
{
if (our && !prefixcmp(our->name, "refs/heads/")) {
if (our && starts_with(our->name, "refs/heads/")) {
/* Local default branch link */
create_symref("HEAD", our->name, NULL);
if (!option_bare) {
Expand Down Expand Up @@ -625,7 +625,7 @@ static int checkout(void)
if (advice_detached_head)
detach_advice(sha1_to_hex(sha1));
} else {
if (prefixcmp(head, "refs/heads/"))
if (!starts_with(head, "refs/heads/"))
die(_("HEAD not found below refs/heads!"));
}
free(head);
Expand Down
2 changes: 1 addition & 1 deletion builtin/column.c
Original file line number Diff line number Diff line change
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 */
if (argc > 1 && !prefixcmp(argv[1], "--command=")) {
if (argc > 1 && starts_with(argv[1], "--command=")) {
command = argv[1] + 10;
git_config(column_config, (void *)command);
} else
Expand Down
10 changes: 5 additions & 5 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
eol = nl - sb.buf;
else
eol = sb.len;
if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
if (starts_with(sb.buf + previous, "\nConflicts:\n")) {
ignore_footer = sb.len - previous;
break;
}
Expand Down Expand Up @@ -904,7 +904,7 @@ static int rest_is_empty(struct strbuf *sb, int start)
eol = sb->len;

if (strlen(sign_off_header) <= eol - i &&
!prefixcmp(sb->buf + i, sign_off_header)) {
starts_with(sb->buf + i, sign_off_header)) {
i = eol;
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;

if (!prefixcmp(k, "column."))
if (starts_with(k, "column."))
return git_column_config(k, v, "status", &s->colopts);
if (!strcmp(k, "status.submodulesummary")) {
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);
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);
if (slot < 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);
printf("[%s%s ",
!prefixcmp(head, "refs/heads/") ?
starts_with(head, "refs/heads/") ?
head + 11 :
!strcmp(head, "HEAD") ?
_("detached HEAD") :
Expand Down
2 changes: 1 addition & 1 deletion builtin/describe.c
Original file line number Diff line number Diff line change
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)
{
int is_tag = !prefixcmp(path, "refs/tags/");
int is_tag = starts_with(path, "refs/tags/");
unsigned char peeled[20];
int is_annotated, prio;

Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Original file line number Diff line number Diff line change
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;
printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
name, tagged_mark,
Expand Down
6 changes: 3 additions & 3 deletions builtin/fetch-pack.c
Original file line number Diff line number Diff line change
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++) {
const char *arg = argv[i];

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

for_each_ref(add_existing, &existing_refs);
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;

/*
Expand All @@ -201,7 +201,7 @@ static void find_non_local_tags(struct transport *transport,
* to fetch then we can mark the ref entry in the list
* 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) &&
!will_fetch(head, ref->old_sha1) &&
!has_sha1_file(item->util) &&
Expand Down Expand Up @@ -431,7 +431,7 @@ static int update_local_ref(struct ref *ref,
}

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

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

0 comments on commit ad70448

Please sign in to comment.