-
-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathcd-repo.ps1
More file actions
executable file
·59 lines (54 loc) · 2.32 KB
/
cd-repo.ps1
File metadata and controls
executable file
·59 lines (54 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<#
.SYNOPSIS
Set working directory to a repo
.DESCRIPTION
This PowerShell script changes the current working directory to the given Git repository.
.PARAMETER folderName
Specifies the folder name of the Git repository
.EXAMPLE
PS> ./cd-repo.ps1 rust
📂C:\Repos\rust entered, branch at: ## main ... origin/main
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
#requires -version 5.1
param([string]$folderName = "")
function GetPathToRepos {
if (Test-Path "~/Repositories" -pathType container) { return "~/Repositories"
} elseif (Test-Path "~/repositories" -pathType container) { return "~/repositories"
} elseif (Test-Path "~/Repos" -pathType container) { return "~/Repos"
} elseif (Test-Path "~/repos" -pathType container) { return "~/repos"
} elseif (Test-Path "~/Git" -pathType container) { return "~/Git"
} elseif (Test-Path "~/git" -pathType container) { return "~/git"
} elseif (Test-Path "/Repositories" -pathType container) { return "/Repositories"
} elseif (Test-Path "/repositories" -pathType container) { return "/repositories"
} elseif (Test-Path "/Repos" -pathType container) { return "/Repos"
} elseif (Test-Path "/repos" -pathType container) { return "/repos"
} elseif (Test-Path "/Git" -pathType container) { return "/Git"
} elseif (Test-Path "/git" -pathType container) { return "/git"
} elseif (Test-Path "~/source/repos" -pathType container) { return "~/source/repos" # Visual Studio default
} elseif (Test-Path "D:/Repos" -pathType container) { return "D:/Repos" # on second HDD
} else {
throw "No Git repositories folder in your home directory or in the root folder yet"
}
}
try {
if ("$folderName" -eq "") { $folderName = Read-Host "Please enter the Git repository's folder name" }
$path = (GetPathToRepos)
$dirs = (Get-ChildItem -Path "$path" -attributes Directory)
foreach ($dir in $dirs) {
if ("$($dir.Name)" -eq "$folderName") {
$path = Resolve-Path -Path "$path/$($dir.Name)"
Set-Location "$path"
Write-Host "📂$path entered, branch at: " -noNewline
& git status --branch --short
exit 0 # success
}
}
throw "No folder '$folderName' in '$path' (yet)"
} catch {
"⚠️ ERROR: $($Error[0]) (at line $($_.InvocationInfo.ScriptLineNumber))"
exit 1
}