Skip to content

Instantly share code, notes, and snippets.

@SirJson
Last active January 14, 2020 21:11
Show Gist options
  • Save SirJson/0c668e8115ad3c4cf0188f00fa881865 to your computer and use it in GitHub Desktop.
Save SirJson/0c668e8115ad3c4cf0188f00fa881865 to your computer and use it in GitHub Desktop.
fco_preview() for PowerShell. git-goto-win32.ps1 is with cmd.exe weirdness in the preview. git-goto-all.ps1 is the platform independent version.
# This version should work on every PowerShell supported OS
# Inspired by fco_preview() from https://github.com/junegunn/fzf/wiki/examples#git
$NEWLINE = [System.Environment]::Newline
$GIT_BRANCHES = $(git --no-pager branch --all --format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)")
$GIT_TAGS = $(git --no-pager tag)
$ITEMS = @()
if (!$GIT_BRANCHES -and !$GIT_TAGS) {
Write-Error "No git repository found"
exit 1
}
if ($GIT_BRANCHES) {
$ITEMS += [Array]::FindAll($GIT_BRANCHES.Split($NEWLINE), [Predicate[string]] { $args[0] -ne [string]::Empty })
}
if ($GIT_TAGS) {
$ITEMS += [Array]::FindAll($GIT_TAGS.Split($NEWLINE), [Predicate[string]] { $args[0] -ne [string]::Empty })
}
$FZFINPUT = [string]::Join($NEWLINE, $ITEMS)
$CURRENT = git rev-parse --abbrev-ref HEAD
$SELECTION = $(Write-Output $FZFINPUT | fzf --no-multi --ansi --no-hscroll --layout=reverse-list --nth=2 --header="Current branch/tag: $CURRENT" --preview="echo '== Branch / Tag: {-1} ==' && git --no-pager log -150 --pretty=reference {-1}")
if ([string]::IsNullOrWhiteSpace($SELECTION)) {
exit 0
}
$BRANCH_PATTERN = ($SELECTION | Select-String -Pattern '(branch)?\s*(origin/)?(.*)')
git checkout $BRANCH_PATTERN.Matches.Groups[3].Value
# Inspired by fco_preview() from https://github.com/junegunn/fzf/wiki/examples#git
$NEWLINE = [System.Environment]::Newline
$GIT_BRANCHES = $(git --no-pager branch --all --format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)")
$GIT_TAGS = $(git --no-pager tag)
$ITEMS = @()
if (!$GIT_BRANCHES -and !$GIT_TAGS) {
Write-Error "No git repository found"
exit 1
}
if ($GIT_BRANCHES) {
$ITEMS += [Array]::FindAll($GIT_BRANCHES.Split($NEWLINE), [Predicate[string]] { $args[0] -ne [string]::Empty })
}
if ($GIT_TAGS) {
$ITEMS += [Array]::FindAll($GIT_TAGS.Split($NEWLINE), [Predicate[string]] { $args[0] -ne [string]::Empty })
}
$FZFINPUT = [string]::Join($NEWLINE, $ITEMS)
$CURRENT = git rev-parse --abbrev-ref HEAD
$SELECTION = $(Write-Output $FZFINPUT | fzf --no-multi --ansi --no-hscroll --layout=reverse-list --nth=2 --header="Current branch/tag: $CURRENT" --preview="echo  Branch / Tag: {-1}  & echo. && git --no-pager log -150 --pretty=reference {-1}")
if ([string]::IsNullOrWhiteSpace($SELECTION)) {
exit 0
}
$BRANCH_PATTERN = ($SELECTION | Select-String -Pattern '(branch)?\s*(origin/)?(.*)')
git checkout $BRANCH_PATTERN.Matches.Groups[3].Value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment