<# .SYNOPSIS Script per creare un nuovo branch Git in un progetto selezionato. .DESCRIPTION Questo script permette di: 1) Selezionare una cartella di progetto 2) Se è un repository Git: a) Stashare eventuali modifiche non committate b) Passare al branch master (o altro branch base specificato) c) Creare un nuovo branch d) Passare al nuovo branch 3) Se non è un repository Git, ritorna un errore .PARAMETER Name Nome del nuovo branch da creare. Se non specificato, verrà richiesto durante l'esecuzione. .PARAMETER Base Branch di base da cui creare il nuovo branch (default: "master") .EXAMPLE .\Create-PDCA0-Branch.ps1 -Name "feature/nuova-funzionalita" Crea un nuovo branch "feature/nuova-funzionalita" a partire dal branch master .EXAMPLE .\Create-PDCA0-Branch.ps1 -Name "bugfix/correzione" -Base "develop" Crea un nuovo branch "bugfix/correzione" a partire dal branch develop .EXAMPLE .\Create-PDCA0-Branch.ps1 Chiede interattivamente il nome del branch e poi lo crea a partire dal branch master .NOTES Autore: Cascade Data: 2025-07-04 #> param( [Parameter(Mandatory=$false, Position=0)] [string]$Name, [Parameter(Mandatory=$false)] [string]$Base = "master" ) function Test-GitRepository { param ( [Parameter(Mandatory=$true)] [string]$Path ) $gitDir = Join-Path -Path $Path -ChildPath ".git" return Test-Path -Path $gitDir -PathType Container } function Invoke-GitCommand { param ( [Parameter(Mandatory=$true)] [string]$Command, [Parameter(Mandatory=$true)] [string]$WorkingDirectory, [Parameter(Mandatory=$false)] [string]$ErrorMessage ) try { $output = & git -C $WorkingDirectory $Command.Split(" ") 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "Errore nell'esecuzione del comando Git: $Command" -ForegroundColor Red Write-Host $output -ForegroundColor Red if ($ErrorMessage) { Write-Host $ErrorMessage -ForegroundColor Red } return $false } return $true } catch { Write-Host "Eccezione nell'esecuzione del comando Git: $Command" -ForegroundColor Red Write-Host $_.Exception.Message -ForegroundColor Red if ($ErrorMessage) { Write-Host $ErrorMessage -ForegroundColor Red } return $false } } function Main { # Verifica che lo script Select-PDCA0-Folders.ps1 esista $selectFoldersPath = Join-Path $PSScriptRoot '../src/general/select-pdca0-folders.ps1' if (-not (Test-Path -Path $selectFoldersPath -PathType Leaf)) { Write-Host "Errore: Lo script Select-PDCA0-Folders.ps1 non è stato trovato nella directory corrente." -ForegroundColor Red return 1 } # Se il nome del branch non è stato specificato, chiedi all'utente if ([string]::IsNullOrEmpty($Name)) { $Name = Read-Host "Inserisci il nome del nuovo branch" if ([string]::IsNullOrEmpty($Name)) { Write-Host "Errore: È necessario specificare un nome per il branch." -ForegroundColor Red return 1 } } # Seleziona una cartella con Select-PDCA0-Folders.ps1 Write-Host "Seleziona un progetto Git..." -ForegroundColor Cyan $selectedFolder = & $selectFoldersPath if ($null -eq $selectedFolder) { Write-Host "Nessuna cartella selezionata. Operazione annullata." -ForegroundColor Yellow return 1 } $folderPath = $selectedFolder.FullName Write-Host "Cartella selezionata: $folderPath" -ForegroundColor Green # Verifica se è un repository Git if (-not (Test-GitRepository -Path $folderPath)) { Write-Host "Errore: La cartella selezionata non è un repository Git." -ForegroundColor Red return 1 } Write-Host "Repository Git rilevato. Procedo con le operazioni..." -ForegroundColor Green # 1. Stasha eventuali modifiche non committate Write-Host "Verifico se ci sono modifiche da stashare..." -ForegroundColor Cyan $hasChanges = & git -C $folderPath status --porcelain if ($hasChanges) { Write-Host "Modifiche rilevate. Procedo con lo stash..." -ForegroundColor Yellow $stashName = "Auto-stash prima di creare il branch $Name" $stashSuccess = Invoke-GitCommand -Command "stash push -m `"$stashName`"" -WorkingDirectory $folderPath -ErrorMessage "Impossibile eseguire lo stash delle modifiche." if (-not $stashSuccess) { return 1 } Write-Host "Modifiche salvate nello stash." -ForegroundColor Green } else { Write-Host "Nessuna modifica da stashare." -ForegroundColor Green } # 2. Passa al branch base (default: master) Write-Host "Passo al branch base '$Base'..." -ForegroundColor Cyan $checkoutSuccess = Invoke-GitCommand -Command "checkout $Base" -WorkingDirectory $folderPath -ErrorMessage "Impossibile passare al branch $Base. Assicurati che esista." if (-not $checkoutSuccess) { return 1 } Write-Host "Ora sei sul branch '$Base'." -ForegroundColor Green # 3. Aggiorna il branch base Write-Host "Aggiorno il branch '$Base'..." -ForegroundColor Cyan $pullSuccess = Invoke-GitCommand -Command "pull" -WorkingDirectory $folderPath -ErrorMessage "Impossibile aggiornare il branch $Base." if (-not $pullSuccess) { Write-Host "Avviso: Impossibile aggiornare il branch $Base. Continuo comunque." -ForegroundColor Yellow } else { Write-Host "Branch '$Base' aggiornato." -ForegroundColor Green } # 4. Crea un nuovo branch Write-Host "Creo il nuovo branch '$Name'..." -ForegroundColor Cyan $createSuccess = Invoke-GitCommand -Command "checkout -b $Name" -WorkingDirectory $folderPath -ErrorMessage "Impossibile creare il branch $Name." if (-not $createSuccess) { return 1 } Write-Host "Branch '$Name' creato con successo." -ForegroundColor Green Write-Host "Operazione completata con successo!" -ForegroundColor Green Write-Host "Ora sei sul branch '$Name' nel repository $folderPath" -ForegroundColor Green return 0 } # Esegui lo script $exitCode = Main exit $exitCode