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

Re: [PATCH v5 7/8] branch.c: use 'ref-filter' APIs

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Sep 20, 2015, 19:24 UTC
Message-ID
<vpqmvwgevx3.fsf@anie.imag.fr>
In-Reply-To
<1442772627-25421-8-git-send-email-Karthik.188@gmail.com>
Karthik Nayak <karthik.188@gmail.com> writes:
Show 13 quoted lines
> --- a/Documentation/git-branch.txt
> +++ b/Documentation/git-branch.txt
> @@ -231,6 +231,13 @@ start-point is either a local or remote-tracking branch.
>  	The new name for an existing branch. The same restrictions as for
>  	<branchname> apply.
>  
> +--sort=<key>::
> +	Sort based on the key given. Prefix `-` to sort in descending
> +	order of the value. You may use the --sort=<key> option
> +	multiple times, in which case the last key becomes the primary
> +	key. The keys supported are the same as those in `git
> +	for-each-ref`. Sort order defaults to sorting based on branch
> +	type.
The last sentence is no longer true. Perhaps something like:

Sort order defaults to sorting based on the full refname (including `refs/...` prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches.

Show 11 quoted lines
> -static void print_ref_list(struct ref_filter *filter)
> +static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting)
>  {
>  	int i;
>  	struct ref_array array;
> -	struct ref_filter_cbdata data;
>  	int maxwidth = 0;
>  	const char *remote_prefix = "";
> -	struct rev_info revs;
> +	struct ref_sorting def_sorting;
> +	const char *sort_type = "refname";

You are using refname without special-casing HEAD at all. So, this is relying on the fact that HEAD comes alphabetically before refs/..., and that we are only listing refs/... and HEAD.

As I mentionned earlyer, if we ever allow branch to list e.g. FETCH_HEAD, then the detached HEAD will come in the middle. I first thought that this was too fragile, but after thinking about it, I think this is actually sensible. After all, if we ever allow FETCH_HEAD, then keeping the alphabetical order still makes sense.

So, your code is OK, but a bit too subtle to my taste: you should add a comment explaining why sorting according to refname is sufficient (I would use a comment, but a better commit message may be OK too).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Previous: Karthik NayakNext: Karthik Nayak
Message 13 of 19 in “port the filtering part of branch.c to use ref-filter APIs”
  1. 0/8 port the filtering part of branch.c to use ref-filter APIsKarthik Nayak, Sep 20, 2015
  2. 1/8 branch: refactor width computationKarthik Nayak, Sep 20, 2015
  3. 2/8 branch: bump get_head_description() to the topKarthik Nayak, Sep 20, 2015
  4. 3/8 branch: roll show_detached HEAD into regular ref_listKarthik Nayak, Sep 20, 2015
  5. Matthieu MoySep 20, 2015
  6. 4/8 branch: move 'current' check down to the presentation layerKarthik Nayak, Sep 20, 2015
  7. 5/8 branch: drop non-commit error reportingKarthik Nayak, Sep 20, 2015
  8. Matthieu MoySep 20, 2015
  9. Karthik NayakSep 21, 2015
  10. Karthik NayakSep 21, 2015
  11. 6/8 branch.c: use 'ref-filter' data structuresKarthik Nayak, Sep 20, 2015
  12. 7/8 branch.c: use 'ref-filter' APIsKarthik Nayak, Sep 20, 2015
  13. Matthieu MoySep 20, 2015
  14. Karthik NayakSep 21, 2015
  15. 8/8 branch: add '--points-at' optionKarthik Nayak, Sep 20, 2015
  16. Matthieu MoySep 20, 2015
  17. Karthik NayakSep 21, 2015
  18. Matthieu MoySep 21, 2015
  19. Karthik NayakSep 21, 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.