Skip to content

Commit

Permalink
Add -Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
leojonathanoh committed Dec 8, 2023
1 parent 59e4b7a commit 58dd1ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
16 changes: 9 additions & 7 deletions Generate-GitBranches.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Get-Module Pester -ListAvailable
Describe "Generate-GitBranches.ps1" {

BeforeEach {
$games = Get-Content $PSScriptRoot/games.json -Encoding utf8 | ConvertFrom-Json -AsHashtable

$testDrive = "TestDrive:"
$sourceRepo = $PSScriptRoot

$games = Get-Content $PSScriptRoot/games.json -Encoding utf8 | ConvertFrom-Json -AsHashtable
$remote = 'origin'
$remoteUrl = git remote get-url $remote
}

AfterEach {
Expand Down Expand Up @@ -50,9 +52,9 @@ Describe "Generate-GitBranches.ps1" {
It "Creates and updates branches of a same repo" {
$currentRef = git rev-parse --short HEAD
if ($LASTEXITCODE) { throw }
& ./Generate-GitBranches.ps1 -Repo . -ErrorAction Stop 6>$null # Create
& ./Generate-GitBranches.ps1 -Repo $sameRepo -ErrorAction Stop 6>$null # Create
git checkout $currentRef
& ./Generate-GitBranches.ps1 -Repo . -ErrorAction Stop 6>$null # Update
& ./Generate-GitBranches.ps1 -Repo $sameRepo -ErrorAction Stop 6>$null # Update

cd $sameRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
Expand All @@ -71,8 +73,8 @@ Describe "Generate-GitBranches.ps1" {
}

It "Creates and updates branches of a same repo of (one game)" {
& $sourceRepo/Generate-GitBranches.ps1 -Repo . -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo . -GamePlatform steam -GameEngine hlds -Game valve -Pull -ErrorAction Stop 6>$null # Update
& $sourceRepo/Generate-GitBranches.ps1 -Repo $sameRepo -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo $sameRepo -Remote $remote -Pull -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Update

cd $sameRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
Expand All @@ -97,7 +99,7 @@ Describe "Generate-GitBranches.ps1" {
$differentRepo = "$testDrive/$( (Get-Item $sourceRepo).Name )"
New-Item $differentRepo -ItemType Directory > $null
cd $differentRepo
git init
git init --initial-branch master
git config user.name "bot"
git config user.email "[email protected]"
git commit --allow-empty -m 'Init'
Expand Down
50 changes: 30 additions & 20 deletions Generate-GitBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,59 @@
.EXAMPLE
# Create branches for all games (dry-run)
./Generate-GitBranches.ps1 -Repo . -Pull -WhatIf
./Generate-GitBranches.ps1 -Repo . -WhatIf
# Create branches for all games
./Generate-GitBranches.ps1 -Repo . -Pull
./Generate-GitBranches.ps1 -Repo .
# Create branches for specific game
./Generate-GitBranches.ps1 -Repo . -Pull -GameEngine hlds -Game valve
./Generate-GitBranches.ps1 -Repo . -Pull -GameEngine srcds -Game csgo
# Create branches for all games, and push to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push
# Create branches for specific game(s)
./Generate-GitBranches.ps1 -Repo . -Pull -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Pull -GameEngine srcds -Game cs2,csgo
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine srcds -Game cs2,csgo
# Update branches for all games
# Create branches for specific game(s), and push to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine srcds -Game cs2,csgo
.EXAMPLE
# Update branches for all games, pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Pull -Push
# Update branches for specific game
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine hlds -Game valve
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine srcds -Game csgo
# Update branches for all games, pulling and pushing to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push
# Update branches for specific game(s)
# Update branches for specific game(s), pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine srcds -Game cs2,csgo
# Update branches for specific game(s), pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push -GameEngine srcds -Game cs2,csgo
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory,HelpMessage="Target repo path")]
[ValidateNotNullOrEmpty()]
[string]$Repo
,
[Parameter(HelpMessage="Git remote. Default: 'origin'")]
[string]$Remote = 'origin'
,
[Parameter(HelpMessage="Whether to pull changes from remote repo before creating / updating branches")]
[switch]$Pull
,
[Parameter(HelpMessage="Whether to push changes after creating / updating branches")]
[switch]$Push
,
[Parameter(HelpMessage="Game platform. E.g. 'steam'")]
[Parameter(HelpMessage="Game platform. E.g. 'steam'. If unspecified, all game platforms are selected.")]
[string[]]$GamePlatform
,
[Parameter(HelpMessage="Game engine. E.g. 'hlds' or 'srcds'")]
[Parameter(HelpMessage="Game engine. E.g. 'hlds' or 'srcds'. If unspecified, all game engines are selected.")]
[string[]]$GameEngine
,
[Parameter(HelpMessage="Game. E.g. 'cstrike'")]
[Parameter(HelpMessage="Game. E.g. 'cstrike'. If unspecified, all games are selected.")]
[string[]]$Game
)
Set-StrictMode -Version Latest
Expand Down Expand Up @@ -150,16 +160,16 @@ try {

{ git checkout -f master } | Execute-Command
if ($Pull) {
{ git pull origin master } | Execute-Command
{ git pull "$Remote" master } | Execute-Command
}
$existingBranch = { git rev-parse --verify $branch 2>$null } | Execute-Command -ErrorAction SilentlyContinue
if ($existingBranch) {
"Updating branch '$branch'" | Write-Host -ForegroundColor Green
if ($Pull) {
{ git fetch origin } | Execute-Command
$existingRemoteBranch = { git rev-parse --verify origin/$branch 2>$null } | Execute-Command -ErrorAction SilentlyContinue
{ git fetch "$Remote" } | Execute-Command
$existingRemoteBranch = { git rev-parse --verify "$Remote/$branch" 2>$null } | Execute-Command -ErrorAction SilentlyContinue
if ($existingRemoteBranch) {
{ git branch -f $branch origin/$branch } | Execute-Command
{ git branch -f $branch "$Remote/$branch" } | Execute-Command
}
}
{ git checkout -f $branch } | Execute-Command
Expand Down Expand Up @@ -247,7 +257,7 @@ LAYERED_SIZE=$( if ($kv.Contains('LAYERED_SIZE')) { $kv['LAYERED_SIZE'] } else {
}

if ($Push) {
{ git push origin "$branch" } | Execute-Command
{ git push "$Remote" "$branch" } | Execute-Command
}
}
}catch {
Expand Down

0 comments on commit 58dd1ec

Please sign in to comment.