From 23a4b3dd7d2afe3b00836552c27ff4b34bd2c5dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Fri, 19 Mar 2021 02:29:39 +0100 Subject: [PATCH] feat: Allow default 'main' branches in the whole bucket ecosystem (#62) --- CHANGELOG.md | 5 +++- bin/auto-pr.ps1 | 27 ++++++++++++------- lib/Diagnostic.ps1 | 56 ++++++++++++++++++++++++++++++++++++++- lib/Update.ps1 | 10 ++++--- lib/buckets.ps1 | 3 ++- libexec/scoop-checkup.ps1 | 1 + libexec/scoop-config.ps1 | 2 +- libexec/scoop-status.ps1 | 2 +- 8 files changed, 89 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5eb16ca47..8eceb3ffcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,11 +20,14 @@ - **binaries**: Indicate binary execution errors with exit codes - Git operations with custom wrapper are now executable under Unix-like systems - **auto-pr** + - Use `main` branch instead of `master` if `remotes/origin/main` exists - Require `-Upstream` only when `-Request` is provided - Scoop proxy configuration will be used for git calls - Call native `git` command instead of `hub` for push operation - Refactor all git/hub calls to use -C option -- **scoop-checkup**: Test full shovel adoption +- **scoop-checkup**: + - Check for main branches adoption (if supported) + - Check for full shovel adoption - **scoop-alias**: First alias addition is correctly registered and created - **autoupdate**: Do not autoupdate unless URL is accessible after successful hash extraction diff --git a/bin/auto-pr.ps1 b/bin/auto-pr.ps1 index 846c7b13b2..7e75ee84fe 100644 --- a/bin/auto-pr.ps1 +++ b/bin/auto-pr.ps1 @@ -2,7 +2,7 @@ .SYNOPSIS Updates manifests and pushes them or creates pull-requests. .DESCRIPTION - Updates manifests and pushes them directly to the master branch or creates pull-requests for upstream. + Updates manifests and pushes them directly to the master (main) branch or creates pull-requests for upstream. .PARAMETER Upstream Specifies the upstream repository with the target branch. Must be in format '/:' @@ -91,13 +91,21 @@ function _gitWrapper { } } +function _selectMasterBranch { + $branches = _gitWrapper @splat -Command 'branch' -Argument '--all' + $master = if ($branches -like '* remotes/origin/main') { 'main' } else { 'master' } + + return $master +} + # json object, application name, upstream repository, relative path to manifest file function pull_requests($json, [String] $app, [String] $upstream, [String] $manifestFile) { $version = $json.version $homepage = $json.homepage $branch = "manifest/$app-$version" - execute "hub $repoContext checkout master" + $master = _selectMasterBranch + execute "hub $repoContext checkout $master" Write-UserMessage "hub rev-parse --verify $branch" -ForegroundColor 'Green' hub -C "$RepositoryRoot" rev-parse --verify $branch @@ -160,12 +168,13 @@ $repoContext = "-C ""$RepositoryRoot""" $splat = @{ 'Repository' = $RepositoryRoot } Write-UserMessage 'Updating ...' -ForegroundColor 'DarkCyan' +$master = _selectMasterBranch if ($Push) { - _gitWrapper @splat -Command 'pull' -Argument 'origin', 'master' -Proxy - _gitWrapper @splat -Command 'checkout' -Argument 'master' + _gitWrapper @splat -Command 'pull' -Argument 'origin', $master -Proxy + _gitWrapper @splat -Command 'checkout' -Argument $master } else { - _gitWrapper @splat -Command 'pull' -Argument 'upstream', 'master' -Proxy - _gitWrapper @splat -Command 'push' -Argument 'origin', 'master' -Proxy + _gitWrapper @splat -Command 'pull' -Argument 'upstream', $master -Proxy + _gitWrapper @splat -Command 'push' -Argument 'origin', $master -Proxy } if (!$SkipCheckver) { @@ -223,10 +232,10 @@ foreach ($changedFile in _gitWrapper @splat -Command 'diff' -Argument '--name-on if ($Push) { Write-UserMessage 'Pushing updates ...' -ForegroundColor 'DarkCyan' - _gitWrapper @splat -Command 'push' -Argument 'origin', 'master' -Proxy + _gitWrapper @splat -Command 'push' -Argument 'origin', $master -Proxy } else { - Write-UserMessage 'Returning to master branch and removing unstaged files ...' -ForegroundColor 'DarkCyan' - _gitWrapper @splat -Command 'checkout' -Argument '--force', 'master' -Proxy + Write-UserMessage "Returning to $master branch and removing unstaged files ..." -ForegroundColor 'DarkCyan' + _gitWrapper @splat -Command 'checkout' -Argument '--force', $master -Proxy } _gitWrapper @splat -Command 'reset' -Argument '--hard' diff --git a/lib/Diagnostic.ps1 b/lib/Diagnostic.ps1 index 58d83b70f5..db12051b0b 100644 --- a/lib/Diagnostic.ps1 +++ b/lib/Diagnostic.ps1 @@ -4,7 +4,7 @@ Return $true if the test passed, otherwise $false. Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the recommended actions to rectify. #> -'core', 'buckets', 'decompress', 'Helpers' | ForEach-Object { +'core', 'buckets', 'decompress', 'Git', 'Helpers' | ForEach-Object { . (Join-Path $PSScriptRoot "$_.ps1") } @@ -301,3 +301,57 @@ function Test-DiagShovelAdoption { return $true } + +function Test-MainBranchAdoption { + <# + .SYNOPSIS + Test if shovel and all locally added buckets were switched to main branch. + #> + [CmdletBinding()] + [OutputType([bool])] + param() + + $verdict = $true + $br = get_config 'SCOOP_BRANCH' + $scoopHome = versiondir 'scoop' 'current' + $fix = @( + ' Fixable with running following command:' + ' scoop update' + ) + + # Shovel - empty config + if ($null -eq $br) { + Write-UserMessage -Message '''SCOOP_BRANCH'' configuration option is not configured.' -Warning + Write-UserMessage -Message $fix + + $verdict = $false + } elseif (($br -eq 'master') -or (Invoke-GitCmd -Repository $scoopHome -Command 'branch' -Argument '--show-current') -eq 'master') { + # Shovel - master config, current master branch + Write-UserMessage -Message 'Default branch was changed to ''main''.' -Warning + Write-UserMessage -Message $fix + + $verdict = $false + } + + $toFix = @() + foreach ($b in Get-LocalBucket) { + $path = Find-BucketDirectory -Name $b -Root + $branches = Invoke-GitCmd -Repository $path -Command 'branch' -Argument '--all' + $current = Invoke-GitCmd -Repository $path -Command 'branch' -Argument '--show-current' + + if (($branches -like '* remotes/origin/main') -and ($current -eq 'master')) { + $toFix += @{ 'name' = $b; 'path' = $path } + $verdict = $false + } + } + + if (($verdict -eq $false) -and ($toFix.Count -gt 0)) { + Write-UserMessage -Message "Locally added buckets should be reconfigured to main branch." -Warning + Write-UserMessage -Message @( + ' Fixable with running following commands:' + ($toFix | ForEach-Object { " git -C '$($_.path)' checkout main" }) + ) + } + + return $verdict +} diff --git a/lib/Update.ps1 b/lib/Update.ps1 index c092e92ab2..6df13db4b5 100644 --- a/lib/Update.ps1 +++ b/lib/Update.ps1 @@ -2,9 +2,8 @@ . (Join-Path $PSScriptRoot "$_.ps1") } -# TODO: Change -$DEFAULT_UPDATE_REPO = 'https://github.com/lukesampson/scoop' -$DEFAULT_UPDATE_BRANCH = 'master' +$DEFAULT_UPDATE_REPO = 'https://github.com/Ash258/Scoop-Core' +$DEFAULT_UPDATE_BRANCH = 'main' # TODO: CONFIG adopt refactor $SHOW_UPDATE_LOG = get_config 'show_update_log' $true @@ -141,6 +140,11 @@ function Update-Scoop { $configRepo = $DEFAULT_UPDATE_REPO set_config 'SCOOP_REPO' $DEFAULT_UPDATE_REPO | Out-Null } + # Main adoption + if ($configBranch -and ($configBranch -eq 'master')) { + Write-UserMessage -Message 'Master branch should not be used anymore. Migrating to ''main''' -Warning + $configBranch = $null # Trigger automatic config handler below + } if (!$configBranch) { $configBranch = $DEFAULT_UPDATE_BRANCH set_config 'SCOOP_BRANCH' $DEFAULT_UPDATE_BRANCH | Out-Null diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index 2dcdad2fde..e77455cf86 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -111,7 +111,7 @@ function Add-Bucket { $bucketDirectory = Find-BucketDirectory -Name $Name -Root if (Test-Path $bucketDirectory) { throw "Bucket with name '$Name' already exists." } - Write-UserMessage -Message 'Checking repository... ' -Output:$false + Write-UserMessage -Message 'Checking repository...' -Output:$false $out = Invoke-GitCmd -Command 'ls-remote' -Argument """$RepositoryUrl""" -Proxy 2>&1 if ($LASTEXITCODE -ne 0) { throw "'$RepositoryUrl' is not valid git repository ($out)" @@ -119,6 +119,7 @@ function Add-Bucket { ensure $SCOOP_BUCKETS_DIRECTORY | Out-Null $bucketDirectory = (ensure $bucketDirectory).Path + Write-UserMessage -Message 'Cloning bucket repository...' -Output:$false Invoke-GitCmd -Command 'clone' -Argument '--quiet', """$RepositoryUrl""", """$bucketDirectory""" -Proxy Write-UserMessage -Message "The $name bucket was added successfully." -Success diff --git a/libexec/scoop-checkup.ps1 b/libexec/scoop-checkup.ps1 index 36cb72b022..ff69b3abf4 100644 --- a/libexec/scoop-checkup.ps1 +++ b/libexec/scoop-checkup.ps1 @@ -20,6 +20,7 @@ $issues += !(Test-DiagDrive) $issues += !(Test-DiagConfig) $issues += !(Test-DiagCompletionRegistered) $issues += !(Test-DiagShovelAdoption) +$issues += !(Test-MainBranchAdoption) if ($issues -gt 0) { Write-UserMessage -Message '', "Found $issues potential $(pluralize $issues 'problem' 'problems')." -Warning diff --git a/libexec/scoop-config.ps1 b/libexec/scoop-config.ps1 index 07511589f8..a197860a54 100644 --- a/libexec/scoop-config.ps1 +++ b/libexec/scoop-config.ps1 @@ -49,7 +49,7 @@ # Git repository containining scoop source code. # This configuration is useful for custom tweaked forks. # -# SCOOP_BRANCH: master|develop +# SCOOP_BRANCH: main|NEW # Allow to use different branch than master. # Could be used for testing specific functionalities before released into all users. # If you want to receive updates earlier to test new functionalities use develop (see: 'https://github.com/lukesampson/scoop/issues/2939') diff --git a/libexec/scoop-status.ps1 b/libexec/scoop-status.ps1 index 5b8fcce056..8250d53024 100644 --- a/libexec/scoop-status.ps1 +++ b/libexec/scoop-status.ps1 @@ -18,7 +18,7 @@ if (Join-Path $currentdir '.git' | Test-Path -PathType 'Container') { $target = @{ 'Repository' = $currentdir } Invoke-GitCmd @target -Command 'fetch' -Argument '--quiet', 'origin' -Proxy - $commits = Invoke-GitCmd @target -Command 'log' -Argument '--oneline', """HEAD..origin/$(get_config SCOOP_BRANCH)""" + $commits = Invoke-GitCmd @target -Command 'log' -Argument '--oneline', """HEAD..origin/$(get_config 'SCOOP_BRANCH' 'main')""" if ($commits) { $needs_update = $true } } else {