Skip to content

Commit 49f92f4

Browse files
committed
Merge branch 'rs/strbuf-add-oid-hex' into seen
Formatting object name in full hexadecimal form has been optimized by using a new strbuf_add_oid_hex() helper function. Comments? * rs/strbuf-add-oid-hex: hex: add and use strbuf_add_oid_hex()
2 parents 39e18be + 63621bc commit 49f92f4

14 files changed

Lines changed: 37 additions & 17 deletions

File tree

bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static char *join_oid_array_hex(struct oid_array *array, char delim)
512512
int i;
513513

514514
for (i = 0; i < array->nr; i++) {
515-
strbuf_addstr(&joined_hexs, oid_to_hex(array->oid + i));
515+
strbuf_add_oid_hex(&joined_hexs, array->oid + i);
516516
if (i + 1 < array->nr)
517517
strbuf_addch(&joined_hexs, delim);
518518
}

builtin/bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
836836
if (!repo_get_oid(the_repository, head, &head_oid) &&
837837
!starts_with(head, "refs/heads/")) {
838838
strbuf_reset(&start_head);
839-
strbuf_addstr(&start_head, oid_to_hex(&head_oid));
839+
strbuf_add_oid_hex(&start_head, &head_oid);
840840
} else if (!repo_get_oid(the_repository, head, &head_oid) &&
841841
skip_prefix(head, "refs/heads/", &head)) {
842842
strbuf_addstr(&start_head, head);

builtin/cat-file.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
334334
{
335335
if (is_atom("objectname", atom, len)) {
336336
if (!data->mark_query)
337-
strbuf_addstr(sb, oid_to_hex(&data->oid));
337+
strbuf_add_oid_hex(sb, &data->oid);
338338
} else if (is_atom("objecttype", atom, len)) {
339339
if (data->mark_query)
340340
data->info.typep = &data->type;
@@ -359,8 +359,7 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
359359
if (data->mark_query)
360360
data->info.delta_base_oid = &data->delta_base_oid;
361361
else
362-
strbuf_addstr(sb,
363-
oid_to_hex(&data->delta_base_oid));
362+
strbuf_add_oid_hex(sb, &data->delta_base_oid);
364363
} else if (is_atom("objectmode", atom, len)) {
365364
if (!data->mark_query && !(S_IFINVALID == data->mode))
366365
strbuf_addf(sb, "%06o", data->mode);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
127127
}
128128

129129
strbuf_setlen(&ref, base_len);
130-
strbuf_addstr(&ref, oid_to_hex(&oid));
130+
strbuf_add_oid_hex(&ref, &oid);
131131
full_hex = ref.buf + base_len;
132132

133133
if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) {

convert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ static int ident_to_worktree(const char *src, size_t len,
12391239

12401240
/* step 4: substitute */
12411241
strbuf_addstr(buf, "Id: ");
1242-
strbuf_addstr(buf, oid_to_hex(&oid));
1242+
strbuf_add_oid_hex(buf, &oid);
12431243
strbuf_addstr(buf, " $");
12441244
}
12451245
strbuf_add(buf, src, len);

fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ const char *fsck_describe_object(struct fsck_options *options,
344344
buf = bufs + b;
345345
b = (b + 1) % ARRAY_SIZE(bufs);
346346
strbuf_reset(buf);
347-
strbuf_addstr(buf, oid_to_hex(oid));
347+
strbuf_add_oid_hex(buf, oid);
348348
if (name)
349349
strbuf_addf(buf, " (%s)", name);
350350

hex.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "git-compat-util.h"
44
#include "hash.h"
55
#include "hex.h"
6+
#include "strbuf.h"
67

78
static int get_hash_hex_algop(const char *hex, unsigned char *hash,
89
const struct git_hash_algo *algop)
@@ -122,3 +123,12 @@ char *oid_to_hex(const struct object_id *oid)
122123
{
123124
return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]);
124125
}
126+
127+
void strbuf_add_oid_hex(struct strbuf *sb, const struct object_id *oid)
128+
{
129+
const struct git_hash_algo *algop = oid->algo ?
130+
&hash_algos[oid->algo] : the_hash_algo;
131+
strbuf_grow(sb, algop->hexsz);
132+
hash_to_hex_algop_r(sb->buf + sb->len, oid->hash, algop);
133+
strbuf_setlen(sb, sb->len + algop->hexsz);
134+
}

hex.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ char *oid_to_hex_r(char *out, const struct object_id *oid);
3333
char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
3434
char *oid_to_hex(const struct object_id *oid); /* same static buffer */
3535

36+
struct strbuf;
37+
38+
/* Apply oid_to_hex_r() to a strbuf to append the hexadecimal hash. */
39+
void strbuf_add_oid_hex(struct strbuf *sb, const struct object_id *oid);
40+
3641
/*
3742
* Parse a 40-character hexadecimal object ID starting from hex, updating the
3843
* pointer specified by end when parsing stops. The resulting object ID is

pretty.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ static void add_merge_info(const struct pretty_print_context *pp,
661661
if (pp->abbrev)
662662
strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
663663
else
664-
strbuf_addstr(sb, oid_to_hex(oidp));
664+
strbuf_add_oid_hex(sb, oidp);
665665
parent = parent->next;
666666
}
667667
strbuf_addch(sb, '\n');
@@ -1566,7 +1566,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15661566
switch (placeholder[0]) {
15671567
case 'H': /* commit hash */
15681568
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1569-
strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
1569+
strbuf_add_oid_hex(sb, &commit->object.oid);
15701570
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
15711571
return 1;
15721572
case 'h': /* abbreviated commit hash */
@@ -1576,7 +1576,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15761576
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
15771577
return 1;
15781578
case 'T': /* tree hash */
1579-
strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit)));
1579+
strbuf_add_oid_hex(sb, get_commit_tree_oid(commit));
15801580
return 1;
15811581
case 't': /* abbreviated tree hash */
15821582
strbuf_add_unique_abbrev(sb,
@@ -1587,7 +1587,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15871587
for (p = commit->parents; p; p = p->next) {
15881588
if (p != commit->parents)
15891589
strbuf_addch(sb, ' ');
1590-
strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
1590+
strbuf_add_oid_hex(sb, &p->item->object.oid);
15911591
}
15921592
return 1;
15931593
case 'p': /* abbreviated parent hashes */

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2532,7 +2532,7 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,
25322532
if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
25332533
struct object_id oid;
25342534
if (!refs_read_ref(refs, ref, &oid)) {
2535-
strbuf_addstr(referent, oid_to_hex(&oid));
2535+
strbuf_add_oid_hex(referent, &oid);
25362536
ret = NOT_A_SYMREF;
25372537
}
25382538
}

0 commit comments

Comments
 (0)