Skip to content

Commit

Permalink
feat: InstallUtils v0.3.0
Browse files Browse the repository at this point in the history
Update-GitRepository
  • Loading branch information
szymonos committed Dec 10, 2023
1 parent d82998c commit e3d77f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
44 changes: 38 additions & 6 deletions modules/InstallUtils/Functions/git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion modules/InstallUtils/InstallUtils.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions modules/InstallUtils/InstallUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $exportModuleMemberParams = @{
'Update-SessionEnvironmentPath'
# git
'Invoke-GhRepoClone'
'Update-GitRepository'
)
Variable = @()
Alias = @()
Expand Down

0 comments on commit e3d77f6

Please sign in to comment.