[CmdletBinding()] param( [string]$RepositoryRoot = "", [string]$RemoteName = "origin", [string]$RemoteUrl = "https://git.tuxinet.dk/thomas/TuxDMX-WebUI.git", [string]$Branch = "main", [string]$CommitMessage = "Update TuxDMX", [string[]]$StageExclude = @( ".pnpm-store/**", "node_modules/**", ".venv/**", "__pycache__/**", ".pytest_cache/**", ".ruff_cache/**", ".mypy_cache/**", "frontend/dist/**", "backend/.pytest_cache/**", "backend/htmlcov/**", "data/**", ".env" ), [switch]$InitIfNeeded, [switch]$SetOrigin, [switch]$StageAll, [switch]$Commit, [switch]$Push ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" function Write-Info { param([string]$Message) Write-Host "[TuxDMX] $Message" } function Normalize-RepoPath { param([string]$Path) return ($Path -replace "\\", "/").Trim() } function Test-IsExcludedPath { param( [string]$Path, [string[]]$Patterns ) $normalizedPath = Normalize-RepoPath -Path $Path foreach ($pattern in $Patterns) { $normalizedPattern = Normalize-RepoPath -Path $pattern if ($normalizedPattern.EndsWith("/**")) { $prefix = $normalizedPattern.Substring(0, $normalizedPattern.Length - 3).TrimEnd("/") if ($normalizedPath -eq $prefix -or $normalizedPath.StartsWith("$prefix/")) { return $true } continue } if ($normalizedPath -eq $normalizedPattern) { return $true } } return $false } function Invoke-Git { param( [Parameter(Mandatory = $true)] [string[]]$Arguments, [switch]$IgnoreFailure ) & git -C $RepositoryRoot @Arguments $exitCode = $LASTEXITCODE if (-not $IgnoreFailure -and $exitCode -ne 0) { throw "git $($Arguments -join ' ') fejlede med exit code $exitCode." } return $exitCode } if (-not (Get-Command git -ErrorAction SilentlyContinue)) { throw "Git blev ikke fundet i PATH." } if ([string]::IsNullOrWhiteSpace($RepositoryRoot)) { $scriptRoot = if (-not [string]::IsNullOrWhiteSpace($PSScriptRoot)) { $PSScriptRoot } else { Split-Path -Parent $PSCommandPath } $RepositoryRoot = (Resolve-Path (Join-Path $scriptRoot "..")).Path } $gitDirectory = Join-Path $RepositoryRoot ".git" if (-not (Test-Path $gitDirectory)) { if (-not $InitIfNeeded) { throw "RepositoryRoot er ikke et git-repository endnu. Koer med -InitIfNeeded for at initialisere det." } Write-Info "Initialiserer git-repository paa branch '$Branch'." & git -C $RepositoryRoot init -b $Branch if ($LASTEXITCODE -ne 0) { throw "git init fejlede med exit code $LASTEXITCODE." } } Write-Info "Repo: $RepositoryRoot" Write-Info "Remote: $RemoteName -> $RemoteUrl" $remoteExists = $false $currentRemoteUrl = "" & git -C $RepositoryRoot remote get-url $RemoteName *> $null if ($LASTEXITCODE -eq 0) { $remoteExists = $true $currentRemoteUrl = (& git -C $RepositoryRoot remote get-url $RemoteName).Trim() } if ($SetOrigin) { if ($remoteExists) { if ($currentRemoteUrl -ne $RemoteUrl) { Write-Info "Opdaterer remote '$RemoteName'." Invoke-Git -Arguments @("remote", "set-url", $RemoteName, $RemoteUrl) | Out-Null } else { Write-Info "Remote '$RemoteName' peger allerede paa den oenskede URL." } } else { Write-Info "Tilfoejer remote '$RemoteName'." Invoke-Git -Arguments @("remote", "add", $RemoteName, $RemoteUrl) | Out-Null $remoteExists = $true } } if ($StageAll) { Write-Info "Stager alle aendringer." Write-Info ("Ekskluderer fra staging: " + ($StageExclude -join ", ")) Invoke-Git -Arguments @("add", "--update", "--", ".") | Out-Null $untrackedFiles = & git -C $RepositoryRoot ls-files --others --exclude-standard --full-name if ($LASTEXITCODE -ne 0) { throw "Kunne ikke hente untracked filer fra git." } $filesToStage = @() foreach ($file in $untrackedFiles) { if ([string]::IsNullOrWhiteSpace($file)) { continue } if (-not (Test-IsExcludedPath -Path $file -Patterns $StageExclude)) { $filesToStage += $file.Trim() } } if ($filesToStage.Count -gt 0) { Invoke-Git -Arguments (@("add", "--") + $filesToStage) | Out-Null } } if ($Commit) { Write-Info "Opretter commit." & git -C $RepositoryRoot diff --cached --quiet if ($LASTEXITCODE -eq 0) { throw "Der er ingen staged aendringer at committe." } Invoke-Git -Arguments @("commit", "-m", $CommitMessage) | Out-Null } $currentBranch = (& git -C $RepositoryRoot branch --show-current).Trim() if ([string]::IsNullOrWhiteSpace($currentBranch)) { $currentBranch = $Branch } if ($Push) { if (-not $remoteExists) { throw "Remote '$RemoteName' findes ikke endnu. Koer med -SetOrigin foerst." } Write-Info "Pusher '$currentBranch' til '$RemoteName/$Branch'." Invoke-Git -Arguments @("push", "-u", $RemoteName, "$currentBranch`:$Branch") | Out-Null } Write-Info "Git status:" Invoke-Git -Arguments @("status", "--short") -IgnoreFailure | Out-Null Write-Info "Faerdig."