Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ jobs:
- uses: actions/checkout@v6
- run: git config url.https://github.com/.insteadOf ssh://git@github.com/

- if: runner.os != 'Windows'
uses: fish-actions/install-fish@v1

- uses: actions/setup-go@v6
with:
go-version-file: go.mod

- run: go mod download
- run: go test ./...

- run: go build .
- run: go vet ./...
- run: go test -v -race ./...
- run: go build -ldflags "-X main.Version=$(git rev-parse --short HEAD)" .
- run: ./git-get github.com/arbourd/git-get

bin:
Expand Down
10 changes: 10 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ checksum:

archives:
- name_template: "{{ .ProjectName }}-v{{ .Version }}-{{ .Os }}-{{ .Arch }}"
files:
- completions/*
- man/*
format_overrides:
- goos: windows
formats:
Expand All @@ -28,6 +31,13 @@ brews:
description: Go gets your code
homepage: https://github.com/arbourd/git-get

install: |
bin.install "git-get"
man1.install "man/git-get.1"
bash_completion.install "completions/git-get.bash" => "git-get"
zsh_completion.install "completions/git-get.zsh" => "_git_get"
fish_completion.install "completions/git-get.fish"

test: |
repo = "github.com/arbourd/git-get"
assert_match "#{testpath}/src/#{repo}", shell_output("#{bin}/git-get #{repo}")
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,31 @@ Install with `go install`.
```console
$ go install github.com/arbourd/git-get@latest
```

### Autocompletion

Homebrew installs shell completions automatically. For other installs, completion scripts are available in the `completions/` directory of each [release](https://github.com/arbourd/git-get/releases).

Comment thread
arbourd marked this conversation as resolved.
**Fish**

```console
$ cp completions/git-get.fish ~/.config/fish/completions/git-get.fish
```

**Bash** — add to `~/.bashrc` to persist.

```console
$ source completions/git-get.bash
```

**Zsh** — copy to a directory in `$fpath`.

```console
$ cp completions/git-get.zsh "${fpath[1]}/_git_get"
```

**PowerShell** — dot-source in `$PROFILE` to persist.

```console
> . completions\git-get.ps1
```
10 changes: 10 additions & 0 deletions completions/git-get.bash
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
Comment thread
arbourd marked this conversation as resolved.
8 changes: 8 additions & 0 deletions completions/git-get.fish
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
17 changes: 17 additions & 0 deletions completions/git-get.ps1
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
}
}
13 changes: 13 additions & 0 deletions completions/git-get.zsh
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
}
Comment thread
arbourd marked this conversation as resolved.
97 changes: 97 additions & 0 deletions completions_test.go
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(),
)
}
70 changes: 70 additions & 0 deletions get/complete.go
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
}

Comment thread
arbourd marked this conversation as resolved.
rel, err := filepath.Rel(getpath, path)
if err != nil {
return nil
}
Comment thread
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
}
Comment thread
arbourd marked this conversation as resolved.
Loading
Loading