Skip to content

Instantly share code, notes, and snippets.

@pclouds
Last active March 25, 2018 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pclouds/f337d4393b5cfab813909b8eea2eaa40 to your computer and use it in GitHub Desktop.
Save pclouds/f337d4393b5cfab813909b8eea2eaa40 to your computer and use it in GitHub Desktop.
#!/bin/sh
build_sort() {
cat >sort.c <<\EOF &&
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int cmp(const void *a_, const void *b_)
{
const char *a = *(const char **)a_;
const char *b = *(const char **)b_;
char *to_free_a = NULL, *to_free_b = NULL;
int ret;
a += !strncmp(a, "--no-", 5) ? 5 : 2;
b += !strncmp(b, "--no-", 5) ? 5 : 2;
if (a[strlen(a) - 1] == '=') {
a = to_free_a = strdup(a);
to_free_a[strlen(a) - 1] = '\0';
}
if (b[strlen(b) - 1] == '=') {
b = to_free_b = strdup(b);
to_free_b[strlen(b) - 1] = '\0';
}
//fprintf(stderr, "%s vs %s\n", a, b);
ret = strcmp(a, b);
free(to_free_a);
free(to_free_b);
return ret;
}
int main(int ac, char **av)
{
char *lines[1024];
int i, nr = 0;
char buf[1024];
while (fgets(buf, sizeof(buf), stdin)) {
int len = strlen(buf);
if (len && buf[len-1] == '\n')
buf[len-1] = '\0';
if (!strlen(buf))
continue;
lines[nr++] = strdup(buf);
}
qsort(lines, nr, sizeof(*lines), cmp);
for (i = 0; i < nr; i++)
puts(lines[i]);
return 0;
}
EOF
gcc -o complete-sort sort.c -Wall -g
}
complete ()
{
# do nothing
return 0
}
. ./contrib/completion/git-completion.bash
__gitcomp()
{
echo "$*" | tr ' ' '\n\n' | ./complete-sort | sed '/^$/d'
}
# We don't need this function to actually join words or do anything special.
# Also, it's cleaner to avoid touching bash's internal completion variables.
# So let's override it with a minimal version for testing purposes.
_get_comp_words_by_ref ()
{
while [ $# -gt 0 ]; do
case "$1" in
cur)
cur=${_words[_cword]}
;;
prev)
prev=${_words[_cword-1]}
;;
words)
words=("${_words[@]}")
;;
cword)
cword=$_cword
;;
esac
shift
done
}
run_completion ()
{
local -a COMPREPLY _words
local _cword
_words=( $1 )
test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
(( _cword = ${#_words[@]} - 1 ))
__git_wrap__git_main
}
test -x complete-sort || build_sort
export PATH=$(pwd):$PATH
while read cmd; do
run_completion "git $cmd --" >output/git-${cmd/ /_}.txt
done <<\EOF
add
am
apply
branch
checkout
cherry-pick
clean
clone
commit
config
describe
difftool
fetch
fsck
gc
grep
help
init
ls-files
ls-remote
merge
merge-base
mv
name-rev
notes
notes add
notes append
notes copy
notes prune
push
remote
remote add
remote set-head
remote update
remote set-url
remote get-url
remote prune
replace
reset
revert
rm
show-branch
status
submodule add
submodule status
submodule deinit
submodule update
submodule summary
submodule foreach
submodule sync
tag
worktree add
worktree list
worktree lock
worktree move
worktree prune
worktree remove
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment