git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 05/10] ref-filter: add support to sort by version

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 25, 2015, 22:40 UTC
Message-ID
<xmqqd1zfkgi3.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1437764685-8633-6-git-send-email-Karthik.188@gmail.com>
Karthik Nayak <karthik.188@gmail.com> writes:
Show 27 quoted lines
> @@ -1180,19 +1181,17 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
>  
>  	get_ref_atom_value(&state, a, s->atom, &va);
>  	get_ref_atom_value(&state, b, s->atom, &vb);
> -	switch (cmp_type) {
> -	case FIELD_STR:
> +	if (s->version)
> +		cmp = versioncmp(va->s, vb->s);
> +	else if (cmp_type == FIELD_STR)
>  		cmp = strcmp(va->s, vb->s);
> -		break;
> -	default:
> -		if (va->ul < vb->ul)
> -			cmp = -1;
> -		else if (va->ul == vb->ul)
> -			cmp = 0;
> -		else
> -			cmp = 1;
> -		break;
> -	}
> +	else if (va->ul < vb->ul)
> +		cmp = -1;
> +	else if (va->ul == vb->ul)
> +		cmp = 0;
> +	else
> +		cmp = 1;
> +
So there are generally three kinds of comparison possible:
    - if it is to be compared as versions, do versioncmp
    - if it is to be compared as strings, do strcmp
    - if it is to be compared as numbers, do <=> but because
      we are writing in C, not in Perl, do so as if/else/else

Having understood that, the above is not really easy to read and extend. We should structure the above more like this:

	if (s->version)
        	... versioncmp
	else if (... FIELD_STR)
        	... strcmp
	else {
		if (a < b)
                	...
		else if (a == b)
                	...
		else
                	...
        }

so that it would be obvious how this code need to be updated when we need to add yet another kind of comparison.

Without looking at the callers, s->version looks like a misdesign that should be updated to use the same cmp_type mechanism? That would lead to even more obvious construct that is easy to enhance, i.e.

	switch (cmp_type) {
        case CMP_VERSION:
        	...
	case CMP_STRING:
        	...
	case CMP_NUMBER:
        	...
        }
        
I dunno.

Other than that (and the structure of that "format-state" stuff we discussed separately), the series was a pleasant read.

Thanks.
Previous: Karthik NayakNext: Karthik Nayak
Message 26 of 36 in “port tag.c to use ref-filter APIs”
  1. 0/10 port tag.c to use ref-filter APIsKarthik Nayak, Jul 24, 2015
  2. 01/10 ref-filter: add option to align atoms to the leftKarthik Nayak, Jul 24, 2015
  3. Junio C HamanoJul 24, 2015
  4. Junio C HamanoJul 24, 2015
  5. Karthik NayakJul 25, 2015
  6. Eric SunshineJul 26, 2015
  7. Karthik NayakJul 26, 2015
  8. Eric SunshineJul 26, 2015
  9. Karthik NayakJul 26, 2015
  10. Matthieu MoyJul 27, 2015
  11. Duy NguyenJul 27, 2015
  12. Jacob KellerJul 27, 2015
  13. Duy NguyenJul 27, 2015
  14. Duy NguyenJul 28, 2015
  15. 02/10 ref-filter: make the 'color' use ref_formatting_stateKarthik Nayak, Jul 24, 2015
  16. Junio C HamanoJul 24, 2015
  17. Karthik NayakJul 25, 2015
  18. Eric SunshineJul 26, 2015
  19. Eric SunshineJul 26, 2015
  20. Karthik NayakJul 26, 2015
  21. 03/10 ref-filter: add option to filter only tagsKarthik Nayak, Jul 24, 2015
  22. 04/10 ref-filter: support printing N lines from tag annotationKarthik Nayak, Jul 24, 2015
  23. Eric SunshineJul 26, 2015
  24. Karthik NayakJul 26, 2015
  25. 05/10 ref-filter: add support to sort by versionKarthik Nayak, Jul 24, 2015
  26. Junio C HamanoJul 25, 2015
  27. Karthik NayakJul 26, 2015
  28. Junio C HamanoJul 27, 2015
  29. Karthik NayakJul 27, 2015
  30. 06/10 ref-filter: add option to match literal patternKarthik Nayak, Jul 24, 2015
  31. Eric SunshineJul 26, 2015
  32. Karthik NayakJul 26, 2015
  33. 07/10 tag.c: use 'ref-filter' data structuresKarthik Nayak, Jul 24, 2015
  34. 08/10 tag.c: use 'ref-filter' APIsKarthik Nayak, Jul 24, 2015
  35. 09/10 tag.c: implement '--format' optionKarthik Nayak, Jul 24, 2015
  36. 10/10 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Jul 24, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.