-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_setup_repos.ps1
94 lines (74 loc) · 2.41 KB
/
git_setup_repos.ps1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
param (
[Parameter(Mandatory = $true)][string]$folder,
[Parameter(Mandatory = $false)][string]$userName,
[Parameter(Mandatory = $false)][string]$email,
[Parameter(Mandatory = $false)][string]$sshKey
)
if (-not (Test-Path -Path $folder)) {
Write-Error 'Folder does not exist'
Exit 1
}
$profileFileName = "rg_profile.json"
$profilePath = Join-Path -Path $folder -ChildPath $profileFileName
$credentialsConfigExist = Test-Path -Path $profilePath
$credentialsGivenAsPrms = $userName -And $email
$credentials = [PSCustomObject]@{
userName = ''
email = ''
sshKey = ''
commands = @()
}
$hasGitRepos = $false
if ($credentialsConfigExist) {
$credentials = (Get-Content $profilePath) | ConvertFrom-Json
}
elseif ($credentialsGivenAsPrms) {
$credentials.userName = $userName
$credentials.email = $email
$credentials.sshKey = $sshKey
}
else {
Write-Error "Give user credentials either via parameters or via an $profileFileName located in the target folder"
Exit 1
}
Clear-Host
if($IsWindows) {
$sshFolder = "$env:USERPROFILE/.ssh";
$sshFolder = $sshFolder -replace '\\', '/'
}
if ($IsMacOS -or $IsLinux) {
Write-Host "Setting up ssh key access rights for macos or linux"
$sshFolder = "~/.ssh"
& chmod 600 $sshFolder/$($credentials.sshKey)
& chmod 644 $sshFolder/$($credentials.sshKey).pub
}
Write-Host "Setting the userName and email for all repos to <$($credentials.userName) ($($credentials.email))>"
Write-Host ""
$originalLocaltion = Get-Location
Get-ChildItem -Directory -Path $folder | Foreach-Object {
$repo = $_.FullName
$gitFolder = Join-Path -Path $repo -ChildPath '.git'
if (-not (Test-Path -Path $gitFolder)) {
Write-Warning "$repo -> is not a git repo"
}
else {
Write-Host "$repo -> setting user info" -ForegroundColor Black -BackgroundColor Yellow
$hasGitRepos = $true
& git -C $repo config user.email $credentials.userName
& git -C $repo config user.name $credentials.email
& git -C $repo config core.sshCommand "ssh -i $sshFolder/$($credentials.sshKey)"
Set-Location $repo
foreach ($command in $credentials.commands)
{
Invoke-Expression $command
}
}
}
Set-Location $originalLocaltion
Write-Host ""
if ($hasGitRepos) {
Write-Host "User updated in all git repos found"
}
else {
Write-Warning "No git repos found to update"
}