Skip to content

Instantly share code, notes, and snippets.

@danilobjr
Created March 22, 2017 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobjr/0a6f54230d00c93494cd2972b27a8608 to your computer and use it in GitHub Desktop.
Save danilobjr/0a6f54230d00c93494cd2972b27a8608 to your computer and use it in GitHub Desktop.
git + presenter + powershell
param(
[switch]$autoclear
)
$options = @{}
$options.Add('next', "Next step")
$options.Add('prev', "Previous step")
$options.Add('reset', "Reset the current changes")
$options.Add('rebase', "Rebase all steps that follow the current")
$current = 0
$skipped = $false
function initialize() {
clear-host
$script:steps = git branch | % { $_.substring(2) }
$script:current = [array]::indexof($steps, (git rev-parse --abbrev-ref HEAD))
if ($current -lt 0) {
$script:current = 0
}
$steps | write-host -fore green
options
}
function rebase() {
reset
checkout
$currentIndex = [array]::indexof($steps, $steps[$current])
$nextIndex = [array]::indexof($steps, $steps[$current+1])
while ($nextIndex -gt $currentIndex) {
next
$currentIndex = [array]::indexof($steps, $steps[$current])
$nextIndex = [array]::indexof($steps, $steps[$current+1])
$currentBranch = $steps[$current]
$prevBranch = $steps[$current-1]
write-host "- Rebasing '$currentBranch' based on '$prevBranch'" -fore yellow
git rebase $prevBranch
}
}
function reset() {
git reset --hard
git clean -df
}
function checkout() {
$step = $steps[$current]
git checkout $step
$branch = (git rev-parse --abbrev-ref HEAD)
if ($branch -ne $step) {
write-error "Failed to checkout out branch '$branch'"
}
}
function next() {
reset
$script:current++
checkout
}
function prev() {
reset
$script:current--
checkout
}
function options() {
if ($autoclear) {
if ($skipped) {
clear-host
} else {
write-host ""
write-host ""
}
$script:skipped = $true
} else {
write-host ""
write-host ""
}
write-host "Current" $steps[$current] -fore green
$action = select-option -title "What next?" -options $options
if ($action -eq 'next') {
next
options
}
if ($action -eq 'prev') {
prev
options
}
if ($action -eq 'reset') {
reset
options
}
if ($action -eq 'rebase') {
rebase
options
}
}
function select-option ([string]$title, $options) {
$menu = ""
$menu += $options.GetEnumerator() | sort-object $_.key | out-string
$menu += "Select an option by Name or Q to quit"
$selectedKey = $null
$run = $true
do {
$key = show-menu -menu $menu -title "`n$title"
if ($key -ne $null -and $key.ToLower() -eq 'Q') {
$run = $false
} else {
if ($options.ContainsKey($key)) {
$selectedKey = $key
$run = $false
} else {
write-warning "Invalid selection. Try again...`n"
}
}
} while ($run)
return $selectedKey
}
function show-menu([string]$menu, [string]$title="Menu") {
$menuPrompt = $title
$menuprompt += "`n"
$menuprompt += "-" * $title.Length
$menuprompt += "`n"
$menuPrompt += $menu
read-host -prompt $menuprompt
}
initialize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment