From 9c860fca9e61fa59d9eb86e04529394e03a1e3a5 Mon Sep 17 00:00:00 2001 From: Maria Zhelezova <43066499+mazhelez@users.noreply.github.com> Date: Fri, 24 Jul 2026 13:33:42 +0200 Subject: [PATCH] Derive Miapp base branch from origin/HEAD instead of $env:RepoBranchName Miapp read the base branch from $env:RepoBranchName. When run against a submodule whose default branch differs from the outer repository (BCApps uses 'main', while the consuming monorepo sets RepoBranchName='master'), this built refs like 'origin/master' that do not exist in BCApps and failed with: fatal: ambiguous argument 'HEAD..origin/master': unknown revision. Replace Initialize-MiappRepoBranchName with Get-MiappBaseBranch, which resolves the base branch from the repository's own origin/HEAD (git symbolic-ref, with 'git remote show origin' as a fallback) and caches the result in a module-scoped variable. No environment variable is read or written, so Miapp always targets the default branch of the repository it operates on. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- build/scripts/Miapp/MicroApp.psm1 | 10 +++--- build/scripts/Miapp/MicroAppGitHelper.psm1 | 38 +++++++++++++++------- build/scripts/Miapp/MicroAppIntegrate.psm1 | 14 ++++---- build/scripts/Miapp/MicroSnapApp.psm1 | 10 +++--- build/scripts/Miapp/README.md | 4 ++- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/build/scripts/Miapp/MicroApp.psm1 b/build/scripts/Miapp/MicroApp.psm1 index b4f54f42f7e..8910f0b24a2 100644 --- a/build/scripts/Miapp/MicroApp.psm1 +++ b/build/scripts/Miapp/MicroApp.psm1 @@ -123,8 +123,8 @@ function Invoke-Miapp try { - if(-not (Initialize-MiappRepoBranchName)) { - Throw "Cannot determine base branch. Set `$env:RepoBranchName to the base branch name (for example 'main')." + if(-not (Get-MiappBaseBranch)) { + Throw "Cannot determine the base branch from 'origin/HEAD'. Ensure the repository has an 'origin' remote with a resolvable default branch (for example: git remote set-head origin --auto)." } $params = @{ @@ -200,11 +200,11 @@ function Assert-RepositoryIsNotBehindTheRemote { Throw "Your branch is behind, pull before continuing." } - if(Test-PendingChangeFromBranch "origin/$env:RepoBranchName") { - Throw "Your branch is behind origin/$($env:RepoBranchName), merge or rebase before continuing." + if(Test-PendingChangeFromBranch "origin/$(Get-MiappBaseBranch)") { + Throw "Your branch is behind origin/$(Get-MiappBaseBranch), merge or rebase before continuing." } - if(-not (Test-GitBranchHasAtLeastOneCommit "origin/$env:RepoBranchName")) { + if(-not (Test-GitBranchHasAtLeastOneCommit "origin/$(Get-MiappBaseBranch)")) { Throw "You need to commit at least once before continuing." } } diff --git a/build/scripts/Miapp/MicroAppGitHelper.psm1 b/build/scripts/Miapp/MicroAppGitHelper.psm1 index 87d2c656ef4..e84a2abad6f 100644 --- a/build/scripts/Miapp/MicroAppGitHelper.psm1 +++ b/build/scripts/Miapp/MicroAppGitHelper.psm1 @@ -434,28 +434,44 @@ function Get-GitCurrentBranch { git rev-parse --abbrev-ref HEAD } -function Initialize-MiappRepoBranchName { +$script:MiappBaseBranch = $null + +function Get-MiappBaseBranch { + <# + .SYNOPSIS + Returns the base branch Miapp integrates from, derived from the repository + Miapp operates on - not from any environment variable. + + .DESCRIPTION + The base branch is resolved from 'origin/HEAD' of the current repository so + that Miapp always targets that repository's own default branch, even when it + runs inside a submodule whose default branch differs from the outer repo. + Deriving it from the repository (rather than an ambient variable such as + $env:RepoBranchName) avoids constructing a non-existent ref like + 'origin/master' when the outer repo and the submodule use different default + branch names. The resolved value is cached for the lifetime of the module. + #> [CmdletBinding()] [OutputType([string])] param() - if ($env:RepoBranchName) { - return $env:RepoBranchName + if ($script:MiappBaseBranch) { + return $script:MiappBaseBranch } [string] $originHeadRef = (git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>$null) if ($originHeadRef -imatch '^origin/(.+)$') { - $env:RepoBranchName = $Matches[1] + $script:MiappBaseBranch = $Matches[1] } - if (-not $env:RepoBranchName) { + if (-not $script:MiappBaseBranch) { [string] $originHeadBranch = (git remote show origin 2>$null | Select-String 'HEAD branch:' | Select-Object -First 1) if ($originHeadBranch -imatch 'HEAD branch:\s*(.+)$') { - $env:RepoBranchName = $Matches[1].Trim() + $script:MiappBaseBranch = $Matches[1].Trim() } } - $env:RepoBranchName + $script:MiappBaseBranch } function Get-GitLastCommitSHA1 { @@ -517,9 +533,9 @@ function GetGitCommittedFiles { [OutputType([string[]])] param() - # Returns files committed locally since origin/RepoBranchName - if (-not (Initialize-MiappRepoBranchName)) { return } - git diff --name-only "origin/$env:RepoBranchName...HEAD" | ? { $_ } + # Returns files committed locally since the base branch (origin/HEAD) + if (-not (Get-MiappBaseBranch)) { return } + git diff --name-only "origin/$(Get-MiappBaseBranch)...HEAD" | ? { $_ } } @@ -724,7 +740,7 @@ Export-ModuleMember Get-GitCanonicalPath Export-ModuleMember Get-GitChangedFiles Export-ModuleMember Get-GitCurrentBranch Export-ModuleMember Get-GitCurrentRemoteBranch -Export-ModuleMember Initialize-MiappRepoBranchName +Export-ModuleMember Get-MiappBaseBranch Export-ModuleMember Get-GitFileStatus Export-ModuleMember Get-GitLastCommitSHA1 Export-ModuleMember Get-GitMergeToolConfig diff --git a/build/scripts/Miapp/MicroAppIntegrate.psm1 b/build/scripts/Miapp/MicroAppIntegrate.psm1 index 09184a548b8..5879b8b3a83 100644 --- a/build/scripts/Miapp/MicroAppIntegrate.psm1 +++ b/build/scripts/Miapp/MicroAppIntegrate.psm1 @@ -120,7 +120,7 @@ function Invoke-IntegrateFile { [string] $File, [ValidateNotNullOrEmpty()] - [char] $Status = (Get-GitFileStatus $File "origin/$env:RepoBranchName"), + [char] $Status = (Get-GitFileStatus $File "origin/$(Get-MiappBaseBranch)"), [ValidateNotNull()] [HashTable] $Params = @{} @@ -133,8 +133,8 @@ function Invoke-IntegrateFile { [string] $fileName = Get-IntegrationFileName $File [string] $branchPath = Get-IntegrationBranchName $File - if(($Status -ne $GitFileStatus.Deleted) -and -not (Test-GitFileDifferentFromRemote $File "origin/$env:RepoBranchName")) { - Write-Verbose "Skipping $File`nThe file status is '$Status' but there is no difference from the version on origin/$($env:RepoBranchName)" + if(($Status -ne $GitFileStatus.Deleted) -and -not (Test-GitFileDifferentFromRemote $File "origin/$(Get-MiappBaseBranch)")) { + Write-Verbose "Skipping $File`nThe file status is '$Status' but there is no difference from the version on origin/$(Get-MiappBaseBranch)" return } @@ -154,7 +154,7 @@ function IntegrateBranchedObjects { [string] $File, [ValidateNotNullOrEmpty()] - [char] $Status = (Get-GitFileStatus $File "origin/$env:RepoBranchName"), + [char] $Status = (Get-GitFileStatus $File "origin/$(Get-MiappBaseBranch)"), [ValidateNotNullOrEmpty()] [string] $BranchPath = (Get-IntegrationBranchName $File), @@ -331,7 +331,7 @@ function ExcludeOrStageFile { $file = Resolve-GitPath -Relative -RemoteStyle $DestFile - if ((Test-Path $DestFile) -and -not (Test-GitFileDifferentFromRemote $file "origin/$env:RepoBranchName")) { + if ((Test-Path $DestFile) -and -not (Test-GitFileDifferentFromRemote $file "origin/$(Get-MiappBaseBranch)")) { Write-Host "$file does not differ from remote version, adding to ignore list" AddFileToExclusionList (Get-GitCanonicalPath $file -Absolute) } else { @@ -520,12 +520,12 @@ function Get-BaseFile { [string] $File, [Parameter()] - [char] $Status = (Get-GitFileStatus $File "origin/$env:RepoBranchName") + [char] $Status = (Get-GitFileStatus $File "origin/$(Get-MiappBaseBranch)") ) if($Status -eq $GitFileStatus.Deleted) { return } - [string] $baseFile = Get-GitRemoteFile $File "origin/$env:RepoBranchName" $Status + [string] $baseFile = Get-GitRemoteFile $File "origin/$(Get-MiappBaseBranch)" $Status if($baseFile) { Write-Verbose "Remote base file found for $File" diff --git a/build/scripts/Miapp/MicroSnapApp.psm1 b/build/scripts/Miapp/MicroSnapApp.psm1 index 12763de8a2b..e0f9692a831 100644 --- a/build/scripts/Miapp/MicroSnapApp.psm1 +++ b/build/scripts/Miapp/MicroSnapApp.psm1 @@ -16,7 +16,7 @@ function Invoke-MiSnapApp ) begin { if(-not $Files) { - # Default: validate all files committed since origin/RepoBranchName + # Default: validate all files committed since the base branch (origin/HEAD) $Files = Get-MiappCommittedFiles } } @@ -45,19 +45,19 @@ function Invoke-MiSnapApp <# .SYNOPSIS -Returns files committed locally since origin/RepoBranchName, relative to the repo root. +Returns files committed locally since the base branch (origin/HEAD), relative to the repo root. #> function Get-MiappCommittedFiles { [CmdletBinding()] [OutputType([string[]])] param() - if (-not (Initialize-MiappRepoBranchName)) { - Write-Host -ForegroundColor Yellow "RepoBranchName is not set and could not be inferred from origin/HEAD. Cannot determine committed files." + if (-not (Get-MiappBaseBranch)) { + Write-Host -ForegroundColor Yellow "Could not determine the base branch from origin/HEAD. Cannot determine committed files." return } - git diff --name-only "origin/$env:RepoBranchName...HEAD" | ? { $_ } + git diff --name-only "origin/$(Get-MiappBaseBranch)...HEAD" | ? { $_ } } function GetBranchedObjectFileNames { diff --git a/build/scripts/Miapp/README.md b/build/scripts/Miapp/README.md index e87189d9306..d06f3506b36 100644 --- a/build/scripts/Miapp/README.md +++ b/build/scripts/Miapp/README.md @@ -266,9 +266,11 @@ $MiappConfig = @{ ### Environment Variables -- `$env:RepoBranchName` - Base branch name used for comparisons (defaults to `origin/HEAD`, typically `main`, when not set) - `$env:MIAPP_DIR` - Directory for miapp temporary files (default: $env:USERPROFILE) +> The base branch used for comparisons is derived automatically from the +> repository's own `origin/HEAD` (typically `main` for BCApps). + ### Output The tool provides colored console output: