diff mbox series

[v3,1/2,GSOC] ref-filter: add objectsize to used_atom

Message ID 91ca57c9d04a822aa4955dbfe3962a6fb2444e81.1620821464.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series ref-filter: introduce enum atom_type | expand

Commit Message

ZheNing Hu May 12, 2021, 12:11 p.m. UTC
From: ZheNing Hu <adlternative@gmail.com>

Since "objectsize:size" is composed of two parts,
"type:attribute". However, the original implementation
did not decouple the two parts "type" and "attribute" well,
we still need to judge separately whether the atom is
"objectsize" or "objectsize:disk" in `grab_common_values()`.

Add a new member `objectsize` to the union `used_atom.u`,
so that we can separate the judgment of atom type from the
judgment of atom attribute, This will bring scalability to
atom `%(objectsize)`.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 ref-filter.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

Comments

Junio C Hamano May 12, 2021, 11:11 p.m. UTC | #1
"ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: ZheNing Hu <adlternative@gmail.com>
>
> Since "objectsize:size" is composed of two parts,
> "type:attribute". However, the original implementation

Y is missing in the above "Since X, Y".  Because it is composed of
two parts, what?

Dropping "Since " would make the sentence work better.  Also, I
think it should be "objectsize:disk".

> did not decouple the two parts "type" and "attribute" well,
> we still need to judge separately whether the atom is
> "objectsize" or "objectsize:disk" in `grab_common_values()`.

Perhaps

	When the support for "objectsize:disk" was bolted onto the
	existing support for "objectsize", it didn't follow the
	usual pattern for handling "atomtype:modifier", which reads
	the <modifier> part just once while parsing the format
	string, and store the parsed result in the union in the
	used_atom structure, so that the string form of it does not
	have to be parsed over and over at runtime (e.g. in
	grab_common_values()).

> Add a new member `objectsize` to the union `used_atom.u`,
> so that we can separate the judgment of atom type from the
> judgment of atom attribute, This will bring scalability to
> atom `%(objectsize)`.

OK.

> +		struct {
> +			enum { O_SIZE, O_SIZE_DISK } option;
> +		} objectsize;

OK.

> @@ -967,12 +972,14 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
>  			name++;
>  		if (!strcmp(name, "objecttype"))
>  			v->s = xstrdup(type_name(oi->type));
> +		else if (starts_with(name, "objectsize")) {
> +			if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
> +				v->value = oi->disk_size;
> +				v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
> +			} else if (used_atom[i].u.objectsize.option == O_SIZE) {
> +				v->value = oi->size;
> +				v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
> +			}

OK.
ZheNing Hu May 13, 2021, 9:04 a.m. UTC | #2
Junio C Hamano <gitster@pobox.com> 于2021年5月13日周四 上午7:11写道:
>
> "ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: ZheNing Hu <adlternative@gmail.com>
> >
> > Since "objectsize:size" is composed of two parts,
> > "type:attribute". However, the original implementation
>
> Y is missing in the above "Since X, Y".  Because it is composed of
> two parts, what?
>
> Dropping "Since " would make the sentence work better.  Also, I
> think it should be "objectsize:disk".
>

Oh, here is my typo.

> > did not decouple the two parts "type" and "attribute" well,
> > we still need to judge separately whether the atom is
> > "objectsize" or "objectsize:disk" in `grab_common_values()`.
>
> Perhaps
>
>         When the support for "objectsize:disk" was bolted onto the
>         existing support for "objectsize", it didn't follow the
>         usual pattern for handling "atomtype:modifier", which reads
>         the <modifier> part just once while parsing the format
>         string, and store the parsed result in the union in the
>         used_atom structure, so that the string form of it does not
>         have to be parsed over and over at runtime (e.g. in
>         grab_common_values()).
>

That’s great.

Thanks.
--
ZheNing Hu
diff mbox series

Patch

diff --git a/ref-filter.c b/ref-filter.c
index a0adb4551d87..f420bae6e5ba 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -146,6 +146,9 @@  static struct used_atom {
 			enum { O_FULL, O_LENGTH, O_SHORT } option;
 			unsigned int length;
 		} oid;
+		struct {
+			enum { O_SIZE, O_SIZE_DISK } option;
+		} objectsize;
 		struct email_option {
 			enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
 		} email_option;
@@ -269,11 +272,13 @@  static int objectsize_atom_parser(const struct ref_format *format, struct used_a
 				  const char *arg, struct strbuf *err)
 {
 	if (!arg) {
+		atom->u.objectsize.option = O_SIZE;
 		if (*atom->name == '*')
 			oi_deref.info.sizep = &oi_deref.size;
 		else
 			oi.info.sizep = &oi.size;
 	} else if (!strcmp(arg, "disk")) {
+		atom->u.objectsize.option = O_SIZE_DISK;
 		if (*atom->name == '*')
 			oi_deref.info.disk_sizep = &oi_deref.disk_size;
 		else
@@ -967,12 +972,14 @@  static void grab_common_values(struct atom_value *val, int deref, struct expand_
 			name++;
 		if (!strcmp(name, "objecttype"))
 			v->s = xstrdup(type_name(oi->type));
-		else if (!strcmp(name, "objectsize:disk")) {
-			v->value = oi->disk_size;
-			v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
-		} else if (!strcmp(name, "objectsize")) {
-			v->value = oi->size;
-			v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
+		else if (starts_with(name, "objectsize")) {
+			if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
+				v->value = oi->disk_size;
+				v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
+			} else if (used_atom[i].u.objectsize.option == O_SIZE) {
+				v->value = oi->size;
+				v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
+			}
 		} else if (!strcmp(name, "deltabase"))
 			v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
 		else if (deref)