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

[PATCH v3] Completion must sort before using uniq

From
Marc Khouzam <marc.khouzam@gmail.com>
Date
Nov 23, 2012, 14:02 UTC
Message-ID
<CAFj1UpH8h6c7xHuRG6F+pLy5YMvsJ0QdXsotCpLKnht0PsdiNw@mail.gmail.com>
In-Reply-To
<CAMP44s2bMub6T1YcUfsYWPQFU1bY4iU1WfSf+jFa7jSXAKTNaw@mail.gmail.com>

The user can be presented with invalid completion results when trying to complete a 'git checkout' command. This can happen when using a branch name prefix that matches multiple remote branches.

For example, if available branches are:
  master
  remotes/GitHub/maint
  remotes/GitHub/master
  remotes/origin/maint
  remotes/origin/master
When performing completion on 'git checkout ma' the user will be
given the choices:
  maint
  master

However, 'git checkout maint' will fail in this case, although completion previously said 'maint' was valid. Furthermore, when performing completion on 'git checkout mai', no choices will be suggested. So, the user is first told that the branch name 'maint' is valid, but when trying to complete 'mai' into 'maint', that completion is no longer valid.

The completion results should never propose 'maint' as a valid branch name, since 'git checkout' will refuse it.

The reason for this bug is that the uniq program only works with sorted input. The man page states "uniq prints the unique lines in a sorted file".

When __git_refs uses the guess heuristic employed by checkout for tracking branches it wants to consider remote branches but only if the branch name is unique. To do that, it calls 'uniq -u'. However the input given to 'uniq -u' is not sorted.

Therefore, in the above example, when dealing with 'git checkout ma',
"__git_refs '' 1" will find the following list:
  master
  maint
  master
  maint
  master

which, when passed to 'uniq -u' will remain the same. Therefore 'maint' will be wrongly suggested as a valid option.

When dealing with 'git checkout mai', the list will be:
  maint
  maint

which happens to be sorted and will be emptied by 'uniq -u', properly ignoring 'maint'.

A solution for preventing the completion script from suggesting such invalid branch names is to first call 'sort' and then 'uniq -u'.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
---
> Mostly cosmetic suggestions, but it looks OK to me.
Thanks for the suggestions, I updated the commit message.
> With this explanation the patch looks good to me.
Thanks for the quick review.
Marc
 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index bc0657a..85ae419 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -321,7 +321,7 @@ __git_refs ()
                                if [[ "$ref" == "$cur"* ]]; then
                                        echo "$ref"
                                fi
-                       done | uniq -u
+                       done | sort | uniq -u
                fi
                return
        fi
--
1.8.0.1.g9fe2839
Previous: Felipe ContrerasNext: Felipe Contreras
Message 6 of 8 in “Completion must sort before using uniq”
  1. Completion must sort before using uniqMarc Khouzam, Nov 22, 2012
  2. Joachim SchmitzNov 23, 2012
  3. Felipe ContrerasNov 23, 2012
  4. Completion must sort before using uniqMarc Khouzam, Nov 23, 2012
  5. Felipe ContrerasNov 23, 2012
  6. Completion must sort before using uniqMarc Khouzam, Nov 23, 2012
  7. Felipe ContrerasNov 23, 2012
  8. Junio C HamanoNov 25, 2012

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.