Skip to content

Commit a94bb68

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39ea59a commit a94bb68

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

merge-recursive.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
202202
strbuf_addf(&o->obuf, "virtual %s\n",
203203
merge_remote_util(commit)->name);
204204
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, ' ');
208208
if (parse_commit(commit) != 0)
209209
strbuf_addstr(&o->obuf, _("(bad commit)\n"));
210210
else {

pretty.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,13 @@ static void add_merge_info(const struct pretty_print_context *pp,
544544
strbuf_addstr(sb, "Merge:");
545545

546546
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, ' ');
549549
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));
553553
parent = parent->next;
554-
555-
strbuf_addf(sb, " %s", hex);
556554
}
557555
strbuf_addch(sb, '\n');
558556
}

submodule.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ void show_submodule_summary(FILE *f, const char *path,
370370
return;
371371
}
372372

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) ? ".." : "...");
377376
strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
378377
if (message)
379378
strbuf_addf(&sb, " %s%s\n", message, reset);

wt-status.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1053,17 +1053,17 @@ static void abbrev_sha1_in_line(struct strbuf *line)
10531053
split = strbuf_split_max(line, ' ', 3);
10541054
if (split[0] && split[1]) {
10551055
unsigned char sha1[20];
1056-
const char *abbrev;
10571056

10581057
/*
10591058
* strbuf_split_max left a space. Trim it and re-add
10601059
* it after abbreviation.
10611060
*/
10621061
strbuf_trim(split[1]);
10631062
if (!get_sha1(split[1]->buf, sha1)) {
1064-
abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
10651063
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], ' ');
10671067
strbuf_reset(line);
10681068
for (i = 0; split[i]; i++)
10691069
strbuf_addbuf(line, split[i]);
@@ -1286,10 +1286,8 @@ static char *get_branch(const struct worktree *wt, const char *path)
12861286
else if (starts_with(sb.buf, "refs/"))
12871287
;
12881288
else if (!get_sha1_hex(sb.buf, sha1)) {
1289-
const char *abbrev;
1290-
abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
12911289
strbuf_reset(&sb);
1292-
strbuf_addstr(&sb, abbrev);
1290+
strbuf_add_unique_abbrev(&sb, sha1, DEFAULT_ABBREV);
12931291
} else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
12941292
goto got_nothing;
12951293
else /* bisect */

0 commit comments

Comments
 (0)