-
Notifications
You must be signed in to change notification settings - Fork 1
Add autocompletion, man page, and standard CLI flags #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| _git_get() { | ||
| local limit=1 | ||
| [[ "${COMP_WORDS[0]}" == "git" ]] && limit=2 | ||
| if [[ $COMP_CWORD -gt $limit ]]; then | ||
| compopt +o default +o bashdefault 2>/dev/null | ||
| return | ||
| fi | ||
| COMPREPLY=($(git-get --complete "${COMP_WORDS[COMP_CWORD]}" 2>/dev/null)) | ||
| } | ||
| complete -F _git_get git-get | ||
|
arbourd marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| function __fish_gitget_completion | ||
| git-get --complete (commandline -ct) 2>/dev/null | ||
| end | ||
|
|
||
| complete -c git-get -f -n 'test (count (commandline -opc)) -le 1' -a "(__fish_gitget_completion)" | ||
| complete -c git-get -f -n 'test (count (commandline -opc)) -gt 1' | ||
| complete -c git -n '__fish_git_using_command get; and test (count (commandline -opc)) -le 2' -f -a "(__fish_gitget_completion)" | ||
| complete -c git -n '__fish_git_using_command get; and test (count (commandline -opc)) -gt 2' -f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| $gitGetCompleter = { | ||
| param($wordToComplete, $commandAst, $cursorPosition) | ||
| if ($commandAst.CommandElements.Count -gt 2) { return } | ||
| git-get --complete "$wordToComplete" 2>$null | ForEach-Object { | ||
| [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) | ||
| } | ||
| } | ||
|
|
||
| Register-ArgumentCompleter -Native -CommandName git-get -ScriptBlock $gitGetCompleter | ||
|
|
||
| Register-ArgumentCompleter -Native -CommandName git -ScriptBlock { | ||
| param($wordToComplete, $commandAst, $cursorPosition) | ||
| $c = $commandAst.CommandElements.Count | ||
| if ($c -ge 2 -and $c -le 3 -and $commandAst.CommandElements[1].Value -eq 'get') { | ||
| & $gitGetCompleter $wordToComplete $commandAst $cursorPosition | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #compdef git-get | ||
|
|
||
| _git_get() { | ||
| emulate -L zsh | ||
| local c="${cword:-$CURRENT}" | ||
| [[ $c -gt 2 ]] && { _ret=0; return } | ||
| local prefix="${cur-${words[$CURRENT]}}" | ||
| local completions | ||
| completions=($(git-get --complete "$prefix" 2>/dev/null)) | ||
| compadd -S "" -- ${(M)completions:#*/} | ||
| compadd -- ${completions:#*/} | ||
| _ret=0 | ||
| } | ||
|
arbourd marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestBashCompletion(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("bash completion not supported on Windows") | ||
| } | ||
| if _, err := exec.LookPath("bash"); err != nil { | ||
| t.Skip("bash not on PATH") | ||
| } | ||
| if err := gitConfigGlobalFixture(t); err != nil { | ||
| t.Fatalf("setup: %v", err) | ||
| } | ||
|
|
||
| bin, getpath := buildCompletionFixture(t) | ||
| cmd := exec.Command("bash", "-c", `source completions/git-get.bash | ||
| COMP_WORDS=("git-get" "github.com/arbourd/") | ||
| COMP_CWORD=1 | ||
| _git_get | ||
| printf '%s\n' "${COMPREPLY[@]}"`) | ||
| cmd.Env = completionEnv(t, bin, getpath) | ||
|
|
||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| t.Fatalf("bash: %v", err) | ||
| } | ||
| if got := string(out); !strings.Contains(got, "github.com/arbourd/git-get") { | ||
| t.Errorf("unexpected output: %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestFishCompletion(t *testing.T) { | ||
| if _, err := exec.LookPath("fish"); err != nil { | ||
| t.Skip("fish not on PATH") | ||
| } | ||
| if err := gitConfigGlobalFixture(t); err != nil { | ||
| t.Fatalf("setup: %v", err) | ||
| } | ||
|
|
||
| bin, getpath := buildCompletionFixture(t) | ||
| cmd := exec.Command("fish", "-c", `source completions/git-get.fish | ||
| complete --do-complete "git-get github.com/arbourd/"`) | ||
| cmd.Env = completionEnv(t, bin, getpath) | ||
|
|
||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| t.Fatalf("fish: %v", err) | ||
| } | ||
| if got := string(out); !strings.Contains(got, "github.com/arbourd/git-get") { | ||
| t.Errorf("unexpected output: %q", got) | ||
| } | ||
| } | ||
|
|
||
| func buildCompletionFixture(t *testing.T) (bin, getpath string) { | ||
| t.Helper() | ||
|
|
||
| bin = filepath.Join(t.TempDir(), "git-get") | ||
| if runtime.GOOS == "windows" { | ||
| bin += ".exe" | ||
| } | ||
| if out, err := exec.Command("go", "build", "-o", bin, ".").CombinedOutput(); err != nil { | ||
| t.Fatalf("go build: %v\n%s", err, out) | ||
| } | ||
|
|
||
| getpath = t.TempDir() | ||
| seedRepos(t, getpath, []string{"github.com/arbourd/git-get"}) | ||
| return bin, getpath | ||
| } | ||
|
|
||
| func completionEnv(t *testing.T, bin, getpath string) []string { | ||
| t.Helper() | ||
| pathSep := ":" | ||
| if runtime.GOOS == "windows" { | ||
| pathSep = ";" | ||
| } | ||
|
|
||
| env := make([]string, 0, len(os.Environ())+3) | ||
| for _, e := range os.Environ() { | ||
| if !strings.HasPrefix(e, "PATH=") && !strings.HasPrefix(e, "GETPATH=") && !strings.HasPrefix(e, "HOME=") { | ||
| env = append(env, e) | ||
| } | ||
| } | ||
|
|
||
| return append(env, | ||
| "PATH="+filepath.Dir(bin)+pathSep+os.Getenv("PATH"), | ||
| "GETPATH="+getpath, | ||
| "HOME="+t.TempDir(), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package get | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Complete returns repository paths relative to GETPATH that match the given prefix, | ||
| // completing one path segment at a time. | ||
| func Complete(prefix string) ([]string, error) { | ||
| trailingSlash := strings.HasSuffix(prefix, "/") || strings.HasSuffix(prefix, string(filepath.Separator)) | ||
| if prefix != "" { | ||
| prefix = strings.ToLower(filepath.ToSlash(filepath.Clean(prefix))) | ||
| } | ||
| prefixDepth := strings.Count(prefix, "/") | ||
| if trailingSlash { | ||
| prefixDepth++ | ||
| } | ||
|
|
||
| getpath, err := AbsolutePath() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("resolving GETPATH: %w", err) | ||
| } | ||
|
|
||
| var matches []string | ||
| err = filepath.WalkDir(getpath, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| if path == getpath { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| if !d.IsDir() || path == getpath { | ||
| return nil | ||
| } | ||
| if strings.HasPrefix(d.Name(), ".") { | ||
| return fs.SkipDir | ||
| } | ||
|
|
||
|
arbourd marked this conversation as resolved.
|
||
| rel, err := filepath.Rel(getpath, path) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
arbourd marked this conversation as resolved.
|
||
| rel = filepath.ToSlash(rel) | ||
| relLower := strings.ToLower(rel) | ||
| relDepth := strings.Count(rel, "/") | ||
|
|
||
| if relDepth == prefixDepth { | ||
| if strings.HasPrefix(relLower, prefix) { | ||
| if !isGitRepository(path) { | ||
| rel += "/" | ||
| } | ||
| matches = append(matches, rel) | ||
| } | ||
| return fs.SkipDir | ||
| } | ||
|
|
||
| if relLower != prefix && !strings.HasPrefix(prefix, relLower+"/") { | ||
| return fs.SkipDir | ||
| } | ||
| return nil | ||
| }) | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return nil, nil | ||
| } | ||
| return matches, err | ||
| } | ||
|
arbourd marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.