Skip to content

Commit

Permalink
convert "oidcmp() == 0" to oideq()
Browse files Browse the repository at this point in the history
Using the more restrictive oideq() should, in the long run,
give the compiler more opportunities to optimize these
callsites. For now, this conversion should be a complete
noop with respect to the generated code.

The result is also perhaps a little more readable, as it
avoids the "zero is equal" idiom. Since it's so prevalent in
C, I think seasoned programmers tend not to even notice it
anymore, but it can sometimes make for awkward double
negations (e.g., we can drop a few !!oidcmp() instances
here).

This patch was generated almost entirely by the included
coccinelle patch. This mechanical conversion should be
completely safe, because we check explicitly for cases where
oidcmp() is compared to 0, which is what oideq() is doing
under the hood. Note that we don't have to catch "!oidcmp()"
separately; coccinelle's standard isomorphisms make sure the
two are treated equivalently.

I say "almost" because I did hand-edit the coccinelle output
to fix up a few style violations (it mostly keeps the
original formatting, but sometimes unwraps long lines).

Signed-off-by: Jeff King <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
peff authored and gitster committed Aug 29, 2018
1 parent 14438c4 commit 4a7e27e
Show file tree
Hide file tree
Showing 52 changed files with 117 additions and 111 deletions.
4 changes: 2 additions & 2 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ static void check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)

for (; result; result = result->next) {
const struct object_id *mb = &result->item->object.oid;
if (!oidcmp(mb, current_bad_oid)) {
if (oideq(mb, current_bad_oid)) {
handle_bad_merge_base();
} else if (0 <= oid_array_lookup(&good_revs, mb)) {
continue;
Expand Down Expand Up @@ -988,7 +988,7 @@ int bisect_next_all(const char *prefix, int no_checkout)

bisect_rev = &revs.commits->item->object.oid;

if (!oidcmp(bisect_rev, current_bad_oid)) {
if (oideq(bisect_rev, current_bad_oid)) {
exit_if_skipped_commits(tried, current_bad_oid);
printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
term_bad);
Expand Down
4 changes: 2 additions & 2 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,14 +1457,14 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
porigin = find(p, origin);
if (!porigin)
continue;
if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
if (oideq(&porigin->blob_oid, &origin->blob_oid)) {
pass_whole_blame(sb, origin, porigin);
blame_origin_decref(porigin);
goto finish;
}
for (j = same = 0; j < i; j++)
if (sg_origin[j] &&
!oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
same = 1;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ static int safe_to_abort(const struct am_state *state)
if (get_oid("HEAD", &head))
oidclr(&head);

if (!oidcmp(&head, &abort_safety))
if (oideq(&head, &abort_safety))
return 1;

warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
Expand Down
2 changes: 1 addition & 1 deletion builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
if (pos >= 0) {
struct cache_entry *old = active_cache[pos];
if (ce->ce_mode == old->ce_mode &&
!oidcmp(&ce->oid, &old->oid)) {
oideq(&ce->oid, &old->oid)) {
old->ce_flags |= CE_UPDATE;
discard_cache_entry(ce);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions builtin/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi

/* Is it annotated? */
if (!peel_ref(path, &peeled)) {
is_annotated = !!oidcmp(oid, &peeled);
is_annotated = !oideq(oid, &peeled);
} else {
oidcpy(&peeled, oid);
is_annotated = 0;
Expand Down Expand Up @@ -469,7 +469,7 @@ static void process_object(struct object *obj, const char *path, void *data)
{
struct process_commit_data *pcd = data;

if (!oidcmp(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
reset_revision_walk();
describe_commit(&pcd->current_commit, pcd->dst);
strbuf_addf(pcd->dst, ":%s", path);
Expand Down
2 changes: 1 addition & 1 deletion builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void stuff_change(struct diff_options *opt,
struct diff_filespec *one, *two;

if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
!oidcmp(old_oid, new_oid) && (old_mode == new_mode))
oideq(old_oid, new_oid) && (old_mode == new_mode))
return;

if (opt->flags.reverse_diff) {
Expand Down
4 changes: 2 additions & 2 deletions builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static int use_wt_file(const char *workdir, const char *name,
if (is_null_oid(oid)) {
oidcpy(oid, &wt_oid);
use = 1;
} else if (!oidcmp(oid, &wt_oid))
} else if (oideq(oid, &wt_oid))
use = 1;
}
}
Expand Down Expand Up @@ -438,7 +438,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
strbuf_reset(&buf);
strbuf_addf(&buf, "Subproject commit %s",
oid_to_hex(&roid));
if (!oidcmp(&loid, &roid))
if (oideq(&loid, &roid))
strbuf_addstr(&buf, "-dirty");
add_left_or_right(&submodules, dst_path, buf.buf, 1);
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static void show_filemodify(struct diff_queue_struct *q,
string_list_insert(changed, spec->path);
putchar('\n');

if (!oidcmp(&ospec->oid, &spec->oid) &&
if (oideq(&ospec->oid, &spec->oid) &&
ospec->mode == spec->mode)
break;
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ static void adjust_refcol_width(const struct ref *ref)
int max, rlen, llen, len;

/* uptodate lines are only shown on high verbosity level */
if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
return;

max = term_columns();
Expand Down Expand Up @@ -644,7 +644,7 @@ static int update_local_ref(struct ref *ref,
if (type < 0)
die(_("object %s not found"), oid_to_hex(&ref->new_oid));

if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
if (oideq(&ref->old_oid, &ref->new_oid)) {
if (verbosity > 0)
format_display(display, '=', _("[up to date]"), NULL,
remote, pretty_ref, summary_width);
Expand Down
2 changes: 1 addition & 1 deletion builtin/fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ static void find_merge_parents(struct merge_parents *result,
while (parents) {
struct commit *cmit = pop_commit(&parents);
for (i = 0; i < result->nr; i++)
if (!oidcmp(&result->item[i].commit, &cmit->object.oid))
if (oideq(&result->item[i].commit, &cmit->object.oid))
result->item[i].used = 1;
}

Expand Down
4 changes: 2 additions & 2 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,9 @@ static void find_ref_delta_children(const struct object_id *oid,
*last_index = -1;
return;
}
while (first > 0 && !oidcmp(&ref_deltas[first - 1].oid, oid))
while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
--first;
while (last < end && !oidcmp(&ref_deltas[last + 1].oid, oid))
while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
++last;
*first_index = first;
*last_index = last;
Expand Down
6 changes: 3 additions & 3 deletions builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ static char *find_branch_name(struct rev_info *rev)
tip_oid = &rev->cmdline.rev[positive].item->oid;
if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
skip_prefix(full_ref, "refs/heads/", &v) &&
!oidcmp(tip_oid, &branch_oid))
oideq(tip_oid, &branch_oid))
branch = xstrdup(v);
free(full_ref);
return branch;
Expand Down Expand Up @@ -1703,7 +1703,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
/* Don't say anything if head and upstream are the same. */
if (rev.pending.nr == 2) {
struct object_array_entry *o = rev.pending.objects;
if (oidcmp(&o[0].item->oid, &o[1].item->oid) == 0)
if (oideq(&o[0].item->oid, &o[1].item->oid))
return 0;
}
get_patch_ids(&rev, &ids);
Expand Down Expand Up @@ -1949,7 +1949,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
/* Don't say anything if head and upstream are the same. */
if (revs.pending.nr == 2) {
struct object_array_entry *o = revs.pending.objects;
if (oidcmp(&o[0].item->oid, &o[1].item->oid) == 0)
if (oideq(&o[0].item->oid, &o[1].item->oid))
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion builtin/merge-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static int same_entry(struct name_entry *a, struct name_entry *b)
{
return a->oid &&
b->oid &&
!oidcmp(a->oid, b->oid) &&
oideq(a->oid, b->oid) &&
a->mode == b->mode;
}

Expand Down
4 changes: 2 additions & 2 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ static int merging_a_throwaway_tag(struct commit *commit)
tag_ref = xstrfmt("refs/tags/%s",
((struct tag *)merge_remote_util(commit)->obj)->tag);
if (!read_ref(tag_ref, &oid) &&
!oidcmp(&oid, &merge_remote_util(commit)->obj->oid))
oideq(&oid, &merge_remote_util(commit)->obj->oid))
is_throwaway_tag = 0;
else
is_throwaway_tag = 1;
Expand Down Expand Up @@ -1448,7 +1448,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
goto done;
} else if (fast_forward != FF_NO && !remoteheads->next &&
!common->next &&
!oidcmp(&common->item->object.oid, &head_commit->object.oid)) {
oideq(&common->item->object.oid, &head_commit->object.oid)) {
/* Again the most common case of merging one remote. */
struct strbuf msg = STRBUF_INIT;
struct commit *commit;
Expand Down
4 changes: 2 additions & 2 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
*/
for (neigh = 0; neigh < 8; neigh++) {
ent = pbase_tree_cache[my_ix];
if (ent && !oidcmp(&ent->oid, oid)) {
if (ent && oideq(&ent->oid, oid)) {
ent->ref++;
return ent;
}
Expand Down Expand Up @@ -1384,7 +1384,7 @@ static void add_preferred_base(struct object_id *oid)
return;

for (it = pbase_tree; it; it = it->next) {
if (!oidcmp(&it->pcache.oid, &tree_oid)) {
if (oideq(&it->pcache.oid, &tree_oid)) {
free(data);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ static int run_rebase(const struct object_id *curr_head,
struct argv_array args = ARGV_ARRAY_INIT;

if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point))
if (!is_null_oid(fork_point) && !oidcmp(&oct_merge_base, fork_point))
if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point))
fork_point = NULL;

argv_array_push(&args, "rebase");
Expand Down
4 changes: 2 additions & 2 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,8 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)

dst_cmd = (struct command *) item->util;

if (!oidcmp(&cmd->old_oid, &dst_cmd->old_oid) &&
!oidcmp(&cmd->new_oid, &dst_cmd->new_oid))
if (oideq(&cmd->old_oid, &dst_cmd->old_oid) &&
oideq(&cmd->new_oid, &dst_cmd->new_oid))
return;

dst_cmd->skip_update = 1;
Expand Down
2 changes: 1 addition & 1 deletion builtin/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ static int get_push_ref_states(const struct ref *remote_refs,

if (is_null_oid(&ref->new_oid)) {
info->status = PUSH_STATUS_DELETE;
} else if (!oidcmp(&ref->old_oid, &ref->new_oid))
} else if (oideq(&ref->old_oid, &ref->new_oid))
info->status = PUSH_STATUS_UPTODATE;
else if (is_null_oid(&ref->old_oid))
info->status = PUSH_STATUS_CREATE;
Expand Down
6 changes: 3 additions & 3 deletions builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ static int edit_and_replace(const char *object_ref, int force, int raw)
}
free(tmpfile);

if (!oidcmp(&old_oid, &new_oid))
if (oideq(&old_oid, &new_oid))
return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));

return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
Expand Down Expand Up @@ -414,7 +414,7 @@ static int check_one_mergetag(struct commit *commit,
if (get_oid(mergetag_data->argv[i], &oid) < 0)
return error(_("not a valid object name: '%s'"),
mergetag_data->argv[i]);
if (!oidcmp(&tag->tagged->oid, &oid))
if (oideq(&tag->tagged->oid, &oid))
return 0; /* found */
}

Expand Down Expand Up @@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)

strbuf_release(&buf);

if (!oidcmp(&old_oid, &new_oid)) {
if (oideq(&old_oid, &new_oid)) {
if (gentle) {
warning(_("graft for '%s' unnecessary"), oid_to_hex(&old_oid));
return 0;
Expand Down
2 changes: 1 addition & 1 deletion builtin/unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ static void added_object(unsigned nr, enum object_type type,
struct delta_info *info;

while ((info = *p) != NULL) {
if (!oidcmp(&info->base_oid, &obj_list[nr].oid) ||
if (oideq(&info->base_oid, &obj_list[nr].oid) ||
info->base_offset == obj_list[nr].offset) {
*p = info->next;
p = &delta_list;
Expand Down
4 changes: 2 additions & 2 deletions builtin/update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ static int unresolve_one(const char *path)
ret = -1;
goto free_return;
}
if (!oidcmp(&ce_2->oid, &ce_3->oid) &&
if (oideq(&ce_2->oid, &ce_3->oid) &&
ce_2->ce_mode == ce_3->ce_mode) {
fprintf(stderr, "%s: identical in both, skipping.\n",
path);
Expand Down Expand Up @@ -754,7 +754,7 @@ static int do_reupdate(int ac, const char **av,
old = read_one_ent(NULL, &head_oid,
ce->name, ce_namelen(ce), 0);
if (old && ce->ce_mode == old->ce_mode &&
!oidcmp(&ce->oid, &old->oid)) {
oideq(&ce->oid, &old->oid)) {
discard_cache_entry(old);
continue; /* unchanged */
}
Expand Down
2 changes: 1 addition & 1 deletion bulk-checkin.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int already_written(struct bulk_checkin_state *state, struct object_id *o

/* Might want to keep the list sorted */
for (i = 0; i < state->nr_written; i++)
if (!oidcmp(&state->written[i]->oid, oid))
if (oideq(&state->written[i]->oid, oid))
return 1;

/* This is a new object we need to keep */
Expand Down
2 changes: 1 addition & 1 deletion cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ int cache_tree_matches_traversal(struct cache_tree *root,

it = find_cache_tree_from_traversal(root, info);
it = cache_tree_find(it, ent->path);
if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
if (it && it->entry_count > 0 && oideq(ent->oid, &it->oid))
return it->entry_count;
return 0;
}
4 changes: 2 additions & 2 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ static inline int is_empty_blob_sha1(const unsigned char *sha1)

static inline int is_empty_blob_oid(const struct object_id *oid)
{
return !oidcmp(oid, the_hash_algo->empty_blob);
return oideq(oid, the_hash_algo->empty_blob);
}

static inline int is_empty_tree_sha1(const unsigned char *sha1)
Expand All @@ -1110,7 +1110,7 @@ static inline int is_empty_tree_sha1(const unsigned char *sha1)

static inline int is_empty_tree_oid(const struct object_id *oid)
{
return !oidcmp(oid, the_hash_algo->empty_tree);
return oideq(oid, the_hash_algo->empty_tree);
}

const char *empty_tree_oid_hex(void);
Expand Down
4 changes: 2 additions & 2 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
for (i = 0; i < num_parent; i++) {
int j;
for (j = 0; j < i; j++) {
if (!oidcmp(&elem->parent[i].oid,
&elem->parent[j].oid)) {
if (oideq(&elem->parent[i].oid,
&elem->parent[j].oid)) {
reuse_combine_diff(sline, cnt, i, j);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ void write_commit_graph(const char *obj_dir,
num_extra_edges = 0;
for (i = 0; i < oids.nr; i++) {
int num_parents = 0;
if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
continue;

commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
Expand Down
2 changes: 1 addition & 1 deletion connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static int process_dummy_ref(const char *line)
return 0;
name++;

return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
}

static void check_no_capabilities(const char *line, int len)
Expand Down
6 changes: 6 additions & 0 deletions contrib/coccinelle/object_id.cocci
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,9 @@ expression E1, E2;
@@
- hashcpy(E1.hash, E2->hash)
+ oidcpy(&E1, E2)

@@
expression E1, E2;
@@
- oidcmp(E1, E2) == 0
+ oideq(E1, E2)
2 changes: 1 addition & 1 deletion diff-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static int show_modified(struct rev_info *revs,
}

oldmode = old_entry->ce_mode;
if (mode == oldmode && !oidcmp(oid, &old_entry->oid) && !dirty_submodule &&
if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
!revs->diffopt.flags.find_copies_harder)
return 0;

Expand Down
Loading

0 comments on commit 4a7e27e

Please sign in to comment.