Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active April 9, 2018 15:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/9676ec57fb28af96c08589e3e1a5b72c to your computer and use it in GitHub Desktop.
Save jdhitsolutions/9676ec57fb28af96c08589e3e1a5b72c to your computer and use it in GitHub Desktop.
Use this command to search the tips file that is part of the Git-Tips project on GitHub. The default behavior is to get all tips but you can search for text in a tip title or select a random tip.
#requires -version 4.0
Function Get-GitTip {
<#
.Synopsis
Get Git Tips
.Description
Use this command to search the tips file that is part of the Git-Tips project on GitHub. The default behavior is to get all tips but you can search for text in a tip title or select a random tip.
.Parameter Random
Select a single, random tip.
.Parameter Pretty
Write the tip colorized to the console host. This will not write anything to the pipeline. If you use this parameter, -Random is implied. The parameter has an alias of 'Show'.
.Parameter Search
A word or regular expression to search for in the title. The parameter has an alias of 'Find'.
.Example
$all = get-gittip
Get all tips and save to a local variable.
.Example
get-gittip -Random
title tip
----- ---
View the GPG signatures in the commit log git log --show-signature
Get a random tip.
.Example
get-gittip -search stash | format-list
title : Show list of all saved stashes
tip : git stash list
title : Apply any stash without deleting from the stashed list
tip : git stash apply <stash@{n}>
title : Apply last stashed state and delete it from stashed list
tip : git stash pop
alternatives : {git stash apply stash@{0} && git stash drop stash@{0}}
title : Delete all stored stashes
tip : git stash clear
alternatives : {git stash drop <stash@{n}>}
title : Grab a single file from a stash
tip : git checkout <stash@{n}> -- <file_path>
alternatives : {git checkout stash@{0} -- <file_path>}
title : Stash changes before rebasing
tip : git rebase --autostash
Get tips that reference stash and display as a list.
.Example
get-gittip -random -Pretty
----------------------
Git Tip of The Day
----------------------
Interactive staging.
git add -i
Display a random tip in pretty format. Actual output would be colorized using Write-Host.
.Notes
version: 1.0
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
.Link
Invoke-RestMethod
.Link
https://github.com/git-tips/tips
.Inputs
[string]
.Outputs
[pscustomobject]
#>
[cmdletbinding(DefaultParameterSetName = "Default")]
Param(
[Parameter(ParameterSetName = "Random")]
[switch]$Random,
[Parameter(ParameterSetName = "Random")]
[Alias("show")]
[switch]$Pretty,
[Parameter(ParameterSetName = "Search")]
[ValidateNotNullorEmpty()]
[Alias("find")]
[string]$Search
)
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
#display PSBoundparameters formatted nicely for Verbose output
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*4)$_"}) | Out-String) `n"
Write-Verbose "ParameterSet: $($PSCmdlet.ParameterSetName)"
Try {
$params = @{
Uri = "https://raw.githubusercontent.com/git-tips/tips/master/tips.json"
ErrorAction = "Stop"
}
Write-Verbose "Retrieving tips from $($params.uri)"
$tips = Invoke-RestMethod @params
Write-Verbose "Found $($tips.count) tips"
Switch ($PSCmdlet.ParameterSetName) {
"Default" { $tips }
"Random" {
Write-Verbose "Displaying a single random tip"
$tip = $tips | Get-Random
if ($Pretty) {
Write-Verbose "Writing pretty output to the host"
$title = @"
----------------------
Git Tip of The Day
----------------------
"@
Write-Host $title -ForegroundColor Yellow -BackgroundColor DarkGray
Write-Host $tip.Title -ForegroundColor Green
Write-Host $tip.tip -ForegroundColor cyan
#write a blank line
Write-Host " "
}
else {
$tip
}
}
"Search" {
Write-Verbose "Filtering title for $Search"
$tips.where({$_.title -match $Search})
}
} #switch
}
Catch {
Write-Error $_
}
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
}
@jdhitsolutions
Copy link
Author

This function is described at http://bit.ly/28VPwds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment