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

Re: t5505-remote fails on Windows

From
Jeff King <peff@peff.net>
Date
Mar 19, 2009, 20:03 UTC
Message-ID
<20090319200308.GB17028@coredump.intra.peff.net>
In-Reply-To
<7vhc1pai84.fsf@gitster.siamese.dyndns.org>
On Thu, Mar 19, 2009 at 04:02:19AM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> > Do we really want an API for that?  Calling qsort() directly should be 
> > obvious enough, no?
> 
> I think so.  If it were done like this (notice the lack of double
> indirection in the cmp_fn signature):
> 
>     typedef int string_list_item_cmp_fn(const struct string_list_item *, const struct string_list_item *);
> 
>     void sort_string_list_with_fn(struct string_list *list, string_list_item_cmp_fn *);
> 
> it would have made more sense, though.

IIRC, that is actually not valid C according to the standard (that is, even though a void* can be implicitly assigned to any other pointer, a function taking a void* and a function taking another pointer do not necessarily have the same function signature or calling conventions). Which is why cmp_items in string-list.c already does the indirection.

That being said, I think this is probably one of those cases where it works fine on any sane platform that we care about and we can choose to ignore the standard.

Actually, I think an even nicer API would be to give a function that _just_ compares the util fields; string_list_sort() would sort on the "string" fields, and only call the util cmp to sort entries with equal strings. Sadly, because qsort lacks a slot for extra data to pass to the callback, implementing this ends up with a global variable:

  static int (*cmp_util)(const void *, const void *);
  int cmp_items(const void *a, const void *b)
  {
    const struct string_list_item *one = a;
    const struct string_list_item *two = b;
    int cmp = strcmp(one->string, two->string);
    return cmp ? cmp : cmp_util(one->util, b->util);
  }
  int sort_string_list_with_util(struct string_list *list,
                       int (*fn)(const void *, const void *))
  {
    cmp_util = fn;
    qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
  }
If only C had closures. ;)

But this is really getting beyond the issue at hand. I can submit a finalized patch; just let me know how you prefer it.

-Peff
Previous: Jay SoffianNext: Johannes Schindelin
Message 11 of 18 in “t5505-remote fails on Windows”
  1. Johannes SixtMar 18, 2009
  2. Jeff KingMar 19, 2009
  3. Jeff KingMar 19, 2009
  4. Jay SoffianMar 19, 2009
  5. Jeff KingMar 19, 2009
  6. Johannes SixtMar 19, 2009
  7. Jeff KingMar 19, 2009
  8. Johannes SchindelinMar 19, 2009
  9. Junio C HamanoMar 19, 2009
  10. Jay SoffianMar 19, 2009
  11. Jeff KingMar 19, 2009
  12. Johannes SchindelinMar 19, 2009
  13. remote: improve sorting of "configure for git push" listJeff King, Mar 22, 2009
  14. Jay SoffianMar 22, 2009
  15. Junio C HamanoMar 22, 2009
  16. Johannes SixtMar 23, 2009
  17. Junio C HamanoMar 23, 2009
  18. Jeff KingMar 19, 2009

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.