From e3d77f60d2ea31a27089e4cc83d502a92a650e65 Mon Sep 17 00:00:00 2001 From: Szymon Osiecki Date: Sun, 10 Dec 2023 17:31:08 +0100 Subject: [PATCH] feat: InstallUtils v0.3.0 Update-GitRepository --- modules/InstallUtils/Functions/git.ps1 | 44 ++++++++++++++++++++++---- modules/InstallUtils/InstallUtils.psd1 | 3 +- modules/InstallUtils/InstallUtils.psm1 | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/modules/InstallUtils/Functions/git.ps1 b/modules/InstallUtils/Functions/git.ps1 index 72077856..5a5fb1db 100644 --- a/modules/InstallUtils/Functions/git.ps1 +++ b/modules/InstallUtils/Functions/git.ps1 @@ -35,12 +35,17 @@ function Invoke-GhRepoClone { try { Push-Location $destPath $status = if ($(Invoke-Command $getOrigin) -match "github\.com[:/]$org/$repo\b") { - # refresh target repository - git fetch --prune --quiet - git switch main --force --quiet 2>$null - git reset --hard --quiet origin/main - Write-Verbose "Repository `"$OrgRepo`" refreshed successfully." - Write-Output 2 + $defaultBranch = (git branch --all | Select-String "(?<=HEAD -> $(git remote)/).+").Matches.Value + if ($defaultBranch) { + # refresh target repository + git switch $defaultBranch --force --quiet 2>$null + Update-GitRepository | Out-Null + Write-Verbose "Repository `"$OrgRepo`" refreshed successfully." + Write-Output 2 + } else { + Write-Warning 'Default branch not found.' + Write-Output 0 + } } else { Write-Warning "Another `"$repo`" repository exists not matching remote." Write-Output 0 @@ -66,3 +71,30 @@ function Invoke-GhRepoClone { return $status } } + +<# +.SYNOPSIS +Function for updating current git branch from remote. +#> +function Update-GitRepository { + # perform check if the repository has remote + $remote = git remote 2>$null + if ($remote) { + # fetch updates from remote + Write-Host "fetching updates from the $remote..." + git fetch --tags --prune --prune-tags --force $remote + # check if current branch is behind remote + $branch = git branch --show-current + if ((git rev-parse HEAD) -ne (git rev-parse "$remote/$branch")) { + Write-Host "$branch branch is behind the $remote, performing hard reset" + git reset --hard "$remote/$branch" + return 2 + } else { + Write-Host "$branch branch up to date" + return 1 + } + } else { + Write-Host 'Not a git repository.' + return 0 + } +} diff --git a/modules/InstallUtils/InstallUtils.psd1 b/modules/InstallUtils/InstallUtils.psd1 index 88b64ba9..ceecb9f3 100644 --- a/modules/InstallUtils/InstallUtils.psd1 +++ b/modules/InstallUtils/InstallUtils.psd1 @@ -12,7 +12,7 @@ RootModule = 'InstallUtils.psm1' # Version number of this module. - ModuleVersion = '0.2.0' + ModuleVersion = '0.3.0' # Supported PSEditions CompatiblePSEditions = @('Core', 'Desk') @@ -77,6 +77,7 @@ 'Update-SessionEnvironmentPath' # git 'Invoke-GhRepoClone' + 'Update-GitRepository' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/modules/InstallUtils/InstallUtils.psm1 b/modules/InstallUtils/InstallUtils.psm1 index f2f2cdfe..a07def98 100644 --- a/modules/InstallUtils/InstallUtils.psm1 +++ b/modules/InstallUtils/InstallUtils.psm1 @@ -12,6 +12,7 @@ $exportModuleMemberParams = @{ 'Update-SessionEnvironmentPath' # git 'Invoke-GhRepoClone' + 'Update-GitRepository' ) Variable = @() Alias = @()