Last active
January 14, 2020 21:11
-
-
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 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
# 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 |
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
# 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 [7m Branch / Tag: {-1} [0m & 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