Skip to content

Instantly share code, notes, and snippets.

@Nishith-Savla
Last active April 18, 2021 08:07
Show Gist options
  • Save Nishith-Savla/60f57b851436f74472ffd7b9e630551a to your computer and use it in GitHub Desktop.
Save Nishith-Savla/60f57b851436f74472ffd7b9e630551a to your computer and use it in GitHub Desktop.
My Powershell 7 Profile
# Theme Settings
Set-PoshPrompt slimfat
Import-Module PoShFuck
# Aliases
Set-Alias scl Set-Clipboard
# "Workon" redefine
function workon ($environment) {
if ($environment) {
& ~\Envs\$environment\Scripts\activate.ps1
} else {
Write-Host "Pass a name to activate one of the following virtualenvs:"
Write-Host "================================================================"
Get-ChildItem ~\Envs -Name
}
}
# Accessibility functions
# Make and cd into the directory
function mk ($directory) {
if ($directory) {
mkdir $directory && cd $directory
} else {
Write-Error "Please enter the name of the directory to create"
}
}
# Clone the repository and cd to it
function gcloned($url) {
git clone "$url"; cd "$($url.Split("/")[-1].Split(".")[-2])"
}
# Display directory stack
function dirs() {
Get-Location -Stack
}
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# Sync the upstream repo with the local repo
function gsyncr([String]$branch = "upstream", [String]$head = "main", [String]$remote_head = "main", [bool]$reswitch = $true) {
git fetch $branch
if ($?) {
git checkout $head;
git merge $branch/$remote_head
Write-Host "Merged $branch/$remote_head to $head"
if ($reswitch) {
git checkout -
Write-Host "Re-switched to previous branch"
}
} else {
Write-Error "Couldn't fetch $branch"
}
}
function sudo(){
if($args) {
runas /savecred /user:administrator "$args"
} else {
$defaultProgramToStart = 'powershell'
$startDefaultProgram = Read-Host -Prompt 'No arguments passed! Start powershell instead(Y/n)'
if ($startDefaultProgram.ToLower().StartsWith('y') -or $startDefaultProgram -eq '') {
runas /savecred /user:administrator "$defaultProgramToStart"
}
}
}
function Get-AllGitStatuses([switch]$ShowNongitRepos=$false, [switch]$Recurse=$false, [int]$Depth=0) {
$lsExpression = "Get-ChildItem -Depth:$Depth"
if($Recurse) {
$lsExpression += " -Recurse"
}
foreach ($directory in $(Invoke-Expreesion $lsExpression)) {
if (Test-Path -Path $directory -Type Container) {
if ($ShowNongitRepos) {
Write-Host $directory -ForegroundColor DarkYellow
git -C $directory status
Write-Host "====================================================================" -ForegroundColor DarkGray
} else {
# Check if there is any output of git status (not error)
git -C $directory status > "$Env:temp\Get-AllGitStatuses-Output.tmp" 2> $null
if(Get-Content "$Env:temp\Get-AllGitStatuses-Output.tmp") {
# Print the directory name
Write-Host $directory -ForegroundColor DarkYellow
git -C $directory status
Write-Host "====================================================================" -ForegroundColor DarkGray
}
}
}
}
}
Function Find-GitRepository {
<#
.SYNOPSIS
Find Git repositories
.DESCRIPTION
Use this command to find Git repositories in the specified folder. It is assumed that you have the Git command line tools already installed.
.PARAMETER Path
The top level path to search.
.EXAMPLE
PS C:\> Find-GitRepository -path D:\Projects
Repository Branch LastAuthor LastLog
---------- ------ ---------- -------
D:\Projects\Excalibur master jdhitsolutions 4/17/2016 9:59:02 AM
D:\Projects\PiedPiper bug jdhitsolutions 5/16/2016 1:15:03 PM
D:\Projects\PSMagic master jdhitsolutions 5/11/2016 1:27:35 PM
D:\Projects\ProjectX dev jdhitsolutions 4/12/2016 4:49:30 PM
.NOTES
NAME : Find-GitRepository
VERSION : 1.0.0
LAST UPDATED: 5/17/2016
AUTHOR : Jeff Hicks (@JeffHicks)
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.LINK
https://gist.github.com/jdhitsolutions/ba3295167ccc997b7714e1f2a2777405
.INPUTS
[string]
.OUTPUTS
[pscustomobject]
#>
[cmdletbinding()]
Param(
[Parameter(
Position = 0,
HelpMessage = "The top level path to search"
)]
[ValidateScript({
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_"
}
})]
[string]$Path = "."
)
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
Write-Verbose "[PROCESS] Searching $(Convert-Path -path $path) for Git repositories"
dir -path $Path -Hidden -filter .git -Recurse |
select @{Name="Repository";Expression={Convert-Path $_.PSParentPath}},
@{Name ="Branch";Expression = {
#save current location
Push-Location
#change location to the repository
Set-Location -Path (Convert-Path -path ($_.psparentPath))
#get current branch with out the leading asterisk
(git branch).where({$_ -match "\*"}).Substring(2)
}},
@{Name="LastAuthor";Expression = { git log --date=local --format=%an -1 }},
@{Name="LastLog";Expression = {
(git log --date=iso --format=%ad -1 ) -as [datetime]
#change back to original location
Pop-Location
}}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end function
# Execute winfetch to display startup info
winfetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment