Skip to content

Commit 360daa7

Browse files
authored
feat(chore): Improve git.exe execution and add parallel bucket updates (#5122)
1 parent f930280 commit 360daa7

7 files changed

Lines changed: 179 additions & 74 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop)
2+
3+
### Features
4+
- **scoop-update:** Add support for parallel syncing buckets in PowerShell 7 and improve output ([#5122](https://github.com/ScoopInstaller/Scoop/pull/5122))
5+
6+
### Code Refactoring
7+
- **git:** Use Invoke-Git() with direct path to git.exe to prevent spawning shim subprocesses ([#5122](https://github.com/ScoopInstaller/Scoop/pull/5122))
8+
19
## [v0.3.1](https://github.com/ScoopInstaller/Scoop/compare/v0.3.0...v0.3.1) - 2022-11-15
210

311
### Features

bin/scoop.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ switch ($subCommand) {
2020
}
2121
({ $subCommand -in @('-v', '--version') }) {
2222
Write-Host 'Current Scoop version:'
23-
if ((Test-CommandAvailable git) -and (Test-Path "$PSScriptRoot\..\.git") -and (get_config SCOOP_BRANCH 'master') -ne 'master') {
24-
git -C "$PSScriptRoot\.." --no-pager log --oneline HEAD -n 1
23+
if (Test-GitAvailable -and (Test-Path "$PSScriptRoot\..\.git") -and (get_config SCOOP_BRANCH 'master') -ne 'master') {
24+
Invoke-Git -Path "$PSScriptRoot\.." -ArgumentList @('log', 'HEAD', '-1', '--oneline')
2525
} else {
2626
$version = Select-String -Pattern '^## \[(v[\d.]+)\].*?([\d-]+)$' -Path "$PSScriptRoot\..\CHANGELOG.md"
2727
Write-Host $version.Matches.Groups[1].Value -ForegroundColor Cyan -NoNewline
@@ -31,9 +31,9 @@ switch ($subCommand) {
3131

3232
Get-LocalBucket | ForEach-Object {
3333
$bucketLoc = Find-BucketDirectory $_ -Root
34-
if ((Test-Path "$bucketLoc\.git") -and (Test-CommandAvailable git)) {
34+
if (Test-GitAvailable -and (Test-Path "$bucketLoc\.git")) {
3535
Write-Host "'$_' bucket:"
36-
git -C "$bucketLoc" --no-pager log --oneline HEAD -n 1
36+
Invoke-Git -Path $bucketLoc -ArgumentList @('log', 'HEAD', '-1', '--oneline')
3737
Write-Host ''
3838
}
3939
}

lib/buckets.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function list_buckets {
9999
$bucket = [Ordered]@{ Name = $_ }
100100
$path = Find-BucketDirectory $_ -Root
101101
if ((Test-Path (Join-Path $path '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) {
102-
$bucket.Source = git -C $path config remote.origin.url
103-
$bucket.Updated = git -C $path log --format='%aD' -n 1 | Get-Date
102+
$bucket.Source = Invoke-Git -Path $path -ArgumentList @('config', 'remote.origin.url')
103+
$bucket.Updated = Invoke-Git -Path $path -ArgumentList @('log', "--format='%aD'", '-n', '1')
104104
} else {
105105
$bucket.Source = friendly_path $path
106106
$bucket.Updated = (Get-Item "$path\bucket").LastWriteTime
@@ -113,7 +113,7 @@ function list_buckets {
113113
}
114114

115115
function add_bucket($name, $repo) {
116-
if (!(Test-CommandAvailable git)) {
116+
if (!(Test-GitAvailable)) {
117117
error "Git is required for buckets. Run 'scoop install git' and try again."
118118
return 1
119119
}
@@ -130,7 +130,7 @@ function add_bucket($name, $repo) {
130130
}
131131
foreach ($bucket in Get-LocalBucket) {
132132
if (Test-Path -Path "$bucketsdir\$bucket\.git") {
133-
$remote = git -C "$bucketsdir\$bucket" config --get remote.origin.url
133+
$remote = Invoke-Git -Path "$bucketsdir\$bucket" -ArgumentList @('config', '--get', 'remote.origin.url')
134134
if ((Convert-RepositoryUri -Uri $remote) -eq $uni_repo) {
135135
warn "Bucket $bucket already exists for $repo"
136136
return 2
@@ -139,14 +139,14 @@ function add_bucket($name, $repo) {
139139
}
140140

141141
Write-Host 'Checking repo... ' -NoNewline
142-
$out = git_cmd ls-remote $repo 2>&1
142+
$out = Invoke-Git -ArgumentList @('ls-remote', $repo) 2>&1
143143
if ($LASTEXITCODE -ne 0) {
144144
error "'$repo' doesn't look like a valid git repository`n`nError given:`n$out"
145145
return 1
146146
}
147147
ensure $bucketsdir | Out-Null
148148
$dir = ensure $dir
149-
git_cmd clone "$repo" "`"$dir`"" -q
149+
Invoke-Git -ArgumentList @('clone', $repo, $dir, '-q')
150150
Write-Host 'OK'
151151
success "The $name bucket was added successfully."
152152
return 0
@@ -169,7 +169,7 @@ function new_issue_msg($app, $bucket, $title, $body) {
169169
$bucket_path = "$bucketsdir\$bucket"
170170

171171
if (Test-Path $bucket_path) {
172-
$remote = git -C "$bucket_path" config --get remote.origin.url
172+
$remote = Invoke-Git -Path $bucket_path -ArgumentList @('config', '--get', 'remote.origin.url')
173173
# Support ssh and http syntax
174174
# git@PROVIDER:USER/REPO.git
175175
# https://PROVIDER/USER/REPO.git

lib/core.ps1

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,78 @@ function setup_proxy() {
128128
}
129129
}
130130

131-
function git_cmd {
131+
function Invoke-Git {
132+
[CmdletBinding()]
133+
[OutputType([String])]
134+
param(
135+
[Parameter(Mandatory = $false, Position = 0)]
136+
[Alias('PSPath', 'Path')]
137+
[ValidateNotNullOrEmpty()]
138+
[String]
139+
$WorkingDirectory,
140+
[Parameter(Mandatory = $true, Position = 1)]
141+
[Alias('Args')]
142+
[String[]]
143+
$ArgumentList
144+
)
145+
132146
$proxy = get_config PROXY
133-
$cmd = "git $($args | ForEach-Object { "$_ " })"
134-
if ($proxy -and $proxy -ne 'none') {
135-
$cmd = "SET HTTPS_PROXY=$proxy&&SET HTTP_PROXY=$proxy&&$cmd"
147+
$git = Get-HelperPath -Helper Git
148+
$arguments = $ArgumentList -join ' '
149+
$cmd = "`"$git`" $arguments"
150+
151+
if ($WorkingDirectory) {
152+
$cmd = "`"$git`" -C `"$WorkingDirectory`" $arguments"
153+
}
154+
$sb = [scriptblock]::Create("& $cmd")
155+
156+
if([String]::IsNullOrEmpty($proxy) -or $proxy -eq 'none') {
157+
return Invoke-Command $sb
158+
}
159+
160+
if($arguments -Match '\b(clone|checkout|pull|fetch|ls-remote)\b') {
161+
$old_https = $env:HTTPS_PROXY
162+
$old_http = $env:HTTP_PROXY
163+
try {
164+
# convert proxy setting for git
165+
if ($proxy.StartsWith('currentuser@')) {
166+
$proxy = $proxy.Replace('currentuser@', ':@')
167+
}
168+
$env:HTTPS_PROXY = $proxy
169+
$env:HTTP_PROXY = $proxy
170+
return Invoke-Command $sb
171+
}
172+
catch {
173+
error $_
174+
return
175+
}
176+
finally {
177+
$env:HTTPS_PROXY = $old_https
178+
$env:HTTP_PROXY = $old_http
179+
}
180+
}
181+
182+
return Invoke-Command $sb
183+
}
184+
185+
function Invoke-GitLog {
186+
[CmdletBinding()]
187+
Param (
188+
[Parameter(Mandatory, ValueFromPipeline)]
189+
[String]$Path,
190+
[Parameter(Mandatory, ValueFromPipeline)]
191+
[String]$CommitHash,
192+
[String]$Name = ''
193+
)
194+
Process {
195+
if ($Name) {
196+
if ($Name.Length -gt 12) {
197+
$Name = "$($Name.Substring(0, 10)).."
198+
}
199+
$Name = "%Cgreen$($Name.PadRight(12, ' ').Substring(0, 12))%Creset "
200+
}
201+
Invoke-Git -Path $Path -ArgumentList @('--no-pager', 'log', '--color', '--no-decorate', "--grep='^(chore)'", '--invert-grep', '--abbrev=12', "--format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s $Name%C(cyan)%cr%Creset'", "$CommitHash..HEAD")
136202
}
137-
cmd.exe /d /c $cmd
138203
}
139204

140205
# helper functions
@@ -293,12 +358,16 @@ Function Test-CommandAvailable {
293358
Return [Boolean](Get-Command $Name -ErrorAction Ignore)
294359
}
295360

361+
Function Test-GitAvailable {
362+
Return [Boolean](Test-Path (Get-HelperPath -Helper Git) -ErrorAction Ignore)
363+
}
364+
296365
function Get-HelperPath {
297366
[CmdletBinding()]
298367
[OutputType([String])]
299368
param(
300369
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
301-
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd')]
370+
[ValidateSet('Git', '7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd')]
302371
[String]
303372
$Helper
304373
)
@@ -307,6 +376,14 @@ function Get-HelperPath {
307376
}
308377
process {
309378
switch ($Helper) {
379+
'Git' {
380+
$internalgit = "$(versiondir 'git' 'current')\mingw64\bin\git.exe"
381+
if (Test-Path $internalgit) {
382+
$HelperPath = $internalgit
383+
} else {
384+
$HelperPath = (Get-Command git -ErrorAction Ignore).Source
385+
}
386+
}
310387
'7zip' {
311388
$HelperPath = Get-AppFilePath '7zip' '7z.exe'
312389
if ([String]::IsNullOrEmpty($HelperPath)) {

libexec/scoop-info.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ if ($manifest.depends) {
8484

8585
if (Test-Path $manifest_file) {
8686
if (Get-Command git -ErrorAction Ignore) {
87-
$gitinfo = (git -C (Split-Path $manifest_file) log -1 -s --format='%aD#%an' $manifest_file 2> $null) -Split '#'
87+
$gitinfo = (Invoke-Git -Path (Split-Path $manifest_file) -ArgumentList @('log', '-1', '-s', "--format='%aD#%an'", $manifest_file) 2> $null) -Split '#'
8888
}
8989
if ($gitinfo) {
9090
$item.'Updated at' = $gitinfo[0] | Get-Date

libexec/scoop-status.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ if (!(Get-FormatData ScoopStatus)) {
2121

2222
function Test-UpdateStatus($repopath) {
2323
if (Test-Path "$repopath\.git") {
24-
git_cmd -C "`"$repopath`"" fetch -q origin
24+
Invoke-Git -Path $repopath -ArgumentList @('fetch', '-q', 'origin')
2525
$script:network_failure = 128 -eq $LASTEXITCODE
26-
$branch = git -C $repopath branch --show-current
27-
$commits = git -C $repopath log "HEAD..origin/$branch" --oneline
26+
$branch = Invoke-Git -Path $repopath -ArgumentList @('branch', '--show-current')
27+
$commits = Invoke-Git -Path $repopath -ArgumentList @('log', "HEAD..origin/$branch", '--oneline')
2828
if ($commits) { return $true }
2929
else { return $false }
3030
} else {

0 commit comments

Comments
 (0)