{"thread":{"id":"63862","subject":"[PATCH] completion: new config var to use --sort in for-each-ref","startedAt":"2025-07-27T14:49:37Z","lastAt":"2025-07-28T17:13:26Z","messageCount":4,"participants":["Nelson Benitez Leon via GitGitGadget","Nelson Benítez León","Junio C Hamano","SZEDER Gábor"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"522824","messageId":"pull.1946.git.1753627773304.gitgitgadget@gmail.com","threadId":"63862","inReplyTo":null,"subject":"[PATCH] completion: new config var to use --sort in for-each-ref","fromName":"Nelson Benitez Leon via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-07-27T14:49:33Z","receivedAt":"2025-07-27T14:49:37Z","isPatch":true,"sender":{"key":"nelsonjesus.benitez@seap.minhap.es","avatar":null},"body":"From: =?UTF-8?q?Nelson=20Ben=C3=ADtez=20Le=C3=B3n?= <nbenitezl@gmail.com>\n\nCurrently when completing refs, e.g. by doing \"git checkout <TAB>\", all\nrefs are shown in alphabetical order, this is an implicit ordering and\ncannot be changed.\n\nThis commit will make the sort criteria to now be explicit, mandated by\na new config var which will be used for the --sort=<val> of for-each-ref\n\nThis new config var will have a default value of alphabetical order,\nso Git's default behaviour remains unchanged.\n\nAlso add '-o nosort' to 'complete' to disable its default alphabetical\nordering so our new explicit ordering prevails.\n\nSigned-off-by: Nelson Benítez León <nbenitezl@gmail.com>\n---\n    completion: new config var to use --sort in for-each-ref\n    \n    Hi, I'm submitting a patch for the Bash completion script, to be able to\n    change the default implicit alphabetical ordering used when returning\n    refs e.g. when doing \"git checkout \"\n    \n    I wanted the completed refs to be sorted by \"recently worked on\", I\n    achieve it by using committer date field in descending order i.e.\n    --sort=\"-committerdate\" because that shows on top the branches that have\n    recently been worked on. The completion script does not allow to set a\n    custom sort order, so we're stuck with the default alphabetical one, so\n    I'm sending a patch which adds a new config var where the user can set\n    their desired custom sort criteria.\n    \n    I've not added tests because I'm not familiar with the test machinery,\n    hopefully this is still useful.\n    \n    I'd also like to ask the Git audience about their preference for\n    changing the default sort value in a future patch:\n    \n     1. stay the same (alphabetical order)\n     2. change it to show recently worked on branches first (like me)\n    \n    I people agree 2. is more useful then we can change it in a follow-up\n    patch.\n    \n    Regards,\n    \n    PD. I previously sent this to the mailing list but resulted in a bad\n    formatted email because I use Gmail (and I don't want to activate 2FA\n    authentication just for this) so I'm sending this time through\n    GitGitGadget and incorporating some fixes from review comments I got,\n    like adapting commit message to 72 chars wide.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1946%2Fnbenitez%2Fbash_completion_explicit_sort-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1946/nbenitez/bash_completion_explicit_sort-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1946\n\n contrib/completion/git-completion.bash | 56 +++++++++++++++++++++-----\n 1 file changed, 47 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex e3d88b06721..59964a8056e 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -80,12 +80,37 @@\n #     When set, uses for-each-ref '--ignore-case' to find refs that match\n #     case insensitively, even on systems with case sensitive file systems\n #     (e.g., completing tag name \"FOO\" on \"git checkout f<TAB>\").\n+#\n+#   GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\n+#\n+#     Fieldname string to use for --sort option of for-each-ref. If empty or\n+#     not defined it defaults to \"refname\" which is the same default git uses\n+#     when no --sort option is provided. Some example values:\n+#       '-committerdate' to descending sort by committer date\n+#       '-version:refname' to descending sort by refname interpreted as version\n+#       More info and examples: https://git-scm.com/docs/git-for-each-ref#_field_names\n \n case \"$COMP_WORDBREAKS\" in\n *:*) : great ;;\n *)   COMP_WORDBREAKS=\"$COMP_WORDBREAKS:\"\n esac\n \n+# Reads and validates GIT_COMPLETION_REFS_SORT_BY_FIELDNAME configuration var,\n+# returning the content of it when it's valid, or if not valid or is empty or\n+# not defined, then it returns the documented default i.e. 'refname'.\n+__git_get_sort_by_fieldname ()\n+{\n+\tif [ -n \"${GIT_COMPLETION_REFS_SORT_BY_FIELDNAME-}\" ]; then\n+\t\t# Validate by using a regex pattern which only allows a set\n+\t\t# of characters that may appear in a --sort expression\n+        if [[ \"$GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\" =~ ^[a-zA-Z0-9%:=*(),_\\ -]+$ ]]; then\n+            echo \"$GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\"\n+            return\n+        fi\n+\tfi\n+\techo 'refname'\n+}\n+\n # Discovers the path to the git repository taking any '--git-dir=<path>' and\n # '-C <path>' options into account and stores it in the $__git_repo_path\n # variable.\n@@ -751,7 +776,9 @@ __git_heads ()\n {\n \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n \n-\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n+\tlocal sortby=$(__git_get_sort_by_fieldname)\n+\n+\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n \t\t\t\"refs/heads/$cur_*\" \"refs/heads/$cur_*/**\"\n }\n@@ -765,7 +792,9 @@ __git_remote_heads ()\n {\n \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n \n-\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n+\tlocal sortby=$(__git_get_sort_by_fieldname)\n+\n+\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n \t\t\t\"refs/remotes/$cur_*\" \"refs/remotes/$cur_*/**\"\n }\n@@ -776,7 +805,9 @@ __git_tags ()\n {\n \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n \n-\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n+\tlocal sortby=$(__git_get_sort_by_fieldname)\n+\n+\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n \t\t\t\"refs/tags/$cur_*\" \"refs/tags/$cur_*/**\"\n }\n@@ -818,7 +849,9 @@ __git_dwim_remote_heads ()\n \t\t}\n \t}\n \t'\n-\t__git for-each-ref --format='%(refname)' refs/remotes/ |\n+\tlocal sortby=$(__git_get_sort_by_fieldname)\n+\n+\t__git for-each-ref --sort=\"$sortby\" --format='%(refname)' refs/remotes/ |\n \t\tPFX=\"$pfx\" SFX=\"$sfx\" CUR_=\"$cur_\" \\\n \t\t\tIGNORE_CASE=${GIT_COMPLETION_IGNORE_CASE+1} \\\n \t\t\tREMOTES=\"$(__git_remotes | sort -r)\" awk \"$awk_script\" |\n@@ -847,6 +880,7 @@ __git_refs ()\n \tlocal match=\"${4-}\"\n \tlocal umatch=\"${4-}\"\n \tlocal fer_pfx=\"${pfx//\\%/%%}\" # \"escape\" for-each-ref format specifiers\n+\tlocal sortby=$(__git_get_sort_by_fieldname)\n \n \t__git_find_repo_path\n \tdir=\"$__git_repo_path\"\n@@ -905,7 +939,8 @@ __git_refs ()\n \t\t\t\t\"refs/remotes/$match*\" \"refs/remotes/$match*/**\")\n \t\t\t;;\n \t\tesac\n-\t\t__git_dir=\"$dir\" __git for-each-ref --format=\"$fer_pfx%($format)$sfx\" \\\n+\t\t__git_dir=\"$dir\" __git for-each-ref --sort=\"$sortby\" \\\n+\t\t\t--format=\"$fer_pfx%($format)$sfx\" \\\n \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n \t\t\t\"${refs[@]}\"\n \t\tif [ -n \"$track\" ]; then\n@@ -929,7 +964,8 @@ __git_refs ()\n \t\t\t$match*|$umatch*)\techo \"${pfx}HEAD$sfx\" ;;\n \t\t\tesac\n \t\t\tlocal strip=\"$(__git_count_path_components \"refs/remotes/$remote\")\"\n-\t\t\t__git for-each-ref --format=\"$fer_pfx%(refname:strip=$strip)$sfx\" \\\n+\t\t\t__git for-each-ref --sort=\"$sortby\" \\\n+\t\t\t\t--format=\"$fer_pfx%(refname:strip=$strip)$sfx\" \\\n \t\t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n \t\t\t\t\"refs/remotes/$remote/$match*\" \\\n \t\t\t\t\"refs/remotes/$remote/$match*/**\"\n@@ -2861,7 +2897,8 @@ __git_complete_config_variable_value ()\n \tremote.*.push)\n \t\tlocal remote=\"${varname#remote.}\"\n \t\tremote=\"${remote%.push}\"\n-\t\t__gitcomp_nl \"$(__git for-each-ref \\\n+\t\tlocal sortby=$(__git_get_sort_by_fieldname)\n+\t\t__gitcomp_nl \"$(__git for-each-ref --sort=\"$sortby\" \\\n \t\t\t--format='%(refname):%(refname)' refs/heads)\" \"\" \"$cur_\"\n \t\treturn\n \t\t;;\n@@ -3983,8 +4020,9 @@ ___git_complete ()\n {\n \tlocal wrapper=\"__git_wrap${2}\"\n \teval \"$wrapper () { __git_func_wrap $2 ; }\"\n-\tcomplete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \\\n-\t\t|| complete -o default -o nospace -F $wrapper $1\n+\tcomplete -o bashdefault -o default -o nospace -o nosort \\\n+\t\t-F $wrapper $1 2>/dev/null \\\n+\t\t|| complete -o default -o nospace -o nosort -F $wrapper $1\n }\n \n # Setup the completion for git commands\n\nbase-commit: e4ef0485fd78fcb05866ea78df35796b904e4a8e\n-- \ngitgitgadget\n"},{"id":"522825","messageId":"CAAUd640D3knxury+E07mh6sLac2YPHiYg5r73Q9PnPMFTDi=0w@mail.gmail.com","threadId":"63862","inReplyTo":"pull.1946.git.1753627773304.gitgitgadget@gmail.com","subject":"Re: [PATCH] completion: new config var to use --sort in for-each-ref","fromName":"Nelson Benítez León","fromEmail":"nbenitezl@gmail.com","sentAt":"2025-07-27T15:06:35Z","receivedAt":"2025-07-27T15:07:14Z","isPatch":true,"sender":{"key":"nbenitezl@gmail.com","avatar":"https://gravatar.com/avatar/43794fe96eb6be6a99b1334207fa0fffa2c6606a64e2abd5ea994ffb65ddeab3?d=mp&s=160"},"body":">     Hi, I'm submitting a patch for the Bash completion script, to be able to\n>     change the default implicit alphabetical ordering used when returning\n>     refs e.g. when doing \"git checkout \"\n\nthat should be \"git checkout <TAB>\" (got removed by Github apparently).\n"},{"id":"522838","messageId":"87bjp56zs0.fsf@gitster.g","threadId":"63862","inReplyTo":"pull.1946.git.1753627773304.gitgitgadget@gmail.com","subject":"Re: [PATCH] completion: new config var to use --sort in for-each-ref","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-07-27T22:21:19Z","receivedAt":"2025-07-27T22:21:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Nelson Benitez Leon via GitGitGadget\" <gitgitgadget@gmail.com>\nwrites:\n\n> From: =?UTF-8?q?Nelson=20Ben=C3=ADtez=20Le=C3=B3n?= <nbenitezl@gmail.com>\n>\n> Currently when completing refs, e.g. by doing \"git checkout <TAB>\", all\n> refs are shown in alphabetical order, this is an implicit ordering and\n> cannot be changed.\n>\n> This commit will make the sort criteria to now be explicit, mandated by\n> a new config var which will be used for the --sort=<val> of for-each-ref\n>\n> This new config var will have a default value of alphabetical order,\n> so Git's default behaviour remains unchanged.\n>\n> Also add '-o nosort' to 'complete' to disable its default alphabetical\n> ordering so our new explicit ordering prevails.\n\nThe usual way to compose a log message of this project is to\n\n - Give an observation on how the current system works in the\n   present tense (so no need to say \"Currently X is Y\", or\n   \"Previously X was Y\" to describe the state before your change;\n   just \"X is Y\" is enough), and discuss what you perceive as a\n   problem in it.\n\n - Propose a solution (optional---often, problem description\n   trivially leads to an obvious solution in reader's minds).\n\n - Give commands to somebody editing the codebase to \"make it so\".\n   There is no place to say \"this commit will\"; instead, give order\n   to the code.\n\nin this order.\n\n> Signed-off-by: Nelson Benítez León <nbenitezl@gmail.com>\n> ---\n\n\n>     I've not added tests because I'm not familiar with the test machinery,\n>     hopefully this is still useful.\n\nTests are required part of a new feature to become part of Git\ncodebase.  Otherwise you cannot notice if somebody else later\nchanges the code further and accidentally breaks your feature.\n\nYou can of course ask for help (i.e. \"I'm not familiar with the test\nmachinery so I am not writing any\" is *not* acceptable.  \"I'm not\nfamiliar with the test machinery; among those who may find this new\nfeature worthwhile to do, could somebody lend me a hand?\" is very\ngood).\n\n>     ... and I don't want to activate 2FA\n>     authentication just for this ...\n\nYou could do OAuth these days, I hear?\n"},{"id":"522867","messageId":"aIevs9Y9/0AlzzhP@szeder.dev","threadId":"63862","inReplyTo":"pull.1946.git.1753627773304.gitgitgadget@gmail.com","subject":"Re: [PATCH] completion: new config var to use --sort in for-each-ref","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2025-07-28T17:13:23Z","receivedAt":"2025-07-28T17:13:26Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Sun, Jul 27, 2025 at 02:49:33PM +0000, Nelson Benitez Leon via GitGitGadget wrote:\n> From: =?UTF-8?q?Nelson=20Ben=C3=ADtez=20Le=C3=B3n?= <nbenitezl@gmail.com>\n> \n> Currently when completing refs, e.g. by doing \"git checkout <TAB>\", all\n> refs are shown in alphabetical order, this is an implicit ordering and\n> cannot be changed.\n> \n> This commit will make the sort criteria to now be explicit, mandated by\n> a new config var which will be used for the --sort=<val> of for-each-ref\n\nBut why would you want to use any other ordering?!\n\n> This new config var will have a default value of alphabetical order,\n> so Git's default behaviour remains unchanged.\n> \n> Also add '-o nosort' to 'complete' to disable its default alphabetical\n> ordering so our new explicit ordering prevails.\n> \n> Signed-off-by: Nelson Benítez León <nbenitezl@gmail.com>\n> ---\n>     completion: new config var to use --sort in for-each-ref\n>     \n>     Hi, I'm submitting a patch for the Bash completion script, to be able to\n>     change the default implicit alphabetical ordering used when returning\n>     refs e.g. when doing \"git checkout \"\n>     \n>     I wanted the completed refs to be sorted by \"recently worked on\", I\n>     achieve it by using committer date field in descending order i.e.\n>     --sort=\"-committerdate\" because that shows on top the branches that have\n>     recently been worked on.\n\nAh, that's why :)\nThis would be a good addition to the log message, and perhaps a\nsufficient justification for the proposed change.\n\n> The completion script does not allow to set a\n>     custom sort order, so we're stuck with the default alphabetical one, so\n>     I'm sending a patch which adds a new config var where the user can set\n>     their desired custom sort criteria.\n>     \n>     I've not added tests because I'm not familiar with the test machinery,\n>     hopefully this is still useful.\n>     \n>     I'd also like to ask the Git audience about their preference for\n>     changing the default sort value in a future patch:\n>     \n>      1. stay the same (alphabetical order)\n>      2. change it to show recently worked on branches first (like me)\n>     \n>     I people agree 2. is more useful then we can change it in a follow-up\n>     patch.\n>     \n>     Regards,\n>     \n>     PD. I previously sent this to the mailing list but resulted in a bad\n>     formatted email because I use Gmail (and I don't want to activate 2FA\n>     authentication just for this) so I'm sending this time through\n>     GitGitGadget and incorporating some fixes from review comments I got,\n>     like adapting commit message to 72 chars wide.\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1946%2Fnbenitez%2Fbash_completion_explicit_sort-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1946/nbenitez/bash_completion_explicit_sort-v1\n> Pull-Request: https://github.com/gitgitgadget/git/pull/1946\n> \n>  contrib/completion/git-completion.bash | 56 +++++++++++++++++++++-----\n>  1 file changed, 47 insertions(+), 9 deletions(-)\n> \n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index e3d88b06721..59964a8056e 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -80,12 +80,37 @@\n>  #     When set, uses for-each-ref '--ignore-case' to find refs that match\n>  #     case insensitively, even on systems with case sensitive file systems\n>  #     (e.g., completing tag name \"FOO\" on \"git checkout f<TAB>\").\n> +#\n> +#   GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\n> +#\n> +#     Fieldname string to use for --sort option of for-each-ref. If empty or\n> +#     not defined it defaults to \"refname\" which is the same default git uses\n> +#     when no --sort option is provided. Some example values:\n> +#       '-committerdate' to descending sort by committer date\n> +#       '-version:refname' to descending sort by refname interpreted as version\n> +#       More info and examples: https://git-scm.com/docs/git-for-each-ref#_field_names\n\nThis approach allows only one sort key to be specified, although 'git\nfor-each-ref' supports more by accepting multiple --sort options.\n\n>  case \"$COMP_WORDBREAKS\" in\n>  *:*) : great ;;\n>  *)   COMP_WORDBREAKS=\"$COMP_WORDBREAKS:\"\n>  esac\n>  \n> +# Reads and validates GIT_COMPLETION_REFS_SORT_BY_FIELDNAME configuration var,\n> +# returning the content of it when it's valid, or if not valid or is empty or\n> +# not defined, then it returns the documented default i.e. 'refname'.\n> +__git_get_sort_by_fieldname ()\n> +{\n> +\tif [ -n \"${GIT_COMPLETION_REFS_SORT_BY_FIELDNAME-}\" ]; then\n> +\t\t# Validate by using a regex pattern which only allows a set\n> +\t\t# of characters that may appear in a --sort expression\n> +        if [[ \"$GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\" =~ ^[a-zA-Z0-9%:=*(),_\\ -]+$ ]]; then\n> +            echo \"$GIT_COMPLETION_REFS_SORT_BY_FIELDNAME\"\n> +            return\n> +        fi\n> +\tfi\n> +\techo 'refname'\n\nPrinting the sort key to the function's stdout requires that it's\ncalled in a command substitution.  Forking a subshell for a command\nsubstitution is very expensive on Windows, therefore we should try to\navoid that in new helper functions, if possible.\n\nPlease consider returning the sort key in a particular variable that\nis specified as local in the function's callers.  See __git_decode()\nfor an example.\n\n> +}\n> +\n>  # Discovers the path to the git repository taking any '--git-dir=<path>' and\n>  # '-C <path>' options into account and stores it in the $__git_repo_path\n>  # variable.\n> @@ -751,7 +776,9 @@ __git_heads ()\n>  {\n>  \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n>  \n> -\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n> +\tlocal sortby=$(__git_get_sort_by_fieldname)\n> +\n> +\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n>  \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n>  \t\t\t\"refs/heads/$cur_*\" \"refs/heads/$cur_*/**\"\n>  }\n> @@ -765,7 +792,9 @@ __git_remote_heads ()\n>  {\n>  \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n>  \n> -\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n> +\tlocal sortby=$(__git_get_sort_by_fieldname)\n> +\n> +\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n>  \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n>  \t\t\t\"refs/remotes/$cur_*\" \"refs/remotes/$cur_*/**\"\n>  }\n> @@ -776,7 +805,9 @@ __git_tags ()\n>  {\n>  \tlocal pfx=\"${1-}\" cur_=\"${2-}\" sfx=\"${3-}\"\n>  \n> -\t__git for-each-ref --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n> +\tlocal sortby=$(__git_get_sort_by_fieldname)\n> +\n> +\t__git for-each-ref --sort=\"$sortby\" --format=\"${pfx//\\%/%%}%(refname:strip=2)$sfx\" \\\n>  \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n>  \t\t\t\"refs/tags/$cur_*\" \"refs/tags/$cur_*/**\"\n>  }\n> @@ -818,7 +849,9 @@ __git_dwim_remote_heads ()\n>  \t\t}\n>  \t}\n>  \t'\n> -\t__git for-each-ref --format='%(refname)' refs/remotes/ |\n> +\tlocal sortby=$(__git_get_sort_by_fieldname)\n> +\n> +\t__git for-each-ref --sort=\"$sortby\" --format='%(refname)' refs/remotes/ |\n>  \t\tPFX=\"$pfx\" SFX=\"$sfx\" CUR_=\"$cur_\" \\\n>  \t\t\tIGNORE_CASE=${GIT_COMPLETION_IGNORE_CASE+1} \\\n>  \t\t\tREMOTES=\"$(__git_remotes | sort -r)\" awk \"$awk_script\" |\n> @@ -847,6 +880,7 @@ __git_refs ()\n>  \tlocal match=\"${4-}\"\n>  \tlocal umatch=\"${4-}\"\n>  \tlocal fer_pfx=\"${pfx//\\%/%%}\" # \"escape\" for-each-ref format specifiers\n> +\tlocal sortby=$(__git_get_sort_by_fieldname)\n>  \n>  \t__git_find_repo_path\n>  \tdir=\"$__git_repo_path\"\n> @@ -905,7 +939,8 @@ __git_refs ()\n>  \t\t\t\t\"refs/remotes/$match*\" \"refs/remotes/$match*/**\")\n>  \t\t\t;;\n>  \t\tesac\n> -\t\t__git_dir=\"$dir\" __git for-each-ref --format=\"$fer_pfx%($format)$sfx\" \\\n> +\t\t__git_dir=\"$dir\" __git for-each-ref --sort=\"$sortby\" \\\n> +\t\t\t--format=\"$fer_pfx%($format)$sfx\" \\\n>  \t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n>  \t\t\t\"${refs[@]}\"\n>  \t\tif [ -n \"$track\" ]; then\n> @@ -929,7 +964,8 @@ __git_refs ()\n>  \t\t\t$match*|$umatch*)\techo \"${pfx}HEAD$sfx\" ;;\n>  \t\t\tesac\n>  \t\t\tlocal strip=\"$(__git_count_path_components \"refs/remotes/$remote\")\"\n> -\t\t\t__git for-each-ref --format=\"$fer_pfx%(refname:strip=$strip)$sfx\" \\\n> +\t\t\t__git for-each-ref --sort=\"$sortby\" \\\n> +\t\t\t\t--format=\"$fer_pfx%(refname:strip=$strip)$sfx\" \\\n>  \t\t\t\t${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \\\n>  \t\t\t\t\"refs/remotes/$remote/$match*\" \\\n>  \t\t\t\t\"refs/remotes/$remote/$match*/**\"\n> @@ -2861,7 +2897,8 @@ __git_complete_config_variable_value ()\n>  \tremote.*.push)\n>  \t\tlocal remote=\"${varname#remote.}\"\n>  \t\tremote=\"${remote%.push}\"\n> -\t\t__gitcomp_nl \"$(__git for-each-ref \\\n> +\t\tlocal sortby=$(__git_get_sort_by_fieldname)\n> +\t\t__gitcomp_nl \"$(__git for-each-ref --sort=\"$sortby\" \\\n>  \t\t\t--format='%(refname):%(refname)' refs/heads)\" \"\" \"$cur_\"\n>  \t\treturn\n>  \t\t;;\n> @@ -3983,8 +4020,9 @@ ___git_complete ()\n>  {\n>  \tlocal wrapper=\"__git_wrap${2}\"\n>  \teval \"$wrapper () { __git_func_wrap $2 ; }\"\n> -\tcomplete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \\\n> -\t\t|| complete -o default -o nospace -F $wrapper $1\n> +\tcomplete -o bashdefault -o default -o nospace -o nosort \\\n> +\t\t-F $wrapper $1 2>/dev/null \\\n> +\t\t|| complete -o default -o nospace -o nosort -F $wrapper $1\n\nThis is problematic, because it turns off sorting for all completion\ninvocations, but in many cases we do need Bash to do the sorting for\nus:\n\n  - Subcommands and --options still hard-coded in the completion\n    script are usually listed in arbitrary order.\n  - Subcommands and --options listed programmatically by the\n    parse-options machinery are listed in the order they are specified\n    in the C source files (which tends to be the order that makes most\n    sense for the help output).\n  - Some completion functions list possible completion words from\n    multiple sources.\n\nI'm afraid that any change that leaves these cases unsorted is\nunacceptable.\n\n>  }\n>  \n>  # Setup the completion for git commands\n> \n> base-commit: e4ef0485fd78fcb05866ea78df35796b904e4a8e\n> -- \n> gitgitgadget\n> \n"}]}