ref-filter: add obj-type check in grab contents

Only tag and commit objects use `grab_sub_body_contents()` to grab object contents in the current codebase. We want to teach the function to also handle blobs and trees to get their raw data, without parsing a blob (whose contents looks like a commit or a tag) incorrectly as a commit or a tag. So it's needed to pass a `struct expand_data *data` instread of only `void *buf` to both `grab_sub_body_contents()` and `grab_values()` to be able to check the object type. Skip the block of code that is specific to handling commits and tags early when the given object is of a wrong type to help later addition to handle other types of objects in this function. Reviewed-by: Jacob Keller <jacob.keller@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Hariom Verma <hariom18599@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: ZheNing Hu <adlternative@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed Jul 26, 2021 at 03:26 UTC 311d0b8e8e370bc9f6168af1f4e120d2b0975702
1 file changed +15 -9
ref-filter.c
+15 -9
@@ -1356,11 +1356,12 @@ static void append_lines(struct strbuf *out, const char *buf, unsigned long size
1356 }
1357
1358 /* See grab_values */
1359 -static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1359 +static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data)
1360 {
1361 int i;
1362 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1363 size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1364 + void *buf = data->content;
1365
1366 for (i = 0; i < used_atom_cnt; i++) {
1367 struct used_atom *atom = &used_atom[i];
@@ -1371,10 +1372,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1372 continue;
1373 if (deref)
1374 name++;
1374 - if (strcmp(name, "body") &&
1375 - !starts_with(name, "subject") &&
1376 - !starts_with(name, "trailers") &&
1377 - !starts_with(name, "contents"))
1375 +
1376 + if ((data->type != OBJ_TAG &&
1377 + data->type != OBJ_COMMIT) ||
1378 + (strcmp(name, "body") &&
1379 + !starts_with(name, "subject") &&
1380 + !starts_with(name, "trailers") &&
1381 + !starts_with(name, "contents")))
1382 continue;
1383 if (!subpos)
1384 find_subpos(buf,
@@ -1438,17 +1442,19 @@ static void fill_missing_values(struct atom_value *val)
1442 * pointed at by the ref itself; otherwise it is the object the
1443 * ref (which is a tag) refers to.
1444 */
1441 -static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)
1445 +static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data)
1446 {
1447 + void *buf = data->content;
1448 +
1449 switch (obj->type) {
1450 case OBJ_TAG:
1451 grab_tag_values(val, deref, obj);
1446 - grab_sub_body_contents(val, deref, buf);
1452 + grab_sub_body_contents(val, deref, data);
1453 grab_person("tagger", val, deref, buf);
1454 break;
1455 case OBJ_COMMIT:
1456 grab_commit_values(val, deref, obj);
1451 - grab_sub_body_contents(val, deref, buf);
1457 + grab_sub_body_contents(val, deref, data);
1458 grab_person("author", val, deref, buf);
1459 grab_person("committer", val, deref, buf);
1460 break;
@@ -1678,7 +1684,7 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj
1684 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1685 oid_to_hex(&oi->oid), ref->refname);
1686 }
1681 - grab_values(ref->value, deref, *obj, oi->content);
1687 + grab_values(ref->value, deref, *obj, oi);
1688 }
1689
1690 grab_common_values(ref->value, deref, oi);