Skip to content

Commit

Permalink
feat: Allow default 'main' branches in the whole bucket ecosystem (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 authored Mar 19, 2021
1 parent 17bf693 commit 23a4b3d
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 18 additions & 9 deletions bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<user>/<repo>:<branch>'
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down
56 changes: 55 additions & 1 deletion lib/Diagnostic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}
10 changes: 7 additions & 3 deletions lib/Update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ 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)"
}

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
Expand Down
1 change: 1 addition & 0 deletions libexec/scoop-checkup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-status.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 23a4b3d

Please sign in to comment.