Last active
February 11, 2021 06:47
-
-
Save st0le/820677e5fb99db5b16c7ae1edd5c05fa to your computer and use it in GitHub Desktop.
Lightweight git autocompletion.
This file contains 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
# Basic Autocompleter for git | |
# Covers most usecases | |
Register-ArgumentCompleter -CommandName "git" -Native -ScriptBlock { | |
param($wordToComplete, $commandAst, $cursorPosition) | |
$cmd = $commandAst.ToString() -split " " | |
# order is based on "popularity" | |
$supportedCommands = @("checkout", "add", "commit", "branch", "status", "pull", "push", "merge", "log", "stash") | |
$comps = @() | |
if ($cmd.Length -eq 1 -or $supportedCommands -inotcontains $cmd[1]) { | |
$comps = $supportedCommands | |
} | |
else { | |
$comps = switch ($cmd[1]) { | |
"checkout" { @("-") + @(git branch --format='%(refname:short)' --sort=-committerdate) } | |
"add" { @(".") } | |
"commit" { @("-m") } | |
"branch" { @("-b", "-D") + @(git branch --format='%(refname:short)' --sort=-committerdate) } | |
"merge" { @(git branch --format='%(refname:short)' --sort=-committerdate) } | |
"log" { @("--oneline") } | |
} | |
} | |
$comps | Where-Object { $_ -like "$wordToComplete*" } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment