strbuf: convert strbuf_add_unique_abbrev to use struct object_id

Convert the declaration and definition of strbuf_add_unique_abbrev to make it take a pointer to struct object_id. Predeclare the struct in strbuf.h, as cache.h includes strbuf.h before it declares the struct, and otherwise the struct declaration would have the wrong scope. Apply the following semantic patch, along with the standard object_id transforms, to adjust the callers: @@ expression E1, E2, E3; @@ - strbuf_add_unique_abbrev(E1, E2.hash, E3); + strbuf_add_unique_abbrev(E1, &E2, E3); @@ expression E1, E2, E3; @@ - strbuf_add_unique_abbrev(E1, E2->hash, E3); + strbuf_add_unique_abbrev(E1, E2, E3); Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Mar 12, 2018 at 02:27 UTC 30e677e0e243ebb29a5f5aeed8c850bbae9020fa
10 files changed +27 -21
builtin/checkout.c
+1 -1
@@ -720,7 +720,7 @@ static int add_pending_uninteresting_ref(const char *refname,
720 static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
721 {
722 strbuf_addstr(sb, " ");
723 - strbuf_add_unique_abbrev(sb, commit->object.oid.hash, DEFAULT_ABBREV);
723 + strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV);
724 strbuf_addch(sb, ' ');
725 if (!parse_commit(commit))
726 pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
builtin/fetch.c
+4 -4
@@ -708,9 +708,9 @@ static int update_local_ref(struct ref *ref,
708 if (in_merge_bases(current, updated)) {
709 struct strbuf quickref = STRBUF_INIT;
710 int r;
711 - strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
711 + strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
712 strbuf_addstr(&quickref, "..");
713 - strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
713 + strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
714 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
715 (recurse_submodules != RECURSE_SUBMODULES_ON))
716 check_for_new_submodule_commits(&ref->new_oid);
@@ -723,9 +723,9 @@ static int update_local_ref(struct ref *ref,
723 } else if (force || ref->force) {
724 struct strbuf quickref = STRBUF_INIT;
725 int r;
726 - strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
726 + strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
727 strbuf_addstr(&quickref, "...");
728 - strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
728 + strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
729 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
730 (recurse_submodules != RECURSE_SUBMODULES_ON))
731 check_for_new_submodule_commits(&ref->new_oid);
builtin/tag.c
+1 -1
@@ -293,7 +293,7 @@ static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
293 strbuf_addstr(sb, rla);
294 } else {
295 strbuf_addstr(sb, "tag: tagging ");
296 - strbuf_add_unique_abbrev(sb, oid->hash, DEFAULT_ABBREV);
296 + strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
297 }
298
299 strbuf_addstr(sb, " (");
merge-recursive.c
+1 -1
@@ -228,7 +228,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
228 strbuf_addf(&o->obuf, "virtual %s\n",
229 merge_remote_util(commit)->name);
230 else {
231 - strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
231 + strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid,
232 DEFAULT_ABBREV);
233 strbuf_addch(&o->obuf, ' ');
234 if (parse_commit(commit) != 0)
pretty.c
+4 -4
@@ -549,7 +549,7 @@ static void add_merge_info(const struct pretty_print_context *pp,
549 struct object_id *oidp = &parent->item->object.oid;
550 strbuf_addch(sb, ' ');
551 if (pp->abbrev)
552 - strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
552 + strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
553 else
554 strbuf_addstr(sb, oid_to_hex(oidp));
555 parent = parent->next;
@@ -1156,7 +1156,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1156 return 1;
1157 case 'h': /* abbreviated commit hash */
1158 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1159 - strbuf_add_unique_abbrev(sb, commit->object.oid.hash,
1159 + strbuf_add_unique_abbrev(sb, &commit->object.oid,
1160 c->pretty_ctx->abbrev);
1161 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1162 return 1;
@@ -1164,7 +1164,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1164 strbuf_addstr(sb, oid_to_hex(&commit->tree->object.oid));
1165 return 1;
1166 case 't': /* abbreviated tree hash */
1167 - strbuf_add_unique_abbrev(sb, commit->tree->object.oid.hash,
1167 + strbuf_add_unique_abbrev(sb, &commit->tree->object.oid,
1168 c->pretty_ctx->abbrev);
1169 return 1;
1170 case 'P': /* parent hashes */
@@ -1178,7 +1178,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1178 for (p = commit->parents; p; p = p->next) {
1179 if (p != commit->parents)
1180 strbuf_addch(sb, ' ');
1181 - strbuf_add_unique_abbrev(sb, p->item->object.oid.hash,
1181 + strbuf_add_unique_abbrev(sb, &p->item->object.oid,
1182 c->pretty_ctx->abbrev);
1183 }
1184 return 1;
strbuf.c
+2 -2
@@ -873,12 +873,12 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
873 strbuf_setlen(sb, sb->len + len);
874 }
875
876 -void strbuf_add_unique_abbrev(struct strbuf *sb, const unsigned char *sha1,
876 +void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
877 int abbrev_len)
878 {
879 int r;
880 strbuf_grow(sb, GIT_SHA1_HEXSZ + 1);
881 - r = find_unique_abbrev_r(sb->buf + sb->len, sha1, abbrev_len);
881 + r = find_unique_abbrev_r(sb->buf + sb->len, oid->hash, abbrev_len);
882 strbuf_setlen(sb, sb->len + r);
883 }
884
strbuf.h
+7 -1
@@ -70,6 +70,12 @@ struct strbuf {
70 extern char strbuf_slopbuf[];
71 #define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
72
73 +/*
74 + * Predeclare this here, since cache.h includes this file before it defines the
75 + * struct.
76 + */
77 +struct object_id;
78 +
79 /**
80 * Life Cycle Functions
81 * --------------------
@@ -539,7 +545,7 @@ extern void strbuf_list_free(struct strbuf **);
545 * the strbuf `sb`.
546 */
547 extern void strbuf_add_unique_abbrev(struct strbuf *sb,
542 - const unsigned char *sha1,
548 + const struct object_id *oid,
549 int abbrev_len);
550
551 /**
submodule.c
+2 -2
@@ -540,9 +540,9 @@ static void show_submodule_header(struct diff_options *o, const char *path,
540
541 output_header:
542 strbuf_addf(&sb, "Submodule %s ", path);
543 - strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
543 + strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
544 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
545 - strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
545 + strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
546 if (message)
547 strbuf_addf(&sb, " %s\n", message);
548 else
transport.c
+2 -2
@@ -367,7 +367,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
367 char type;
368 const char *msg;
369
370 - strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
370 + strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
371 DEFAULT_ABBREV);
372 if (ref->forced_update) {
373 strbuf_addstr(&quickref, "...");
@@ -378,7 +378,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
378 type = ' ';
379 msg = NULL;
380 }
381 - strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
381 + strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
382 DEFAULT_ABBREV);
383
384 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
wt-status.c
+3 -3
@@ -1188,7 +1188,7 @@ static void abbrev_sha1_in_line(struct strbuf *line)
1188 strbuf_trim(split[1]);
1189 if (!get_oid(split[1]->buf, &oid)) {
1190 strbuf_reset(split[1]);
1191 - strbuf_add_unique_abbrev(split[1], oid.hash,
1191 + strbuf_add_unique_abbrev(split[1], &oid,
1192 DEFAULT_ABBREV);
1193 strbuf_addch(split[1], ' ');
1194 strbuf_reset(line);
@@ -1422,7 +1422,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
1422 ;
1423 else if (!get_oid_hex(sb.buf, &oid)) {
1424 strbuf_reset(&sb);
1425 - strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
1425 + strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1426 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1427 goto got_nothing;
1428 else /* bisect */
@@ -1459,7 +1459,7 @@ static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
1459 if (!strcmp(cb->buf.buf, "HEAD")) {
1460 /* HEAD is relative. Resolve it to the right reflog entry. */
1461 strbuf_reset(&cb->buf);
1462 - strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
1462 + strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1463 }
1464 return 1;
1465 }