Skip to content

Commit da4ac18

Browse files
committed
fix(git): Use try-catch-finally to revert proxy env variables
- use arrays for git argumentlist
1 parent 60a839d commit da4ac18

File tree

6 files changed

+68
-47
lines changed

6 files changed

+68
-47
lines changed

bin/scoop.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ switch ($subCommand) {
2121
({ $subCommand -in @('-v', '--version') }) {
2222
Write-Host 'Current Scoop version:'
2323
if (Test-GitAvailable -and (Test-Path "$PSScriptRoot\..\.git") -and (get_config SCOOP_BRANCH 'master') -ne 'master') {
24-
Invoke-Git -Path "$PSScriptRoot\.." -ArgumentList "--no-pager log --oneline HEAD -n 1"
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
@@ -33,7 +33,7 @@ switch ($subCommand) {
3333
$bucketLoc = Find-BucketDirectory $_ -Root
3434
if (Test-GitAvailable -and (Test-Path "$bucketLoc\.git")) {
3535
Write-Host "'$_' bucket:"
36-
Invoke-Git -Path $bucketLoc -ArgumentList "--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

+6-6
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 = Invoke-Git -Path $path -ArgumentList "config remote.origin.url"
103-
$bucket.Updated = Invoke-Git -Path $path -ArgumentList "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
@@ -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 = Invoke-Git -Path "$bucketsdir\$bucket" -ArgumentList "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 = Invoke-Git -ArgumentList "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-
Invoke-Git -ArgumentList "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 = Invoke-Git -Path $bucket_path -ArgumentList "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

+37-14
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,54 @@ function Invoke-Git {
132132
[CmdletBinding()]
133133
[OutputType([String])]
134134
param(
135-
[String] $Path,
136-
[String[]] $ArgumentList
135+
[Parameter(Mandatory = $true, 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
137144
)
138145

139146
$proxy = get_config PROXY
140147
$git = Get-HelperPath -Helper Git
141148
$arguments = $ArgumentList -join ' '
142-
if ($Path) {
143-
$cmd = "`"$git`" -C `"$Path`" $arguments"
144-
} else {
145-
$cmd = "`"$git`" $arguments"
149+
$cmd = "`"$git`" $arguments"
150+
151+
if ($WorkingDirectory) {
152+
$cmd = "`"$git`" -C `"$WorkingDirectory`" $arguments"
146153
}
154+
$sb = [scriptblock]::Create("& $cmd")
147155

148-
# invoke git if proxy is not set or not required
149-
if ([String]::IsNullOrEmpty($proxy) -or $proxy -eq 'none' -or $arguments -NotMatch "\b(clone|checkout|pull|fetch|ls-remote)\b") {
150-
return Invoke-Command ([scriptblock]::Create("& $cmd"))
156+
if([String]::IsNullOrEmpty($proxy) -or $proxy -eq 'none') {
157+
return Invoke-Command $sb
151158
}
152159

153-
# convert proxy setting for git
154-
if ($proxy.StartsWith('currentuser@')) {
155-
$proxy = $proxy.Replace("currentuser@",":@")
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+
}
156180
}
157181

158-
# use proxy with cmd.exe to prevent polluting PowerShell session
159-
cmd.exe /d /c "SET HTTPS_PROXY=$proxy&&SET HTTP_PROXY=$proxy&&$cmd"
182+
return Invoke-Command $sb
160183
}
161184

162185
# helper functions

libexec/scoop-info.ps1

+1-1
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 = (Invoke-Git -Path (Split-Path $manifest_file) -ArgumentList "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

+3-3
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-
Invoke-Git -Path $repopath -ArgumentList "fetch -q origin"
24+
Invoke-Git -Path $repopath -ArgumentList @('fetch', '-q', 'origin')
2525
$script:network_failure = 128 -eq $LASTEXITCODE
26-
$branch = Invoke-Git -Path $repopath -ArgumentList "branch --show-current"
27-
$commits = Invoke-Git -Path $repopath -ArgumentList "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 {

libexec/scoop-update.ps1

+19-21
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function Sync-Scoop {
7676
$olddir = "$currentdir\..\old"
7777

7878
# get git scoop
79-
Invoke-Git -ArgumentList "clone -q $configRepo --branch $configBranch --single-branch `"$newdir`""
79+
Invoke-Git -ArgumentList @('clone', '-q', $configRepo, '--branch', $configBranch, '--single-branch', $newdir)
8080

8181
# check if scoop was successful downloaded
8282
if (!(Test-Path "$newdir\bin\scoop.ps1")) {
@@ -97,18 +97,18 @@ function Sync-Scoop {
9797
Remove-Item "$currentdir\..\old" -Recurse -Force -ErrorAction SilentlyContinue
9898
}
9999

100-
$previousCommit = Invoke-Git -Path $currentdir -ArgumentList "rev-parse HEAD"
101-
$currentRepo = Invoke-Git -Path $currentdir -ArgumentList "config remote.origin.url"
102-
$currentBranch = Invoke-Git -Path $currentdir -ArgumentList "branch"
100+
$previousCommit = Invoke-Git -Path $currentdir -ArgumentList @('rev-parse', 'HEAD')
101+
$currentRepo = Invoke-Git -Path $currentdir -ArgumentList @('config', 'remote.origin.url')
102+
$currentBranch = Invoke-Git -Path $currentdir -ArgumentList @('branch')
103103

104104
$isRepoChanged = !($currentRepo -match $configRepo)
105105
$isBranchChanged = !($currentBranch -match "\*\s+$configBranch")
106106

107107
# Stash uncommitted changes
108-
if (Invoke-Git -Path $currentdir -ArgumentList "diff HEAD --name-only") {
108+
if (Invoke-Git -Path $currentdir -ArgumentList @('diff', 'HEAD', '--name-only')) {
109109
if (get_config AUTOSTASH_ON_CONFLICT) {
110110
warn "Uncommitted changes detected. Stashing..."
111-
Invoke-Git -Path $currentdir -ArgumentList "stash push -m 'WIP at $([System.DateTime]::Now.ToString('o'))' -u -q"
111+
Invoke-Git -Path $currentdir -ArgumentList @('stash', 'push', '-m', "WIP at $([System.DateTime]::Now.ToString('o'))", '-u', '-q')
112112
} else {
113113
warn "Uncommitted changes detected. Update aborted."
114114
return
@@ -117,26 +117,26 @@ function Sync-Scoop {
117117

118118
# Change remote url if the repo is changed
119119
if ($isRepoChanged) {
120-
Invoke-Git -Path $currentdir -ArgumentList "config remote.origin.url '$configRepo'"
120+
Invoke-Git -Path $currentdir -ArgumentList @('config', 'remote.origin.url', $configRepo)
121121
}
122122

123123
# Fetch and reset local repo if the repo or the branch is changed
124124
if ($isRepoChanged -or $isBranchChanged) {
125125
# Reset git fetch refs, so that it can fetch all branches (GH-3368)
126-
Invoke-Git -Path $currentdir -ArgumentList "config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'"
126+
Invoke-Git -Path $currentdir -ArgumentList @('config', 'remote.origin.fetch', '+refs/heads/*:refs/remotes/origin/*')
127127
# fetch remote branch
128-
Invoke-Git -Path $currentdir -ArgumentList "fetch --force origin 'refs/heads/`"$configBranch`":refs/remotes/origin/$configBranch' -q"
128+
Invoke-Git -Path $currentdir -ArgumentList @('fetch', '--force', 'origin', "refs/heads/$configBranch`:refs/remotes/origin/$configBranch", '-q')
129129
# checkout and track the branch
130-
Invoke-Git -Path $currentdir -ArgumentList "checkout -B $configBranch -t origin/$configBranch -q"
130+
Invoke-Git -Path $currentdir -ArgumentList @('checkout', '-B', $configBranch, '-t', "origin/$configBranch", '-q')
131131
# reset branch HEAD
132-
Invoke-Git -Path $currentdir -ArgumentList "reset --hard origin/$configBranch -q"
132+
Invoke-Git -Path $currentdir -ArgumentList @('reset', '--hard', "origin/$configBranch", '-q')
133133
} else {
134-
Invoke-Git -Path $currentdir -ArgumentList "pull -q"
134+
Invoke-Git -Path $currentdir -ArgumentList @('pull', '-q')
135135
}
136136

137137
$res = $lastexitcode
138138
if ($Log) {
139-
Invoke-Git -Path $currentdir -ArgumentList "--no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
139+
Invoke-Git -Path $currentdir -ArgumentList @('--no-pager', 'log', '--color', '--no-decorate', "--grep='^(chore)'", '--invert-grep', "--format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset'", "$previousCommit..HEAD")
140140
}
141141

142142
if ($res -ne 0) {
@@ -163,12 +163,10 @@ function Sync-Bucket {
163163
return
164164
}
165165

166-
Write-Host "Updating '$Name' bucket..."
167-
168-
$previousCommit = Invoke-Git -Path $bucketLoc -ArgumentList "rev-parse HEAD"
169-
Invoke-Git -Path $bucketLoc "pull -q"
166+
$previousCommit = Invoke-Git -Path $bucketLoc -ArgumentList @('rev-parse', 'HEAD')
167+
Invoke-Git -Path $bucketLoc @('pull', '-q')
170168
if ($Log) {
171-
Invoke-Git -Path $bucketLoc -ArgumentList "--no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset [$Name] %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
169+
Invoke-Git -Path $bucketLoc -ArgumentList @('--no-pager', 'log', '--color', '--no-decorate', "--grep='^(chore)'", '--invert-grep', "--format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %Cgreen$Name%Creset %C(cyan)%cr%Creset'", "$previousCommit..HEAD")
172170
}
173171
}
174172
}
@@ -211,10 +209,10 @@ function Sync-Buckets {
211209
$bucketLoc = $_.path
212210
$name = $_.name
213211

214-
$previousCommit = Invoke-Git -Path $bucketLoc -ArgumentList "rev-parse HEAD"
215-
Invoke-Git -Path $bucketLoc -ArgumentList "pull -q"
212+
$previousCommit = Invoke-Git -Path $bucketLoc -ArgumentList @('rev-parse', 'HEAD')
213+
Invoke-Git -Path $bucketLoc -ArgumentList @('pull', '-q')
216214
if ($using:Log) {
217-
Invoke-Git -Path $bucketLoc -ArgumentList "--no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset [$name] %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
215+
Invoke-Git -Path $bucketLoc -ArgumentList @('--no-pager', 'log', '--color', '--no-decorate', "--grep='^(chore)'", '--invert-grep', "--format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %Cgreen$name%Creset %C(cyan)%cr%Creset'", "$previousCommit..HEAD")
218216
}
219217
}
220218
} else {

0 commit comments

Comments
 (0)