{"thread":{"id":"32168","subject":"[PATCH] Completion script must sort before using uniq","startedAt":"2012-11-22T20:02:20Z","lastAt":"2012-11-22T20:02:20Z","messageCount":1,"participants":["Marc Khouzam"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"203657","messageId":"CAFj1UpEU+JVpN_BSUwCA91e8xu5JMtqcCEH5S6s80Kzx3qbXfg@mail.gmail.com","threadId":"32168","inReplyTo":null,"subject":"[PATCH] Completion script must sort before using uniq","fromName":"Marc Khouzam","fromEmail":"marc.khouzam@gmail.com","sentAt":"2012-11-22T20:02:20Z","receivedAt":"2012-11-22T20:02:20Z","isPatch":true,"sender":{"key":"marc.khouzam@ericsson.com","avatar":"https://gravatar.com/avatar/de564e23ad14e2945f9f1cdb4d0227c935b5b54c39576a304d47caa3e23dcd33?d=mp&s=160"},"body":"The uniq program only works with sorted input.  The man page states\n\"uniq prints the unique lines in a sorted file\".\n\nWhen __git_refs use the guess heuristic employed by checkout for\ntracking branches it wants to consider remote branches but only if\nthe branch name is unique.  To do that, it calls 'uniq -u'.  However\nthe input given to 'uniq -u' is not sorted.\n\nFor example if all available branches are:\n  master\n  remotes/GitHub/maint\n  remotes/GitHub/master\n  remotes/origin/maint\n  remotes/origin/master\n\nWhen performing completion on 'git checkout ma' the choices given are\n  maint\n  master\nbut when performing completion on 'git checkout mai', no choices\nappear, which is obviously contradictory.\n\nThe reason is that, when dealing with 'git checkout ma',\n\"__git_refs '' 1\" will find the following list:\n  master\n  maint\n  master\n  maint\n  master\nwhich, when passed to 'uniq -u' will remain the same.\nBut when dealing with 'git checkout mai', the list will be:\n  maint\n  maint\nwhich happens to be sorted and will be emptied by 'uniq -u'.\n\nThe solution is to first call 'sort' and then 'uniq -u'.\n\nSigned-off-by: Marc Khouzam <marc.khouzam@gmail.com>\n---\n\nSorry if you get this twice, my first try never showed up on the list.\n\nI ran into this by fluke when testing the tcsh completion.\n\nThanks for considering the fix.\n\nMarc\n\n contrib/completion/git-completion.bash | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/contrib/completion/git-completion.bash\nb/contrib/completion/git-completion.bash\nindex bc0657a..85ae419 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -321,7 +321,7 @@ __git_refs ()\n                                if [[ \"$ref\" == \"$cur\"* ]]; then\n                                        echo \"$ref\"\n                                fi\n-                       done | uniq -u\n+                       done | sort | uniq -u\n                fi\n                return\n        fi\n--\n1.8.0.1.g9fe2839\n"}]}