Skip to content

Commit e339611

Browse files
telezhnayagitster
authored andcommitted
ref-filter: libify get_ref_atom_value()
Finish removing die() calls from ref-filter formatting logic, so that it could be used by other commands. Change the signature of get_ref_atom_value() and underlying functions by adding return value and strbuf parameter for error message. Return value equals 0 upon success and -1 upon failure. Upon failure, error message is appended to the strbuf. Signed-off-by: Olga Telezhnaia <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74efea9 commit e339611

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

ref-filter.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,28 +1427,30 @@ static const char *get_refname(struct used_atom *atom, struct ref_array_item *re
14271427
return show_ref(&atom->u.refname, ref->refname);
14281428
}
14291429

1430-
static void get_object(struct ref_array_item *ref, const struct object_id *oid,
1431-
int deref, struct object **obj)
1430+
static int get_object(struct ref_array_item *ref, const struct object_id *oid,
1431+
int deref, struct object **obj, struct strbuf *err)
14321432
{
14331433
int eaten;
1434+
int ret = 0;
14341435
unsigned long size;
14351436
void *buf = get_obj(oid, obj, &size, &eaten);
14361437
if (!buf)
1437-
die(_("missing object %s for %s"),
1438-
oid_to_hex(oid), ref->refname);
1439-
if (!*obj)
1440-
die(_("parse_object_buffer failed on %s for %s"),
1441-
oid_to_hex(oid), ref->refname);
1442-
1443-
grab_values(ref->value, deref, *obj, buf, size);
1438+
ret = strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1439+
oid_to_hex(oid), ref->refname);
1440+
else if (!*obj)
1441+
ret = strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1442+
oid_to_hex(oid), ref->refname);
1443+
else
1444+
grab_values(ref->value, deref, *obj, buf, size);
14441445
if (!eaten)
14451446
free(buf);
1447+
return ret;
14461448
}
14471449

14481450
/*
14491451
* Parse the object referred by ref, and grab needed value.
14501452
*/
1451-
static void populate_value(struct ref_array_item *ref)
1453+
static int populate_value(struct ref_array_item *ref, struct strbuf *err)
14521454
{
14531455
struct object *obj;
14541456
int i;
@@ -1570,16 +1572,17 @@ static void populate_value(struct ref_array_item *ref)
15701572
break;
15711573
}
15721574
if (used_atom_cnt <= i)
1573-
return;
1575+
return 0;
15741576

1575-
get_object(ref, &ref->objectname, 0, &obj);
1577+
if (get_object(ref, &ref->objectname, 0, &obj, err))
1578+
return -1;
15761579

15771580
/*
15781581
* If there is no atom that wants to know about tagged
15791582
* object, we are done.
15801583
*/
15811584
if (!need_tagged || (obj->type != OBJ_TAG))
1582-
return;
1585+
return 0;
15831586

15841587
/*
15851588
* If it is a tag object, see if we use a value that derefs
@@ -1593,20 +1596,23 @@ static void populate_value(struct ref_array_item *ref)
15931596
* is not consistent with what deref_tag() does
15941597
* which peels the onion to the core.
15951598
*/
1596-
get_object(ref, tagged, 1, &obj);
1599+
return get_object(ref, tagged, 1, &obj, err);
15971600
}
15981601

15991602
/*
16001603
* Given a ref, return the value for the atom. This lazily gets value
16011604
* out of the object by calling populate value.
16021605
*/
1603-
static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1606+
static int get_ref_atom_value(struct ref_array_item *ref, int atom,
1607+
struct atom_value **v, struct strbuf *err)
16041608
{
16051609
if (!ref->value) {
1606-
populate_value(ref);
1610+
if (populate_value(ref, err))
1611+
return -1;
16071612
fill_missing_values(ref->value);
16081613
}
16091614
*v = &ref->value[atom];
1615+
return 0;
16101616
}
16111617

16121618
/*
@@ -2130,9 +2136,13 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
21302136
int cmp;
21312137
cmp_type cmp_type = used_atom[s->atom].type;
21322138
int (*cmp_fn)(const char *, const char *);
2139+
struct strbuf err = STRBUF_INIT;
21332140

2134-
get_ref_atom_value(a, s->atom, &va);
2135-
get_ref_atom_value(b, s->atom, &vb);
2141+
if (get_ref_atom_value(a, s->atom, &va, &err))
2142+
die("%s", err.buf);
2143+
if (get_ref_atom_value(b, s->atom, &vb, &err))
2144+
die("%s", err.buf);
2145+
strbuf_release(&err);
21362146
cmp_fn = s->ignore_case ? strcasecmp : strcmp;
21372147
if (s->version)
21382148
cmp = versioncmp(va->s, vb->s);
@@ -2210,12 +2220,8 @@ int format_ref_array_item(struct ref_array_item *info,
22102220
if (cp < sp)
22112221
append_literal(cp, sp, &state);
22122222
pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
2213-
if (pos < 0) {
2214-
pop_stack_element(&state.stack);
2215-
return -1;
2216-
}
2217-
get_ref_atom_value(info, pos, &atomv);
2218-
if (atomv->handler(atomv, &state, error_buf)) {
2223+
if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
2224+
atomv->handler(atomv, &state, error_buf)) {
22192225
pop_stack_element(&state.stack);
22202226
return -1;
22212227
}

0 commit comments

Comments
 (0)