use strbuf_add_unique_abbrev() for adding short hashes, part 3

Call strbuf_add_unique_abbrev() to add abbreviated hashes to strbufs instead of taking detours through find_unique_abbrev() and its static buffer. This is shorter in most cases and a bit more efficient. The changes here are not easily handled by a semantic patch because they involve removing temporary variables and deconstructing format strings for strbuf_addf(). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 8, 2016 at 17:38 UTC a94bb683970a111b467a36590ca36e52754ad504
4 files changed +15 -20
merge-recursive.c
+3 -3
@@ -202,9 +202,9 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
202 strbuf_addf(&o->obuf, "virtual %s\n",
203 merge_remote_util(commit)->name);
204 else {
205 - strbuf_addf(&o->obuf, "%s ",
206 - find_unique_abbrev(commit->object.oid.hash,
207 - DEFAULT_ABBREV));
205 + strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
206 + DEFAULT_ABBREV);
207 + strbuf_addch(&o->obuf, ' ');
208 if (parse_commit(commit) != 0)
209 strbuf_addstr(&o->obuf, _("(bad commit)\n"));
210 else {
pretty.c
+5 -7
@@ -544,15 +544,13 @@ static void add_merge_info(const struct pretty_print_context *pp,
544 strbuf_addstr(sb, "Merge:");
545
546 while (parent) {
547 - struct commit *p = parent->item;
548 - const char *hex = NULL;
547 + struct object_id *oidp = &parent->item->object.oid;
548 + strbuf_addch(sb, ' ');
549 if (pp->abbrev)
550 - hex = find_unique_abbrev(p->object.oid.hash, pp->abbrev);
551 - if (!hex)
552 - hex = oid_to_hex(&p->object.oid);
550 + strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
551 + else
552 + strbuf_addstr(sb, oid_to_hex(oidp));
553 parent = parent->next;
554 -
555 - strbuf_addf(sb, " %s", hex);
554 }
555 strbuf_addch(sb, '\n');
556 }
submodule.c
+3 -4
@@ -370,10 +370,9 @@ void show_submodule_summary(FILE *f, const char *path,
370 return;
371 }
372
373 - strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
374 - find_unique_abbrev(one, DEFAULT_ABBREV));
375 - if (!fast_backward && !fast_forward)
376 - strbuf_addch(&sb, '.');
373 + strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
374 + strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
375 + strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
376 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
377 if (message)
378 strbuf_addf(&sb, " %s%s\n", message, reset);
wt-status.c
+4 -6
@@ -1053,7 +1053,6 @@ static void abbrev_sha1_in_line(struct strbuf *line)
1053 split = strbuf_split_max(line, ' ', 3);
1054 if (split[0] && split[1]) {
1055 unsigned char sha1[20];
1056 - const char *abbrev;
1056
1057 /*
1058 * strbuf_split_max left a space. Trim it and re-add
@@ -1061,9 +1060,10 @@ static void abbrev_sha1_in_line(struct strbuf *line)
1060 */
1061 strbuf_trim(split[1]);
1062 if (!get_sha1(split[1]->buf, sha1)) {
1064 - abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1063 strbuf_reset(split[1]);
1066 - strbuf_addf(split[1], "%s ", abbrev);
1064 + strbuf_add_unique_abbrev(split[1], sha1,
1065 + DEFAULT_ABBREV);
1066 + strbuf_addch(split[1], ' ');
1067 strbuf_reset(line);
1068 for (i = 0; split[i]; i++)
1069 strbuf_addbuf(line, split[i]);
@@ -1286,10 +1286,8 @@ static char *get_branch(const struct worktree *wt, const char *path)
1286 else if (starts_with(sb.buf, "refs/"))
1287 ;
1288 else if (!get_sha1_hex(sb.buf, sha1)) {
1289 - const char *abbrev;
1290 - abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1289 strbuf_reset(&sb);
1292 - strbuf_addstr(&sb, abbrev);
1290 + strbuf_add_unique_abbrev(&sb, sha1, DEFAULT_ABBREV);
1291 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1292 goto got_nothing;
1293 else /* bisect */