convert "oidcmp() != 0" to "!oideq()"

This is the flip side of the previous two patches: checking for a non-zero oidcmp() can be more strictly expressed as inequality. Like those patches, we write "!= 0" in the coccinelle transformation, which covers by isomorphism the more common: if (oidcmp(E1, E2)) As with the previous two patches, this patch can be achieved almost entirely by running "make coccicheck"; the only differences are manual line-wrap fixes to match the original code. There is one thing to note for anybody replicating this, though: coccinelle 1.0.4 seems to miss the case in builtin/tag.c, even though it's basically the same as all the others. Running with 1.0.7 does catch this, so presumably it's just a coccinelle bug that was fixed in the interim. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 28, 2018 at 17:22 UTC 9001dc2a7493f1366a183c3a9175f608769321d5
31 files changed +55 -49
bisect.c
+1 -1
@@ -595,7 +595,7 @@ static struct commit_list *skip_away(struct commit_list *list, int count)
595
596 for (i = 0; cur; cur = cur->next, i++) {
597 if (i == index) {
598 - if (oidcmp(&cur->item->object.oid, current_bad_oid))
598 + if (!oideq(&cur->item->object.oid, current_bad_oid))
599 return cur;
600 if (previous)
601 return previous;
blame.c
+2 -2
@@ -1832,7 +1832,7 @@ void setup_scoreboard(struct blame_scoreboard *sb,
1832
1833 sb->revs->children.name = "children";
1834 while (c->parents &&
1835 - oidcmp(&c->object.oid, &sb->final->object.oid)) {
1835 + !oideq(&c->object.oid, &sb->final->object.oid)) {
1836 struct commit_list *l = xcalloc(1, sizeof(*l));
1837
1838 l->item = c;
@@ -1842,7 +1842,7 @@ void setup_scoreboard(struct blame_scoreboard *sb,
1842 c = c->parents->item;
1843 }
1844
1845 - if (oidcmp(&c->object.oid, &sb->final->object.oid))
1845 + if (!oideq(&c->object.oid, &sb->final->object.oid))
1846 die(_("--reverse --first-parent together require range along first-parent chain"));
1847 }
1848
builtin/fmt-merge-msg.c
+2 -2
@@ -78,9 +78,9 @@ static struct merge_parent *find_merge_parent(struct merge_parents *table,
78 {
79 int i;
80 for (i = 0; i < table->nr; i++) {
81 - if (given && oidcmp(&table->item[i].given, given))
81 + if (given && !oideq(&table->item[i].given, given))
82 continue;
83 - if (commit && oidcmp(&table->item[i].commit, commit))
83 + if (commit && !oideq(&table->item[i].commit, commit))
84 continue;
85 return &table->item[i];
86 }
builtin/merge.c
+1 -1
@@ -1521,7 +1521,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1521 * HEAD^^" would be missed.
1522 */
1523 common_one = get_merge_bases(head_commit, j->item);
1524 - if (oidcmp(&common_one->item->object.oid, &j->item->object.oid)) {
1524 + if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) {
1525 up_to_date = 0;
1526 break;
1527 }
builtin/pull.c
+1 -1
@@ -902,7 +902,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
902 oidclr(&curr_head);
903
904 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) &&
905 - oidcmp(&orig_head, &curr_head)) {
905 + !oideq(&orig_head, &curr_head)) {
906 /*
907 * The fetch involved updating the current branch.
908 *
builtin/rm.c
+1 -1
@@ -180,7 +180,7 @@ static int check_local_mod(struct object_id *head, int index_only)
180 if (no_head
181 || get_tree_entry(head, name, &oid, &mode)
182 || ce->ce_mode != create_ce_mode(mode)
183 - || oidcmp(&ce->oid, &oid))
183 + || !oideq(&ce->oid, &oid))
184 staged_changes = 1;
185
186 /*
builtin/show-branch.c
+2 -2
@@ -412,7 +412,7 @@ static int append_head_ref(const char *refname, const struct object_id *oid,
412 /* If both heads/foo and tags/foo exists, get_sha1 would
413 * get confused.
414 */
415 - if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
415 + if (get_oid(refname + ofs, &tmp) || !oideq(&tmp, oid))
416 ofs = 5;
417 return append_ref(refname + ofs, oid, 0);
418 }
@@ -427,7 +427,7 @@ static int append_remote_ref(const char *refname, const struct object_id *oid,
427 /* If both heads/foo and tags/foo exists, get_sha1 would
428 * get confused.
429 */
430 - if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
430 + if (get_oid(refname + ofs, &tmp) || !oideq(&tmp, oid))
431 ofs = 5;
432 return append_ref(refname + ofs, oid, 0);
433 }
builtin/tag.c
+1 -1
@@ -559,7 +559,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
559 ref_transaction_commit(transaction, &err))
560 die("%s", err.buf);
561 ref_transaction_free(transaction);
562 - if (force && !is_null_oid(&prev) && oidcmp(&prev, &object))
562 + if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
563 printf(_("Updated tag '%s' (was %s)\n"), tag,
564 find_unique_abbrev(&prev, DEFAULT_ABBREV));
565
bundle.c
+1 -1
@@ -369,7 +369,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
369 * commit that is referenced by the tag, and not the tag
370 * itself.
371 */
372 - if (oidcmp(&oid, &e->item->oid)) {
372 + if (!oideq(&oid, &e->item->oid)) {
373 /*
374 * Is this the positive end of a range expressed
375 * in terms of a tag (e.g. v2.0 from the range
commit-graph.c
+3 -3
@@ -765,7 +765,7 @@ void write_commit_graph(const char *obj_dir,
765
766 count_distinct = 1;
767 for (i = 1; i < oids.nr; i++) {
768 - if (oidcmp(&oids.list[i-1], &oids.list[i]))
768 + if (!oideq(&oids.list[i - 1], &oids.list[i]))
769 count_distinct++;
770 }
771
@@ -960,7 +960,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
960 continue;
961 }
962
963 - if (oidcmp(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
963 + if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
964 get_commit_tree_oid(odb_commit)))
965 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
966 oid_to_hex(&cur_oid),
@@ -977,7 +977,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
977 break;
978 }
979
980 - if (oidcmp(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
980 + if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
981 graph_report("commit-graph parent for %s is %s != %s",
982 oid_to_hex(&cur_oid),
983 oid_to_hex(&graph_parents->item->object.oid),
commit.c
+1 -1
@@ -46,7 +46,7 @@ struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref
46 struct commit *c = lookup_commit_reference(the_repository, oid);
47 if (!c)
48 die(_("could not parse %s"), ref_name);
49 - if (oidcmp(oid, &c->object.oid)) {
49 + if (!oideq(oid, &c->object.oid)) {
50 warning(_("%s %s is not a commit!"),
51 ref_name, oid_to_hex(oid));
52 }
contrib/coccinelle/object_id.cocci
+6
@@ -123,3 +123,9 @@ expression E1, E2;
123 - hashcmp(E1, E2) == 0
124 + hasheq(E1, E2)
125 ...>}
126 +
127 +@@
128 +expression E1, E2;
129 +@@
130 +- oidcmp(E1, E2) != 0
131 ++ !oideq(E1, E2)
diff-lib.c
+1 -1
@@ -342,7 +342,7 @@ static int show_modified(struct rev_info *revs,
342 }
343
344 if (revs->combine_merges && !cached &&
345 - (oidcmp(oid, &old_entry->oid) || oidcmp(&old_entry->oid, &new_entry->oid))) {
345 + (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
346 struct combine_diff_path *p;
347 int pathlen = ce_namelen(new_entry);
348
diff.c
+3 -3
@@ -3765,7 +3765,7 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
3765 * This is not the sha1 we are looking for, or
3766 * unreusable because it is not a regular file.
3767 */
3768 - if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
3768 + if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
3769 return 0;
3770
3771 /*
@@ -4170,7 +4170,7 @@ static void fill_metainfo(struct strbuf *msg,
4170 default:
4171 *must_show_header = 0;
4172 }
4173 - if (one && two && oidcmp(&one->oid, &two->oid)) {
4173 + if (one && two && !oideq(&one->oid, &two->oid)) {
4174 const unsigned hexsz = the_hash_algo->hexsz;
4175 int abbrev = o->flags.full_index ? hexsz : DEFAULT_ABBREV;
4176
@@ -5457,7 +5457,7 @@ static void diff_resolve_rename_copy(void)
5457 else
5458 p->status = DIFF_STATUS_RENAMED;
5459 }
5460 - else if (oidcmp(&p->one->oid, &p->two->oid) ||
5460 + else if (!oideq(&p->one->oid, &p->two->oid) ||
5461 p->one->mode != p->two->mode ||
5462 p->one->dirty_submodule ||
5463 p->two->dirty_submodule ||
diffcore-rename.c
+1 -1
@@ -286,7 +286,7 @@ static int find_identical_files(struct hashmap *srcs,
286 struct diff_filespec *source = p->filespec;
287
288 /* False hash collision? */
289 - if (oidcmp(&source->oid, &target->oid))
289 + if (!oideq(&source->oid, &target->oid))
290 continue;
291 /* Non-regular files? If so, the modes must match! */
292 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
dir.c
+3 -3
@@ -1282,7 +1282,7 @@ static void prep_exclude(struct dir_struct *dir,
1282 * order, though, if you do that.
1283 */
1284 if (untracked &&
1285 - oidcmp(&oid_stat.oid, &untracked->exclude_oid)) {
1285 + !oideq(&oid_stat.oid, &untracked->exclude_oid)) {
1286 invalidate_gitignore(dir->untracked, untracked);
1287 oidcpy(&untracked->exclude_oid, &oid_stat.oid);
1288 }
@@ -2248,12 +2248,12 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
2248
2249 /* Validate $GIT_DIR/info/exclude and core.excludesfile */
2250 root = dir->untracked->root;
2251 - if (oidcmp(&dir->ss_info_exclude.oid,
2251 + if (!oideq(&dir->ss_info_exclude.oid,
2252 &dir->untracked->ss_info_exclude.oid)) {
2253 invalidate_gitignore(dir->untracked, root);
2254 dir->untracked->ss_info_exclude = dir->ss_info_exclude;
2255 }
2256 - if (oidcmp(&dir->ss_excludes_file.oid,
2256 + if (!oideq(&dir->ss_excludes_file.oid,
2257 &dir->untracked->ss_excludes_file.oid)) {
2258 invalidate_gitignore(dir->untracked, root);
2259 dir->untracked->ss_excludes_file = dir->ss_excludes_file;
fast-import.c
+2 -2
@@ -2649,7 +2649,7 @@ static int parse_from(struct branch *b)
2649 struct object_entry *oe = find_mark(idnum);
2650 if (oe->type != OBJ_COMMIT)
2651 die("Mark :%" PRIuMAX " not a commit", idnum);
2652 - if (oidcmp(&b->oid, &oe->idx.oid)) {
2652 + if (!oideq(&b->oid, &oe->idx.oid)) {
2653 oidcpy(&b->oid, &oe->idx.oid);
2654 if (oe->pack_id != MAX_PACK_ID) {
2655 unsigned long size;
@@ -2667,7 +2667,7 @@ static int parse_from(struct branch *b)
2667 else
2668 die("Invalid ref name or SHA1 expression: %s", from);
2669
2670 - if (b->branch_tree.tree && oidcmp(&oid, &b->branch_tree.versions[1].oid)) {
2670 + if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
2671 release_tree_content_recursive(b->branch_tree.tree);
2672 b->branch_tree.tree = NULL;
2673 }
fetch-pack.c
+1 -1
@@ -599,7 +599,7 @@ static void filter_refs(struct fetch_pack_args *args,
599 continue;
600 if (parse_oid_hex(ref->name, &oid, &p) ||
601 *p != '\0' ||
602 - oidcmp(&oid, &ref->old_oid))
602 + !oideq(&oid, &ref->old_oid))
603 continue;
604
605 if ((allow_unadvertised_object_request &
match-trees.c
+1 -1
@@ -106,7 +106,7 @@ static int score_trees(const struct object_id *hash1, const struct object_id *ha
106 update_tree_entry(&two);
107 } else {
108 /* path appears in both */
109 - if (oidcmp(one.entry.oid, two.entry.oid)) {
109 + if (!oideq(one.entry.oid, two.entry.oid)) {
110 /* they are different */
111 score += score_differs(one.entry.mode,
112 two.entry.mode,
notes.c
+1 -1
@@ -206,7 +206,7 @@ static void note_tree_remove(struct notes_tree *t,
206 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
207 return; /* type mismatch, nothing to remove */
208 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
209 - if (oidcmp(&l->key_oid, &entry->key_oid))
209 + if (!oideq(&l->key_oid, &entry->key_oid))
210 return; /* key mismatch, nothing to remove */
211
212 /* we have found a matching entry */
read-cache.c
+1 -1
@@ -2029,7 +2029,7 @@ int read_index_from(struct index_state *istate, const char *path,
2029 base_oid_hex = oid_to_hex(&split_index->base_oid);
2030 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
2031 ret = do_read_index(split_index->base, base_path, 1);
2032 - if (oidcmp(&split_index->base_oid, &split_index->base->oid))
2032 + if (!oideq(&split_index->base_oid, &split_index->base->oid))
2033 die("broken index, expect %s in %s, got %s",
2034 base_oid_hex, base_path,
2035 oid_to_hex(&split_index->base->oid));
refs.c
+4 -4
@@ -702,7 +702,7 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
702 pseudoref);
703 rollback_lock_file(&lock);
704 goto done;
705 - } else if (oidcmp(&actual_old_oid, old_oid)) {
705 + } else if (!oideq(&actual_old_oid, old_oid)) {
706 strbuf_addf(err, _("unexpected object ID when writing '%s'"),
707 pseudoref);
708 rollback_lock_file(&lock);
@@ -744,7 +744,7 @@ static int delete_pseudoref(const char *pseudoref, const struct object_id *old_o
744 }
745 if (read_ref(pseudoref, &actual_old_oid))
746 die(_("could not read ref '%s'"), pseudoref);
747 - if (oidcmp(&actual_old_oid, old_oid)) {
747 + if (!oideq(&actual_old_oid, old_oid)) {
748 error(_("unexpected object ID when deleting '%s'"),
749 pseudoref);
750 rollback_lock_file(&lock);
@@ -875,13 +875,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
875 */
876 if (!is_null_oid(&cb->ooid)) {
877 oidcpy(cb->oid, noid);
878 - if (oidcmp(&cb->ooid, noid))
878 + if (!oideq(&cb->ooid, noid))
879 warning(_("log for ref %s has gap after %s"),
880 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
881 }
882 else if (cb->date == cb->at_time)
883 oidcpy(cb->oid, noid);
884 - else if (oidcmp(noid, cb->oid))
884 + else if (!oideq(noid, cb->oid))
885 warning(_("log for ref %s unexpectedly ended on %s"),
886 cb->refname, show_date(cb->date, cb->tz,
887 DATE_MODE(RFC2822)));
refs/files-backend.c
+1 -1
@@ -841,7 +841,7 @@ static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
841 return 0;
842 }
843 }
844 - if (old_oid && oidcmp(&lock->old_oid, old_oid)) {
844 + if (old_oid && !oideq(&lock->old_oid, old_oid)) {
845 strbuf_addf(err, "ref '%s' is at %s but expected %s",
846 lock->ref_name,
847 oid_to_hex(&lock->old_oid),
refs/packed-backend.c
+1 -1
@@ -1160,7 +1160,7 @@ static int write_with_updates(struct packed_ref_store *refs,
1160 "reference already exists",
1161 update->refname);
1162 goto error;
1163 - } else if (oidcmp(&update->old_oid, iter->oid)) {
1163 + } else if (!oideq(&update->old_oid, iter->oid)) {
1164 strbuf_addf(err, "cannot update ref '%s': "
1165 "is at %s but expected %s",
1166 update->refname,
refs/ref-cache.c
+1 -1
@@ -272,7 +272,7 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
272 /* This is impossible by construction */
273 die("Reference directory conflict: %s", ref1->name);
274
275 - if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
275 + if (!oideq(&ref1->u.value.oid, &ref2->u.value.oid))
276 die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
277
278 warning("Duplicated ref: %s", ref1->name);
remote.c
+1 -1
@@ -1403,7 +1403,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1403 * branch.
1404 */
1405 if (ref->expect_old_sha1) {
1406 - if (oidcmp(&ref->old_oid, &ref->old_oid_expect))
1406 + if (!oideq(&ref->old_oid, &ref->old_oid_expect))
1407 reject_reason = REF_STATUS_REJECT_STALE;
1408 else
1409 /* If the ref isn't stale then force the update. */
sequencer.c
+4 -4
@@ -1188,7 +1188,7 @@ static int parse_head(struct commit **head)
1188 current_head = lookup_commit_reference(the_repository, &oid);
1189 if (!current_head)
1190 return error(_("could not parse HEAD"));
1191 - if (oidcmp(&oid, &current_head->object.oid)) {
1191 + if (!oideq(&oid, &current_head->object.oid)) {
1192 warning(_("HEAD %s is not a commit!"),
1193 oid_to_hex(&oid));
1194 }
@@ -3034,7 +3034,7 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
3034 struct commit_list *p = commit->parents->next;
3035
3036 for (j = to_merge; j && p; j = j->next, p = p->next)
3037 - if (oidcmp(&j->item->object.oid,
3037 + if (!oideq(&j->item->object.oid,
3038 &p->item->object.oid)) {
3039 can_fast_forward = 0;
3040 break;
@@ -3566,7 +3566,7 @@ static int commit_staged_changes(struct replay_opts *opts,
3566 if (get_oid_hex(rev.buf, &to_amend))
3567 return error(_("invalid contents: '%s'"),
3568 rebase_path_amend());
3569 - if (!is_clean && oidcmp(&head, &to_amend))
3569 + if (!is_clean && !oideq(&head, &to_amend))
3570 return error(_("\nYou have uncommitted changes in your "
3571 "working tree. Please, commit them\n"
3572 "first and then run 'git rebase "
@@ -4545,7 +4545,7 @@ int skip_unnecessary_picks(void)
4545 if (item->commit->parents->next)
4546 break; /* merge commit */
4547 parent_oid = &item->commit->parents->item->object.oid;
4548 - if (oidcmp(parent_oid, oid))
4548 + if (!oideq(parent_oid, oid))
4549 break;
4550 oid = &item->commit->object.oid;
4551 }
sha1-file.c
+3 -3
@@ -825,7 +825,7 @@ int check_object_signature(const struct object_id *oid, void *map,
825
826 if (map) {
827 hash_object_file(map, size, type, &real_oid);
828 - return oidcmp(oid, &real_oid) ? -1 : 0;
828 + return !oideq(oid, &real_oid) ? -1 : 0;
829 }
830
831 st = open_istream(oid, &obj_type, &size, NULL);
@@ -852,7 +852,7 @@ int check_object_signature(const struct object_id *oid, void *map,
852 }
853 the_hash_algo->final_fn(real_oid.hash, &c);
854 close_istream(st);
855 - return oidcmp(oid, &real_oid) ? -1 : 0;
855 + return !oideq(oid, &real_oid) ? -1 : 0;
856 }
857
858 int git_open_cloexec(const char *name, int flags)
@@ -1671,7 +1671,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
1671 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
1672 ret);
1673 the_hash_algo->final_fn(parano_oid.hash, &c);
1674 - if (oidcmp(oid, &parano_oid) != 0)
1674 + if (!oideq(oid, &parano_oid))
1675 die(_("confused by unstable object source data for %s"),
1676 oid_to_hex(oid));
1677
submodule-config.c
+2 -2
@@ -45,7 +45,7 @@ static int config_path_cmp(const void *unused_cmp_data,
45 const struct submodule_entry *b = entry_or_key;
46
47 return strcmp(a->config->path, b->config->path) ||
48 - oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
48 + !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
49 }
50
51 static int config_name_cmp(const void *unused_cmp_data,
@@ -57,7 +57,7 @@ static int config_name_cmp(const void *unused_cmp_data,
57 const struct submodule_entry *b = entry_or_key;
58
59 return strcmp(a->config->name, b->config->name) ||
60 - oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
60 + !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
61 }
62
63 static struct submodule_cache *submodule_cache_alloc(void)
t/helper/test-dump-cache-tree.c
+1 -1
@@ -33,7 +33,7 @@ static int dump_cache_tree(struct cache_tree *it,
33 }
34 else {
35 dump_one(it, pfx, "");
36 - if (oidcmp(&it->oid, &ref->oid) ||
36 + if (!oideq(&it->oid, &ref->oid) ||
37 ref->entry_count != it->entry_count ||
38 ref->subtree_nr != it->subtree_nr) {
39 /* claims to be valid but is lying */
tree-diff.c
+1 -1
@@ -491,7 +491,7 @@ static struct combine_diff_path *ll_diff_tree_paths(
491 continue;
492
493 /* diff(t,pi) != ø */
494 - if (oidcmp(t.entry.oid, tp[i].entry.oid) ||
494 + if (!oideq(t.entry.oid, tp[i].entry.oid) ||
495 (t.entry.mode != tp[i].entry.mode))
496 continue;
497